Search in sources :

Example 16 with MCRCategoryImpl

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

the class MCRCategoryDAOImpl method copyDeep.

private static MCRCategoryImpl copyDeep(MCRCategory category, int level) {
    if (category == null) {
        return null;
    }
    MCRCategoryImpl newCateg = new MCRCategoryImpl();
    int childAmount;
    try {
        childAmount = level != 0 ? category.getChildren().size() : 0;
    } catch (RuntimeException e) {
        LOGGER.error("Cannot get children size for category: {}", category.getId(), e);
        throw e;
    }
    newCateg.setChildren(new ArrayList<>(childAmount));
    newCateg.setId(category.getId());
    newCateg.setLabels(category.getLabels());
    newCateg.setRoot(category.getRoot());
    newCateg.setURI(category.getURI());
    newCateg.setLevel(category.getLevel());
    if (category instanceof MCRCategoryImpl) {
        // to allow optimized hasChildren() to work without db query
        newCateg.setLeft(((MCRCategoryImpl) category).getLeft());
        newCateg.setRight(((MCRCategoryImpl) category).getRight());
        newCateg.setInternalID(((MCRCategoryImpl) category).getInternalID());
    }
    if (childAmount > 0) {
        for (MCRCategory child : category.getChildren()) {
            newCateg.getChildren().add(copyDeep(child, level - 1));
        }
    }
    return newCateg;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Example 17 with MCRCategoryImpl

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

the class MCRCategoryDAOImpl method buildCategoryFromPrefetchedList.

private static MCRCategoryImpl buildCategoryFromPrefetchedList(List<MCRCategoryDTO> list, MCRCategoryID returnID) {
    LOGGER.debug(() -> "using prefetched list: " + list);
    MCRCategoryImpl predecessor = null;
    for (MCRCategoryDTO entry : list) {
        predecessor = entry.merge(predecessor);
    }
    return MCRStreamUtils.flatten(predecessor.getRoot(), MCRCategory::getChildren, Collection::parallelStream).filter(c -> c.getId().equals(returnID)).findFirst().map(MCRCategoryImpl.class::cast).orElseThrow(() -> new MCRException("Could not find " + returnID + " in database result."));
}
Also used : NoResultException(javax.persistence.NoResultException) HashMap(java.util.HashMap) FlushModeType(javax.persistence.FlushModeType) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) URI(java.net.URI) Collector(java.util.stream.Collector) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Set(java.util.Set) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) AbstractMap(java.util.AbstractMap) List(java.util.List) Query(javax.persistence.Query) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) MCRStreamUtils(org.mycore.common.MCRStreamUtils) LogManager(org.apache.logging.log4j.LogManager) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRException(org.mycore.common.MCRException) Collection(java.util.Collection)

Example 18 with MCRCategoryImpl

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

the class MCRCategoryDTO method merge.

public MCRCategoryImpl merge(MCRCategoryImpl predecessor) {
    if (predecessor == null) {
        return toCategory();
    }
    if (predecessor.getInternalID() == internalID) {
        // only add label
        return appendLabel(predecessor);
    }
    MCRCategoryImpl cat = toCategory();
    MCRCategory parent = predecessor;
    while (parent.getLevel() >= level) {
        parent = parent.getParent();
    }
    parent.getChildren().add(cat);
    // is reset to parent.level+1 in step before
    cat.setLevel(level);
    return cat;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Example 19 with MCRCategoryImpl

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

the class MCRCategoryDTO method appendLabel.

private MCRCategoryImpl appendLabel(MCRCategoryImpl cat) {
    if (lang != null) {
        MCRLabel label = new MCRLabel(lang, text, Optional.ofNullable(description).filter(s -> !s.isEmpty()).orElse(null));
        cat.getLabels().add(label);
    }
    return cat;
}
Also used : MCRLabel(org.mycore.datamodel.classifications2.MCRLabel)

Example 20 with MCRCategoryImpl

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

the class MCRCategoryImpl method calculateLeftRightAndLevel.

/**
 * calculates left and right value throug the subtree rooted at
 * <code>co</code>.
 *
 * @param leftStart
 *            this.left will be set to this value
 * @param levelStart
 *            this.getLevel() will return this value
 * @return this.right
 */
public int calculateLeftRightAndLevel(int leftStart, int levelStart) {
    int curValue = leftStart;
    final int nextLevel = levelStart + 1;
    setLeft(leftStart);
    setLevel(levelStart);
    for (MCRCategory child : getChildren()) {
        LOGGER.debug(child.getId());
        curValue = ((MCRCategoryImpl) child).calculateLeftRightAndLevel(++curValue, nextLevel);
    }
    setRight(++curValue);
    return curValue;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) UniqueConstraint(javax.persistence.UniqueConstraint)

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