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);
}
}
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;
}
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();
}
}
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;
}
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;
}));
}
Aggregations