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