Search in sources :

Example 11 with MCRCategoryImpl

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

the class MCRClassification2Commands method checkClassification.

@MCRCommand(syntax = "check classification {0}", help = "checks if all redundant information are stored without conflicts", order = 150)
public static void checkClassification(String id) {
    LOGGER.info("Checking classifcation {}", id);
    ArrayList<String> log = new ArrayList<>();
    LOGGER.info("{}: checking for missing parentID", id);
    checkMissingParent(id, log);
    LOGGER.info("{}: checking for empty labels", id);
    checkEmptyLabels(id, log);
    if (log.isEmpty()) {
        MCRCategoryImpl category = (MCRCategoryImpl) MCRCategoryDAOFactory.getInstance().getCategory(MCRCategoryID.rootID(id), -1);
        LOGGER.info("{}: checking left, right and level values and for non-null children", id);
        checkLeftRightAndLevel(category, 0, 0, log);
    }
    if (log.size() > 0) {
        LOGGER.error("Some errors occured on last test, report will follow");
        StringBuilder sb = new StringBuilder();
        for (String msg : log) {
            sb.append(msg).append('\n');
        }
        LOGGER.error("Error report for classification {}\n{}", id, sb);
    } else {
        LOGGER.info("Classifcation {} has no errors.", id);
    }
}
Also used : ArrayList(java.util.ArrayList) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 12 with MCRCategoryImpl

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

the class MCRClassification2Commands method checkLeftRightAndLevel.

private static int checkLeftRightAndLevel(MCRCategoryImpl category, int leftStart, int levelStart, List<String> log) {
    int curValue = leftStart;
    final int nextLevel = levelStart + 1;
    if (leftStart != category.getLeft())
        log.add("LEFT of " + category.getId() + " is " + category.getLeft() + " should be " + leftStart);
    if (levelStart != category.getLevel())
        log.add("LEVEL of " + category.getId() + " is " + category.getLevel() + " should be " + levelStart);
    int position = 0;
    for (MCRCategory child : category.getChildren()) {
        if (child == null) {
            log.add("NULL child of parent " + category.getId() + " on position " + position);
            continue;
        }
        LOGGER.debug(child.getId());
        curValue = checkLeftRightAndLevel((MCRCategoryImpl) child, ++curValue, nextLevel, log);
        position++;
    }
    ++curValue;
    if (curValue != category.getRight())
        log.add("RIGHT of " + category.getId() + " is " + category.getRight() + " should be " + curValue);
    return curValue;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)

Example 13 with MCRCategoryImpl

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

the class MCRCategoryJsonTest method deserialize.

