1
|
package eu.dnetlib.vocabularies.publisher;
|
2
|
|
3
|
import java.util.List;
|
4
|
|
5
|
import org.apache.commons.logging.Log;
|
6
|
import org.apache.commons.logging.LogFactory;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.cache.annotation.Cacheable;
|
9
|
|
10
|
import com.google.common.collect.Lists;
|
11
|
|
12
|
import eu.dnetlib.enabling.tools.SplittedQueryExecutor;
|
13
|
import eu.dnetlib.vocabularies.publisher.model.Vocabulary;
|
14
|
import eu.dnetlib.vocabularies.publisher.model.VocabularyTerm;
|
15
|
|
16
|
public class VocabularyRetriever {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(VocabularyRetriever.class); // NOPMD by marko on 11/24/08 5:02 PM
|
19
|
|
20
|
@Autowired
|
21
|
private SplittedQueryExecutor queryExecutor;
|
22
|
|
23
|
@Cacheable("vocabularies")
|
24
|
public List<Vocabulary> listVocabularies() {
|
25
|
log.debug("listVocabularies(): not using cache");
|
26
|
String query = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType') order by $x//VOCABULARY_NAME "
|
27
|
+ "return concat ($x//RESOURCE_IDENTIFIER/@value, ':-:',$x//VOCABULARY_NAME, ':-:',$x//VOCABULARY_DESCRIPTION, ':-:',$x//VOCABULARY_NAME/@code)";
|
28
|
Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
|
29
|
return Lists.newArrayList(vocs);
|
30
|
}
|
31
|
|
32
|
@Cacheable(value = "terms", key = "#id")
|
33
|
public Vocabulary getVocabulary(final String id) throws VocabularyNotFoundException {
|
34
|
log.debug("getVocabulary(): not using cache");
|
35
|
// get the vocabulary info:
|
36
|
String queryVoc = "let $x := /*[.//RESOURCE_IDENTIFIER/@value='"
|
37
|
+ id
|
38
|
+ "']"
|
39
|
+ " return concat ($x//RESOURCE_IDENTIFIER/@value, ':-:',$x//VOCABULARY_NAME, ':-:',$x//VOCABULARY_DESCRIPTION, ':-:',$x//VOCABULARY_NAME/@code)";
|
40
|
|
41
|
Vocabulary voc = getVocabularyByQuery(queryVoc);
|
42
|
if (voc == null) throw new VocabularyNotFoundException("Can't find vocabulary by id: " + id);
|
43
|
else return voc;
|
44
|
}
|
45
|
|
46
|
@Cacheable(value = "termsByCode", key = "#code")
|
47
|
public Vocabulary getVocabularyByCode(final String code) throws VocabularyNotFoundException {
|
48
|
String queryVoc = "let $x := //RESOURCE_PROFILE[.//VOCABULARY_NAME/@code='"
|
49
|
+ code
|
50
|
+ "']"
|
51
|
+ " return concat ($x//RESOURCE_IDENTIFIER/@value, ':-:',$x//VOCABULARY_NAME, ':-:',$x//VOCABULARY_DESCRIPTION, ':-:',$x//VOCABULARY_NAME/@code)";
|
52
|
Vocabulary voc = getVocabularyByQuery(queryVoc);
|
53
|
if (voc == null) throw new VocabularyNotFoundException("Can't find vocabulary by code: " + code);
|
54
|
else return voc;
|
55
|
}
|
56
|
|
57
|
private Vocabulary getVocabularyByQuery(final String query) {
|
58
|
Iterable<Vocabulary> vocs = this.queryExecutor.query(Vocabulary.class, query);
|
59
|
if (vocs.iterator().hasNext()) {
|
60
|
Vocabulary theVoc = vocs.iterator().next();
|
61
|
if (theVoc.getId() == null) return null;
|
62
|
// now the terms
|
63
|
String queryTerms = "for $x in /*[.//RESOURCE_IDENTIFIER/@value='" + theVoc.getId() + "']//TERM order by $x/@english_name "
|
64
|
+ "return concat ($x/@english_name,':-:',$x/@native_name,':-:',$x/@encoding,':-:',$x/@code)";
|
65
|
Iterable<VocabularyTerm> terms = this.queryExecutor.query(VocabularyTerm.class, queryTerms);
|
66
|
theVoc.setTerms(Lists.newArrayList(terms));
|
67
|
return theVoc;
|
68
|
} else return null;
|
69
|
}
|
70
|
|
71
|
}
|