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;
}));
}
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;
}
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;
}
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."));
}
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;
}
Aggregations