1
|
package eu.dnetlib.openaire.context;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import com.google.common.base.Splitter;
|
9
|
import eu.dnetlib.openaire.common.ISClient;
|
10
|
import org.apache.commons.lang3.StringUtils;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
|
17
|
@Component
|
18
|
@ConditionalOnProperty(value = "openaire.exporter.enable.context", havingValue = "true")
|
19
|
public class ContextApiCore {
|
20
|
|
21
|
private static final Log log = LogFactory.getLog(ContextApiCore.class);
|
22
|
private static final String SEPARATOR = "::";
|
23
|
|
24
|
@Autowired
|
25
|
private ISClient isClient;
|
26
|
|
27
|
public List<ContextSummary> listContexts() throws ContextException {
|
28
|
return getContextMap().values().stream()
|
29
|
.map(c -> new ContextSummary()
|
30
|
.setId(c.getId())
|
31
|
.setType(c.getType())
|
32
|
.setLabel(c.getLabel())
|
33
|
.setStatus(c.getParams().containsKey("status") ? c.getParams().get("status").get(0).getValue() : ""))
|
34
|
.collect(Collectors.toList());
|
35
|
}
|
36
|
|
37
|
public List<CategorySummary> listCategories(final String contextId) throws ContextException {
|
38
|
return getContextMap().get(contextId).getCategories().values().stream()
|
39
|
.filter(Category::isClaim)
|
40
|
.map(c -> new CategorySummary()
|
41
|
.setId(c.getId())
|
42
|
.setLabel(c.getLabel())
|
43
|
.setHasConcept(c.hasConcepts()))
|
44
|
.collect(Collectors.toList());
|
45
|
}
|
46
|
|
47
|
public List<ConceptSummary> listConcepts(final String categoryId) throws ContextException {
|
48
|
final String contextId = StringUtils.substringBefore(categoryId, SEPARATOR);
|
49
|
return getContextMap().get(contextId).getCategories().get(categoryId).getConcepts().stream()
|
50
|
.filter(Concept::isClaim)
|
51
|
.map(c -> new ConceptSummary()
|
52
|
.setId(c.getId())
|
53
|
.setLabel(c.getLabel())
|
54
|
.setHasSubConcept(c.hasSubConcepts()))
|
55
|
.collect(Collectors.toList());
|
56
|
|
57
|
}
|
58
|
|
59
|
public List<ConceptSummary> listSubConcepts(final String conceptId) throws ContextException {
|
60
|
final List<String> ids = Splitter.on(SEPARATOR).splitToList(conceptId);
|
61
|
if (ids.size() < 3) {
|
62
|
throw new ContextException("");
|
63
|
}
|
64
|
|
65
|
final String contextId = ids.get(0);
|
66
|
final String categoryId = contextId + SEPARATOR + ids.get(1);
|
67
|
|
68
|
final List<Concept> concepts = getContextMap().get(contextId).getCategories().get(categoryId).getConcepts()
|
69
|
.stream()
|
70
|
.filter(c -> conceptId.equals(c.getId()))
|
71
|
.filter(Concept::isClaim)
|
72
|
.collect(Collectors.toList());
|
73
|
|
74
|
return mapConcepts(concepts);
|
75
|
}
|
76
|
|
77
|
private List<ConceptSummary> mapConcepts(final List<Concept> concepts) {
|
78
|
if (concepts == null || concepts.isEmpty()) {
|
79
|
return null;
|
80
|
}
|
81
|
return concepts.stream()
|
82
|
.map(c -> new ConceptSummary()
|
83
|
.setId(c.getId())
|
84
|
.setLabel(c.getLabel())
|
85
|
.setHasSubConcept(c.hasSubConcepts())
|
86
|
.setConcept(mapConcepts(c.getConcepts())))
|
87
|
.collect(Collectors.toList());
|
88
|
}
|
89
|
|
90
|
private Map<String, Context> getContextMap() throws ContextException {
|
91
|
try {
|
92
|
return isClient.getContextMap();
|
93
|
} catch (IOException e) {
|
94
|
throw new ContextException(e);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
|
99
|
}
|