use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRClassificationMappingEventHandler method createMapping.
private void createMapping(MCRObject obj) {
MCRMetaElement mappings = obj.getMetadata().getMetadataElement("mappings");
if (mappings != null) {
oldMappings = mappings.clone();
obj.getMetadata().removeMetadataElement("mappings");
}
Element currentClassElement = null;
try {
Document doc = new Document(obj.getMetadata().createXML().detach());
XPathExpression<Element> classElementPath = XPathFactory.instance().compile("//*[@categid]", Filters.element());
List<Element> classList = classElementPath.evaluate(doc);
if (classList.size() > 0) {
mappings = new MCRMetaElement();
mappings.setTag("mappings");
mappings.setClass(MCRMetaClassification.class);
mappings.setHeritable(false);
mappings.setNotInherit(true);
obj.getMetadata().setMetadataElement(mappings);
}
for (Element classElement : classList) {
currentClassElement = classElement;
MCRCategory categ = DAO.getCategory(new MCRCategoryID(classElement.getAttributeValue("classid"), classElement.getAttributeValue("categid")), 0);
addMappings(mappings, categ);
}
} catch (Exception je) {
if (currentClassElement == null) {
LOGGER.error("Error while finding classification elements", je);
} else {
LOGGER.error("Error while finding classification elements for {}", new XMLOutputter().outputString(currentClassElement), je);
}
} finally {
if (mappings == null || mappings.size() == 0) {
obj.getMetadata().removeMetadataElement("mappings");
}
}
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRURLRetriever method getParentCollection.
private static String getParentCollection(String collection) {
MCRCategoryID categoryId = new MCRCategoryID(MCRConstants.COLLECTION_CLASS_ID.getRootID(), collection);
List<MCRCategory> parents = CATEGORY_DAO.getParents(categoryId);
if (parents == null || parents.isEmpty()) {
return null;
}
return parents.iterator().next().getId().getID();
}
use of org.mycore.datamodel.classifications2.MCRCategory 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.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRCategLinkServiceImpl method countLinksForType.
@Override
public Map<MCRCategoryID, Number> countLinksForType(MCRCategory parent, String type, boolean childrenOnly) {
boolean restrictedByType = type != null;
String queryName;
if (childrenOnly) {
queryName = restrictedByType ? "NumberByTypePerChildOfParentID" : "NumberPerChildOfParentID";
} else {
queryName = restrictedByType ? "NumberByTypePerClassID" : "NumberPerClassID";
}
Map<MCRCategoryID, Number> countLinks = new HashMap<>();
Collection<MCRCategoryID> ids = childrenOnly ? getAllChildIDs(parent) : getAllCategIDs(parent);
for (MCRCategoryID id : ids) {
// initialize all categIDs with link count of zero
countLinks.put(id, 0);
}
// old classification browser/editor could not determine links correctly otherwise
if (!childrenOnly) {
parent = parent.getRoot();
} else if (!(parent instanceof MCRCategoryImpl) || ((MCRCategoryImpl) parent).getInternalID() == 0) {
parent = MCRCategoryDAOImpl.getByNaturalID(MCREntityManagerProvider.getCurrentEntityManager(), parent.getId());
}
LOGGER.info("parentID:{}", parent.getId());
String classID = parent.getId().getRootID();
Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + queryName);
// query can take long time, please cache result
q.setCacheable(true);
q.setReadOnly(true);
q.setParameter("classID", classID);
if (childrenOnly) {
q.setParameter("parentID", ((MCRCategoryImpl) parent).getInternalID());
}
if (restrictedByType) {
q.setParameter("type", type);
}
// get object count for every category (not accumulated)
@SuppressWarnings("unchecked") List<Object[]> result = (List<Object[]>) q.getResultList();
for (Object[] sr : result) {
MCRCategoryID key = new MCRCategoryID(classID, sr[0].toString());
Number value = (Number) sr[1];
countLinks.put(key, value);
}
return countLinks;
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRCategoryChildList method remove.
@Override
public MCRCategory remove(int index) {
MCRCategory category = super.remove(index);
removeAncestorReferences(category);
return category;
}
Aggregations