1
|
package eu.dnetlib.administration.uis.modules.datasources;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
import java.util.Objects;
|
7
|
import java.util.Set;
|
8
|
import java.util.stream.Collectors;
|
9
|
|
10
|
import javax.servlet.http.HttpServletRequest;
|
11
|
|
12
|
import org.apache.commons.io.IOUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.dom4j.Document;
|
16
|
import org.dom4j.Node;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.ui.ModelMap;
|
20
|
import org.springframework.web.bind.annotation.PathVariable;
|
21
|
import org.springframework.web.bind.annotation.RequestMapping;
|
22
|
import org.springframework.web.bind.annotation.RequestParam;
|
23
|
import org.springframework.web.bind.annotation.RestController;
|
24
|
|
25
|
import com.google.common.base.Splitter;
|
26
|
import com.google.common.collect.Sets;
|
27
|
import com.google.gson.Gson;
|
28
|
|
29
|
import eu.dnetlib.administration.uis.annotations.Authorization;
|
30
|
import eu.dnetlib.administration.uis.annotations.MenuEntry;
|
31
|
import eu.dnetlib.administration.uis.annotations.MenuGroup;
|
32
|
import eu.dnetlib.administration.uis.annotations.PermissionLevel;
|
33
|
import eu.dnetlib.clients.dsManager.BrowseTerm;
|
34
|
import eu.dnetlib.clients.dsManager.DatasourceDesc;
|
35
|
import eu.dnetlib.clients.dsManager.DsManagerClient;
|
36
|
import eu.dnetlib.clients.dsManager.SearchInterfacesEntry;
|
37
|
import eu.dnetlib.clients.is.InformationServiceClient;
|
38
|
import eu.dnetlib.clients.locators.ServiceClientFactory;
|
39
|
import eu.dnetlib.clients.msro.MsroClient;
|
40
|
import eu.dnetlib.exceptions.DnetGenericException;
|
41
|
import eu.dnetlib.miscutils.collections.Pair;
|
42
|
import eu.dnetlib.msro.workflows.WorkflowRef;
|
43
|
|
44
|
@RestController
|
45
|
@RequestMapping("/ajax/apis")
|
46
|
@MenuEntry(value = "Overview", order = 1, urlSection = "apis", group = MenuGroup.DS_MANAGEMENT)
|
47
|
@Authorization(PermissionLevel.DS_ADMIN)
|
48
|
public class ApisModule extends AbstractDsModule {
|
49
|
|
50
|
@Autowired
|
51
|
private InformationServiceClient isClient;
|
52
|
|
53
|
@Autowired
|
54
|
private DsManagerClient dsManager;
|
55
|
|
56
|
@Autowired
|
57
|
private ServiceClientFactory clientFactory;
|
58
|
|
59
|
@Autowired
|
60
|
private DatasourceVocabularies vocs;
|
61
|
|
62
|
private static final Log log = LogFactory.getLog(ApisModule.class);
|
63
|
|
64
|
@Override
|
65
|
public void populateModelMap(final ModelMap map, final HttpServletRequest request) throws Exception {
|
66
|
final Gson gson = new Gson();
|
67
|
map.addAttribute("compatibilityLevels", fetchVocabularyTermsAsJson(vocs.getCompatibilityLevels()));
|
68
|
map.addAttribute("browseFields", gson.toJson(dsManager.listBrowsableFields()));
|
69
|
}
|
70
|
|
71
|
@RequestMapping(value = "ds")
|
72
|
public DatasourceDesc getDS(@RequestParam final String id) throws Exception {
|
73
|
return dsManager.getDatasource(id);
|
74
|
}
|
75
|
|
76
|
@RequestMapping(value = "browse/{field}")
|
77
|
public List<BrowseTerm> browseField(@PathVariable final String field) throws Exception {
|
78
|
return dsManager.browseField(field);
|
79
|
}
|
80
|
|
81
|
@RequestMapping(value = "search")
|
82
|
public List<SearchInterfacesEntry> listApis(@RequestParam final String param,
|
83
|
@RequestParam final String value,
|
84
|
@RequestParam(required = false, defaultValue = "false") final boolean refresh) throws Exception {
|
85
|
if (refresh) {
|
86
|
dsManager.emptyCache();
|
87
|
}
|
88
|
return dsManager.searchInterface(param, value);
|
89
|
}
|
90
|
|
91
|
@RequestMapping(value = "repohi/wfs")
|
92
|
public List<RepoHIWorkflow> listRepoHiWfs(@RequestParam final String compliance, @RequestParam final String type) throws Exception {
|
93
|
|
94
|
try {
|
95
|
final String query =
|
96
|
IOUtils.toString(getClass().getResourceAsStream("/xquery/find_repohi.xquery"));
|
97
|
|
98
|
final SAXReader reader = new SAXReader();
|
99
|
|
100
|
return isClient.find(query)
|
101
|
.stream().map(s -> {
|
102
|
try {
|
103
|
final Document doc = reader.read(new StringReader(s));
|
104
|
final Set<String> ifcTypes = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(doc.valueOf("//TYPES")));
|
105
|
final Set<String> compliances =
|
106
|
Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(doc.valueOf("//COMPLIANCES")));
|
107
|
final String id = doc.valueOf("//ID");
|
108
|
final String name = doc.valueOf("//NAME");
|
109
|
final String desc = doc.valueOf("//DESC");
|
110
|
final List<Pair<String, String>> fields = new ArrayList<Pair<String, String>>();
|
111
|
for (final Object o : doc.selectNodes(".//FIELD")) {
|
112
|
final Node currentNode = (Node) o;
|
113
|
final String key = currentNode.valueOf("@name");
|
114
|
final String value = currentNode.getText();
|
115
|
fields.add(new Pair<String, String>(key, value));
|
116
|
}
|
117
|
return new RepoHIWorkflow(id, name, desc, ifcTypes, compliances, fields);
|
118
|
} catch (final Exception ex) {
|
119
|
log.warn("Error parsing xml: " + s);
|
120
|
return null;
|
121
|
}
|
122
|
})
|
123
|
.filter(Objects::nonNull)
|
124
|
.filter(wf -> wf.isValidFor(type, compliance))
|
125
|
.sorted()
|
126
|
.collect(Collectors.toList());
|
127
|
} catch (final Exception e) {
|
128
|
log.error("Error finding repo-hi wfs", e);
|
129
|
throw new DnetGenericException("Error finding repo-hi wfs", e);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
@RequestMapping(value = "repohi/start")
|
134
|
public WorkflowRef startRepoHiWf(@RequestParam final String dsId,
|
135
|
@RequestParam final String iface,
|
136
|
@RequestParam final String wfId,
|
137
|
@RequestParam final String workerId) throws Exception {
|
138
|
return clientFactory.getClient(MsroClient.class).startRepoHiWorkflow(wfId, workerId, dsId, iface);
|
139
|
}
|
140
|
|
141
|
@RequestMapping(value = "repobye")
|
142
|
public WorkflowRef startRepoHiWf(@RequestParam final String wfId) throws Exception {
|
143
|
return clientFactory.getClient(MsroClient.class).startRepoByeForWorkflow(wfId);
|
144
|
}
|
145
|
|
146
|
}
|