Search in sources :

Example 16 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRCategoryImplTest method getLeftSiblingOrParent.

@Test
public void getLeftSiblingOrParent() throws URISyntaxException, MCRException, SAXParseException, IOException {
    loadWorldClassification();
    MCRCategory europe = category.getChildren().get(0);
    MCRCategoryImpl asia = (MCRCategoryImpl) category.getChildren().get(1);
    MCRCategoryImpl germany = (MCRCategoryImpl) europe.getChildren().get(0);
    assertEquals("Did not get Europe as left sibling of Asia", europe.getId(), asia.getLeftSiblingOrParent().getId());
    assertEquals("Did not get Europa as left sibling or ancestor of Germany", europe.getId(), germany.getLeftSiblingOrParent().getId());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Test(org.junit.Test)

Example 17 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRClassificationBrowser2 method countLinks.

/**
 * Add link count to each category
 */
private void countLinks(HttpServletRequest req, boolean emptyLeaves, String objectType, MCRCategory category, List<Element> data) {
    if (!Boolean.valueOf(req.getParameter("countlinks"))) {
        return;
    }
    if (objectType != null && objectType.trim().length() == 0) {
        objectType = null;
    }
    String classifID = category.getId().getRootID();
    Map<MCRCategoryID, Number> count = MCRCategLinkServiceFactory.getInstance().countLinksForType(category, objectType, true);
    for (Iterator<Element> it = data.iterator(); it.hasNext(); ) {
        Element child = it.next();
        MCRCategoryID childID = new MCRCategoryID(classifID, child.getAttributeValue("id"));
        int num = (count.containsKey(childID) ? count.get(childID).intValue() : 0);
        child.setAttribute("numLinks", String.valueOf(num));
        if ((!emptyLeaves) && (num < 1)) {
            it.remove();
        }
    }
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Element(org.jdom2.Element)

Example 18 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRClassificationBrowser2 method doGetPost.

@Override
public void doGetPost(MCRServletJob job) throws Exception {
    long time = System.nanoTime();
    HttpServletRequest req = job.getRequest();
    String classifID = req.getParameter("classification");
    String categID = req.getParameter("category");
    boolean countResults = Boolean.valueOf(req.getParameter("countresults"));
    boolean addClassId = Boolean.valueOf(req.getParameter("addclassid"));
    boolean uri = Boolean.valueOf(req.getParameter("adduri"));
    String el = req.getParameter("emptyleaves");
    boolean emptyLeaves = true;
    if ((el != null) && (el.trim().length() > 0)) {
        emptyLeaves = Boolean.valueOf(el);
    }
    LOGGER.info("ClassificationBrowser {} {}", classifID, categID == null ? "" : categID);
    MCRCategoryID id = new MCRCategoryID(classifID, categID);
    Element xml = new Element("classificationBrowserData");
    xml.setAttribute("classification", classifID);
    xml.setAttribute("webpage", req.getParameter("webpage"));
    MCRQueryAdapter queryAdapter = null;
    String field = req.getParameter("field");
    if (countResults || (field != null && field.length() > 0)) {
        queryAdapter = getQueryAdapter(field);
        configureQueryAdapter(queryAdapter, req);
        if (queryAdapter.getObjectType() != null) {
            xml.setAttribute("objectType", queryAdapter.getObjectType());
        }
    }
    String parameters = req.getParameter("parameters");
    if (parameters != null) {
        xml.setAttribute("parameters", parameters);
    }
    List<Element> data = new ArrayList<>();
    MCRCategory category = MCRCategoryDAOFactory.getInstance().getCategory(id, 1);
    if (category == null) {
        job.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find category: " + id);
        return;
    }
    for (MCRCategory child : category.getChildren()) {
        String childID = child.getId().getID();
        long numResults = 0;
        if (queryAdapter != null) {
            queryAdapter.setCategory(addClassId ? child.getId().toString() : childID);
        }
        if (countResults) {
            numResults = queryAdapter.getResultCount();
            if ((!emptyLeaves) && (numResults < 1)) {
                continue;
            }
        }
        Element categoryE = new Element("category");
        data.add(categoryE);
        if (countResults) {
            categoryE.setAttribute("numResults", String.valueOf(numResults));
        }
        categoryE.setAttribute("id", childID);
        categoryE.setAttribute("children", Boolean.toString(child.hasChildren()));
        if (queryAdapter != null) {
            categoryE.setAttribute("query", queryAdapter.getQueryAsString());
        }
        if (uri && (child.getURI() != null)) {
            categoryE.addContent(new Element("uri").setText(child.getURI().toString()));
        }
        addLabel(req, child, categoryE);
    }
    String objectType = queryAdapter == null ? null : queryAdapter.getObjectType();
    countLinks(req, emptyLeaves, objectType, category, data);
    sortCategories(req, data);
    xml.addContent(data);
    renderToHTML(job, req, xml);
    time = (System.nanoTime() - time) / 1000000;
    LOGGER.info("ClassificationBrowser finished in {} ms", time);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Element(org.jdom2.Element) ArrayList(java.util.ArrayList)

Example 19 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRCategoryListTypeAdapter method createCategRefJSONObj.

private JsonElement createCategRefJSONObj(MCRCategory categ, Boolean hasLink) {
    JsonObject categRefJsonObject = new JsonObject();
    categRefJsonObject.add(ID, serializationContext.serialize(categ.getId()));
    Set<MCRLabel> labels = categ.getLabels();
    categRefJsonObject.add(LABELS, serializationContext.serialize(new MCRLabelSetWrapper(labels)));
    URI uri = categ.getURI();
    if (uri != null) {
        categRefJsonObject.addProperty(URISTR, uri.toString());
    }
    categRefJsonObject.addProperty(HASCHILDREN, categ.hasChildren());
    categRefJsonObject.addProperty(HASLINK, hasLink);
    return categRefJsonObject;
}
Also used : JsonObject(com.google.gson.JsonObject) MCRLabelSetWrapper(org.mycore.frontend.classeditor.wrapper.MCRLabelSetWrapper) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) URI(java.net.URI)

Example 20 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRCategoryTypeAdapter method serialize.

@Override
public JsonElement serialize(MCRJSONCategory category, Type arg1, JsonSerializationContext contextSerialization) {
    JsonObject rubricJsonObject = new JsonObject();
    MCRCategoryID id = category.getId();
    if (id != null) {
        rubricJsonObject.add(ID, contextSerialization.serialize(id));
    }
    Set<MCRLabel> labels = category.getLabels();
    rubricJsonObject.add(LABELS, contextSerialization.serialize(new MCRLabelSetWrapper(labels)));
    URI uri = category.getURI();
    if (uri != null) {
        rubricJsonObject.addProperty(URISTR, uri.toString());
    }
    if (category.hasChildren()) {
        List<MCRCategory> children = category.getChildren();
        Map<MCRCategoryID, Boolean> linkMap = getLinkService().hasLinks(category);
        if (linkMap.values().contains(true)) {
            rubricJsonObject.addProperty(HASLINK, true);
        }
        rubricJsonObject.add(CHILDREN, contextSerialization.serialize(new MCRCategoryListWrapper(children, linkMap)));
    }
    return rubricJsonObject;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryListWrapper(org.mycore.frontend.classeditor.wrapper.MCRCategoryListWrapper) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) JsonObject(com.google.gson.JsonObject) MCRLabelSetWrapper(org.mycore.frontend.classeditor.wrapper.MCRLabelSetWrapper) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) URI(java.net.URI)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)87 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)36 Test (org.junit.Test)24 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)17 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)10 ArrayList (java.util.ArrayList)9 Document (org.jdom2.Document)9 Element (org.jdom2.Element)8 MCRException (org.mycore.common.MCRException)8 IOException (java.io.IOException)6 EntityManager (javax.persistence.EntityManager)6 URI (java.net.URI)5 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4