Search in sources :

Example 26 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method testClassEditorBatch.

@Test
public void testClassEditorBatch() throws Exception {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    Document xml = MCRXMLParserFactory.getParser().parseXML(new MCRURLContent(new URL("http://mycore.de/classifications/nameIdentifier.xml")));
    MCRCategory nameIdentifier = MCRXMLTransformer.getCategory(xml);
    MCRCategory secondCateg = nameIdentifier.getChildren().get(1);
    DAO.addCategory(null, nameIdentifier);
    startNewTransaction();
    MCRCategLinkServiceFactory.getInstance().getLinksFromCategory(secondCateg.getId());
    assertTrue(secondCateg.getId() + " should exist.", DAO.exist(secondCateg.getId()));
    // re-set labels
    DAO.setLabels(secondCateg.getId(), secondCateg.getLabels().stream().collect(Collectors.toSet()));
    // re-set URI
    entityManager.detach(DAO.setURI(secondCateg.getId(), secondCateg.getURI()));
    // move to new index
    DAO.moveCategory(secondCateg.getId(), secondCateg.getParent().getId(), 0);
    startNewTransaction();
    MCRCategoryImpl copyOfDB = MCRCategoryDAOImpl.getByNaturalID(entityManager, secondCateg.getId());
    assertNotNull(secondCateg.getId() + " must hav a parent.", copyOfDB.getParent());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) MCRURLContent(org.mycore.common.content.MCRURLContent) Document(org.jdom2.Document) URL(java.net.URL) Test(org.junit.Test)

Example 27 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method addCategoryToPosition.

/**
 * Test case for https://sourceforge.net/p/mycore/bugs/664/
 */
@Test
public void addCategoryToPosition() {
    addWorldClassification();
    MCRCategoryImpl america = new MCRCategoryImpl();
    MCRCategoryID categoryID = new MCRCategoryID(category.getId().getRootID(), "America");
    america.setId(categoryID);
    america.setLabels(new HashSet<>());
    america.getLabels().add(new MCRLabel("de", "Amerika", null));
    america.getLabels().add(new MCRLabel("en", "America", null));
    DAO.addCategory(category.getId(), america, 1);
    startNewTransaction();
    america = MCRCategoryDAOImpl.getByNaturalID(getEntityManager().get(), america.getId());
    assertNotNull(categoryID + " was not added to database.", america);
    assertEquals("invalid position in parent", 1, america.getPositionInParent());
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) Test(org.junit.Test)

Example 28 with MCRCategoryImpl

use of org.mycore.datamodel.classifications2.impl.MCRCategoryImpl 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 29 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method moveCategoryDeep.

@Test
public void moveCategoryDeep() {
    String rootIDStr = "rootID";
    MCRCategoryID rootID = MCRCategoryID.rootID(rootIDStr);
    MCRCategoryID child1ID = new MCRCategoryID(rootIDStr, "child1");
    MCRCategoryID child2ID = new MCRCategoryID(rootIDStr, "child2");
    MCRCategoryID child3ID = new MCRCategoryID(rootIDStr, "child3");
    MCRCategoryID child4ID = new MCRCategoryID(rootIDStr, "child4");
    MCRCategoryImpl root = newCategory(rootID, "root node");
    MCRCategoryImpl a = newCategory(child1ID, "child node 1");
    MCRCategoryImpl aa = newCategory(child2ID, "child node 2");
    MCRCategoryImpl aaa = newCategory(child3ID, "child node 3");
    MCRCategoryImpl aab = newCategory(child4ID, "child node 4");
    addChild(root, a);
    addChild(a, aa);
    addChild(aa, aaa);
    addChild(aa, aab);
    startNewTransaction();
    DAO.addCategory(null, root);
    endTransaction();
    startNewTransaction();
    DAO.moveCategory(child4ID, child3ID);
    endTransaction();
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) Test(org.junit.Test)

Example 30 with MCRCategoryImpl

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

the class MCRCategoryDAOImplTest method moveCategoryWithoutIndex.

@Test
public void moveCategoryWithoutIndex() throws SQLException {
    addWorldClassification();
    checkLeftRightLevelValue(getRootCategoryFromSession(), 0, 0);
    startNewTransaction();
    MCRCategory moveNode = category.getChildren().get(1);
    // Europe conquer Asia
    DAO.moveCategory(moveNode.getId(), category.getChildren().get(0).getId());
    startNewTransaction();
    MCRCategoryImpl rootNode = getRootCategoryFromSession();
    checkLeftRightLevelValue(rootNode, 0, 0);
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Test(org.junit.Test)

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