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