use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRCategoryImplTest method getLeftSiblingOrParent.
@Test
public void getLeftSiblingOrParent() throws URISyntaxException, MCRException, SAXParseException, IOException {
loadWorldClassification();
MCRCategory europe = category.getChildren().get(0);
MCRCategoryImpl asia = (MCRCategoryImpl) category.getChildren().get(1);
MCRCategoryImpl germany = (MCRCategoryImpl) europe.getChildren().get(0);
assertEquals("Did not get Europe as left sibling of Asia", europe.getId(), asia.getLeftSiblingOrParent().getId());
assertEquals("Did not get Europa as left sibling or ancestor of Germany", europe.getId(), germany.getLeftSiblingOrParent().getId());
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRClassificationBrowser2 method countLinks.
/**
* Add link count to each category
*/
private void countLinks(HttpServletRequest req, boolean emptyLeaves, String objectType, MCRCategory category, List<Element> data) {
if (!Boolean.valueOf(req.getParameter("countlinks"))) {
return;
}
if (objectType != null && objectType.trim().length() == 0) {
objectType = null;
}
String classifID = category.getId().getRootID();
Map<MCRCategoryID, Number> count = MCRCategLinkServiceFactory.getInstance().countLinksForType(category, objectType, true);
for (Iterator<Element> it = data.iterator(); it.hasNext(); ) {
Element child = it.next();
MCRCategoryID childID = new MCRCategoryID(classifID, child.getAttributeValue("id"));
int num = (count.containsKey(childID) ? count.get(childID).intValue() : 0);
child.setAttribute("numLinks", String.valueOf(num));
if ((!emptyLeaves) && (num < 1)) {
it.remove();
}
}
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRClassificationBrowser2 method doGetPost.
@Override
public void doGetPost(MCRServletJob job) throws Exception {
long time = System.nanoTime();
HttpServletRequest req = job.getRequest();
String classifID = req.getParameter("classification");
String categID = req.getParameter("category");
boolean countResults = Boolean.valueOf(req.getParameter("countresults"));
boolean addClassId = Boolean.valueOf(req.getParameter("addclassid"));
boolean uri = Boolean.valueOf(req.getParameter("adduri"));
String el = req.getParameter("emptyleaves");
boolean emptyLeaves = true;
if ((el != null) && (el.trim().length() > 0)) {
emptyLeaves = Boolean.valueOf(el);
}
LOGGER.info("ClassificationBrowser {} {}", classifID, categID == null ? "" : categID);
MCRCategoryID id = new MCRCategoryID(classifID, categID);
Element xml = new Element("classificationBrowserData");
xml.setAttribute("classification", classifID);
xml.setAttribute("webpage", req.getParameter("webpage"));
MCRQueryAdapter queryAdapter = null;
String field = req.getParameter("field");
if (countResults || (field != null && field.length() > 0)) {
queryAdapter = getQueryAdapter(field);
configureQueryAdapter(queryAdapter, req);
if (queryAdapter.getObjectType() != null) {
xml.setAttribute("objectType", queryAdapter.getObjectType());
}
}
String parameters = req.getParameter("parameters");
if (parameters != null) {
xml.setAttribute("parameters", parameters);
}
List<Element> data = new ArrayList<>();
MCRCategory category = MCRCategoryDAOFactory.getInstance().getCategory(id, 1);
if (category == null) {
job.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find category: " + id);
return;
}
for (MCRCategory child : category.getChildren()) {
String childID = child.getId().getID();
long numResults = 0;
if (queryAdapter != null) {
queryAdapter.setCategory(addClassId ? child.getId().toString() : childID);
}
if (countResults) {
numResults = queryAdapter.getResultCount();
if ((!emptyLeaves) && (numResults < 1)) {
continue;
}
}
Element categoryE = new Element("category");
data.add(categoryE);
if (countResults) {
categoryE.setAttribute("numResults", String.valueOf(numResults));
}
categoryE.setAttribute("id", childID);
categoryE.setAttribute("children", Boolean.toString(child.hasChildren()));
if (queryAdapter != null) {
categoryE.setAttribute("query", queryAdapter.getQueryAsString());
}
if (uri && (child.getURI() != null)) {
categoryE.addContent(new Element("uri").setText(child.getURI().toString()));
}
addLabel(req, child, categoryE);
}
String objectType = queryAdapter == null ? null : queryAdapter.getObjectType();
countLinks(req, emptyLeaves, objectType, category, data);
sortCategories(req, data);
xml.addContent(data);
renderToHTML(job, req, xml);
time = (System.nanoTime() - time) / 1000000;
LOGGER.info("ClassificationBrowser finished in {} ms", time);
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRCategoryListTypeAdapter method createCategRefJSONObj.
private JsonElement createCategRefJSONObj(MCRCategory categ, Boolean hasLink) {
JsonObject categRefJsonObject = new JsonObject();
categRefJsonObject.add(ID, serializationContext.serialize(categ.getId()));
Set<MCRLabel> labels = categ.getLabels();
categRefJsonObject.add(LABELS, serializationContext.serialize(new MCRLabelSetWrapper(labels)));
URI uri = categ.getURI();
if (uri != null) {
categRefJsonObject.addProperty(URISTR, uri.toString());
}
categRefJsonObject.addProperty(HASCHILDREN, categ.hasChildren());
categRefJsonObject.addProperty(HASLINK, hasLink);
return categRefJsonObject;
}
use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.
the class MCRCategoryTypeAdapter method serialize.
@Override
public JsonElement serialize(MCRJSONCategory category, Type arg1, JsonSerializationContext contextSerialization) {
JsonObject rubricJsonObject = new JsonObject();
MCRCategoryID id = category.getId();
if (id != null) {
rubricJsonObject.add(ID, contextSerialization.serialize(id));
}
Set<MCRLabel> labels = category.getLabels();
rubricJsonObject.add(LABELS, contextSerialization.serialize(new MCRLabelSetWrapper(labels)));
URI uri = category.getURI();
if (uri != null) {
rubricJsonObject.addProperty(URISTR, uri.toString());
}
if (category.hasChildren()) {
List<MCRCategory> children = category.getChildren();
Map<MCRCategoryID, Boolean> linkMap = getLinkService().hasLinks(category);
if (linkMap.values().contains(true)) {
rubricJsonObject.addProperty(HASLINK, true);
}
rubricJsonObject.add(CHILDREN, contextSerialization.serialize(new MCRCategoryListWrapper(children, linkMap)));
}
return rubricJsonObject;
}
Aggregations