use of org.mycore.datamodel.classifications2.MCRCategoryID in project mycore by MyCoRe-Org.
the class MCRClassificationMappingEventHandler method createMapping.
private void createMapping(MCRObject obj) {
MCRMetaElement mappings = obj.getMetadata().getMetadataElement("mappings");
if (mappings != null) {
oldMappings = mappings.clone();
obj.getMetadata().removeMetadataElement("mappings");
}
Element currentClassElement = null;
try {
Document doc = new Document(obj.getMetadata().createXML().detach());
XPathExpression<Element> classElementPath = XPathFactory.instance().compile("//*[@categid]", Filters.element());
List<Element> classList = classElementPath.evaluate(doc);
if (classList.size() > 0) {
mappings = new MCRMetaElement();
mappings.setTag("mappings");
mappings.setClass(MCRMetaClassification.class);
mappings.setHeritable(false);
mappings.setNotInherit(true);
obj.getMetadata().setMetadataElement(mappings);
}
for (Element classElement : classList) {
currentClassElement = classElement;
MCRCategory categ = DAO.getCategory(new MCRCategoryID(classElement.getAttributeValue("classid"), classElement.getAttributeValue("categid")), 0);
addMappings(mappings, categ);
}
} catch (Exception je) {
if (currentClassElement == null) {
LOGGER.error("Error while finding classification elements", je);
} else {
LOGGER.error("Error while finding classification elements for {}", new XMLOutputter().outputString(currentClassElement), je);
}
} finally {
if (mappings == null || mappings.size() == 0) {
obj.getMetadata().removeMetadataElement("mappings");
}
}
}
use of org.mycore.datamodel.classifications2.MCRCategoryID in project mycore by MyCoRe-Org.
the class MCRWorkflowRuleParser method parseString.
/* (non-Javadoc)
* @see org.mycore.access.mcrimpl.MCRRuleParser#parseString(java.lang.String)
*/
@Override
protected MCRCondition<?> parseString(String s) {
if (s.startsWith(STATUS)) {
s = s.substring(STATUS.length()).trim();
boolean not;
String value;
if (s.startsWith(EQUALS_NOT)) {
not = true;
value = s.substring(EQUALS_NOT.length()).trim();
} else if (s.startsWith(EQUALS)) {
not = false;
value = s.substring(EQUALS.length()).trim();
} else {
throw new MCRParseException("syntax error: " + s);
}
return new MCRCategoryCondition(STATUS, new MCRCategoryID(statusClassId.getRootID(), value), not);
}
return super.parseString(s);
}
use of org.mycore.datamodel.classifications2.MCRCategoryID in project mycore by MyCoRe-Org.
the class MCRURLRetriever method getParentCollection.
private static String getParentCollection(String collection) {
MCRCategoryID categoryId = new MCRCategoryID(MCRConstants.COLLECTION_CLASS_ID.getRootID(), collection);
List<MCRCategory> parents = CATEGORY_DAO.getParents(categoryId);
if (parents == null || parents.isEmpty()) {
return null;
}
return parents.iterator().next().getId().getID();
}
use of org.mycore.datamodel.classifications2.MCRCategoryID in project mycore by MyCoRe-Org.
the class MCRCategLinkServiceImpl method countLinksForType.
@Override
public Map<MCRCategoryID, Number> countLinksForType(MCRCategory parent, String type, boolean childrenOnly) {
boolean restrictedByType = type != null;
String queryName;
if (childrenOnly) {
queryName = restrictedByType ? "NumberByTypePerChildOfParentID" : "NumberPerChildOfParentID";
} else {
queryName = restrictedByType ? "NumberByTypePerClassID" : "NumberPerClassID";
}
Map<MCRCategoryID, Number> countLinks = new HashMap<>();
Collection<MCRCategoryID> ids = childrenOnly ? getAllChildIDs(parent) : getAllCategIDs(parent);
for (MCRCategoryID id : ids) {
// initialize all categIDs with link count of zero
countLinks.put(id, 0);
}
// old classification browser/editor could not determine links correctly otherwise
if (!childrenOnly) {
parent = parent.getRoot();
} else if (!(parent instanceof MCRCategoryImpl) || ((MCRCategoryImpl) parent).getInternalID() == 0) {
parent = MCRCategoryDAOImpl.getByNaturalID(MCREntityManagerProvider.getCurrentEntityManager(), parent.getId());
}
LOGGER.info("parentID:{}", parent.getId());
String classID = parent.getId().getRootID();
Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + queryName);
// query can take long time, please cache result
q.setCacheable(true);
q.setReadOnly(true);
q.setParameter("classID", classID);
if (childrenOnly) {
q.setParameter("parentID", ((MCRCategoryImpl) parent).getInternalID());
}
if (restrictedByType) {
q.setParameter("type", type);
}
// get object count for every category (not accumulated)
@SuppressWarnings("unchecked") List<Object[]> result = (List<Object[]>) q.getResultList();
for (Object[] sr : result) {
MCRCategoryID key = new MCRCategoryID(classID, sr[0].toString());
Number value = (Number) sr[1];
countLinks.put(key, value);
}
return countLinks;
}
use of org.mycore.datamodel.classifications2.MCRCategoryID 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;
}
Aggregations