use of org.mycore.datamodel.classifications2.MCRCategory 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);
});
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRCategLinkServiceImpl method getMCRCategory.
private static MCRCategory getMCRCategory(EntityManager entityManager, MCRCategoryID categID) {
MCRCategory categ = categCache.getIfUpToDate(categID, DAO.getLastModified());
if (categ != null) {
return categ;
}
categ = MCRCategoryDAOImpl.getByNaturalID(entityManager, categID);
if (categ == null) {
return null;
}
categCache.put(categID, categ);
return categ;
}
use of org.mycore.datamodel.classifications2.MCRCategory 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;
}
use of org.mycore.datamodel.classifications2.MCRCategory 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());
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRXMLTransformer method buildCategory.
public static MCRCategory buildCategory(String classID, Element e, MCRCategory parent) throws URISyntaxException {
MCRCategoryImpl category = new MCRCategoryImpl();
// setId must be called before setParent (info important)
category.setId(new MCRCategoryID(classID, e.getAttributeValue("ID")));
category.setRoot(parent.getRoot());
category.setChildren(new ArrayList<>());
category.setParent(parent);
try {
category.setLabels(getLabels(e.getChildren("label")));
} catch (NullPointerException | IllegalArgumentException ex) {
throw new MCRException("Error while adding labels to category: " + category.getId(), ex);
}
category.setLevel(parent.getLevel() + 1);
setURL(e, category);
buildChildCategories(classID, e.getChildren("category"), category);
return category;
}
Aggregations