Revision 56035
Added by Claudio Atzori almost 5 years ago
VocabularyRetriever.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.vocabularies.publisher; |
2 | 2 |
|
3 |
import java.io.StringReader; |
|
3 | 4 |
import java.util.List; |
4 | 5 |
|
6 |
import eu.dnetlib.vocabularies.publisher.model.Synonym; |
|
5 | 7 |
import org.apache.commons.logging.Log; |
6 | 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; |
|
7 | 13 |
import org.springframework.beans.factory.annotation.Autowired; |
8 | 14 |
import org.springframework.cache.annotation.Cacheable; |
9 | 15 |
|
... | ... | |
31 | 37 |
|
32 | 38 |
@Cacheable(value = "terms", key = "#id") |
33 | 39 |
public Vocabulary getVocabulary(final String id) throws VocabularyNotFoundException { |
34 |
log.debug("getVocabulary(): not using cache");
|
|
40 |
log.warn(String.format("getVocabulary(%s): not using cache", id));
|
|
35 | 41 |
// get the vocabulary info: |
36 | 42 |
String queryVoc = "let $x := /*[.//RESOURCE_IDENTIFIER/@value='" |
37 | 43 |
+ id |
... | ... | |
45 | 51 |
|
46 | 52 |
@Cacheable(value = "termsByCode", key = "#code") |
47 | 53 |
public Vocabulary getVocabularyByCode(final String code) throws VocabularyNotFoundException { |
48 |
log.debug("getVocabularyByCode(): not using cache");
|
|
54 |
log.warn(String.format("getVocabularyByCode(%s): not using cache", code));
|
|
49 | 55 |
String queryVoc = "let $x := //RESOURCE_PROFILE[.//VOCABULARY_NAME/@code='" |
50 | 56 |
+ code |
51 | 57 |
+ "']" |
... | ... | |
55 | 61 |
else return voc; |
56 | 62 |
} |
57 | 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 |
|
|
58 | 83 |
private Vocabulary getVocabularyByQuery(final String query) { |
59 | 84 |
Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query); |
60 | 85 |
if (vocs.iterator().hasNext()) { |
... | ... | |
69 | 94 |
} else return null; |
70 | 95 |
} |
71 | 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 |
|
|
72 | 142 |
} |
Also available in: Unified diff
include synonyms in the published information