1
|
package eu.dnetlib.functionality.modular.ui.repositories;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Date;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
|
8
|
import javax.annotation.Resource;
|
9
|
import javax.servlet.http.HttpServletRequest;
|
10
|
import javax.servlet.http.HttpServletResponse;
|
11
|
|
12
|
import org.apache.commons.io.IOUtils;
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.apache.commons.lang.exception.ExceptionUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.cache.annotation.CacheEvict;
|
19
|
import org.springframework.cache.annotation.Cacheable;
|
20
|
import org.springframework.http.HttpStatus;
|
21
|
import org.springframework.stereotype.Controller;
|
22
|
import org.springframework.ui.ModelMap;
|
23
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
24
|
import org.springframework.web.bind.annotation.RequestMapping;
|
25
|
import org.springframework.web.bind.annotation.RequestParam;
|
26
|
import org.springframework.web.bind.annotation.ResponseBody;
|
27
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
28
|
|
29
|
import com.google.common.collect.Maps;
|
30
|
import com.google.gson.Gson;
|
31
|
import com.google.gson.reflect.TypeToken;
|
32
|
|
33
|
import eu.dnetlib.data.collector.rmi.CollectorService;
|
34
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
35
|
import eu.dnetlib.data.collector.rmi.ProtocolParameterValue;
|
36
|
import eu.dnetlib.enabling.datasources.common.Api;
|
37
|
import eu.dnetlib.enabling.datasources.common.BrowseTerm;
|
38
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
39
|
import eu.dnetlib.enabling.datasources.common.DsmException;
|
40
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
41
|
import eu.dnetlib.enabling.datasources.common.SearchApisEntry;
|
42
|
import eu.dnetlib.enabling.datasources.common.SimpleDatasource;
|
43
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
44
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
45
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
46
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
47
|
import eu.dnetlib.functionality.modular.ui.error.ErrorMessage;
|
48
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoInterfaceEntry;
|
49
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.sections.WorkflowSectionGrouper;
|
50
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
51
|
import eu.dnetlib.msro.workflows.sarasvati.loader.WorkflowExecutor;
|
52
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
53
|
|
54
|
@Controller
|
55
|
public class RepoInternalController {
|
56
|
|
57
|
@Autowired
|
58
|
private LocalDatasourceManager<Datasource<?, ?>, Api<?>> dsManager;
|
59
|
|
60
|
@Resource
|
61
|
private UniqueServiceLocator serviceLocator;
|
62
|
|
63
|
@Resource
|
64
|
private WorkflowSectionGrouper workflowSectionGrouper;
|
65
|
|
66
|
@Resource
|
67
|
private WorkflowExecutor workflowExecutor;
|
68
|
|
69
|
@Resource
|
70
|
private RepoUIUtils repoUIUtils;
|
71
|
|
72
|
private static final Log log = LogFactory.getLog(RepoInternalController.class);
|
73
|
|
74
|
@RequestMapping(value = "/ui/browseRepoField.do")
|
75
|
public @ResponseBody List<? extends BrowseTerm> browseRepoField(@RequestParam(value = "field", required = true) final String field) throws Exception {
|
76
|
return dsManager.browseField(field);
|
77
|
}
|
78
|
|
79
|
@Cacheable(cacheNames = "repoUIJsonCache", key = "#param, #value", condition = "#refresh == false")
|
80
|
@RequestMapping(value = "/ui/listApis.do")
|
81
|
public @ResponseBody List<? extends SearchApisEntry> listApis(
|
82
|
@RequestParam(value = "param", required = true) final String param,
|
83
|
@RequestParam(value = "value", required = true) final String value,
|
84
|
@RequestParam(value = "refresh", required = false) final String refresh) throws Exception {
|
85
|
|
86
|
return dsManager.searchApis(param, value);
|
87
|
}
|
88
|
|
89
|
@RequestMapping(value = "/ui/listRepositories.json")
|
90
|
public @ResponseBody List<SimpleDatasource> listRepositories(@RequestParam(value = "type", required = true) final String type) throws Exception {
|
91
|
return dsManager.searchDatasourcesByType(type);
|
92
|
}
|
93
|
|
94
|
@CacheEvict("repoUIJsonCache")
|
95
|
@RequestMapping(value = "/ui/validateRepo.do")
|
96
|
public @ResponseBody String listRepositories(@RequestParam(value = "id", required = true) final String id,
|
97
|
@RequestParam(value = "b", required = true) final boolean b) throws Exception {
|
98
|
|
99
|
final String query = "count(/*[.//RESOURCE_TYPE/@value='MetaWorkflowDSResourceType' and .//DATAPROVIDER/@id='" + id + "'])";
|
100
|
if (!b && Integer.parseInt(serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query)) > 0) { throw new Exception("Repo " + id
|
101
|
+ " can be invalidated: it is related to some metawfs"); }
|
102
|
|
103
|
final String newId = b ? serviceLocator.getService(ISRegistryService.class).validateProfile(id)
|
104
|
: serviceLocator.getService(ISRegistryService.class).invalidateProfile(id);
|
105
|
|
106
|
return newId;
|
107
|
}
|
108
|
|
109
|
@RequestMapping(value = "/ui/getRepoDetails.do")
|
110
|
public void getRepoDetails(final HttpServletResponse response, @RequestParam(value = "id", required = true) final String id) throws Exception {
|
111
|
String profile;
|
112
|
try {
|
113
|
profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
|
114
|
} catch (final ISLookUpDocumentNotFoundException e) {
|
115
|
profile = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
116
|
"collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//DATASOURCE_ORIGINAL_ID='" + id + "']");
|
117
|
}
|
118
|
|
119
|
final ApplyXslt xslt = new ApplyXslt(IOUtils.toString(getClass().getResourceAsStream(
|
120
|
"/eu/dnetlib/functionality/modular/ui/repositories/xslt/repoDetails.xslt")));
|
121
|
|
122
|
IOUtils.copy(new StringReader(xslt.evaluate(profile)), response.getOutputStream());
|
123
|
}
|
124
|
|
125
|
@RequestMapping("/ui/repoMetaWf.new")
|
126
|
public @ResponseBody String newDataProviderWorkflow(@RequestParam(value = "id", required = true) final String repoId,
|
127
|
@RequestParam(value = "name", required = true) final String repoName,
|
128
|
@RequestParam(value = "iface", required = true) final String ifaceId,
|
129
|
@RequestParam(value = "wf", required = true) final String wfId) throws Exception {
|
130
|
|
131
|
final Map<String, Object> params = Maps.newHashMap();
|
132
|
params.put(WorkflowsConstants.DATAPROVIDER_ID, repoId);
|
133
|
params.put(WorkflowsConstants.DATAPROVIDER_NAME, repoName);
|
134
|
params.put(WorkflowsConstants.DATAPROVIDER_INTERFACE, ifaceId);
|
135
|
|
136
|
return workflowExecutor.startProcess(wfId, params);
|
137
|
}
|
138
|
|
139
|
@RequestMapping("/ui/repoMetaWf.destroy")
|
140
|
public @ResponseBody String destroyDataProviderWorkflow(@RequestParam(value = "destroyWf", required = true) final String destroyWfId)
|
141
|
throws Exception {
|
142
|
return workflowExecutor.startProcess(destroyWfId, null);
|
143
|
}
|
144
|
|
145
|
@RequestMapping("/ui/repoApi.get")
|
146
|
public @ResponseBody RepoInterfaceEntry getRepoApi(@RequestParam(value = "repoId", required = true) final String repoId,
|
147
|
@RequestParam(value = "ifaceId", required = true) final String ifaceId) throws Exception {
|
148
|
return repoUIUtils.getApi(repoId, ifaceId);
|
149
|
}
|
150
|
|
151
|
@RequestMapping("/ui/repoApi.update")
|
152
|
public @ResponseBody boolean updateRepoApi(
|
153
|
@RequestParam(value = "id", required = true) final String repoId,
|
154
|
@RequestParam(value = "iface", required = true) final String ifaceId,
|
155
|
@RequestParam(value = "accessParams", required = false) final String accessParamsJson,
|
156
|
@RequestParam(value = "mdIdPath", required = false) final String mdIdPath) throws Exception {
|
157
|
|
158
|
if (!StringUtils.isEmpty(accessParamsJson)) {
|
159
|
final Map<String, String> params = new Gson().fromJson(accessParamsJson, new TypeToken<Map<String, String>>() {}.getType());
|
160
|
final String baseUrl = params.remove("baseUrl");
|
161
|
dsManager.updateApiDetails(repoId, ifaceId, mdIdPath, baseUrl, params);
|
162
|
}
|
163
|
|
164
|
return true;
|
165
|
}
|
166
|
|
167
|
@RequestMapping("/ui/repoApi.delete")
|
168
|
public @ResponseBody boolean updateRepoApi(
|
169
|
@RequestParam(value = "repo", required = true) final String repoId,
|
170
|
@RequestParam(value = "iface", required = true) final String ifaceId) throws Exception {
|
171
|
dsManager.deleteApi(repoId, ifaceId);
|
172
|
return true;
|
173
|
}
|
174
|
|
175
|
@CacheEvict("repoUIJsonCache")
|
176
|
@RequestMapping("/ui/repoApiCompliance.update")
|
177
|
public @ResponseBody boolean updateRepoApiCompliance(@RequestParam(value = "id", required = true) final String repoId,
|
178
|
@RequestParam(value = "iface", required = true) final String ifaceId,
|
179
|
@RequestParam(value = "compliance", required = true) final String compliance) throws Exception {
|
180
|
|
181
|
log.debug("SET COMPLIANCE TO " + compliance);
|
182
|
|
183
|
dsManager.updateCompliance(repoId, ifaceId, compliance, true);
|
184
|
|
185
|
return true;
|
186
|
}
|
187
|
|
188
|
@CacheEvict("repoUIJsonCache")
|
189
|
@RequestMapping("/ui/repoApiCompliance.reset")
|
190
|
public @ResponseBody boolean resetRepoApiCompliance(@RequestParam(value = "id", required = true) final String repoId,
|
191
|
@RequestParam(value = "iface", required = true) final String ifaceId) throws Exception {
|
192
|
|
193
|
log.debug("RESET COMPLIANCE");
|
194
|
|
195
|
dsManager.updateCompliance(repoId, ifaceId, null, true);
|
196
|
|
197
|
return true;
|
198
|
}
|
199
|
|
200
|
@RequestMapping("/ui/repos/repoApi.html")
|
201
|
public void resetRepoApiCompliance(final ModelMap map) throws Exception {}
|
202
|
|
203
|
@RequestMapping("/ui/repoApi.new")
|
204
|
public @ResponseBody boolean addRepoApi(@RequestParam(value = "repoId", required = true) final String repoId,
|
205
|
@RequestParam(value = "iface", required = true) final String ifaceJson) throws DsmException {
|
206
|
final Api<?> iface = new Gson().fromJson(ifaceJson, Api.class);
|
207
|
|
208
|
iface.setDatasource(repoId);
|
209
|
|
210
|
log.info("Adding api " + iface.getId() + " to repository " + repoId);
|
211
|
|
212
|
dsManager.addApi(iface);
|
213
|
|
214
|
return true;
|
215
|
}
|
216
|
|
217
|
@RequestMapping("/ui/repo.new")
|
218
|
public @ResponseBody boolean addRepoApi(@RequestParam(value = "repo", required = true) final String repoJson) throws DsmException {
|
219
|
final Datasource<?, ?> ds = new Gson().fromJson(repoJson, Datasource.class);
|
220
|
final Date now = new Date();
|
221
|
ds.setDateofcollection(new java.sql.Date(now.getTime()));
|
222
|
|
223
|
if (StringUtils.isBlank(ds.getEnglishname())) {
|
224
|
ds.setEnglishname(ds.getOfficialname());
|
225
|
}
|
226
|
|
227
|
log.info("Adding datasource " + ds.getId() + " - name " + ds.getOfficialname());
|
228
|
|
229
|
dsManager.saveDs(ds);
|
230
|
|
231
|
return true;
|
232
|
}
|
233
|
|
234
|
@RequestMapping("/ui/listValidValuesForParam.do")
|
235
|
public @ResponseBody List<ProtocolParameterValue> listValidValuesForParam(
|
236
|
@RequestParam(value = "protocol", required = true) final String protocol,
|
237
|
@RequestParam(value = "param", required = true) final String param,
|
238
|
@RequestParam(value = "baseUrl", required = true) final String baseUrl) throws CollectorServiceException {
|
239
|
|
240
|
return serviceLocator.getService(CollectorService.class).listValidValuesForParam(protocol, baseUrl, param, null);
|
241
|
}
|
242
|
|
243
|
@ExceptionHandler(Exception.class)
|
244
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
245
|
public @ResponseBody ErrorMessage handleException(final HttpServletRequest req, final Exception e) {
|
246
|
log.error("Error processing " + req.getRequestURI(), e);
|
247
|
return new ErrorMessage(e.getMessage(), ExceptionUtils.getStackTrace(e));
|
248
|
}
|
249
|
|
250
|
}
|