context property to count childs
This commit is contained in:
parent
a402e0c85d
commit
9a451d13f5
|
@ -46,7 +46,7 @@ public class ContextRestController extends AbstractDnetController {
|
|||
@GetMapping("/{level}/{parent}/concepts")
|
||||
public Iterable<? extends CtxChildInfo> listCategories(@PathVariable final int level, @PathVariable final String parent)
|
||||
throws InformationServiceException {
|
||||
return contextService.listCategories(level, parent);
|
||||
return contextService.listConcepts(level, parent);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
package eu.dnetlib.is.context;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -14,9 +13,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import eu.dnetlib.is.context.model.Category;
|
||||
import eu.dnetlib.is.context.model.ConceptLevel0;
|
||||
import eu.dnetlib.is.context.model.ConceptLevel1;
|
||||
import eu.dnetlib.is.context.model.ConceptLevel2;
|
||||
import eu.dnetlib.is.context.model.Context;
|
||||
import eu.dnetlib.is.context.model.CtxChildInfo;
|
||||
import eu.dnetlib.is.context.model.repository.CategoryRepository;
|
||||
|
@ -40,21 +36,24 @@ public class ContextService {
|
|||
@Autowired
|
||||
private ConceptLevel2Repository conceptLevel2Repository;
|
||||
|
||||
private static final Log log = LogFactory.getLog(ContextService.class);
|
||||
|
||||
public List<Context> listContexts() {
|
||||
return contextRepository.findAll()
|
||||
.stream()
|
||||
.sorted((v1, v2) -> StringUtils.compareIgnoreCase(v1.getId(), v2.getId()))
|
||||
.sorted((c1, c2) -> StringUtils.compareIgnoreCase(c1.getId(), c2.getId()))
|
||||
.map(c -> {
|
||||
c.setnChilds(categoryRepository.countByParent(c.getId()));
|
||||
return c;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Context getContext(final String ctxId) throws InformationServiceException {
|
||||
return contextRepository.findById(ctxId).orElseThrow(() -> new InformationServiceException("Context Not found"));
|
||||
final Context c = contextRepository.findById(ctxId).orElseThrow(() -> new InformationServiceException("Context Not found"));
|
||||
c.setnChilds(categoryRepository.countByParent(c.getId()));
|
||||
return c;
|
||||
}
|
||||
|
||||
public ObjectNode getContextFull(final String ctxId) throws InformationServiceException {
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final Context ctx = getContext(ctxId);
|
||||
|
||||
|
@ -62,22 +61,22 @@ public class ContextService {
|
|||
|
||||
final ArrayNode catArray = mapper.createArrayNode();
|
||||
|
||||
for (final Category cat : categoryRepository.findByParentOrderById(ctxId)) {
|
||||
for (final Category cat : listCategories(ctxId)) {
|
||||
final ObjectNode catNode = mapper.convertValue(cat, ObjectNode.class);
|
||||
catArray.add(catNode);
|
||||
|
||||
final ArrayNode c0Array = mapper.createArrayNode();
|
||||
for (final ConceptLevel0 c0 : conceptLevel0Repository.findByParentOrderById(cat.getId())) {
|
||||
for (final CtxChildInfo c0 : listConcepts(0, cat.getId())) {
|
||||
final ObjectNode c0Node = mapper.convertValue(c0, ObjectNode.class);
|
||||
c0Array.add(c0Node);
|
||||
|
||||
final ArrayNode c1Array = mapper.createArrayNode();
|
||||
for (final ConceptLevel1 c1 : conceptLevel1Repository.findByParentOrderById(c0.getId())) {
|
||||
for (final CtxChildInfo c1 : listConcepts(1, c0.getId())) {
|
||||
final ObjectNode c1Node = mapper.convertValue(c1, ObjectNode.class);
|
||||
c1Array.add(c1Node);
|
||||
|
||||
final ArrayNode c2Array = mapper.createArrayNode();
|
||||
for (final ConceptLevel2 c2 : conceptLevel2Repository.findByParentOrderById(c1.getId())) {
|
||||
for (final CtxChildInfo c2 : listConcepts(2, c1.getId())) {
|
||||
final ObjectNode c2Node = mapper.convertValue(c2, ObjectNode.class);
|
||||
c2Array.add(c2Node);
|
||||
}
|
||||
|
@ -92,21 +91,44 @@ public class ContextService {
|
|||
return ctxNode;
|
||||
}
|
||||
|
||||
public Iterable<Category> listCategories(final String parent) {
|
||||
return categoryRepository.findByParentOrderById(parent);
|
||||
public List<Category> listCategories(final String parent) {
|
||||
final List<Category> list = new ArrayList<>();
|
||||
|
||||
categoryRepository.findByParentOrderById(parent).forEach(c -> {
|
||||
c.setnChilds(conceptLevel0Repository.countByParent(c.getId()));
|
||||
list.add(c);
|
||||
});
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public Iterable<? extends CtxChildInfo> listCategories(final int level, final String parent) throws InformationServiceException {
|
||||
public List<? extends CtxChildInfo> listConcepts(final int level, final String parent) throws InformationServiceException {
|
||||
final List<CtxChildInfo> list = new ArrayList<>();
|
||||
|
||||
switch (level) {
|
||||
case 0:
|
||||
return conceptLevel0Repository.findByParentOrderById(parent);
|
||||
conceptLevel0Repository.findByParentOrderById(parent).forEach(c -> {
|
||||
c.setnChilds(conceptLevel1Repository.countByParent(c.getId()));
|
||||
list.add(c);
|
||||
});
|
||||
break;
|
||||
case 1:
|
||||
return conceptLevel1Repository.findByParentOrderById(parent);
|
||||
conceptLevel1Repository.findByParentOrderById(parent).forEach(c -> {
|
||||
c.setnChilds(conceptLevel2Repository.countByParent(c.getId()));
|
||||
list.add(c);
|
||||
});
|
||||
break;
|
||||
case 2:
|
||||
return conceptLevel2Repository.findByParentOrderById(parent);
|
||||
conceptLevel2Repository.findByParentOrderById(parent).forEach(c -> {
|
||||
c.setnChilds(0);
|
||||
list.add(c);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
throw new InformationServiceException("Invalid concept level - valid levels are 0, 1, 2");
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,17 +29,17 @@
|
|||
|
||||
<ul>
|
||||
<li ng-repeat="cat in categories" style="display: in">
|
||||
<a href="javascript:void(0)" ng-click="populateNode(0, cat)" ng-hide="cat.populated">[+]</a>
|
||||
<a href="javascript:void(0)" ng-click="populateNode(0, cat)" ng-hide="cat.populated || cat.nChilds == 0">[+{{cat.nChilds}}]</a>
|
||||
<span class="badge badge-info" ng-show="cat.claim">claim</span>
|
||||
<a href="javascript:void(0)" title="{{cat.label}}" data-toggle="modal" data-target="#showParametersModal" ng-click="initShowParameters(cat.parameters)">{{cat.id}}</a>
|
||||
<ul ng-show="cat.concepts.length > 0">
|
||||
<li ng-repeat="c0 in cat.concepts">
|
||||
<a href="javascript:void(0)" ng-click="populateNode(1, c0)" ng-hide="c0.populated">[+]</a>
|
||||
<a href="javascript:void(0)" ng-click="populateNode(1, c0)" ng-hide="c0.populated || c0.nChilds == 0">[+{{c0.nChilds}}]</a>
|
||||
<span class="badge badge-info" ng-show="c0.claim">claim</span>
|
||||
<a href="javascript:void(0)" title="{{c0.label}}" data-toggle="modal" data-target="#showParametersModal" ng-click="initShowParameters(c0.parameters)">{{c0.id}}</a>
|
||||
<ul ng-show="c0.concepts.length > 0">
|
||||
<li ng-repeat="c1 in c0.concepts">
|
||||
<a href="javascript:void(0)" ng-click="populateNode(2, c1)" ng-hide="c1.populated">[+]</a>
|
||||
<a href="javascript:void(0)" ng-click="populateNode(2, c1)" ng-hide="c1.populated || c1.nChilds == 0">[+{{c1.nChilds}}]</a>
|
||||
<span class="badge badge-info" ng-show="c1.claim">claim</span>
|
||||
<a href="javascript:void(0)" title="{{c1.label}}" data-toggle="modal" data-target="#showParametersModal" ng-click="initShowParameters(c1.parameters)">{{c1.id}}</a>
|
||||
<ul ng-show="c1.concepts.length > 0">
|
||||
|
|
|
@ -22,7 +22,7 @@ public abstract class CtxChildInfo extends CtxInfo {
|
|||
this.claim = claim;
|
||||
}
|
||||
|
||||
public String isParent() {
|
||||
public String getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.io.Serializable;
|
|||
import javax.persistence.Column;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
@ -33,6 +34,9 @@ public abstract class CtxInfo implements Serializable {
|
|||
@Column(name = "params")
|
||||
private Parameter[] parameters;
|
||||
|
||||
@Transient
|
||||
private long nChilds = 0;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -57,4 +61,12 @@ public abstract class CtxInfo implements Serializable {
|
|||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public long getnChilds() {
|
||||
return nChilds;
|
||||
}
|
||||
|
||||
public void setnChilds(final long nChilds) {
|
||||
this.nChilds = nChilds;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,5 +6,7 @@ import eu.dnetlib.is.context.model.Category;
|
|||
|
||||
public interface CategoryRepository extends JpaRepository<Category, String> {
|
||||
|
||||
long countByParent(String parent);
|
||||
|
||||
Iterable<Category> findByParentOrderById(String parent);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import eu.dnetlib.is.context.model.ConceptLevel0;
|
|||
|
||||
public interface ConceptLevel0Repository extends JpaRepository<ConceptLevel0, String> {
|
||||
|
||||
long countByParent(String parent);
|
||||
|
||||
Iterable<ConceptLevel0> findByParentOrderById(String parent);
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import eu.dnetlib.is.context.model.ConceptLevel1;
|
|||
|
||||
public interface ConceptLevel1Repository extends JpaRepository<ConceptLevel1, String> {
|
||||
|
||||
long countByParent(String parent);
|
||||
|
||||
Iterable<ConceptLevel1> findByParentOrderById(String parent);
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import eu.dnetlib.is.context.model.ConceptLevel2;
|
|||
|
||||
public interface ConceptLevel2Repository extends JpaRepository<ConceptLevel2, String> {
|
||||
|
||||
long countByParent(String parent);
|
||||
|
||||
Iterable<ConceptLevel2> findByParentOrderById(String parent);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue