Search in sources :

Example 76 with MCRCategoryID

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

the class MCRRoleManager method getExternalRole.

/**
 * Factory for external roles
 * @param name a valid {@link MCRCategoryID}
 * @return MCRRole instance or null if category does not exist
 */
public static MCRRole getExternalRole(String name) {
    MCRCategoryID categoryID = MCRCategoryID.fromString(name);
    if (categoryID.isRootID()) {
        LOGGER.debug("External role may not be a rootCategory: {}", categoryID);
        return null;
    }
    MCRCategory category = DAO.getCategory(categoryID, 0);
    if (category == null) {
        LOGGER.debug("Category does not exist: {}", categoryID);
        return null;
    }
    return new MCRRole(name, category.getLabels());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 77 with MCRCategoryID

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

the class MCRMODSLinksEventHandler method handleObjectCreated.

/* (non-Javadoc)
     * @see org.mycore.common.events.MCREventHandlerBase#handleObjectCreated(org.mycore.common.events.MCREvent, org.mycore.datamodel.metadata.MCRObject)
     */
@Override
protected void handleObjectCreated(final MCREvent evt, final MCRObject obj) {
    if (!MCRMODSWrapper.isSupported(obj)) {
        return;
    }
    MCRMODSWrapper modsWrapper = new MCRMODSWrapper(obj);
    final HashSet<MCRCategoryID> categories = new HashSet<>(modsWrapper.getMcrCategoryIDs());
    if (!categories.isEmpty()) {
        final MCRCategLinkReference objectReference = new MCRCategLinkReference(obj.getId());
        MCRCategLinkServiceFactory.getInstance().setLinks(objectReference, categories);
    }
    List<Element> linkingNodes = modsWrapper.getLinkedRelatedItems();
    if (!linkingNodes.isEmpty()) {
        MCRLinkTableManager linkTableManager = MCRLinkTableManager.instance();
        for (Element linkingNode : linkingNodes) {
            String targetID = linkingNode.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
            if (targetID == null) {
                continue;
            }
            String relationshipTypeRaw = linkingNode.getAttributeValue("type");
            MCRMODSRelationshipType relType = MCRMODSRelationshipType.valueOf(relationshipTypeRaw);
            // MCR-1328 (no reference links for 'host')
            if (relType != MCRMODSRelationshipType.host) {
                linkTableManager.addReferenceLink(obj.getId(), MCRObjectID.getInstance(targetID), MCRLinkTableManager.ENTRY_TYPE_REFERENCE, relType.toString());
            }
        }
    }
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Element(org.jdom2.Element) MCRLinkTableManager(org.mycore.datamodel.common.MCRLinkTableManager) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) HashSet(java.util.HashSet)

Example 78 with MCRCategoryID

use of org.mycore.datamodel.classifications2.MCRCategoryID 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 79 with MCRCategoryID

use of org.mycore.datamodel.classifications2.MCRCategoryID 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 80 with MCRCategoryID

use of org.mycore.datamodel.classifications2.MCRCategoryID 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)

Aggregations

MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)82 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)42 Test (org.junit.Test)14 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)13 Element (org.jdom2.Element)12 MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)11 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)8 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)7 HashMap (java.util.HashMap)6 MCRException (org.mycore.common.MCRException)6 EntityManager (javax.persistence.EntityManager)5 Document (org.jdom2.Document)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 Collection (java.util.Collection)4 LinkedList (java.util.LinkedList)4 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)4 MCRJSONCategory (org.mycore.frontend.classeditor.json.MCRJSONCategory)4