@Test
public void deserialize() throws Exception {
    MCRConfiguration mcrProperties = MCRConfiguration.instance();
    mcrProperties.initialize(MCRConfigurationLoaderFactory.getConfigurationLoader().load(), true);
    mcrProperties.set("MCR.Metadata.DefaultLang", "de");
    mcrProperties.set("MCR.Category.DAO", CategoryDAOMock.class.getName());
    SAXBuilder saxBuilder = new SAXBuilder();
    Document doc = saxBuilder.build(getClass().getResourceAsStream("/classi/categoryJsonErr.xml"));
    String json = doc.getRootElement().getText();
    Gson gson = MCRJSONManager.instance().createGson();
    try {
        MCRCategoryImpl fromJson = gson.fromJson(json, MCRCategoryImpl.class);
        System.out.println("FOO");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CategoryDAOMock(org.mycore.frontend.classeditor.mocks.CategoryDAOMock) SAXBuilder(org.jdom2.input.SAXBuilder) MCRConfiguration(org.mycore.common.config.MCRConfiguration) Gson(com.google.gson.Gson) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) Document(org.jdom2.Document) Test(org.junit.Test)

Example 14 with MCRCategoryImpl

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

the class MCRCategLinkServiceImpl method countLinksForType.

@Override
public Map<MCRCategoryID, Number> countLinksForType(MCRCategory parent, String type, boolean childrenOnly) {
    boolean restrictedByType = type != null;
    String queryName;
    if (childrenOnly) {
        queryName = restrictedByType ? "NumberByTypePerChildOfParentID" : "NumberPerChildOfParentID";
    } else {
        queryName = restrictedByType ? "NumberByTypePerClassID" : "NumberPerClassID";
    }
    Map<MCRCategoryID, Number> countLinks = new HashMap<>();
    Collection<MCRCategoryID> ids = childrenOnly ? getAllChildIDs(parent) : getAllCategIDs(parent);
    for (MCRCategoryID id : ids) {
        // initialize all categIDs with link count of zero
        countLinks.put(id, 0);
    }
    // old classification browser/editor could not determine links correctly otherwise
    if (!childrenOnly) {
        parent = parent.getRoot();
    } else if (!(parent instanceof MCRCategoryImpl) || ((MCRCategoryImpl) parent).getInternalID() == 0) {
        parent = MCRCategoryDAOImpl.getByNaturalID(MCREntityManagerProvider.getCurrentEntityManager(), parent.getId());
    }
    LOGGER.info("parentID:{}", parent.getId());
    String classID = parent.getId().getRootID();
    Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + queryName);
    // query can take long time, please cache result
    q.setCacheable(true);
    q.setReadOnly(true);
    q.setParameter("classID", classID);
    if (childrenOnly) {
        q.setParameter("parentID", ((MCRCategoryImpl) parent).getInternalID());
    }
    if (restrictedByType) {
        q.setParameter("type", type);
    }
    // get object count for every category (not accumulated)
    @SuppressWarnings("unchecked") List<Object[]> result = (List<Object[]>) q.getResultList();
    for (Object[] sr : result) {
        MCRCategoryID key = new MCRCategoryID(classID, sr[0].toString());
        Number value = (Number) sr[1];
        countLinks.put(key, value);
    }
    return countLinks;
}
Also used : HashMap(java.util.HashMap) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) LinkedList(java.util.LinkedList) List(java.util.List)

Example 15 with MCRCategoryImpl

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

the class MCRCategoryDAOImpl method getRootCategories.

@Override
@SuppressWarnings("unchecked")
public List<MCRCategory> getRootCategories() {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    List<MCRCategoryDTO> resultList = entityManager.createNamedQuery(NAMED_QUERY_NAMESPACE + "rootCategs").getResultList();
    BiConsumer<List<MCRCategory>, MCRCategoryImpl> merge = (l, c) -> {
        MCRCategoryImpl last = (MCRCategoryImpl) l.get(l.size() - 1);
        if (last.getInternalID() != c.getInternalID()) {
            l.add(c);
        } else {
            last.getLabels().addAll(c.getLabels());
        }
    };
    return resultList.parallelStream().map(c -> c.merge(null)).collect(Collector.of(ArrayList::new, (ArrayList<MCRCategory> l, MCRCategoryImpl c) -> {
        if (l.isEmpty()) {
            l.add(c);
        } else {
            merge.accept(l, c);
        }
    }, (l, r) -> {
        if (l.isEmpty()) {
            return r;
        }
        if (r.isEmpty()) {
            return l;
        }
        MCRCategoryImpl first = (MCRCategoryImpl) r.get(0);
        merge.accept(l, first);
        l.addAll(r.subList(1, r.size()));
        return l;
    }));
}
Also used : NoResultException(javax.persistence.NoResultException) HashMap(java.util.HashMap) FlushModeType(javax.persistence.FlushModeType) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) URI(java.net.URI) Collector(java.util.stream.Collector) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Set(java.util.Set) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) AbstractMap(java.util.AbstractMap) List(java.util.List) Query(javax.persistence.Query) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) MCRStreamUtils(org.mycore.common.MCRStreamUtils) LogManager(org.apache.logging.log4j.LogManager) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) List(java.util.List)

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