Search in sources :

Example 31 with MCRCategoryImpl

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

the class GsonSerializationTest method createCateg.

protected MCRCategoryImpl createCateg(String rootID, String id2, String text) {
    MCRCategoryImpl mcrCategoryImpl = new MCRCategoryImpl();
    MCRCategoryID id = new MCRCategoryID(rootID, id2);
    mcrCategoryImpl.setId(id);
    Set<MCRLabel> labels = new HashSet<>();
    labels.add(new MCRLabel("de", text + "_de", "desc_" + text + "_de"));
    labels.add(new MCRLabel("en", text + "_en", "desc_" + text + "_en"));
    mcrCategoryImpl.setLabels(labels);
    return mcrCategoryImpl;
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) HashSet(java.util.HashSet)

Example 32 with MCRCategoryImpl

use of org.mycore.datamodel.classifications2.impl.MCRCategoryImpl 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 33 with MCRCategoryImpl

use of org.mycore.datamodel.classifications2.impl.MCRCategoryImpl 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 34 with MCRCategoryImpl

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

the class MCRCategoryDAOImpl method repairLeftRightValue.

public void repairLeftRightValue(String classID) {
    final MCRCategoryID rootID = MCRCategoryID.rootID(classID);
    withoutFlush(MCREntityManagerProvider.getCurrentEntityManager(), true, entityManager -> {
        MCRCategoryImpl classification = MCRCategoryDAOImpl.getByNaturalID(entityManager, rootID);
        classification.calculateLeftRightAndLevel(Integer.MAX_VALUE / 2, LEVEL_START_VALUE);
        entityManager.flush();
        classification.calculateLeftRightAndLevel(LEFT_START_VALUE, LEVEL_START_VALUE);
    });
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 35 with MCRCategoryImpl

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

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)17 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)17 Test (org.junit.Test)15 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)10 MCRCategoryImpl (org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)7 EntityManager (javax.persistence.EntityManager)6 HashMap (java.util.HashMap)4 MCRException (org.mycore.common.MCRException)4 MCRPersistenceException (org.mycore.common.MCRPersistenceException)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 AbstractMap (java.util.AbstractMap)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 BiConsumer (java.util.function.BiConsumer)2 Consumer (java.util.function.Consumer)2