private static final Log LOG = ExoLogger.getLogger("exo.jcr.component.ext.AddMetadataAction");
public boolean execute(Context ctx) throws Exception
{
PropertyImpl property = (PropertyImpl)ctx.get("currentItem");
NodeImpl parent = property.getParent();
if (!parent.isNodeType("nt:resource"))
{
throw new Exception("incoming node is not nt:resource type");
}
InputStream data = null;
String mimeType;
try
{
if (property.getInternalName().equals(Constants.JCR_DATA))
{
data = ((PropertyData)property.getData()).getValues().get(0).getAsStream();
try
{
mimeType = parent.getProperty("jcr:mimeType").getString();
}
catch (PathNotFoundException e)
{
return false;
}
}
else if (property.getInternalName().equals(Constants.JCR_MIMETYPE))
{
int evt = (Integer)ctx.get(InvocationContext.EVENT);
if (evt != ExtendedEvent.PROPERTY_ADDED)
{
// In case the mime type is modified we assume that the property jcr:data is modified too so to
// prevent issue like JCR-1873 we do the data extraction only on jcr:data change
return false;
}
mimeType = property.getString();
try
{
PropertyImpl propertyImpl = (PropertyImpl)parent.getProperty("jcr:data");
data = ((PropertyData)propertyImpl.getData()).getValues().get(0).getAsStream();
}
catch (PathNotFoundException e)
{
return false;
}
}
else
{
return false;
}
if (data.available() == 0)
{
return false;
}
if (!parent.isNodeType("dc:elementSet"))
{
parent.addMixin("dc:elementSet");
}
DocumentReaderService readerService =
(DocumentReaderService)((ExoContainer)ctx.get("exocontainer"))
.getComponentInstanceOfType(DocumentReaderService.class);
if (readerService == null)
{
throw new IllegalArgumentException("No DocumentReaderService configured for current container");
}
Properties props = new Properties();
try
{
props = readerService.getDocumentReader(mimeType).getProperties(data);
}
catch (HandlerNotFoundException e)
{
LOG.debug("Binary value reader error, content by path " + property.getPath() + ", property id "
+ property.getData().getIdentifier() + " : " + e.getMessage());
}
catch (DocumentReadException e)
{
printWarning(property, e);
}
catch (IOException e)
{
printWarning(property, e);
}
Iterator entries = props.entrySet().iterator();
while (entries.hasNext())
{
Entry entry = (Entry)entries.next();
QName qname = (QName)entry.getKey();
JCRName jcrName =
property.getSession().getLocationFactory().createJCRName(
new InternalQName(qname.getNamespace(), qname.getName()));
PropertyDefinitionDatas pds =
parent.getSession().getWorkspace().getNodeTypesHolder().getPropertyDefinitions(
jcrName.getInternalName(), ((NodeData)parent.getData()).getPrimaryTypeName(),
((NodeData)parent.getData()).getMixinTypeNames());
if (pds.getDefinition(true) != null)
{
Value[] values = {createValue(entry.getValue(), property.getSession().getValueFactory())};
parent.setProperty(jcrName.getAsString(), values);
}
else
{
parent.setProperty(jcrName.getAsString(), createValue(entry.getValue(), property.getSession()
.getValueFactory()));
}
}
return false;