Search in sources :

Example 11 with MCRCategoryID

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

the class MCRCategoryImplTest method calculateLeftRightAndLevel.

/**
 * Test method for {@link org.mycore.datamodel.classifications2.impl.MCRCategoryImpl#calculateLeftRightAndLevel(int, int)}.
 */
@Test
public void calculateLeftRightAndLevel() {
    MCRCategoryImpl rootNode = buildNode(MCRCategoryID.rootID("co1"));
    final int leftStart = 1;
    final int levelStart = 0;
    assertEquals(2, rootNode.calculateLeftRightAndLevel(leftStart, levelStart));
    assertEquals(levelStart, rootNode.getLevel());
    MCRCategoryImpl co2 = buildNode(new MCRCategoryID(rootNode.getId().getRootID(), "co2"));
    rootNode.getChildren().add(co2);
    assertEquals(4, rootNode.calculateLeftRightAndLevel(leftStart, levelStart));
    assertEquals(leftStart, co2.getLevel());
    MCRCategoryImpl co3 = buildNode(new MCRCategoryID(rootNode.getId().getRootID(), "co3"));
    rootNode.getChildren().add(co3);
    assertEquals(6, rootNode.calculateLeftRightAndLevel(leftStart, levelStart));
    assertEquals(leftStart, co3.getLevel());
    MCRCategoryImpl co4 = buildNode(new MCRCategoryID(rootNode.getId().getRootID(), "co4"));
    co3.getChildren().add(co4);
    assertEquals(8, rootNode.calculateLeftRightAndLevel(leftStart, levelStart));
    assertEquals(2, co4.getLevel());
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Test(org.junit.Test)

Example 12 with MCRCategoryID

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

the class MCRCategoryImplTest method getLeftSiblingOrOfAncestor.

@Test
public void getLeftSiblingOrOfAncestor() 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.getLeftSiblingOrOfAncestor().getId());
    assertEquals("Did not get World as left sibling or ancestor of Germany", category.getId(), germany.getLeftSiblingOrOfAncestor().getId());
    MCRCategoryImpl america = buildNode(new MCRCategoryID(category.getRootID(), "America"));
    category.getChildren().add(0, america);
    assertEquals("Did not get America as left sibling or ancestor of Germany", america.getId(), germany.getLeftSiblingOrOfAncestor().getId());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Test(org.junit.Test)

Example 13 with MCRCategoryID

use of org.mycore.datamodel.classifications2.MCRCategoryID 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 14 with MCRCategoryID

use of org.mycore.datamodel.classifications2.MCRCategoryID 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 15 with MCRCategoryID

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

the class MCRCategoryIDTypeAdapter method deserialize.

@Override
public MCRCategoryID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject idJsonObj = json.getAsJsonObject();
    JsonElement rootIDObj = idJsonObj.get(ROOTID);
    JsonElement categIDObj = idJsonObj.get(CATEGID);
    String rootID = rootIDObj.getAsString();
    String categID = categIDObj == null ? "" : categIDObj.getAsString();
    return new MCRCategoryID(rootID, categID);
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

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