1
|
package eu.dnetlib.functionality.modular.ui.vocabularies.persistence;
|
2
|
|
3
|
import com.google.common.base.Function;
|
4
|
import com.google.common.collect.Lists;
|
5
|
import com.google.common.collect.Maps;
|
6
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
7
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
8
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
9
|
import eu.dnetlib.enabling.is.registry.ISRegistryDocumentNotFoundException;
|
10
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
|
11
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
12
|
import eu.dnetlib.functionality.modular.ui.vocabularies.model.Relation;
|
13
|
import eu.dnetlib.functionality.modular.ui.vocabularies.model.Synonym;
|
14
|
import eu.dnetlib.functionality.modular.ui.vocabularies.model.Term;
|
15
|
import eu.dnetlib.functionality.modular.ui.vocabularies.model.Vocabulary;
|
16
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
17
|
import org.antlr.stringtemplate.StringTemplate;
|
18
|
import org.apache.commons.io.IOUtils;
|
19
|
import org.apache.commons.lang.StringEscapeUtils;
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
import org.dom4j.Document;
|
22
|
import org.dom4j.DocumentException;
|
23
|
import org.dom4j.Element;
|
24
|
import org.dom4j.io.SAXReader;
|
25
|
|
26
|
import java.io.IOException;
|
27
|
import java.io.StringReader;
|
28
|
import java.util.ArrayList;
|
29
|
import java.util.Collections;
|
30
|
import java.util.List;
|
31
|
import java.util.Map;
|
32
|
|
33
|
public class RegistryServiceVocabularyDAO extends VocabularyDAO {
|
34
|
|
35
|
@Override
|
36
|
public List<Vocabulary> getVocabularies() throws VocabularyException {
|
37
|
try {
|
38
|
final String query =
|
39
|
"for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType') order by $x//VOCABULARY_NAME "
|
40
|
+ "return concat ($x//RESOURCE_IDENTIFIER/@value,' §§§ ',$x//VOCABULARY_NAME,' §§§ ',$x//VOCABULARY_DESCRIPTION,' §§§ ',$x//VOCABULARY_NAME/@code)";
|
41
|
|
42
|
List<Vocabulary> vocabularies = Lists.transform(serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query),
|
43
|
new Function<String, Vocabulary>() {
|
44
|
|
45
|
@Override
|
46
|
public Vocabulary apply(final String s) {
|
47
|
String[] tokens = s.split("§§§");
|
48
|
return new Vocabulary(tokens[0].trim(), tokens[1].trim(), tokens[2].trim(), tokens[3].trim());
|
49
|
}
|
50
|
});
|
51
|
return vocabularies;
|
52
|
} catch (ISLookUpException e) {
|
53
|
throw new VocabularyException(e);
|
54
|
}
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public List<Term> getTerms(final String vocabularyId) throws VocabularyException {
|
59
|
try {
|
60
|
String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(vocabularyId);
|
61
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
62
|
final Map<String, Term> terms = Maps.newHashMap();
|
63
|
|
64
|
for (Object t : doc.selectNodes("//TERM")) {
|
65
|
final Element termNode = (Element) t;
|
66
|
final String code = termNode.valueOf("@code");
|
67
|
|
68
|
if (!terms.containsKey(code)) {
|
69
|
final Term term = new Term();
|
70
|
term.setEnglishName(termNode.valueOf("@english_name"));
|
71
|
term.setNativeName(termNode.valueOf("@native_name"));
|
72
|
term.setEncoding(termNode.valueOf("@encoding"));
|
73
|
term.setCode(code);
|
74
|
term.setSynonyms(new ArrayList<Synonym>());
|
75
|
term.setRelations(new ArrayList<Relation>());
|
76
|
terms.put(code, term);
|
77
|
}
|
78
|
final Term term = terms.get(code);
|
79
|
|
80
|
for (Object s : termNode.selectNodes(".//SYNONYM")) {
|
81
|
final Element synNode = (Element) s;
|
82
|
final Synonym syn = new Synonym();
|
83
|
syn.setTerm(synNode.valueOf("@term"));
|
84
|
syn.setEncoding(synNode.valueOf("@encoding"));
|
85
|
term.getSynonyms().add(syn);
|
86
|
}
|
87
|
|
88
|
for (Object r : termNode.selectNodes(".//RELATION")) {
|
89
|
final Element relNode = (Element) r;
|
90
|
final Relation rel = new Relation();
|
91
|
rel.setCode(relNode.valueOf("@code"));
|
92
|
rel.setType(relNode.valueOf("@type"));
|
93
|
term.getRelations().add(rel);
|
94
|
}
|
95
|
|
96
|
Collections.sort(term.getSynonyms());
|
97
|
Collections.sort(term.getRelations());
|
98
|
}
|
99
|
|
100
|
final List<Term> list = Lists.newArrayList(terms.values());
|
101
|
Collections.sort(list);
|
102
|
|
103
|
return list;
|
104
|
} catch (ISLookUpDocumentNotFoundException e) {
|
105
|
throw new VocabularyException(e);
|
106
|
} catch (ISLookUpException e) {
|
107
|
throw new VocabularyException(e);
|
108
|
} catch (DocumentException e) {
|
109
|
throw new VocabularyException(e);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
@Override
|
114
|
public void commitTerms(final List<Term> terms, final String vocabularyId) throws VocabularyException {
|
115
|
try {
|
116
|
// prepare terms for XML
|
117
|
for (Term t : terms) {
|
118
|
t.setCode(StringEscapeUtils.escapeXml(t.getCode()));
|
119
|
if(StringUtils.isBlank(t.getEncoding())) t.setEncoding(Term.DEFAULT_ENCODING);
|
120
|
t.setEncoding(StringEscapeUtils.escapeXml(t.getEncoding()));
|
121
|
t.setEnglishName(StringEscapeUtils.escapeXml(t.getEnglishName()));
|
122
|
t.setNativeName(StringEscapeUtils.escapeXml(t.getNativeName()));
|
123
|
for (Synonym s : t.getSynonyms()) {
|
124
|
if(StringUtils.isBlank(s.getEncoding())) s.setEncoding(Synonym.DEFAULT_ENCODING);
|
125
|
s.setEncoding(StringEscapeUtils.escapeXml(s.getEncoding()));
|
126
|
s.setTerm(StringEscapeUtils.escapeXml(s.getTerm()));
|
127
|
}
|
128
|
for (Relation r : t.getRelations()) {
|
129
|
r.setType(StringEscapeUtils.escapeXml(r.getType()));
|
130
|
r.setCode(StringEscapeUtils.escapeXml(r.getCode()));
|
131
|
}
|
132
|
}
|
133
|
|
134
|
StringTemplate st =
|
135
|
new StringTemplate(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/templates/terms.xml.st")));
|
136
|
st.setAttribute("terms", terms);
|
137
|
serviceLocator.getService(ISRegistryService.class).updateProfileNode(vocabularyId, "//TERMS", st.toString());
|
138
|
} catch (IOException e) {
|
139
|
throw new VocabularyException(e);
|
140
|
} catch (ISRegistryException e) {
|
141
|
throw new VocabularyException(e);
|
142
|
}
|
143
|
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public void commitVocabularyInfo(final Vocabulary voc, final String vocabularyId) throws VocabularyException {
|
148
|
try {
|
149
|
String xml = "<VOCABULARY_DESCRIPTION>{desc}</VOCABULARY_DESCRIPTION>";
|
150
|
xml = xml.replace("{desc}", StringEscapeUtils.escapeXml(voc.getDescription()));
|
151
|
serviceLocator.getService(ISRegistryService.class).updateProfileNode(vocabularyId, "//VOCABULARY_DESCRIPTION", xml);
|
152
|
} catch (ISRegistryException e) {
|
153
|
throw new VocabularyException(e);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public String createVocabulary(final Vocabulary voc) throws VocabularyException {
|
159
|
try {
|
160
|
StringTemplate st = new StringTemplate(
|
161
|
IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/templates/vocabulary.xml.st")));
|
162
|
st.setAttribute("name", voc.getName());
|
163
|
st.setAttribute("description", voc.getDescription());
|
164
|
st.setAttribute("code", voc.getCode());
|
165
|
st.setAttribute("date", DateUtils.now_ISO8601());
|
166
|
String newVocabularyId = serviceLocator.getService(ISRegistryService.class).registerProfile(st.toString());
|
167
|
|
168
|
return newVocabularyId;
|
169
|
} catch (IOException e) {
|
170
|
throw new VocabularyException(e);
|
171
|
} catch (ISRegistryException e) {
|
172
|
throw new VocabularyException(e);
|
173
|
}
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public void dropVocabulary(final String vocabularyId) throws VocabularyException {
|
178
|
try {
|
179
|
serviceLocator.getService(ISRegistryService.class).deleteProfile(vocabularyId);
|
180
|
} catch (ISRegistryDocumentNotFoundException e) {
|
181
|
throw new VocabularyException(e);
|
182
|
} catch (ISRegistryException e) {
|
183
|
throw new VocabularyException(e);
|
184
|
}
|
185
|
}
|
186
|
|
187
|
}
|