1
|
package eu.dnetlib.functionality.modular.ui.workflows.util;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
import javax.annotation.PostConstruct;
|
9
|
import javax.annotation.Resource;
|
10
|
import javax.xml.transform.stream.StreamSource;
|
11
|
|
12
|
import com.google.common.base.Function;
|
13
|
import com.google.common.collect.Lists;
|
14
|
import com.google.common.collect.Maps;
|
15
|
import com.google.common.collect.Sets;
|
16
|
import com.google.gson.Gson;
|
17
|
import com.google.gson.reflect.TypeToken;
|
18
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
19
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
20
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
21
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.WorkflowItem;
|
22
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.WorkflowParamUI;
|
23
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
24
|
import org.apache.commons.logging.Log;
|
25
|
import org.apache.commons.logging.LogFactory;
|
26
|
import org.dom4j.DocumentException;
|
27
|
import org.dom4j.Node;
|
28
|
import org.dom4j.io.SAXReader;
|
29
|
import org.springframework.beans.factory.annotation.Required;
|
30
|
|
31
|
public class ISLookupClient {
|
32
|
|
33
|
private static final Log log = LogFactory.getLog(ISLookupClient.class);
|
34
|
private static final ApplyXslt workflowToHTML = new ApplyXslt(new StreamSource(
|
35
|
ISLookupClient.class.getResourceAsStream("/eu/dnetlib/functionality/modular/workflows/xslt/wf_profile2html.xslt")));
|
36
|
@Resource
|
37
|
private UniqueServiceLocator serviceLocator;
|
38
|
private String categoryUisJson;
|
39
|
|
40
|
private Map<String, List<WorkflowParamUI.UI>> categoryUis;
|
41
|
|
42
|
@PostConstruct
|
43
|
private void init() {
|
44
|
log.info("Initialing categoryUis map using JSON: " + getCategoryUisJson());
|
45
|
this.categoryUis = new Gson().fromJson(getCategoryUisJson(), new TypeToken<Map<String, List<WorkflowParamUI.UI>>>() {
|
46
|
}.getType());
|
47
|
}
|
48
|
|
49
|
public List<String> listSimpleWorflowSections() {
|
50
|
final String xquery = "distinct-values(//WORKFLOW_NAME/@menuSection/string())";
|
51
|
try {
|
52
|
return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
|
53
|
} catch (ISLookUpException e) {
|
54
|
log.error("Error obtaining worflowSections", e);
|
55
|
return Lists.newArrayList();
|
56
|
}
|
57
|
}
|
58
|
|
59
|
public List<WorkflowItem> listWorflowsForSection(final String name) {
|
60
|
return listWorflowsByCondition("//WORKFLOW_NAME/@menuSection='" + name + "'");
|
61
|
}
|
62
|
|
63
|
public List<WorkflowItem> listWorflowsForDatasource(final String dsId) {
|
64
|
return listWorflowsByCondition("//DATASOURCE/@id='" + dsId + "'");
|
65
|
}
|
66
|
|
67
|
private List<WorkflowItem> listWorflowsByCondition(final String cond) {
|
68
|
final String query =
|
69
|
"for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where $x" + cond
|
70
|
+ " order by $x//WORKFLOW_NAME return concat($x//RESOURCE_IDENTIFIER/@value, ' ==@== ', $x//WORKFLOW_NAME, ' ==@== ', $x//WORKFLOW_DESCRIPTION)";
|
71
|
|
72
|
try {
|
73
|
return Lists.transform(serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query), new Function<String, WorkflowItem>() {
|
74
|
@Override
|
75
|
public WorkflowItem apply(final String s) {
|
76
|
final String[] arr = s.split("==@==");
|
77
|
return new WorkflowItem(arr[0].trim(), arr[1].trim(), arr[2].trim());
|
78
|
}
|
79
|
});
|
80
|
} catch (ISLookUpException e) {
|
81
|
log.error("Error obtaining wfs using query: " + query, e);
|
82
|
return Lists.newArrayList();
|
83
|
}
|
84
|
}
|
85
|
|
86
|
public String getProfile(final String id) {
|
87
|
try {
|
88
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
|
89
|
} catch (ISLookUpException e) {
|
90
|
log.error("Error finding profile: " + id, e);
|
91
|
return null;
|
92
|
}
|
93
|
}
|
94
|
|
95
|
public Set<String> getNotConfiguredNodes(final String id) {
|
96
|
final String query = "for $x in (/*[.//RESOURCE_IDENTIFIER/@value='" + id + "']//NODE) "
|
97
|
+ "where count($x//PARAM[@required='true' and string-length(normalize-space(.)) = 0]) > 0 " + "return $x/@name/string()";
|
98
|
|
99
|
try {
|
100
|
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
101
|
return Sets.newHashSet(list);
|
102
|
} catch (Exception e) {
|
103
|
log.error("Error executing xquery: " + query, e);
|
104
|
return Sets.newHashSet();
|
105
|
}
|
106
|
}
|
107
|
|
108
|
public Map<String, String> listRepoHiWorkflows() {
|
109
|
final Map<String, String> map = Maps.newHashMap();
|
110
|
|
111
|
final String query = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') " + "where $x//WORKFLOW_TYPE='REPO_HI' "
|
112
|
+ "return concat($x//RESOURCE_IDENTIFIER/@value, ' @@@ ', $x//WORKFLOW_NAME)";
|
113
|
|
114
|
try {
|
115
|
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query)) {
|
116
|
String[] arr = s.split("@@@");
|
117
|
map.put(arr[0].trim(), arr[1].trim());
|
118
|
}
|
119
|
} catch (Exception e) {
|
120
|
log.error("Error executing xquery: " + query, e);
|
121
|
}
|
122
|
return map;
|
123
|
}
|
124
|
|
125
|
public List<String> listWfFamilies() {
|
126
|
final String query = "distinct-values(for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') "
|
127
|
+ "where string-length($x//DATASOURCE/@id) > 0 return $x//WORKFLOW_TYPE/@text())";
|
128
|
try {
|
129
|
return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
130
|
} catch (ISLookUpException e) {
|
131
|
log.error("Error executing xquery: " + query, e);
|
132
|
return Lists.newArrayList();
|
133
|
}
|
134
|
}
|
135
|
|
136
|
public String getDatasourceName(final String dsId) {
|
137
|
final String query = "/*[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//OFFICIAL_NAME/text()";
|
138
|
|
139
|
try {
|
140
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
|
141
|
} catch (ISLookUpException e) {
|
142
|
log.error("Error executing xquery: " + query, e);
|
143
|
return "UNKNOWN";
|
144
|
}
|
145
|
}
|
146
|
|
147
|
public Map<String, List<WorkflowParamUI.UI>> getCategoryUis() {
|
148
|
return categoryUis;
|
149
|
}
|
150
|
|
151
|
public String getCategoryUisJson() {
|
152
|
return categoryUisJson;
|
153
|
}
|
154
|
|
155
|
@Required
|
156
|
public void setCategoryUisJson(final String categoryUisJson) {
|
157
|
this.categoryUisJson = categoryUisJson;
|
158
|
}
|
159
|
|
160
|
public List<Map<String, String>> obtainSubWorkflows(final String wfId) {
|
161
|
|
162
|
final String query = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType')[.//RESOURCE_IDENTIFIER/@value='" + wfId
|
163
|
+ "']//NODE[@type='LaunchWorkflow']\n"
|
164
|
+ "for $y in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType')[.//RESOURCE_IDENTIFIER/@value = $x//PARAM[@name='wfId']/@value]\n"
|
165
|
+ "return <res>\n"
|
166
|
+ "<id>{$x//PARAM[@name='wfId']/@value/string()}</id>\n"
|
167
|
+ "<name>{$x/@name/string()}</name>\n"
|
168
|
+ "<description>{$x/DESCRIPTION/text()}</description>\n"
|
169
|
+ "<lastDate>{$y//LAST_EXECUTION_DATE/string()}</lastDate>\n"
|
170
|
+ "<lastStatus>{$y//LAST_EXECUTION_STATUS/string()}</lastStatus>\n"
|
171
|
+ "<disabled>{$y//CONFIGURATION/@start=\"DISABLED\" or $y//CONFIGURATION/@status !=\"EXECUTABLE\"}</disabled>\n"
|
172
|
+ "</res>";
|
173
|
try {
|
174
|
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
175
|
return Lists.transform(list, new Function<String, Map<String, String>>() {
|
176
|
|
177
|
private SAXReader reader = new SAXReader();
|
178
|
|
179
|
@Override
|
180
|
public Map<String, String> apply(final String s) {
|
181
|
try {
|
182
|
final Map<String, String> map = new HashMap<String, String>();
|
183
|
for (Object o : reader.read(new StringReader(s)).selectNodes("/res/*")) {
|
184
|
map.put(((Node) o).getName(), ((Node) o).getText());
|
185
|
}
|
186
|
return map;
|
187
|
} catch (DocumentException e) {
|
188
|
throw new RuntimeException(e);
|
189
|
}
|
190
|
}
|
191
|
});
|
192
|
} catch (ISLookUpException e) {
|
193
|
log.error("Error executing xquery: " + query, e);
|
194
|
return Lists.newArrayList();
|
195
|
}
|
196
|
|
197
|
}
|
198
|
}
|