Search in sources :

Example 1 with MCRCategoryImpl

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

the class MCRXMLTransformer method getCategory.

public static MCRCategory getCategory(Document xml) throws URISyntaxException {
    MCRCategoryImpl category = new MCRCategoryImpl();
    category.setRoot(category);
    final String classID = xml.getRootElement().getAttributeValue("ID");
    category.setLevel(0);
    category.setId(MCRCategoryID.rootID(classID));
    setURL(xml.getRootElement(), category);
    // setChildren has to be called before setParent (below) can be called without
    // database access see: org.mycore.datamodel.classifications2.impl.MCRAbstractCategoryImpl.getChildren()
    category.setChildren(new ArrayList<>());
    buildChildCategories(classID, xml.getRootElement().getChild("categories").getChildren("category"), category);
    try {
        category.setLabels(getLabels(xml.getRootElement().getChildren("label")));
    } catch (NullPointerException | IllegalArgumentException ex) {
        throw new MCRException("Error while adding labels to classification: " + classID, ex);
    }
    return category;
}
Also used : MCRException(org.mycore.common.MCRException) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)

Example 2 with MCRCategoryImpl

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

the class MCRCategLinkServiceImplTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    if (SERVICE == null) {
        SERVICE = new MCRCategLinkServiceImpl();
    }
    loadWorldClassification();
    MCRCategoryImpl germany = (MCRCategoryImpl) category.getChildren().get(0).getChildren().get(0);
    MCRCategoryImpl uk = (MCRCategoryImpl) category.getChildren().get(0).getChildren().get(1);
    DAO.addCategory(null, category);
    testLinks = new ArrayList<>();
    testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Jena", "city")));
    testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Thüringen", "state")));
    testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Hessen", "state")));
    testLinks.add(new MCRCategoryLinkImpl(germany, new MCRCategLinkReference("Saale", "river")));
    final MCRCategLinkReference northSeaReference = new MCRCategLinkReference("North Sea", "sea");
    testLinks.add(new MCRCategoryLinkImpl(germany, northSeaReference));
    testLinks.add(new MCRCategoryLinkImpl(uk, LONDON_REFERENCE));
    testLinks.add(new MCRCategoryLinkImpl(uk, ENGLAND_REFERENCE));
    testLinks.add(new MCRCategoryLinkImpl(uk, new MCRCategLinkReference("Thames", "river")));
    testLinks.add(new MCRCategoryLinkImpl(uk, northSeaReference));
}
Also used : MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) Before(org.junit.Before)

Example 3 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method addCategorySingleSteps.

/**
 * Test case for https://sourceforge.net/p/mycore/bugs/612/
 */
@Test
public void addCategorySingleSteps() {
    MCRCategory root = new MCRCategoryImpl();
    MCRCategoryID rootID = MCRCategoryID.rootID("junit");
    root.setId(rootID);
    DAO.addCategory(null, root);
    startNewTransaction();
    MCRCategory a = new MCRCategoryImpl();
    MCRCategoryID aId = new MCRCategoryID(rootID.getRootID(), "a");
    a.setId(aId);
    MCRCategory b = new MCRCategoryImpl();
    MCRCategoryID bId = new MCRCategoryID(rootID.getRootID(), "b");
    b.setId(bId);
    MCRCategory c = new MCRCategoryImpl();
    MCRCategoryID cId = new MCRCategoryID(rootID.getRootID(), "c");
    c.setId(cId);
    DAO.addCategory(rootID, a);
    DAO.addCategory(rootID, c);
    DAO.addCategory(aId, b);
    startNewTransaction();
    assertTrue("Category c should not contain child categories.", DAO.getChildren(cId).isEmpty());
    assertFalse("Category a should contain child categories.", DAO.getChildren(aId).isEmpty());
    root = DAO.getCategory(rootID, -1);
    checkLeftRightLevelValue((MCRCategoryImpl) root, 0, 0);
    assertEquals("Did not get all categories", 4, countNodes(root));
    List<MCRCategory> children = root.getChildren();
    assertEquals("Children count mismatch", 2, children.size());
    a = children.get(0);
    c = children.get(1);
    assertEquals("Wrong order of children", aId, a.getId());
    assertEquals("Wrong order of children", cId, c.getId());
    assertTrue("Category c should not contain child categories.", c.getChildren().isEmpty());
    assertFalse("Category a should contain child categories.", a.getChildren().isEmpty());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Test(org.junit.Test)

Example 4 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method replaceCategory.

@Test
public void replaceCategory() throws URISyntaxException, MCRException, SAXParseException, IOException {
    loadWorldClassification2();
    addWorldClassification();
    DAO.replaceCategory(category2);
    startNewTransaction();
    MCRCategoryImpl rootNode = getRootCategoryFromSession();
    assertEquals("Category count does not match.", countNodes(category2), countNodes(rootNode));
    assertEquals("Label count does not match.", category2.getChildren().get(0).getLabels().size(), rootNode.getChildren().get(0).getLabels().size());
    checkLeftRightLevelValue(rootNode, 0, 0);
    final URI germanURI = new URI("http://www.deutschland.de");
    final MCRCategory germany = rootNode.getChildren().get(0).getChildren().get(0);
    assertEquals("URI was not updated", germanURI, germany.getURI());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) URI(java.net.URI) Test(org.junit.Test)

Example 5 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method checkLeftRightLevelValue.

private int checkLeftRightLevelValue(MCRCategoryImpl node, int leftStart, int levelStart) {
    int curValue = leftStart;
    final int nextLevel = levelStart + 1;
    assertEquals("Left value did not match on ID: " + node.getId(), leftStart, node.getLeft());
    assertEquals("Level value did not match on ID: " + node.getId(), levelStart, node.getLevel());
    for (MCRCategory child : node.getChildren()) {
        curValue = checkLeftRightLevelValue((MCRCategoryImpl) child, ++curValue, nextLevel);
    }
    assertEquals("Right value did not match on ID: " + node.getId(), ++curValue, node.getRight());
    return curValue;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)17 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)17 Test (org.junit.Test)15 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)10 MCRCategoryImpl (org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)7 EntityManager (javax.persistence.EntityManager)6 HashMap (java.util.HashMap)4 MCRException (org.mycore.common.MCRException)4 MCRPersistenceException (org.mycore.common.MCRPersistenceException)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 AbstractMap (java.util.AbstractMap)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 BiConsumer (java.util.function.BiConsumer)2 Consumer (java.util.function.Consumer)2