Search in sources :

Example 41 with MCRCategory

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");
        }
    }
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) XMLOutputter(org.jdom2.output.XMLOutputter) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 42 with MCRCategory

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();
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 43 with MCRCategory

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());
    }
}
Also used : Path(java.nio.file.Path) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 44 with MCRCategory

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;
}
Also used : HashMap(java.util.HashMap) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) LinkedList(java.util.LinkedList) List(java.util.List)

Example 45 with MCRCategory

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;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)87 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)36 Test (org.junit.Test)24 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)17 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)10 ArrayList (java.util.ArrayList)9 Document (org.jdom2.Document)9 Element (org.jdom2.Element)8 MCRException (org.mycore.common.MCRException)8 IOException (java.io.IOException)6 EntityManager (javax.persistence.EntityManager)6 URI (java.net.URI)5 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4