use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRDefaultDerivateTitleResolver method resolveTitle.
@Override
public String resolveTitle(String derivateIDString) {
MCRObjectID derivateID = MCRObjectID.getInstance(derivateIDString);
final MCRObjectID objectID = MCRMetadataManager.getObjectId(derivateID, EXPIRE_METADATA_CACHE_TIME, TimeUnit.SECONDS);
MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
return object.getStructure().getDerivateLink(derivateID).getXLinkTitle();
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCREventHandlerBase method undoHandleEvent.
/**
* This method roll back all calls for EventHandler for the event types
* MCRObject, MCRDerivate and MCRFile.
*
* @param evt
* The MCREvent object
*/
public void undoHandleEvent(MCREvent evt) {
if (evt.getObjectType().equals(MCREvent.OBJECT_TYPE)) {
MCRObject obj = (MCRObject) evt.get("object");
if (obj != null) {
logger.debug("{} handling {} {}", getClass().getName(), obj.getId(), evt.getEventType());
switch(evt.getEventType()) {
case MCREvent.CREATE_EVENT:
undoObjectCreated(evt, obj);
break;
case MCREvent.UPDATE_EVENT:
undoObjectUpdated(evt, obj);
break;
case MCREvent.DELETE_EVENT:
undoObjectDeleted(evt, obj);
break;
case MCREvent.REPAIR_EVENT:
undoObjectRepaired(evt, obj);
break;
default:
logger.warn("Can't find method for an object data handler for event type {}", evt.getEventType());
break;
}
return;
}
logger.warn("Can't find method for " + MCREvent.OBJECT_TYPE + " for event type {}", evt.getEventType());
return;
}
if (evt.getObjectType().equals(MCREvent.DERIVATE_TYPE)) {
MCRDerivate der = (MCRDerivate) evt.get("derivate");
if (der != null) {
logger.debug("{} handling {}{}", getClass().getName(), der.getId(), evt.getEventType());
switch(evt.getEventType()) {
case MCREvent.CREATE_EVENT:
undoDerivateCreated(evt, der);
break;
case MCREvent.UPDATE_EVENT:
undoDerivateUpdated(evt, der);
break;
case MCREvent.DELETE_EVENT:
undoDerivateDeleted(evt, der);
break;
case MCREvent.REPAIR_EVENT:
undoDerivateRepaired(evt, der);
break;
default:
logger.warn("Can't find method for a derivate data handler for event type {}", evt.getEventType());
break;
}
return;
}
logger.warn("Can't find method for " + MCREvent.DERIVATE_TYPE + " for event type {}", evt.getEventType());
return;
}
if (evt.getObjectType().equals(MCREvent.PATH_TYPE)) {
Path path = (Path) evt.get(MCREvent.PATH_KEY);
if (path != null) {
if (!path.isAbsolute()) {
logger.warn("Cannot handle path events on non absolute paths: {}", path);
}
logger.debug("{} handling {} {}", getClass().getName(), path, evt.getEventType());
BasicFileAttributes attrs = (BasicFileAttributes) evt.get(MCREvent.FILEATTR_KEY);
if (attrs == null && !evt.getEventType().equals(MCREvent.DELETE_EVENT)) {
logger.warn("BasicFileAttributes for {} was not given. Resolving now.", path);
try {
attrs = Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
} catch (IOException e) {
logger.error("Could not get BasicFileAttributes from path: {}", path, e);
}
}
switch(evt.getEventType()) {
case MCREvent.CREATE_EVENT:
undoPathCreated(evt, path, attrs);
break;
case MCREvent.UPDATE_EVENT:
undoPathUpdated(evt, path, attrs);
break;
case MCREvent.DELETE_EVENT:
undoPathDeleted(evt, path, attrs);
break;
case MCREvent.REPAIR_EVENT:
undoPathRepaired(evt, path, attrs);
break;
default:
logger.warn("Can't find method for Path data handler for event type {}", evt.getEventType());
break;
}
return;
}
logger.warn("Can't find method for " + MCREvent.PATH_TYPE + " for event type {}", evt.getEventType());
return;
}
if (evt.getObjectType().equals(MCREvent.CLASS_TYPE)) {
MCRCategory obj = (MCRCategory) evt.get("class");
if (obj != null) {
logger.debug("{} handling {} {}", getClass().getName(), obj.getId(), evt.getEventType());
switch(evt.getEventType()) {
case MCREvent.CREATE_EVENT:
undoClassificationCreated(evt, obj);
break;
case MCREvent.UPDATE_EVENT:
undoClassificationUpdated(evt, obj);
break;
case MCREvent.DELETE_EVENT:
undoClassificationDeleted(evt, obj);
break;
case MCREvent.REPAIR_EVENT:
undoClassificationRepaired(evt, obj);
break;
default:
logger.warn("Can't find method for an classification data handler for event type {}", evt.getEventType());
break;
}
return;
}
logger.warn("Can't find method for " + MCREvent.CLASS_TYPE + " for event type {}", evt.getEventType());
}
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRCreateObjectServlet method createObject.
/**
* Adds a new mycore object to the persistence backend.
* @param doc
* MyCoRe object as XML
* @return
* MCRObjectID of the newly created object.
* @throws MCRActiveLinkException
* If links from or to other objects will fail.
* @throws JDOMException
* from {@link MCRPersistenceHelper#getMCRObject(Document)}
* @throws IOException
* from {@link MCRPersistenceHelper#getMCRObject(Document)}
* @throws SAXParseException
* @throws MCRException
* @throws MCRAccessException
*/
private MCRObjectID createObject(Document doc) throws MCRActiveLinkException, JDOMException, IOException, MCRException, SAXParseException, MCRAccessException {
MCRObject mcrObject = MCRPersistenceHelper.getMCRObject(doc);
MCRObjectID objectId = mcrObject.getId();
// noinspection SynchronizeOnNonFinalField
checkCreatePrivilege(objectId);
synchronized (this) {
if (objectId.getNumberAsInteger() == 0) {
String objId = mcrObject.getId().toString();
objectId = MCRObjectID.getNextFreeId(objectId.getBase());
if (mcrObject.getLabel().equals(objId))
mcrObject.setLabel(objectId.toString());
mcrObject.setId(objectId);
}
}
MCRMetadataManager.create(mcrObject);
return objectId;
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRPersistenceHelper method getMCRObject.
/**
* creates a MCRObject instance on base of JDOM document
* @param doc
* MyCoRe object as XML.
* @return
* @throws JDOMException
* exception from underlying {@link MCREditorOutValidator}
* @throws IOException
* exception from underlying {@link MCREditorOutValidator} or {@link XMLOutputter}
* @throws SAXParseException
* @throws MCRException
*/
static MCRObject getMCRObject(Document doc) throws JDOMException, IOException, MCRException, SAXParseException {
String id = doc.getRootElement().getAttributeValue("ID");
MCRObjectID objectID = MCRObjectID.getInstance(id);
MCREditorOutValidator ev = new MCREditorOutValidator(doc, objectID);
Document jdom_out = ev.generateValidMyCoReObject();
if (ev.getErrorLog().size() > 0 && LOGGER.isDebugEnabled()) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
StringWriter swOrig = new StringWriter();
xout.output(doc, swOrig);
LOGGER.debug("Input document \n{}", swOrig);
for (String logMsg : ev.getErrorLog()) {
LOGGER.debug(logMsg);
}
StringWriter swClean = new StringWriter();
xout.output(jdom_out, swClean);
LOGGER.debug("Results in \n{}", swClean);
}
return new MCRObject(jdom_out);
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRUpdateDerivateServlet method updateDerivateXML.
/**
* Updates derivate xml in the persistence backend
* @param editorSubmission
* MyCoRe derivate as XML
* @return
* MCRObjectID of the MyCoRe object
* @throws SAXParseException
* @throws MCRAccessException
*/
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
MCRObjectID objectID;
Element root = editorSubmission.getRootElement();
root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
root.addNamespaceDeclaration(XLINK_NAMESPACE);
root.addNamespaceDeclaration(XSI_NAMESPACE);
byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
MCRDerivate der = new MCRDerivate(xml, true);
MCRObjectID derivateID = der.getId();
// store entry of derivate xlink:title in object
objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
MCRObjectStructure structure = obj.getStructure();
MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
linkID.setXLinkTitle(der.getLabel());
try {
MCRMetadataManager.update(obj);
MCRMetadataManager.update(der);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
}
return objectID;
}
Aggregations