1
|
package eu.dnetlib.functionality.modular.ui.repositories;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.Collections;
|
6
|
import java.util.List;
|
7
|
import java.util.Set;
|
8
|
|
9
|
import javax.annotation.Resource;
|
10
|
import javax.servlet.http.HttpServletRequest;
|
11
|
import javax.servlet.http.HttpServletResponse;
|
12
|
|
13
|
import org.dom4j.Document;
|
14
|
import org.dom4j.Node;
|
15
|
import org.dom4j.io.SAXReader;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.beans.factory.annotation.Required;
|
18
|
import org.springframework.ui.ModelMap;
|
19
|
|
20
|
import com.google.common.base.Splitter;
|
21
|
import com.google.common.collect.Lists;
|
22
|
import com.google.common.collect.Sets;
|
23
|
import com.google.gson.Gson;
|
24
|
|
25
|
import eu.dnetlib.enabling.datasources.common.Api;
|
26
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
27
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
28
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
29
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
30
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
31
|
import eu.dnetlib.functionality.modular.ui.ModuleEntryPoint;
|
32
|
import eu.dnetlib.miscutils.collections.Pair;
|
33
|
|
34
|
public class RepoApisEntryPointController extends ModuleEntryPoint {
|
35
|
|
36
|
@Resource
|
37
|
private UniqueServiceLocator serviceLocator;
|
38
|
|
39
|
@Resource
|
40
|
private RepoUIUtils repoUIUtils;
|
41
|
|
42
|
private String compatibilityLevelsVocabulary;
|
43
|
|
44
|
private String validatorAddress;
|
45
|
private String validatorServiceAddress;
|
46
|
|
47
|
@Autowired
|
48
|
private LocalDatasourceManager<Datasource<?, ?>, Api<?>> dsManager;
|
49
|
|
50
|
public class RepoHIWorkflow implements Comparable<RepoHIWorkflow> {
|
51
|
|
52
|
private final String id;
|
53
|
private final String name;
|
54
|
private final Set<String> ifaceTypes;
|
55
|
private final Set<String> compliances;
|
56
|
private List<Pair<String, String>> fields;
|
57
|
|
58
|
/**
|
59
|
* Instantiates a new repo hi workflow.
|
60
|
*
|
61
|
* @param id
|
62
|
* the id
|
63
|
* @param name
|
64
|
* the name
|
65
|
* @param ifaceTypes
|
66
|
* the iface types
|
67
|
* @param compliances
|
68
|
* the compliances
|
69
|
* @param fields
|
70
|
* the fields
|
71
|
*/
|
72
|
public RepoHIWorkflow(final String id, final String name, final Set<String> ifaceTypes, final Set<String> compliances,
|
73
|
final List<Pair<String, String>> fields) {
|
74
|
super();
|
75
|
this.id = id;
|
76
|
this.name = name;
|
77
|
this.ifaceTypes = ifaceTypes;
|
78
|
this.compliances = compliances;
|
79
|
this.fields = fields;
|
80
|
}
|
81
|
|
82
|
public String getId() {
|
83
|
return id;
|
84
|
}
|
85
|
|
86
|
public String getName() {
|
87
|
return name;
|
88
|
}
|
89
|
|
90
|
public Set<String> getIfaceTypes() {
|
91
|
return ifaceTypes;
|
92
|
}
|
93
|
|
94
|
public Set<String> getCompliances() {
|
95
|
return compliances;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public int compareTo(final RepoHIWorkflow o) {
|
100
|
return getName().compareTo(o.getName());
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* @return the fields
|
105
|
*/
|
106
|
public List<Pair<String, String>> getFields() {
|
107
|
return fields;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* @param fields
|
112
|
* the fields to set
|
113
|
*/
|
114
|
public void setFields(final List<Pair<String, String>> fields) {
|
115
|
this.fields = fields;
|
116
|
}
|
117
|
|
118
|
}
|
119
|
|
120
|
private RepoHIWorkflow parseQuery(final String input) {
|
121
|
final SAXReader reader = new SAXReader();
|
122
|
try {
|
123
|
final Document doc = reader.read(new StringReader(input));
|
124
|
|
125
|
final String id = doc.valueOf("//id");
|
126
|
final String name = doc.valueOf("//name");
|
127
|
final String type = doc.valueOf("//types");
|
128
|
final String compliance = doc.valueOf("//compliances");
|
129
|
final Set<String> ifcTypes = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(type));
|
130
|
final Set<String> compliances = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(compliance));
|
131
|
|
132
|
final List<Pair<String, String>> fields = new ArrayList<>();
|
133
|
for (final Object o : doc.selectNodes(".//FIELD")) {
|
134
|
final Node currentNode = (Node) o;
|
135
|
final String key = currentNode.valueOf("@name");
|
136
|
final String value = currentNode.getText();
|
137
|
fields.add(new Pair<>(key, value));
|
138
|
}
|
139
|
return new RepoHIWorkflow(id, name, ifcTypes, compliances, fields);
|
140
|
} catch (final Exception e) {
|
141
|
return null;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
private List<RepoHIWorkflow> listRepoHIWorkflows() throws ISLookUpException {
|
146
|
|
147
|
final String xquery =
|
148
|
"for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where $x//WORKFLOW_TYPE='REPO_HI' return <result> <id>{$x//RESOURCE_IDENTIFIER/@value/string()}</id> <name>{$x//WORKFLOW_NAME/text()}</name> <types>{$x//PARAM[@name='expectedInterfaceTypologyPrefixes']/text()}</types> <compliances>{$x//PARAM[@name='expectedCompliancePrefixes']/text()}</compliances> {$x//FIELD} </result>";
|
149
|
final List<RepoHIWorkflow> list = Lists.newArrayList();
|
150
|
for (final String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
|
151
|
final RepoHIWorkflow repoHiInfo = parseQuery(s);
|
152
|
if (repoHiInfo != null) {
|
153
|
list.add(repoHiInfo);
|
154
|
}
|
155
|
}
|
156
|
Collections.sort(list);
|
157
|
return list;
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
protected void initialize(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
|
162
|
final Gson gson = new Gson();
|
163
|
map.addAttribute("availableRepohiWfs", gson.toJson(listRepoHIWorkflows()));
|
164
|
map.addAttribute("compatibilityLevels", gson.toJson(repoUIUtils.fetchVocabularyTerms(getCompatibilityLevelsVocabulary())));
|
165
|
map.addAttribute("browseFields", gson.toJson(dsManager.listBrowsableFields()));
|
166
|
map.addAttribute("validatorAddress", getValidatorAddress());
|
167
|
map.addAttribute("validatorServiceAddress", getValidatorServiceAddress());
|
168
|
}
|
169
|
|
170
|
public String getCompatibilityLevelsVocabulary() {
|
171
|
return compatibilityLevelsVocabulary;
|
172
|
}
|
173
|
|
174
|
@Required
|
175
|
public void setCompatibilityLevelsVocabulary(final String compatibilityLevelsVocabulary) {
|
176
|
this.compatibilityLevelsVocabulary = compatibilityLevelsVocabulary;
|
177
|
}
|
178
|
|
179
|
public String getValidatorAddress() {
|
180
|
return validatorAddress;
|
181
|
}
|
182
|
|
183
|
@Required
|
184
|
public void setValidatorAddress(final String validatorAddress) {
|
185
|
this.validatorAddress = validatorAddress;
|
186
|
}
|
187
|
|
188
|
public String getValidatorServiceAddress() {
|
189
|
return validatorServiceAddress;
|
190
|
}
|
191
|
|
192
|
@Required
|
193
|
public void setValidatorServiceAddress(final String validatorServiceAddress) {
|
194
|
this.validatorServiceAddress = validatorServiceAddress;
|
195
|
}
|
196
|
|
197
|
}
|