Search in sources :

Example 21 with MCRLabel

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

the class MCRCategoryDAOImplTest method addCategory.

@Test
public void addCategory() throws MCRException {
    addWorldClassification();
    assertTrue("Exist check failed for Category " + category.getId(), DAO.exist(category.getId()));
    MCRCategoryImpl india = new MCRCategoryImpl();
    india.setId(new MCRCategoryID(category.getId().getRootID(), "India"));
    india.setLabels(new HashSet<>());
    india.getLabels().add(new MCRLabel("de", "Indien", null));
    india.getLabels().add(new MCRLabel("en", "India", null));
    DAO.addCategory(new MCRCategoryID(category.getId().getRootID(), "Asia"), india);
    startNewTransaction();
    assertTrue("Exist check failed for Category " + india.getId(), DAO.exist(india.getId()));
    MCRCategoryImpl rootCategory = getRootCategoryFromSession();
    assertEquals("Child category count does not match.", category.getChildren().size(), rootCategory.getChildren().size());
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Number> countQuery = cb.createQuery(Number.class);
    long allNodes = em.createQuery(countQuery.select(cb.count(countQuery.from(MCRCategoryImpl.class)))).getSingleResult().longValue();
    // category + india
    assertEquals("Complete category count does not match.", countNodes(category) + 1, allNodes);
    assertTrue("No root category present", rootCategory.getRoot() != null);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) Test(org.junit.Test)

Example 22 with MCRLabel

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

the class MCRClassificationBrowser2 method addLabel.

/**
 * Add label in current lang, otherwise default lang, optional with
 * description
 */
private void addLabel(HttpServletRequest req, MCRCategory child, Element category) {
    MCRLabel label = child.getCurrentLabel().orElseThrow(() -> new MCRException("Category " + child.getId() + " has no labels."));
    category.addContent(new Element("label").setText(label.getText()));
    // if true, add description
    boolean descr = Boolean.valueOf(req.getParameter("adddescription"));
    if (descr && (label.getDescription() != null)) {
        category.addContent(new Element("description").setText(label.getDescription()));
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel)

Example 23 with MCRLabel

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

the class MCRLabelSetTypeAdapter method jsonLabelToMCRLabel.

private MCRLabel jsonLabelToMCRLabel(JsonObject labelJsonObject) {
    String lang = labelJsonObject.get(LANG).getAsString();
    String text = labelJsonObject.get(TEXT).getAsString();
    JsonElement jsonElement = labelJsonObject.get(DESCRIPTION);
    String description = null;
    if (jsonElement != null) {
        description = jsonElement.getAsString();
    }
    if (lang == null || lang.trim().equals("") || text == null || text.trim().equals("")) {
        return null;
    }
    return new MCRLabel(lang, text, description);
}
Also used : JsonElement(com.google.gson.JsonElement) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel)

Example 24 with MCRLabel

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

the class MCRLabelSetTypeAdapter method deserialize.

@Override
public MCRLabelSetWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    Set<MCRLabel> labels = new HashSet<>();
    for (JsonElement jsonElement : json.getAsJsonArray()) {
        JsonObject labelJsonObject = jsonElement.getAsJsonObject();
        MCRLabel label = jsonLabelToMCRLabel(labelJsonObject);
        if (label != null) {
            labels.add(label);
        } else {
            LOGGER.warn("Unable to add label with empty lang or text: {}", labelJsonObject);
        }
    }
    return new MCRLabelSetWrapper(labels);
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) MCRLabelSetWrapper(org.mycore.frontend.classeditor.wrapper.MCRLabelSetWrapper) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) HashSet(java.util.HashSet)

Example 25 with MCRLabel

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

the class GsonSerializationTest method createCateg.

protected MCRCategoryImpl createCateg(String rootID, String id2, String text) {
    MCRCategoryImpl mcrCategoryImpl = new MCRCategoryImpl();
    MCRCategoryID id = new MCRCategoryID(rootID, id2);
    mcrCategoryImpl.setId(id);
    Set<MCRLabel> labels = new HashSet<>();
    labels.add(new MCRLabel("de", text + "_de", "desc_" + text + "_de"));
    labels.add(new MCRLabel("en", text + "_en", "desc_" + text + "_en"));
    mcrCategoryImpl.setLabels(labels);
    return mcrCategoryImpl;
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) HashSet(java.util.HashSet)

Aggregations

MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)30 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)14 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)11 HashSet (java.util.HashSet)8 Test (org.junit.Test)6 JsonObject (com.google.gson.JsonObject)4 Element (org.jdom2.Element)4 MCRException (org.mycore.common.MCRException)3 MCRLabelSetWrapper (org.mycore.frontend.classeditor.wrapper.MCRLabelSetWrapper)3 JsonElement (com.google.gson.JsonElement)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 MCRCategoryImpl (org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)2 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)2 JsonArray (com.google.gson.JsonArray)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1