1
|
package eu.dnetlib.functionality.modular.ui.workflows.util;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.Collections;
|
6
|
import java.util.HashMap;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
10
|
|
11
|
import javax.annotation.PostConstruct;
|
12
|
import javax.annotation.Resource;
|
13
|
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.commons.logging.Log;
|
17
|
import org.apache.commons.logging.LogFactory;
|
18
|
import org.dom4j.Document;
|
19
|
import org.dom4j.DocumentException;
|
20
|
import org.dom4j.Node;
|
21
|
import org.dom4j.io.SAXReader;
|
22
|
import org.springframework.beans.factory.annotation.Required;
|
23
|
|
24
|
import com.google.common.base.Function;
|
25
|
import com.google.common.base.Splitter;
|
26
|
import com.google.common.collect.Lists;
|
27
|
import com.google.common.collect.Sets;
|
28
|
import com.google.gson.Gson;
|
29
|
import com.google.gson.reflect.TypeToken;
|
30
|
|
31
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
32
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
33
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
34
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
35
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoHIWorkflow;
|
36
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.WorkflowItem;
|
37
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.WorkflowParamUI;
|
38
|
import eu.dnetlib.miscutils.collections.Pair;
|
39
|
|
40
|
public class ISLookupClient {
|
41
|
|
42
|
private static final Log log = LogFactory.getLog(ISLookupClient.class);
|
43
|
|
44
|
@Resource
|
45
|
private UniqueServiceLocator serviceLocator;
|
46
|
private String categoryUisJson;
|
47
|
private Map<String, List<WorkflowParamUI.UI>> categoryUis;
|
48
|
|
49
|
@PostConstruct
|
50
|
private void init() {
|
51
|
log.info("Initialing categoryUis map using JSON: " + getCategoryUisJson());
|
52
|
this.categoryUis = new Gson().fromJson(getCategoryUisJson(), new TypeToken<Map<String, List<WorkflowParamUI.UI>>>() {}.getType());
|
53
|
}
|
54
|
|
55
|
public List<String> listSimpleWorflowSections() {
|
56
|
final String xquery = "distinct-values(//WORKFLOW_NAME/@menuSection/string())";
|
57
|
try {
|
58
|
return this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
|
59
|
} catch (final ISLookUpException e) {
|
60
|
log.error("Error obtaining worflowSections", e);
|
61
|
return Lists.newArrayList();
|
62
|
}
|
63
|
}
|
64
|
|
65
|
public List<WorkflowItem> listWorflowsForSection(final String name) {
|
66
|
return listWorflowsByCondition("//WORKFLOW_NAME/@menuSection='" + name + "'");
|
67
|
}
|
68
|
|
69
|
public List<WorkflowItem> listWorflowsForDatasource(final String dsId) {
|
70
|
return listWorflowsByCondition("//DATASOURCE/@id='" + dsId + "'");
|
71
|
}
|
72
|
|
73
|
public List<WorkflowItem> listWorflowsForApi(final String dsId, final String ifaceId) {
|
74
|
return listWorflowsByCondition("//DATASOURCE[@id='" + dsId + "' and @interface='" + ifaceId + "']");
|
75
|
}
|
76
|
|
77
|
private List<WorkflowItem> listWorflowsByCondition(final String cond) {
|
78
|
final String query =
|
79
|
"for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where $x" + cond
|
80
|
+ " order by $x//WORKFLOW_NAME return concat($x//RESOURCE_IDENTIFIER/@value, ' ==@== ', $x//WORKFLOW_NAME, ' ==@== ', $x//WORKFLOW_DESCRIPTION)";
|
81
|
|
82
|
try {
|
83
|
return Lists.transform(this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query), new Function<String, WorkflowItem>() {
|
84
|
|
85
|
@Override
|
86
|
public WorkflowItem apply(final String s) {
|
87
|
final String[] arr = s.split("==@==");
|
88
|
return new WorkflowItem(arr[0].trim(), arr[1].trim(), arr[2].trim());
|
89
|
}
|
90
|
});
|
91
|
} catch (final ISLookUpException e) {
|
92
|
log.error("Error obtaining wfs using query: " + query, e);
|
93
|
return Lists.newArrayList();
|
94
|
}
|
95
|
}
|
96
|
|
97
|
public String getProfile(final String id) {
|
98
|
try {
|
99
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
|
100
|
} catch (final ISLookUpException e) {
|
101
|
log.error("Error finding profile: " + id, e);
|
102
|
return null;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
public String getRepoProfile(final String id) throws ISLookUpException {
|
107
|
try {
|
108
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
|
109
|
} catch (final ISLookUpDocumentNotFoundException e) {
|
110
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
111
|
"collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//DATASOURCE_ORIGINAL_ID='" + id + "']");
|
112
|
}
|
113
|
}
|
114
|
|
115
|
public Set<String> getNotConfiguredNodes(final String id) {
|
116
|
final String query = "for $x in (/*[.//RESOURCE_IDENTIFIER/@value='" + id + "']//NODE) "
|
117
|
+ "where count($x//PARAM[@required='true' and string-length(normalize-space(.)) = 0]) > 0 " + "return $x/@name/string()";
|
118
|
|
119
|
try {
|
120
|
final List<String> list = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
121
|
return Sets.newHashSet(list);
|
122
|
} catch (final Exception e) {
|
123
|
log.error("Error executing xquery: " + query, e);
|
124
|
return Sets.newHashSet();
|
125
|
}
|
126
|
}
|
127
|
|
128
|
public List<RepoHIWorkflow> listRepoHiWorkflows(final String compliance, final String type) {
|
129
|
|
130
|
final List<RepoHIWorkflow> list = Lists.newArrayList();
|
131
|
|
132
|
if (StringUtils.isNotBlank(compliance) && StringUtils.isNotBlank(type)) {
|
133
|
|
134
|
try {
|
135
|
final String query =
|
136
|
IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/ui/workflows/xquery/find_repohi.xquery"));
|
137
|
|
138
|
final SAXReader reader = new SAXReader();
|
139
|
|
140
|
for (final String s : this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query)) {
|
141
|
final Document doc = reader.read(new StringReader(s));
|
142
|
final Set<String> ifcTypes = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(doc.valueOf("//TYPES")));
|
143
|
final Set<String> compliances = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(doc.valueOf("//COMPLIANCES")));
|
144
|
|
145
|
if (isValidRepoHiTerm(compliance, compliances) && isValidRepoHiTerm(type, ifcTypes)) {
|
146
|
|
147
|
final String id = doc.valueOf("//ID");
|
148
|
final String name = doc.valueOf("//NAME");
|
149
|
final String desc = doc.valueOf("//DESC");
|
150
|
|
151
|
final List<Pair<String, String>> fields = new ArrayList<Pair<String, String>>();
|
152
|
for (final Object o : doc.selectNodes(".//FIELD")) {
|
153
|
final Node currentNode = (Node) o;
|
154
|
final String key = currentNode.valueOf("@name");
|
155
|
final String value = currentNode.getText();
|
156
|
fields.add(new Pair<String, String>(key, value));
|
157
|
}
|
158
|
|
159
|
final RepoHIWorkflow r = new RepoHIWorkflow(id, name, desc, ifcTypes, compliances, fields);
|
160
|
|
161
|
list.add(r);
|
162
|
}
|
163
|
}
|
164
|
Collections.sort(list);
|
165
|
} catch (final Exception e) {
|
166
|
log.error("Error finding repo-hi wfs", e);
|
167
|
}
|
168
|
}
|
169
|
return list;
|
170
|
|
171
|
}
|
172
|
|
173
|
private boolean isValidRepoHiTerm(final String term, final Set<String> valids) {
|
174
|
if (StringUtils.isBlank(term)) {
|
175
|
return false;
|
176
|
} else if (valids.size() == 0) {
|
177
|
return true;
|
178
|
} else {
|
179
|
boolean res = false;
|
180
|
for (final String v : valids) {
|
181
|
if (term.startsWith(v)) {
|
182
|
res = true;
|
183
|
}
|
184
|
}
|
185
|
return res;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
public List<String> listWfFamilies() {
|
190
|
final String query = "distinct-values(for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') "
|
191
|
+ "where string-length($x//DATASOURCE/@id) > 0 return $x//WORKFLOW_TYPE/@text())";
|
192
|
try {
|
193
|
return this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
194
|
} catch (final ISLookUpException e) {
|
195
|
log.error("Error executing xquery: " + query, e);
|
196
|
return Lists.newArrayList();
|
197
|
}
|
198
|
}
|
199
|
|
200
|
public String getDatasourceName(final String dsId) {
|
201
|
final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//OFFICIAL_NAME/text()";
|
202
|
|
203
|
try {
|
204
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
|
205
|
} catch (final ISLookUpException e) {
|
206
|
log.error("Error executing xquery: " + query, e);
|
207
|
return "UNKNOWN";
|
208
|
}
|
209
|
}
|
210
|
|
211
|
public Map<String, List<WorkflowParamUI.UI>> getCategoryUis() {
|
212
|
return this.categoryUis;
|
213
|
}
|
214
|
|
215
|
public String getCategoryUisJson() {
|
216
|
return this.categoryUisJson;
|
217
|
}
|
218
|
|
219
|
@Required
|
220
|
public void setCategoryUisJson(final String categoryUisJson) {
|
221
|
this.categoryUisJson = categoryUisJson;
|
222
|
}
|
223
|
|
224
|
public List<Map<String, String>> obtainSubWorkflows(final String wfId) {
|
225
|
|
226
|
final String query = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType')[.//RESOURCE_IDENTIFIER/@value='" + wfId
|
227
|
+ "']//NODE[@type='LaunchWorkflow']\n"
|
228
|
+ "for $y in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType')[.//RESOURCE_IDENTIFIER/@value = $x//PARAM[@name='wfId']/@value]\n"
|
229
|
+ "return <res>\n"
|
230
|
+ "<id>{$x//PARAM[@name='wfId']/@value/string()}</id>\n"
|
231
|
+ "<name>{$x/@name/string()}</name>\n"
|
232
|
+ "<description>{$x/DESCRIPTION/text()}</description>\n"
|
233
|
+ "<lastDate>{$y//LAST_EXECUTION_DATE/string()}</lastDate>\n"
|
234
|
+ "<lastStatus>{$y//LAST_EXECUTION_STATUS/string()}</lastStatus>\n"
|
235
|
+ "<disabled>{$y//CONFIGURATION/@start=\"DISABLED\" or $y//CONFIGURATION/@status !=\"EXECUTABLE\"}</disabled>\n"
|
236
|
+ "</res>";
|
237
|
try {
|
238
|
final List<String> list = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
239
|
return Lists.transform(list, new Function<String, Map<String, String>>() {
|
240
|
|
241
|
private final SAXReader reader = new SAXReader();
|
242
|
|
243
|
@Override
|
244
|
public Map<String, String> apply(final String s) {
|
245
|
try {
|
246
|
final Map<String, String> map = new HashMap<String, String>();
|
247
|
for (final Object o : this.reader.read(new StringReader(s)).selectNodes("/res/*")) {
|
248
|
map.put(((Node) o).getName(), ((Node) o).getText());
|
249
|
}
|
250
|
return map;
|
251
|
} catch (final DocumentException e) {
|
252
|
throw new RuntimeException(e);
|
253
|
}
|
254
|
}
|
255
|
});
|
256
|
} catch (final ISLookUpException e) {
|
257
|
log.error("Error executing xquery: " + query, e);
|
258
|
return Lists.newArrayList();
|
259
|
}
|
260
|
|
261
|
}
|
262
|
}
|