1
|
package eu.dnetlib.functionality.modular.ui.workflows.util;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.Set;
|
9
|
|
10
|
import javax.annotation.Resource;
|
11
|
import javax.xml.transform.stream.StreamSource;
|
12
|
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.dom4j.Document;
|
17
|
import org.dom4j.Element;
|
18
|
import org.dom4j.io.SAXReader;
|
19
|
|
20
|
import com.google.common.collect.Lists;
|
21
|
import com.google.common.collect.Maps;
|
22
|
import com.google.common.collect.Sets;
|
23
|
|
24
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
25
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
26
|
import eu.dnetlib.enabling.tools.ServiceLocator;
|
27
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.AdvancedMetaWorkflowDescriptor;
|
28
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.AtomicWorkflowDescriptor;
|
29
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.MetaWorkflowDescriptor;
|
30
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.NodeInfo;
|
31
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.NodeWithUserParams;
|
32
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
33
|
import eu.dnetlib.msro.workflows.nodes.NodeStatus;
|
34
|
import eu.dnetlib.msro.workflows.util.WorkflowParam;
|
35
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants.WorkflowStatus;
|
36
|
|
37
|
public class ISLookupClient {
|
38
|
|
39
|
@Resource(name = "lookupLocator")
|
40
|
private ServiceLocator<ISLookUpService> lookupLocator;
|
41
|
|
42
|
private static final Log log = LogFactory.getLog(ISLookupClient.class);
|
43
|
|
44
|
private static final ApplyXslt metaWorkflowToHTML = new ApplyXslt(new StreamSource(
|
45
|
ISLookupClient.class.getResourceAsStream("/eu/dnetlib/functionality/modular/workflows/xslt/metawf_profile2html.xslt")));
|
46
|
|
47
|
public List<String> listSimpleWorflowSections() {
|
48
|
final String xquery = "distinct-values(//METAWORKFLOW_SECTION/text())";
|
49
|
try {
|
50
|
return lookupLocator.getService().quickSearchProfile(xquery);
|
51
|
} catch (ISLookUpException e) {
|
52
|
log.error("Error obtaining worflowSections", e);
|
53
|
return Lists.newArrayList();
|
54
|
}
|
55
|
}
|
56
|
|
57
|
public List<MetaWorkflowDescriptor> listMetaWorflowsForSection(final String name) {
|
58
|
final String query = queryForMetaWorkflowDescriptor(".//RESOURCE_TYPE/@value='MetaWorkflowDSResourceType' and .//METAWORKFLOW_SECTION='" + name + "'");
|
59
|
|
60
|
try {
|
61
|
return Lists.transform(lookupLocator.getService().quickSearchProfile(query), TransformerUtils.toMetaWorkflow);
|
62
|
} catch (ISLookUpException e) {
|
63
|
return Lists.newArrayList();
|
64
|
}
|
65
|
}
|
66
|
|
67
|
public AtomicWorkflowDescriptor getAtomicWorkflow(final String id) {
|
68
|
return TransformerUtils.toAtomicWorkflow.apply(getProfile(id));
|
69
|
}
|
70
|
|
71
|
public List<MetaWorkflowDescriptor> listMetaWorflowsForDatasource(final String dsId) {
|
72
|
final String query = queryForMetaWorkflowDescriptor(".//RESOURCE_TYPE/@value='MetaWorkflowDSResourceType' and .//DATAPROVIDER/@id='" + dsId + "'");
|
73
|
|
74
|
try {
|
75
|
return Lists.transform(lookupLocator.getService().quickSearchProfile(query), TransformerUtils.toMetaWorkflow);
|
76
|
} catch (ISLookUpException e) {
|
77
|
return Lists.newArrayList();
|
78
|
}
|
79
|
}
|
80
|
|
81
|
private String queryForMetaWorkflowDescriptor(final String cond) {
|
82
|
return "for $x in /*[" + cond + "] order by $x//METAWORKFLOW_NAME return concat(" + "$x//RESOURCE_IDENTIFIER/@value, ' @@@ ', "
|
83
|
+ "$x//METAWORKFLOW_NAME, ' @@@ ', " + "$x//METAWORKFLOW_NAME/@family, ' @@@ ', " + "$x//CONFIGURATION/@status)";
|
84
|
}
|
85
|
|
86
|
public String getProfile(final String id) {
|
87
|
try {
|
88
|
return lookupLocator.getService().getResourceProfile(id);
|
89
|
} catch (ISLookUpException e) {
|
90
|
log.error("Error finding profile: " + id, e);
|
91
|
return null;
|
92
|
}
|
93
|
}
|
94
|
|
95
|
public NodeInfo getNodeInfo(final String wfId, final String nodeName) {
|
96
|
try {
|
97
|
final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + wfId + "']//NODE[@name='" + nodeName + "']";
|
98
|
final String xml = lookupLocator.getService().getResourceProfileByQuery(query);
|
99
|
return TransformerUtils.toNodeInfo.apply(xml);
|
100
|
} catch (Exception e) {
|
101
|
log.error("Error accessing node " + nodeName + " of wf " + wfId, e);
|
102
|
return new NodeInfo("", "", new ArrayList<WorkflowParam>());
|
103
|
}
|
104
|
}
|
105
|
|
106
|
public Set<String> getNotConfiguredNodes(final String id) {
|
107
|
final String query = "for $x in (/*[.//RESOURCE_IDENTIFIER/@value='" + id + "']//NODE) "
|
108
|
+ "where count($x//PARAM[@required='true' and string-length(normalize-space(.)) = 0]) > 0 " + "return $x/@name/string()";
|
109
|
|
110
|
try {
|
111
|
final List<String> list = lookupLocator.getService().quickSearchProfile(query);
|
112
|
return Sets.newHashSet(list);
|
113
|
} catch (Exception e) {
|
114
|
log.error("Error executing xquery: " + query, e);
|
115
|
return Sets.newHashSet();
|
116
|
}
|
117
|
}
|
118
|
|
119
|
public AdvancedMetaWorkflowDescriptor getMetaWorkflow(final String id) {
|
120
|
final String xml = getProfile(id);
|
121
|
final Set<String> innerWfs = Sets.newHashSet();
|
122
|
try {
|
123
|
final SAXReader reader = new SAXReader();
|
124
|
final Document doc = reader.read(new StringReader(xml));
|
125
|
|
126
|
final String name = doc.valueOf("//METAWORKFLOW_NAME");
|
127
|
final String family = doc.valueOf("//METAWORKFLOW_NAME/@family");
|
128
|
|
129
|
final String email = doc.valueOf("//ADMIN_EMAIL");
|
130
|
|
131
|
final boolean scheduled = Boolean.valueOf(doc.valueOf("//SCHEDULING/@enabled"));;
|
132
|
final String cronExpression = doc.valueOf("//SCHEDULING/CRON");;
|
133
|
final int minInterval = Integer.parseInt(doc.valueOf("//SCHEDULING/MININTERVAL"));
|
134
|
final WorkflowStatus status = WorkflowStatus.valueOf(doc.valueOf("//CONFIGURATION/@status"));
|
135
|
for (Object o : doc.selectNodes("//WORKFLOW")) {
|
136
|
final Element wf = (Element) o;
|
137
|
final String wfId = wf.valueOf("@id");
|
138
|
innerWfs.add(wfId);
|
139
|
try {
|
140
|
final String profile = lookupLocator.getService().getResourceProfile(wfId);
|
141
|
final Document doc2 = (new SAXReader()).read(new StringReader(profile));
|
142
|
final String start = doc2.valueOf("//CONFIGURATION/@start");
|
143
|
final String date = doc2.valueOf("//STATUS/LAST_EXECUTION_DATE");
|
144
|
wf.addAttribute("start", start);
|
145
|
|
146
|
if (!StringUtils.isEmpty(date)) {
|
147
|
wf.addAttribute("date", date);
|
148
|
}
|
149
|
|
150
|
if ("disabled".equalsIgnoreCase(start)) {
|
151
|
wf.addAttribute("disabled", "true");
|
152
|
}
|
153
|
|
154
|
if (doc2.selectNodes("//PARAM[@required='true' and string-length(normalize-space(.)) = 0]").isEmpty()) {
|
155
|
wf.addAttribute("configured", "true");
|
156
|
} else {
|
157
|
wf.addAttribute("configured", "false");
|
158
|
}
|
159
|
|
160
|
} catch (Exception e) {
|
161
|
log.error("Error obtaining wf status", e);
|
162
|
wf.addAttribute("status", NodeStatus.NOT_CONFIGURED.toString());
|
163
|
}
|
164
|
}
|
165
|
|
166
|
// TODO delete this
|
167
|
ApplyXslt metaWorkflowToHTML = new ApplyXslt(new StreamSource(
|
168
|
ISLookupClient.class.getResourceAsStream("/eu/dnetlib/functionality/modular/workflows/xslt/metawf_profile2html.xslt")));
|
169
|
|
170
|
final String html = metaWorkflowToHTML.evaluate(doc.asXML());
|
171
|
|
172
|
return new AdvancedMetaWorkflowDescriptor(id, name, email, scheduled, cronExpression, minInterval, status, family, innerWfs, html);
|
173
|
|
174
|
} catch (Exception e) {
|
175
|
log.error("Error parsing xml: " + xml, e);
|
176
|
return new AdvancedMetaWorkflowDescriptor("", "", "", false, "", 0, WorkflowStatus.MISSING, "", new HashSet<String>(), "");
|
177
|
}
|
178
|
}
|
179
|
|
180
|
public Map<String, String> listRepoHiWorkflows() {
|
181
|
final Map<String, String> map = Maps.newHashMap();
|
182
|
|
183
|
final String query = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') " + "where $x//WORKFLOW_TYPE='REPO_HI' "
|
184
|
+ "return concat($x//RESOURCE_IDENTIFIER/@value, ' @@@ ', $x//WORKFLOW_NAME)";
|
185
|
|
186
|
try {
|
187
|
for (String s : lookupLocator.getService().quickSearchProfile(query)) {
|
188
|
String[] arr = s.split("@@@");
|
189
|
map.put(arr[0].trim(), arr[1].trim());
|
190
|
}
|
191
|
} catch (Exception e) {
|
192
|
log.error("Error executing xquery: " + query, e);
|
193
|
}
|
194
|
return map;
|
195
|
}
|
196
|
|
197
|
public List<String> listMetaWorflowsForWfId(final String wfId) {
|
198
|
final String query = "for $x in collection('/db/DRIVER/MetaWorkflowDSResources/MetaWorkflowDSResourceType') where $x//WORKFLOW/@id='" + wfId
|
199
|
+ "' return $x//RESOURCE_IDENTIFIER/@value/string()";
|
200
|
try {
|
201
|
return lookupLocator.getService().quickSearchProfile(query);
|
202
|
} catch (ISLookUpException e) {
|
203
|
log.error("Error executing xquery: " + query, e);
|
204
|
return Lists.newArrayList();
|
205
|
}
|
206
|
}
|
207
|
|
208
|
public boolean isExecutable(final String metaWfId) {
|
209
|
final String query = "for $x in collection('/db/DRIVER/MetaWorkflowDSResources/MetaWorkflowDSResourceType')[.//RESOURCE_IDENTIFIER/@value = '"
|
210
|
+ metaWfId
|
211
|
+ "']//WORKFLOW/@id \n"
|
212
|
+ "for $y in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType')[.//RESOURCE_IDENTIFIER/@value = $x]//PARAM[@required='true' and string-length(normalize-space(.)) = 0] \n"
|
213
|
+ "return $y";
|
214
|
try {
|
215
|
return lookupLocator.getService().quickSearchProfile(query).isEmpty();
|
216
|
} catch (ISLookUpException e) {
|
217
|
log.error("Error executing xquery: " + query, e);
|
218
|
return false;
|
219
|
}
|
220
|
}
|
221
|
|
222
|
public List<String> listWfFamilies() {
|
223
|
final String query = "distinct-values(for $x in collection('/db/DRIVER/MetaWorkflowDSResources/MetaWorkflowDSResourceType') "
|
224
|
+ "where string-length($x//DATAPROVIDER/@id) > 0 return $x//METAWORKFLOW_NAME/@family/string())";
|
225
|
try {
|
226
|
return lookupLocator.getService().quickSearchProfile(query);
|
227
|
} catch (ISLookUpException e) {
|
228
|
log.error("Error executing xquery: " + query, e);
|
229
|
return Lists.newArrayList();
|
230
|
}
|
231
|
}
|
232
|
|
233
|
public String getDatasourceName(final String dsId) {
|
234
|
final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//OFFICIAL_NAME/text()";
|
235
|
|
236
|
try {
|
237
|
return lookupLocator.getService().getResourceProfileByQuery(query);
|
238
|
} catch (ISLookUpException e) {
|
239
|
log.error("Error executing xquery: " + query, e);
|
240
|
return "UNKNOWN";
|
241
|
}
|
242
|
}
|
243
|
|
244
|
public List<NodeWithUserParams> listWorkflowUserParams(final String wfId) {
|
245
|
final List<NodeWithUserParams> res = Lists.newArrayList();
|
246
|
|
247
|
final String xml = getProfile(wfId);
|
248
|
try {
|
249
|
final SAXReader reader = new SAXReader();
|
250
|
final Document doc = reader.read(new StringReader(xml));
|
251
|
|
252
|
for (Object o : doc.selectNodes("//NODE[.//PARAM/@managedBy='user']")) {
|
253
|
final Element node = (Element) o;
|
254
|
final String name = node.valueOf("@name");
|
255
|
final String desc = node.valueOf("./DESCRIPTION");
|
256
|
|
257
|
final List<WorkflowParam> params = Lists.newArrayList();
|
258
|
for (Object k : node.selectNodes(".//PARAM[@managedBy='user']")) {
|
259
|
final Element param = (Element) k;
|
260
|
final String key = param.valueOf("@name");
|
261
|
final boolean required = "true".equalsIgnoreCase(param.valueOf("@required"));
|
262
|
final String type = param.valueOf("@type");
|
263
|
final String function = param.valueOf("@function");
|
264
|
final String value = param.getText();
|
265
|
params.add(new WorkflowParam(key, value, required, true, type, function));
|
266
|
}
|
267
|
res.add(new NodeWithUserParams(name, desc, params));
|
268
|
}
|
269
|
} catch (Exception e) {
|
270
|
log.error("Error obtaing params of wf: " + wfId, e);
|
271
|
}
|
272
|
|
273
|
return res;
|
274
|
}
|
275
|
|
276
|
}
|