Search in sources :

Example 36 with MCRCategoryID

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");
        }
    }
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) XMLOutputter(org.jdom2.output.XMLOutputter) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 37 with MCRCategoryID

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);
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRParseException(org.mycore.parsers.bool.MCRParseException)

Example 38 with MCRCategoryID

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();
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID)

Example 39 with MCRCategoryID

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;
}
Also used : HashMap(java.util.HashMap) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) LinkedList(java.util.LinkedList) List(java.util.List)

Example 40 with MCRCategoryID

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;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) ArrayList(java.util.ArrayList)

Aggregations

MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)82 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)42 Test (org.junit.Test)14 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)13 Element (org.jdom2.Element)12 MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)11 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)8 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)7 HashMap (java.util.HashMap)6 MCRException (org.mycore.common.MCRException)6 EntityManager (javax.persistence.EntityManager)5 Document (org.jdom2.Document)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 Collection (java.util.Collection)4 LinkedList (java.util.LinkedList)4 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)4 MCRJSONCategory (org.mycore.frontend.classeditor.json.MCRJSONCategory)4