Search in sources :

Example 46 with MCRCategory

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

the class MCRCategoryDAOImpl method getRootCategories.

@Override
@SuppressWarnings("unchecked")
public List<MCRCategory> getRootCategories() {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    List<MCRCategoryDTO> resultList = entityManager.createNamedQuery(NAMED_QUERY_NAMESPACE + "rootCategs").getResultList();
    BiConsumer<List<MCRCategory>, MCRCategoryImpl> merge = (l, c) -> {
        MCRCategoryImpl last = (MCRCategoryImpl) l.get(l.size() - 1);
        if (last.getInternalID() != c.getInternalID()) {
            l.add(c);
        } else {
            last.getLabels().addAll(c.getLabels());
        }
    };
    return resultList.parallelStream().map(c -> c.merge(null)).collect(Collector.of(ArrayList::new, (ArrayList<MCRCategory> l, MCRCategoryImpl c) -> {
        if (l.isEmpty()) {
            l.add(c);
        } else {
            merge.accept(l, c);
        }
    }, (l, r) -> {
        if (l.isEmpty()) {
            return r;
        }
        if (r.isEmpty()) {
            return l;
        }
        MCRCategoryImpl first = (MCRCategoryImpl) r.get(0);
        merge.accept(l, first);
        l.addAll(r.subList(1, r.size()));
        return l;
    }));
}
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) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) List(java.util.List)

Example 47 with MCRCategory

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

the class MCRCategoryDAOImpl method getParents.

@Override
public List<MCRCategory> getParents(MCRCategoryID id) {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    MCRCategoryDTO leftRight = getLeftRightLevelValues(entityManager, id);
    if (leftRight == null) {
        return null;
    }
    Query parentQuery = entityManager.createNamedQuery(NAMED_QUERY_NAMESPACE + "parentQuery").setParameter("classID", id.getRootID()).setParameter("categID", id.getID()).setParameter("left", leftRight.leftValue).setParameter("right", leftRight.rightValue);
    @SuppressWarnings("unchecked") List<MCRCategoryDTO> resultList = parentQuery.getResultList();
    MCRCategory category = buildCategoryFromPrefetchedList(resultList, id);
    List<MCRCategory> parents = new ArrayList<>();
    while (category.getParent() != null) {
        category = category.getParent();
        parents.add(category);
    }
    return parents;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) ArrayList(java.util.ArrayList)

Example 48 with MCRCategory

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

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

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

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