Search in sources :

Example 81 with MCRCategoryID

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

the class MCRXMLFunctions method isInCategory.

/**
 * Verifies if object is in specified category.
 * @see MCRCategLinkService#isInCategory(MCRCategLinkReference, MCRCategoryID)
 * @param objectId valid MCRObjectID as String
 * @param categoryId valid MCRCategoryID as String
 * @return true if object is in category, else false
 */
public static boolean isInCategory(String objectId, String categoryId) {
    try {
        MCRCategoryID categID = MCRCategoryID.fromString(categoryId);
        MCRObjectID mcrObjectID = MCRObjectID.getInstance(objectId);
        MCRCategLinkReference reference = new MCRCategLinkReference(mcrObjectID);
        return MCRCategLinkServiceHolder.instance.isInCategory(reference, categID);
    } catch (Throwable e) {
        LOGGER.error("Error while checking if object is in category", e);
        return false;
    }
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference)

Example 82 with MCRCategoryID

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

Example 83 with MCRCategoryID

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

the class MCRCategLinkServiceImpl method setLinks.

@Override
public void setLinks(MCRCategLinkReference objectReference, Collection<MCRCategoryID> categories) {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    categories.stream().distinct().forEach(categID -> {
        final MCRCategory category = getMCRCategory(entityManager, categID);
        if (category == null) {
            throw new MCRPersistenceException("Could not link to unknown category " + categID);
        }
        MCRCategoryLinkImpl link = new MCRCategoryLinkImpl(category, objectReference);
        if (LOGGER.isDebugEnabled()) {
            MCRCategory linkedCategory = link.getCategory();
            StringBuilder debugMessage = new StringBuilder("Adding Link from ").append(linkedCategory.getId());
            if (linkedCategory instanceof MCRCategoryImpl) {
                debugMessage.append("(").append(((MCRCategoryImpl) linkedCategory).getInternalID()).append(") ");
            }
            debugMessage.append("to ").append(objectReference);
            LOGGER.debug(debugMessage.toString());
        }
        entityManager.persist(link);
        LOGGER.debug("===DONE: {}", link.id);
    });
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 84 with MCRCategoryID

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

the class MCRCategLinkServiceImpl method getMCRCategory.

private static MCRCategory getMCRCategory(EntityManager entityManager, MCRCategoryID categID) {
    MCRCategory categ = categCache.getIfUpToDate(categID, DAO.getLastModified());
    if (categ != null) {
        return categ;
    }
    categ = MCRCategoryDAOImpl.getByNaturalID(entityManager, categID);
    if (categ == null) {
        return null;
    }
    categCache.put(categID, categ);
    return categ;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Example 85 with MCRCategoryID

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

the class MCRCategLinkServiceImpl method hasLinks.

@Override
public Map<MCRCategoryID, Boolean> hasLinks(MCRCategory category) {
    if (category == null) {
        return hasLinksForClassifications();
    }
    MCRCategoryImpl rootImpl = (MCRCategoryImpl) MCRCategoryDAOFactory.getInstance().getCategory(category.getRoot().getId(), -1);
    if (rootImpl == null) {
        // Category does not exist, so it has no links
        return getNoLinksMap(category);
    }
    HashMap<MCRCategoryID, Boolean> boolMap = new HashMap<>();
    final BitSet linkedInternalIds = getLinkedInternalIds();
    storeHasLinkValues(boolMap, linkedInternalIds, rootImpl);
    return boolMap;
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) HashMap(java.util.HashMap) BitSet(java.util.BitSet)

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