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