1
|
package eu.dnetlib.vocabularies.publisher;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
|
6
|
import eu.dnetlib.vocabularies.publisher.model.Synonym;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.dom4j.Document;
|
10
|
import org.dom4j.DocumentException;
|
11
|
import org.dom4j.Element;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.cache.annotation.Cacheable;
|
15
|
|
16
|
import com.google.common.collect.Lists;
|
17
|
|
18
|
import eu.dnetlib.enabling.tools.SplittedQueryExecutor;
|
19
|
import eu.dnetlib.vocabularies.publisher.model.Vocabulary;
|
20
|
import eu.dnetlib.vocabularies.publisher.model.VocabularyTerm;
|
21
|
|
22
|
public class VocabularyRetriever {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(VocabularyRetriever.class); // NOPMD by marko on 11/24/08 5:02 PM
|
25
|
|
26
|
@Autowired
|
27
|
private SplittedQueryExecutor queryExecutor;
|
28
|
|
29
|
@Cacheable("vocabularies")
|
30
|
public List<Vocabulary> listVocabularies() {
|
31
|
log.debug("listVocabularies(): not using cache");
|
32
|
String query = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType') order by $x//VOCABULARY_NAME "
|
33
|
+ "return concat ($x//RESOURCE_IDENTIFIER/@value, ':-:',$x//VOCABULARY_NAME, ':-:',$x//VOCABULARY_DESCRIPTION, ':-:',$x//VOCABULARY_NAME/@code)";
|
34
|
Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
|
35
|
return Lists.newArrayList(vocs);
|
36
|
}
|
37
|
|
38
|
@Cacheable(value = "terms", key = "#id")
|
39
|
public Vocabulary getVocabulary(final String id) throws VocabularyNotFoundException {
|
40
|
log.warn(String.format("getVocabulary(%s): not using cache", id));
|
41
|
// get the vocabulary info:
|
42
|
String queryVoc = "let $x := /*[.//RESOURCE_IDENTIFIER/@value='"
|
43
|
+ id
|
44
|
+ "']"
|
45
|
+ " return concat ($x//RESOURCE_IDENTIFIER/@value, ':-:',$x//VOCABULARY_NAME, ':-:',$x//VOCABULARY_DESCRIPTION, ':-:',$x//VOCABULARY_NAME/@code)";
|
46
|
|
47
|
Vocabulary voc = getVocabularyByQuery(queryVoc);
|
48
|
if (voc == null) throw new VocabularyNotFoundException("Can't find vocabulary by id: " + id);
|
49
|
else return voc;
|
50
|
}
|
51
|
|
52
|
@Cacheable(value = "termsByCode", key = "#code")
|
53
|
public Vocabulary getVocabularyByCode(final String code) throws VocabularyNotFoundException {
|
54
|
log.warn(String.format("getVocabularyByCode(%s): not using cache", code));
|
55
|
String queryVoc = "let $x := //RESOURCE_PROFILE[.//VOCABULARY_NAME/@code='"
|
56
|
+ code
|
57
|
+ "']"
|
58
|
+ " return concat ($x//RESOURCE_IDENTIFIER/@value, ':-:',$x//VOCABULARY_NAME, ':-:',$x//VOCABULARY_DESCRIPTION, ':-:',$x//VOCABULARY_NAME/@code)";
|
59
|
Vocabulary voc = getVocabularyByQuery(queryVoc);
|
60
|
if (voc == null) throw new VocabularyNotFoundException("Can't find vocabulary by code: " + code);
|
61
|
else return voc;
|
62
|
}
|
63
|
|
64
|
@Cacheable(value = "synonymsByCode", key = "#termCode")
|
65
|
public VocabularyTerm getTermSynonyms(final String vocabularyCode, final String termCode) throws VocabularyNotFoundException {
|
66
|
log.warn(String.format("getTermSynonyms(%s, %s): not using cache", vocabularyCode, termCode));
|
67
|
final Vocabulary voc = getVocabularyByCode(vocabularyCode);
|
68
|
|
69
|
final VocabularyTerm vt = voc.getTerms().stream()
|
70
|
.filter(t -> t.getCode().equalsIgnoreCase(termCode))
|
71
|
.findFirst()
|
72
|
.get();
|
73
|
|
74
|
String querySynonyms =
|
75
|
String.format("for $x in /*[.//RESOURCE_IDENTIFIER/@value='%s']//TERM[./@code = '%s']//SYNONYM " +
|
76
|
"return concat ($x/@term,':-:',$x/@encoding)", voc.getId(), termCode);
|
77
|
|
78
|
vt.setSynonyms(Lists.newArrayList(queryExecutor.query(Synonym.class, querySynonyms)));
|
79
|
|
80
|
return vt;
|
81
|
}
|
82
|
|
83
|
private Vocabulary getVocabularyByQuery(final String query) {
|
84
|
Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
|
85
|
if (vocs.iterator().hasNext()) {
|
86
|
Vocabulary theVoc = vocs.iterator().next();
|
87
|
if (theVoc.getId() == null) return null;
|
88
|
// now the terms
|
89
|
String queryTerms = "for $x in /*[.//RESOURCE_IDENTIFIER/@value='" + theVoc.getId() + "']//TERM order by $x/@english_name "
|
90
|
+ "return concat ($x/@english_name,':-:',$x/@native_name,':-:',$x/@encoding,':-:',$x/@code)";
|
91
|
Iterable<VocabularyTerm> terms = this.queryExecutor.query(VocabularyTerm.class, queryTerms);
|
92
|
theVoc.setTerms(Lists.newArrayList(terms));
|
93
|
return theVoc;
|
94
|
} else return null;
|
95
|
}
|
96
|
|
97
|
|
98
|
/*
|
99
|
private Vocabulary getTermByQuery(final String query) throws DocumentException {
|
100
|
Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
|
101
|
if (vocs.iterator().hasNext()) {
|
102
|
Vocabulary vocabulary = vocs.iterator().next();
|
103
|
if (vocabulary.getId() == null) return null;
|
104
|
// now the terms
|
105
|
String queryTerms = String.format(
|
106
|
"for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType') \n" +
|
107
|
"where $x[./RESOURCE_PROFILE/HEADER/RESOURCE_IDENTIFIER/@value/string() = '%s']\n" +
|
108
|
"return $x/RESOURCE_PROFILE/BODY/CONFIGURATION", vocabulary.getId());
|
109
|
|
110
|
List<String> vocabProfiles = queryExecutor.performQuery(queryTerms);
|
111
|
if (vocabProfiles != null && vocabProfiles.size() == 1) {
|
112
|
final String termsXml = vocabProfiles.get(0);
|
113
|
log.debug("got terms: " + termsXml);
|
114
|
|
115
|
final Document doc = new SAXReader().read(new StringReader(termsXml));
|
116
|
|
117
|
final List<VocabularyTerm> terms = Lists.newArrayList();
|
118
|
for(Object t : doc.selectNodes("//TERM")) {
|
119
|
final Element term = (Element) t;
|
120
|
final List<Synonym> synonyms = Lists.newArrayList();
|
121
|
for(Object s : term.selectNodes("//SYNONYM")) {
|
122
|
Element syn = (Element) s;
|
123
|
synonyms.add(new Synonym(syn.valueOf("./@term"), syn.valueOf("./@encoding")));
|
124
|
}
|
125
|
terms.add(new VocabularyTerm(
|
126
|
term.valueOf("./@english_name"),
|
127
|
term.valueOf("./@native_name"),
|
128
|
term.valueOf("./@encoding"),
|
129
|
term.valueOf("./@code"),
|
130
|
synonyms));
|
131
|
}
|
132
|
vocabulary.setTerms(terms);
|
133
|
return vocabulary;
|
134
|
} else {
|
135
|
throw new IllegalStateException(String.format("found more than one Vocabulary with id '%s'", vocabulary.getId()));
|
136
|
}
|
137
|
|
138
|
} else return null;
|
139
|
}
|
140
|
*/
|
141
|
|
142
|
}
|