1
|
package eu.dnetlib.functionality.modular.ui.repositories;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Collections;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
|
9
|
import javax.annotation.Resource;
|
10
|
|
11
|
import org.antlr.stringtemplate.StringTemplate;
|
12
|
import org.apache.commons.io.IOUtils;
|
13
|
import org.apache.commons.lang.StringEscapeUtils;
|
14
|
import org.apache.commons.lang.math.NumberUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.dom4j.Document;
|
18
|
import org.dom4j.Node;
|
19
|
import org.dom4j.io.SAXReader;
|
20
|
import org.springframework.core.io.ClassPathResource;
|
21
|
|
22
|
import com.google.common.collect.Lists;
|
23
|
import com.google.common.collect.Maps;
|
24
|
import com.google.common.collect.Sets;
|
25
|
|
26
|
import eu.dnetlib.data.collector.rmi.CollectorService;
|
27
|
import eu.dnetlib.data.collector.rmi.ProtocolDescriptor;
|
28
|
import eu.dnetlib.data.collector.rmi.ProtocolParameter;
|
29
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
30
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
31
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
32
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoInterfaceEntry;
|
33
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoMetaWfEntry;
|
34
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.SimpleParamEntry;
|
35
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.VocabularyEntry;
|
36
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants.WorkflowStatus;
|
37
|
|
38
|
public class RepoUIUtils {
|
39
|
|
40
|
@Resource
|
41
|
private UniqueServiceLocator serviceLocator;
|
42
|
|
43
|
private final ClassPathResource getRepoApiQueryTmpl =
|
44
|
new ClassPathResource("/eu/dnetlib/functionality/modular/ui/repositories/templates/getRepoApi.xquery.st");
|
45
|
|
46
|
private static final Log log = LogFactory.getLog(RepoUIUtils.class);
|
47
|
|
48
|
private final Map<String, List<ProtocolParameter>> parametersMap = Maps.newHashMap();
|
49
|
|
50
|
public RepoInterfaceEntry getApi(final String repoId, final String ifaceId) throws Exception {
|
51
|
final RepoInterfaceEntry ifc = new RepoInterfaceEntry();
|
52
|
|
53
|
final StringTemplate st = new StringTemplate(IOUtils.toString(getRepoApiQueryTmpl.getInputStream()));
|
54
|
st.setAttribute("dsId", repoId);
|
55
|
st.setAttribute("ifaceId", ifaceId);
|
56
|
|
57
|
final String query = st.toString();
|
58
|
|
59
|
final SAXReader reader = new SAXReader();
|
60
|
|
61
|
final String s = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
|
62
|
|
63
|
final Document doc = reader.read(new StringReader(s));
|
64
|
|
65
|
ifc.setId(doc.valueOf("/api/id"));
|
66
|
ifc.setLabel(doc.valueOf("/api/label"));
|
67
|
ifc.setRemovable(doc.valueOf("/api/removable").equalsIgnoreCase("true") && doc.selectNodes("//metaWF").isEmpty());
|
68
|
ifc.setRepoId(doc.valueOf("/api/repo/@id"));
|
69
|
ifc.setRepoName(StringEscapeUtils.unescapeHtml(doc.valueOf("/api/repo")));
|
70
|
ifc.setRepoCountry(doc.valueOf("/api/repo/@country"));
|
71
|
ifc.setRepoPrefix(doc.valueOf("/api/repo/@prefix"));
|
72
|
ifc.setRepoType(doc.valueOf("/api/repo/@type"));
|
73
|
ifc.setEmail(doc.valueOf("/api/repo/@email"));
|
74
|
final String protocol = doc.valueOf("/api/protocol");
|
75
|
ifc.setProtocol(protocol);
|
76
|
|
77
|
final Set<String> toVerifyParams = getParameterNamesForProtocol(ifc.getProtocol());
|
78
|
for (final Object o : doc.selectNodes("/api/commonParams/param")) {
|
79
|
final Node n = (Node) o;
|
80
|
ifc.getCommonParams().add(new SimpleParamEntry(n.valueOf("@name"), n.getText()));
|
81
|
}
|
82
|
|
83
|
log.debug("****** " + toVerifyParams);
|
84
|
for (final Object o : doc.selectNodes("/api/accessParams/param")) {
|
85
|
final Node n = (Node) o;
|
86
|
final String pname = n.valueOf("@name");
|
87
|
if (toVerifyParams.contains(pname)) {
|
88
|
ifc.getAccessParams().add(new SimpleParamEntry(pname, n.getText()));
|
89
|
toVerifyParams.remove(pname);
|
90
|
} else {
|
91
|
log.warn("Invalid param [" + pname + "] for protocol " + protocol + " in repo " + repoId);
|
92
|
}
|
93
|
}
|
94
|
for (final String pname : toVerifyParams) {
|
95
|
ifc.getAccessParams().add(new SimpleParamEntry(pname, ""));
|
96
|
log.info("Adding missing param [" + pname + "] for protocol " + protocol + " in repo " + repoId);
|
97
|
}
|
98
|
|
99
|
for (final Object o : doc.selectNodes("/api/extraFields/field")) {
|
100
|
final Node n = (Node) o;
|
101
|
final String name = n.valueOf("@name");
|
102
|
final String value = n.getText();
|
103
|
if (name.equalsIgnoreCase("overriding_compliance")) {
|
104
|
for (final SimpleParamEntry e : ifc.getCommonParams()) {
|
105
|
if (e.getName().equals("compliance")) {
|
106
|
// The original compliance (assigned by the validator) is stored in otherValue
|
107
|
e.setOtherValue(value);
|
108
|
}
|
109
|
}
|
110
|
} else if (name.equalsIgnoreCase("last_aggregation_date")) {
|
111
|
ifc.setAggrDate(value);
|
112
|
} else if (name.equalsIgnoreCase("last_aggregation_total")) {
|
113
|
ifc.setAggrTotal(NumberUtils.toInt(value, 0));
|
114
|
} else if (name.equalsIgnoreCase("last_aggregation_mdId")) {
|
115
|
ifc.setAggrMdId(value);
|
116
|
} else if (name.equalsIgnoreCase("last_collection_date")) {
|
117
|
ifc.setCollDate(value);
|
118
|
} else if (name.equalsIgnoreCase("last_collection_total")) {
|
119
|
ifc.setCollTotal(NumberUtils.toInt(value, 0));
|
120
|
} else if (name.equalsIgnoreCase("last_collection_mdId")) {
|
121
|
ifc.setCollMdId(value);
|
122
|
} else if (name.equalsIgnoreCase("last_download_date")) {
|
123
|
ifc.setDownloadDate(value);
|
124
|
} else if (name.equalsIgnoreCase("last_download_total")) {
|
125
|
ifc.setDownloadTotal(NumberUtils.toInt(value, 0));
|
126
|
} else if (name.equalsIgnoreCase("last_download_objId")) {
|
127
|
ifc.setDownloadObjId(value);
|
128
|
} else {
|
129
|
ifc.getOtherParams().add(new SimpleParamEntry(name, value));
|
130
|
}
|
131
|
}
|
132
|
|
133
|
if (doc.selectNodes("/api/extraFields/field[@name='metadata_identifier_path']").isEmpty()) {
|
134
|
ifc.getOtherParams().add(new SimpleParamEntry("metadata_identifier_path", ""));
|
135
|
}
|
136
|
|
137
|
for (final Object o : doc.selectNodes("//metaWF")) {
|
138
|
final Node n = (Node) o;
|
139
|
|
140
|
final String id = n.valueOf("./id");
|
141
|
final String name = n.valueOf("./name");
|
142
|
final String status = n.valueOf("./status");
|
143
|
final String repoByeWfId = n.valueOf("./destroyWorkflow");
|
144
|
|
145
|
int progress = 0;
|
146
|
try {
|
147
|
switch (WorkflowStatus.valueOf(status)) {
|
148
|
case MISSING:
|
149
|
progress = 0;
|
150
|
break;
|
151
|
case ASSIGNED:
|
152
|
progress = 25;
|
153
|
break;
|
154
|
case WAIT_SYS_SETTINGS:
|
155
|
progress = 50;
|
156
|
break;
|
157
|
case WAIT_USER_SETTINGS:
|
158
|
progress = 75;
|
159
|
break;
|
160
|
case EXECUTABLE:
|
161
|
progress = 100;
|
162
|
break;
|
163
|
}
|
164
|
} catch (final Exception e) {
|
165
|
progress = 0;
|
166
|
}
|
167
|
ifc.getMetaWFs().add(new RepoMetaWfEntry(id, name, status, repoByeWfId, progress));
|
168
|
}
|
169
|
|
170
|
return ifc;
|
171
|
}
|
172
|
|
173
|
public List<VocabularyEntry> fetchVocabularyTerms(final String voc) throws ISLookUpException {
|
174
|
final String xquery = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType')[.//VOCABULARY_NAME/@code = '"
|
175
|
+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)";
|
176
|
|
177
|
final List<VocabularyEntry> list = Lists.newArrayList();
|
178
|
for (final String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
|
179
|
final String[] arr = s.split("@@@");
|
180
|
list.add(new VocabularyEntry(arr[0].trim(), arr[1].trim()));
|
181
|
}
|
182
|
Collections.sort(list);
|
183
|
|
184
|
return list;
|
185
|
}
|
186
|
|
187
|
private Set<String> getParameterNamesForProtocol(final String protocol) {
|
188
|
if (parametersMap.isEmpty()) {
|
189
|
for (final ProtocolDescriptor d : serviceLocator.getService(CollectorService.class).listProtocols()) {
|
190
|
parametersMap.put(d.getName().toLowerCase(), d.getParams());
|
191
|
}
|
192
|
}
|
193
|
final Set<String> res = Sets.newHashSet();
|
194
|
if (parametersMap.containsKey(protocol.toLowerCase())) {
|
195
|
res.add("baseUrl");
|
196
|
for (final ProtocolParameter p : parametersMap.get(protocol.toLowerCase())) {
|
197
|
res.add(p.getName());
|
198
|
}
|
199
|
}
|
200
|
|
201
|
return res;
|
202
|
}
|
203
|
|
204
|
}
|