Search in sources :

Example 81 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory 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());
    }
}
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 82 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRAuthorityWithURI method lookupCategoryID.

@Override
protected MCRCategoryID lookupCategoryID() {
    for (MCRCategory category : getCategoryByURI(valueURI)) {
        if (authorityURI.equals(category.getRoot().getLabel(LABEL_LANG_URI).get().getText())) {
            return category.getId();
        }
    }
    // maybe valueUri is in form {authorityURI}#{categId}
    if (valueURI.startsWith(authorityURI) && authorityURI.length() < valueURI.length()) {
        String categId;
        try {
            categId = valueURI.substring(authorityURI.length() + 1);
        } catch (RuntimeException e) {
            LOGGER.warn("authorityURI:{}, valueURI:{}", authorityURI, valueURI);
            throw e;
        }
        int internalStylePos = authorityURI.indexOf(CLASS_URI_PART);
        if (internalStylePos > 0) {
            String rootId = authorityURI.substring(internalStylePos + CLASS_URI_PART.length());
            MCRCategoryID catId = new MCRCategoryID(rootId, categId);
            if (DAO.exist(catId)) {
                return catId;
            }
        }
        return getCategoryByURI(authorityURI).stream().map(cat -> new MCRCategoryID(cat.getId().getRootID(), categId)).filter(DAO::exist).findFirst().orElse(null);
    }
    return null;
}
Also used : Logger(org.apache.logging.log4j.Logger) Element(org.w3c.dom.Element) Collection(java.util.Collection) MCRCategoryDAOFactory(org.mycore.datamodel.classifications2.MCRCategoryDAOFactory) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRFrontendUtil(org.mycore.frontend.MCRFrontendUtil) LogManager(org.apache.logging.log4j.LogManager) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 83 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRAccessCondition method lookupCategoryID.

/* (non-Javadoc)
     * @see org.mycore.mods.classification.MCRAuthorityInfo#lookupCategoryID()
     */
@Override
protected MCRCategoryID lookupCategoryID() {
    Collection<MCRCategory> categoryByURI = MCRAuthorityWithURI.getCategoryByURI(href);
    if (categoryByURI.size() > 1) {
        throw new MCRException(href + " is ambigous: " + categoryByURI.stream().map(MCRCategory::getId).collect(Collectors.toList()));
    }
    if (!categoryByURI.isEmpty()) {
        return categoryByURI.iterator().next().getId();
    }
    // maybe href is in form {authorityURI}#{categId}
    String categId, authorityURI = null;
    try {
        authorityURI = href.substring(0, href.lastIndexOf("#"));
        categId = href.substring(authorityURI.length() + 1);
    } catch (RuntimeException e) {
        LOGGER.warn("authorityURI:{}, valueURI:{}", authorityURI, href);
        return null;
    }
    int internalStylePos = authorityURI.indexOf(MCRAuthorityWithURI.CLASS_URI_PART);
    if (internalStylePos > 0) {
        String rootId = authorityURI.substring(internalStylePos + MCRAuthorityWithURI.CLASS_URI_PART.length());
        MCRCategoryID catId = new MCRCategoryID(rootId, categId);
        if (DAO.exist(catId)) {
            return catId;
        }
    }
    Collection<MCRCategory> classes = MCRAuthorityWithURI.getCategoryByURI(authorityURI);
    return classes.stream().map(cat -> new MCRCategoryID(cat.getId().getRootID(), categId)).filter(DAO::exist).findFirst().orElse(null);
}
Also used : Logger(org.apache.logging.log4j.Logger) Collection(java.util.Collection) MCRConstants(org.mycore.common.MCRConstants) MCRCategoryDAOFactory(org.mycore.datamodel.classifications2.MCRCategoryDAOFactory) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Collectors(java.util.stream.Collectors) LogManager(org.apache.logging.log4j.LogManager) MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRException(org.mycore.common.MCRException) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 84 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRXMLFunctions method getDisplayName.

/**
 * @param classificationId
 * @param categoryId
 * @return
 */
public static String getDisplayName(String classificationId, String categoryId) {
    try {
        MCRCategoryID categID = new MCRCategoryID(classificationId, categoryId);
        MCRCategoryDAO dao = MCRCategoryDAOFactory.getInstance();
        MCRCategory category = dao.getCategory(categID, 0);
        return Optional.ofNullable(category).map(MCRCategory::getCurrentLabel).filter(Optional::isPresent).map(Optional::get).map(MCRLabel::getText).orElse("");
    } catch (Throwable e) {
        LOGGER.error("Could not determine display name for classification id {} and category id {}", classificationId, categoryId, e);
        return "";
    }
}
Also used : MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Optional(java.util.Optional)

Example 85 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRXMLFunctions method isCategoryID.

/**
 * @param classificationId
 * @param categoryId
 * @return
 */
public static boolean isCategoryID(String classificationId, String categoryId) {
    MCRCategory category = null;
    try {
        MCRCategoryID categID = MCRCategoryID.fromString(classificationId + ":" + categoryId);
        MCRCategoryDAO dao = MCRCategoryDAOFactory.getInstance();
        category = dao.getCategory(categID, 0);
    } catch (Throwable e) {
        LOGGER.error("Could not determine state for classification id {} and category id {}", classificationId, categoryId, e);
    }
    return category != null;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

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