1
|
package eu.dnetlib.administration.uis.modules.workflows;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Collections;
|
5
|
import java.util.List;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import org.apache.commons.lang3.StringUtils;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Component;
|
13
|
|
14
|
import eu.dnetlib.administration.uis.modules.datasources.ProtocolParameter;
|
15
|
import eu.dnetlib.administration.uis.modules.datasources.VocabularyEntry;
|
16
|
import eu.dnetlib.clients.is.InformationServiceClient;
|
17
|
import eu.dnetlib.exceptions.InformationServiceException;
|
18
|
|
19
|
@Component
|
20
|
public class WorkflowsUIUtils {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(WorkflowsUIUtils.class);
|
23
|
|
24
|
@Autowired
|
25
|
private InformationServiceClient isClient;
|
26
|
|
27
|
public List<VocabularyEntry> fetchVocabularyTerms(final String voc) throws InformationServiceException {
|
28
|
final String xquery = "for $x in collection('/db/DRIVER/conf/vocabulary')[.//VOCABULARY_NAME/@code = '"
|
29
|
+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)";
|
30
|
|
31
|
final List<VocabularyEntry> list = new ArrayList<>();
|
32
|
for (final String s : isClient.find(xquery)) {
|
33
|
final String[] arr = s.split("@@@");
|
34
|
list.add(new VocabularyEntry(arr[0].trim(), arr[1].trim()));
|
35
|
}
|
36
|
Collections.sort(list);
|
37
|
|
38
|
return list;
|
39
|
}
|
40
|
|
41
|
// @Cacheable("protocol_parameters")
|
42
|
public List<ProtocolParameter> listParametersForProtocol(final String protocol) {
|
43
|
|
44
|
final String query = "for $x in collection('/db/DRIVER/dnetService/msroWorker')//PLUGIN "
|
45
|
+ "where lower-case($x/@protocol) = '" + protocol.toLowerCase() + "'"
|
46
|
+ "return $x/PARAM/concat(@name, ' @-@ ', @type, ' @-@ ', @optional, ' @-@ ', @regex, ' @-@ ', @functionPopulated)";
|
47
|
|
48
|
try {
|
49
|
return isClient.find(query)
|
50
|
.stream()
|
51
|
.distinct()
|
52
|
.map(s -> {
|
53
|
final String[] arr = s.split("@-@");
|
54
|
|
55
|
final ProtocolParameter p = new ProtocolParameter();
|
56
|
|
57
|
p.setName(arr[0].trim());
|
58
|
|
59
|
if (StringUtils.isNotBlank(arr[1].trim())) {
|
60
|
p.setType(arr[1].trim());
|
61
|
}
|
62
|
|
63
|
if (StringUtils.isNotBlank(arr[2].trim())) {
|
64
|
p.setOptional("true".equalsIgnoreCase(arr[2].trim()));
|
65
|
}
|
66
|
|
67
|
if (StringUtils.isNotBlank(arr[3].trim())) {
|
68
|
p.setRegex(arr[3].trim());
|
69
|
}
|
70
|
|
71
|
if (StringUtils.isNotBlank(arr[4].trim())) {
|
72
|
p.setFunctionPopulated("true".equalsIgnoreCase(arr[4].trim()));
|
73
|
}
|
74
|
|
75
|
return p;
|
76
|
})
|
77
|
.collect(Collectors.toList());
|
78
|
} catch (final InformationServiceException e) {
|
79
|
log.warn("Error finding parameters of protocol " + protocol);
|
80
|
return new ArrayList<>();
|
81
|
}
|
82
|
}
|
83
|
|
84
|
public List<String> sections() {
|
85
|
final String xquery = "distinct-values(collection('/db/DRIVER/wf/simple')//WORKFLOW_NAME/@menuSection/string())";
|
86
|
try {
|
87
|
return isClient.find(xquery);
|
88
|
} catch (final InformationServiceException e) {
|
89
|
log.error("Error obtaining worflowSections", e);
|
90
|
return new ArrayList<>();
|
91
|
}
|
92
|
}
|
93
|
|
94
|
public List<WorkflowItem> listWorflowsForSection(final String name) {
|
95
|
return listWorflowsByCondition("//WORKFLOW_NAME/@menuSection='" + name + "'");
|
96
|
}
|
97
|
|
98
|
public List<WorkflowItem> listWorflowsForDatasource(final String dsId) {
|
99
|
return listWorflowsByCondition("//DATASOURCE/@id='" + dsId + "'");
|
100
|
}
|
101
|
|
102
|
public List<WorkflowItem> listWorflowsForApi(final String dsId, final String ifaceId) {
|
103
|
return listWorflowsByCondition("//DATASOURCE[@id='" + dsId + "' and @interface='" + ifaceId + "']");
|
104
|
}
|
105
|
|
106
|
private List<WorkflowItem> listWorflowsByCondition(final String cond) {
|
107
|
final String query =
|
108
|
"for $x in collection('/db/DRIVER/wf/simple') where $x" + cond
|
109
|
+ " order by $x//WORKFLOW_NAME return concat($x//RESOURCE_IDENTIFIER/@value, ' ==@== ', $x//WORKFLOW_NAME, ' ==@== ', $x//WORKFLOW_DESCRIPTION, ' ==@== ', $x//DESTROY_WORKFLOW_TEMPLATE/@id)";
|
110
|
|
111
|
try {
|
112
|
return isClient.find(query)
|
113
|
.stream()
|
114
|
.map(s -> {
|
115
|
final String[] arr = s.split("==@==");
|
116
|
return new WorkflowItem(arr[0].trim(), arr[1].trim(), arr[2].trim(), arr[3].trim().length() > 0);
|
117
|
})
|
118
|
.collect(Collectors.toList());
|
119
|
} catch (final InformationServiceException e) {
|
120
|
log.error("Error obtaining wfs using query: " + query, e);
|
121
|
return new ArrayList<>();
|
122
|
}
|
123
|
}
|
124
|
}
|