Search in sources :

Example 86 with MCRCategory

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

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

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

Example 89 with MCRCategory

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

the class MCRCategoryDAOImpl method deleteCategory.

@Override
public void deleteCategory(MCRCategoryID id) {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    LOGGER.debug("Will get: {}", id);
    MCRCategoryImpl category = getByNaturalID(entityManager, id);
    if (category == null) {
        throw new MCRPersistenceException("Category " + id + " was not found. Delete aborted.");
    }
    LOGGER.debug("Will delete: {}", category.getId());
    MCRCategory parent = category.parent;
    category.detachFromParent();
    entityManager.remove(category);
    if (parent != null) {
        entityManager.flush();
        LOGGER.debug("Left: {} Right: {}", category.getLeft(), category.getRight());
        // always add +1 for the currentNode
        int nodes = 1 + (category.getRight() - category.getLeft()) / 2;
        final int increment = nodes * -2;
        // decrement left and right values by nodes
        updateLeftRightValue(entityManager, category.getRootID(), category.getLeft(), increment);
    }
    updateTimeStamp();
    updateLastModified(category.getRootID());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 90 with MCRCategory

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

the class MCRXMLTransformer method buildCategory.

public static MCRCategory buildCategory(String classID, Element e, MCRCategory parent) throws URISyntaxException {
    MCRCategoryImpl category = new MCRCategoryImpl();
    // setId must be called before setParent (info important)
    category.setId(new MCRCategoryID(classID, e.getAttributeValue("ID")));
    category.setRoot(parent.getRoot());
    category.setChildren(new ArrayList<>());
    category.setParent(parent);
    try {
        category.setLabels(getLabels(e.getChildren("label")));
    } catch (NullPointerException | IllegalArgumentException ex) {
        throw new MCRException("Error while adding labels to category: " + category.getId(), ex);
    }
    category.setLevel(parent.getLevel() + 1);
    setURL(e, category);
    buildChildCategories(classID, e.getChildren("category"), category);
    return category;
}
Also used : MCRException(org.mycore.common.MCRException) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)

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