use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRClassificationMappingEventHandlerTest method testMapping.
@Test
public void testMapping() throws SAXParseException, IOException, JDOMException, URISyntaxException {
MCRSessionMgr.getCurrentSession().isTransactionActive();
ClassLoader classLoader = getClass().getClassLoader();
SAXBuilder saxBuilder = new SAXBuilder();
loadCategory("diniPublType.xml");
loadCategory("genre.xml");
Document document = saxBuilder.build(classLoader.getResourceAsStream(TEST_DIRECTORY + "testMods.xml"));
MCRObject mcro = new MCRObject();
MCRMODSWrapper mw = new MCRMODSWrapper(mcro);
mw.setMODS(document.getRootElement().detach());
mw.setID("junit", 1);
MCRClassificationMappingEventHandler mapper = new MCRClassificationMappingEventHandler();
mapper.handleObjectUpdated(null, mcro);
String expression = "//mods:classification[contains(@generator,'-mycore') and contains(@valueURI, 'StudyThesis')]";
XPathExpression<Element> expressionObject = XPathFactory.instance().compile(expression, Filters.element(), null, MCRConstants.MODS_NAMESPACE, MCRConstants.XLINK_NAMESPACE);
Document xml = mcro.createXML();
Assert.assertNotNull("The mapped classification should be in the MyCoReObject now!", expressionObject.evaluateFirst(xml));
expression = "//mods:classification[contains(@generator,'-mycore') and contains(@valueURI, 'masterThesis')]";
expressionObject = XPathFactory.instance().compile(expression, Filters.element(), null, MCRConstants.MODS_NAMESPACE, MCRConstants.XLINK_NAMESPACE);
Assert.assertNull("The mapped classification of the child should not be contained in the MyCoReObject now!", expressionObject.evaluateFirst(xml));
LOGGER.info(new XMLOutputter(Format.getPrettyFormat()).outputString(xml));
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCREventHandlerBase method doHandleEvent.
/**
* This method handle all calls for EventHandler for the event types
* MCRObject, MCRDerivate and MCRFile.
*
* @param evt
* The MCREvent object
*/
public void doHandleEvent(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:
handleObjectCreated(evt, obj);
break;
case MCREvent.UPDATE_EVENT:
handleObjectUpdated(evt, obj);
break;
case MCREvent.DELETE_EVENT:
handleObjectDeleted(evt, obj);
break;
case MCREvent.REPAIR_EVENT:
handleObjectRepaired(evt, obj);
break;
case MCREvent.INDEX_EVENT:
handleObjectIndex(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:
handleDerivateCreated(evt, der);
break;
case MCREvent.UPDATE_EVENT:
handleDerivateUpdated(evt, der);
break;
case MCREvent.DELETE_EVENT:
handleDerivateDeleted(evt, der);
break;
case MCREvent.REPAIR_EVENT:
handleDerivateRepaired(evt, der);
break;
case MCREvent.INDEX_EVENT:
updateDerivateFileIndex(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:
handlePathCreated(evt, path, attrs);
break;
case MCREvent.UPDATE_EVENT:
handlePathUpdated(evt, path, attrs);
break;
case MCREvent.DELETE_EVENT:
handlePathDeleted(evt, path, attrs);
break;
case MCREvent.REPAIR_EVENT:
handlePathRepaired(evt, path, attrs);
break;
case MCREvent.INDEX_EVENT:
updatePathIndex(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 cl = (MCRCategory) evt.get("class");
if (cl != null) {
logger.debug("{} handling {} {}", getClass().getName(), cl.getId(), evt.getEventType());
switch(evt.getEventType()) {
case MCREvent.CREATE_EVENT:
handleClassificationCreated(evt, cl);
break;
case MCREvent.UPDATE_EVENT:
handleClassificationUpdated(evt, cl);
break;
case MCREvent.DELETE_EVENT:
handleClassificationDeleted(evt, cl);
break;
case MCREvent.REPAIR_EVENT:
handleClassificationRepaired(evt, cl);
break;
default:
logger.warn("Can't find method for a 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 MCRTransferPackage method getContent.
/**
* Returns the content for this transfer package. You have to call {@link #build()}
* before you can retrieve this data.
*
* @return a map where key = filename; value = MCRContent
*/
public Map<String, MCRContent> getContent() throws IOException {
Map<String, MCRContent> content = new HashMap<>();
// config
content.put(IMPORT_CONFIG_FILENAME, new MCRJDOMContent(buildImportConfiguration()));
// objects
for (MCRObject object : this.objects) {
String fileName = CONTENT_PATH + object.getId() + ".xml";
Document xml = object.createXML();
content.put(fileName, new MCRJDOMContent(xml));
}
// file containers
for (MCRTransferPackageFileContainer fc : this.fileContainers) {
// derivate
MCRObjectID derivateId = fc.getDerivateId();
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
Document derivateXML = derivate.createXML();
String folder = CONTENT_PATH + fc.getName();
String derivateFileName = folder + "/" + fc.getName() + ".xml";
content.put(derivateFileName, new MCRJDOMContent(derivateXML));
// files of derivate
for (MCRFile file : fc.getFiles()) {
content.put(folder + file.getAbsolutePath(), file.getContent());
}
}
// classifications
for (String classId : this.classifications) {
Document classification = MCRClassificationUtils.asDocument(classId);
String path = CLASS_PATH + classId + ".xml";
content.put(path, new MCRJDOMContent(classification));
}
return content;
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRTransferPackage method resolveChildrenAndLinks.
/**
* Fills the given objectMap with all children and links of the object. The object
* itself is also added.
*
* @param object the source object
* @param objectMap the map which will be created
*/
protected void resolveChildrenAndLinks(MCRObject object, LinkedHashMap<MCRObjectID, MCRObject> objectMap, Set<MCRCategoryID> categories) {
// add links
for (MCRObject entityLink : MCRObjectUtils.getLinkedObjects(object)) {
if (!objectMap.containsKey(entityLink.getId())) {
objectMap.put(entityLink.getId(), entityLink);
}
}
// add classifications
categories.addAll(MCRObjectUtils.getCategories(object));
// add the object to the objectMap
objectMap.put(object.getId(), object);
// resolve children
for (MCRMetaLinkID metaLinkId : object.getStructure().getChildren()) {
MCRObjectID childId = MCRObjectID.getInstance(metaLinkId.toString());
if (!MCRMetadataManager.exists(childId)) {
throw new MCRUsageException("Requested object '" + childId + "' does not exist. Thus a transfer package cannot be created.");
}
MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
resolveChildrenAndLinks(child, objectMap, categories);
}
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRTransferPackage method buildImportConfiguration.
/**
* Generates an xml file, which contains import configuration.
*
* @return import configuration document
*/
public Document buildImportConfiguration() {
Element configElement = new Element("config");
Element orderElement = new Element("order");
for (MCRObject object : objects) {
Element e = new Element("object");
e.setText(object.toString());
orderElement.addContent(e);
}
configElement.addContent(orderElement);
return new Document(configElement);
}
Aggregations