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.HttpServletResponse;
|
10
|
|
11
|
import org.apache.commons.io.IOUtils;
|
12
|
import org.apache.commons.lang.StringUtils;
|
13
|
import org.apache.commons.lang.exception.ExceptionUtils;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.springframework.http.HttpStatus;
|
17
|
import org.springframework.stereotype.Controller;
|
18
|
import org.springframework.ui.ModelMap;
|
19
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
20
|
import org.springframework.web.bind.annotation.RequestMapping;
|
21
|
import org.springframework.web.bind.annotation.RequestParam;
|
22
|
import org.springframework.web.bind.annotation.ResponseBody;
|
23
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
24
|
|
25
|
import com.google.common.collect.Maps;
|
26
|
import com.google.gson.Gson;
|
27
|
|
28
|
import eu.dnetlib.data.collector.rmi.CollectorService;
|
29
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
30
|
import eu.dnetlib.data.collector.rmi.ProtocolParameterValue;
|
31
|
import eu.dnetlib.enabling.datasources.rmi.BrowseTerm;
|
32
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
|
33
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerService;
|
34
|
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException;
|
35
|
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc;
|
36
|
import eu.dnetlib.enabling.datasources.rmi.RepositoryMapEntry;
|
37
|
import eu.dnetlib.enabling.datasources.rmi.SearchInterfacesEntry;
|
38
|
import eu.dnetlib.enabling.datasources.rmi.SimpleDatasourceDesc;
|
39
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
40
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
41
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
42
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
43
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoInterfaceEntry;
|
44
|
import eu.dnetlib.functionality.modular.ui.workflows.objects.sections.WorkflowSectionGrouper;
|
45
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
46
|
import eu.dnetlib.msro.workflows.sarasvati.loader.WorkflowExecutor;
|
47
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
48
|
import net.sf.ehcache.Cache;
|
49
|
import net.sf.ehcache.Element;
|
50
|
|
51
|
@Controller
|
52
|
public class RepoInternalController {
|
53
|
|
54
|
@Resource
|
55
|
private UniqueServiceLocator serviceLocator;
|
56
|
|
57
|
@Resource
|
58
|
private WorkflowSectionGrouper workflowSectionGrouper;
|
59
|
|
60
|
@Resource
|
61
|
private WorkflowExecutor workflowExecutor;
|
62
|
|
63
|
@Resource
|
64
|
private RepoUIUtils repoUIUtils;
|
65
|
|
66
|
@Resource(name = "repoUIJsonCache")
|
67
|
private Cache repoUIJsonCache;
|
68
|
|
69
|
private static final Log log = LogFactory.getLog(RepoInternalController.class);
|
70
|
|
71
|
@RequestMapping(value = "/ui/browseRepoField.do")
|
72
|
public @ResponseBody List<BrowseTerm> browseRepoField(@RequestParam(value = "field", required = true) final String field) throws Exception {
|
73
|
return serviceLocator.getService(DatasourceManagerService.class).browseField(field);
|
74
|
}
|
75
|
|
76
|
@SuppressWarnings("unchecked")
|
77
|
@RequestMapping(value = "/ui/listApis.do")
|
78
|
public @ResponseBody List<SearchInterfacesEntry> listApis(
|
79
|
@RequestParam(value = "param", required = true) final String param,
|
80
|
@RequestParam(value = "value", required = true) final String value,
|
81
|
@RequestParam(value = "refresh", required = false) final String refresh) throws Exception {
|
82
|
|
83
|
final String cacheKey = "list@@@" + param + "@@@" + value;
|
84
|
|
85
|
final Element elem = repoUIJsonCache.get(cacheKey);
|
86
|
|
87
|
if ((elem != null) && (refresh == null)) {
|
88
|
return (List<SearchInterfacesEntry>) elem.getObjectValue();
|
89
|
} else {
|
90
|
log.info("Refreshing " + cacheKey + " cache...");
|
91
|
final List<SearchInterfacesEntry> list = serviceLocator.getService(DatasourceManagerService.class).searchInterface(param, value);
|
92
|
repoUIJsonCache.put(new Element(cacheKey, list));
|
93
|
return list;
|
94
|
}
|
95
|
}
|
96
|
|
97
|
@RequestMapping(value = "/ui/listRepositories.map")
|
98
|
public @ResponseBody List<RepositoryMapEntry> listRepositories_asMap() throws Exception {
|
99
|
return serviceLocator.getService(DatasourceManagerService.class).getRepositoryMap();
|
100
|
}
|
101
|
|
102
|
@RequestMapping(value = "/ui/listRepositories.json")
|
103
|
public @ResponseBody List<SimpleDatasourceDesc> listRepositories(@RequestParam(value = "type", required = true) final String type) throws Exception {
|
104
|
return serviceLocator.getService(DatasourceManagerService.class).simpleListDatasourcesByType(type);
|
105
|
}
|
106
|
|
107
|
@RequestMapping(value = "/ui/validateRepo.do")
|
108
|
public @ResponseBody String listRepositories(@RequestParam(value = "id", required = true) final String id,
|
109
|
@RequestParam(value = "b", required = true) final boolean b) throws Exception {
|
110
|
|
111
|
final String query = "count(/*[.//RESOURCE_TYPE/@value='MetaWorkflowDSResourceType' and .//DATAPROVIDER/@id='" + id + "'])";
|
112
|
if (!b && (Integer.parseInt(serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query)) > 0)) { throw new Exception("Repo " + id
|
113
|
+ " can be invalidated: it is related to some metawfs"); }
|
114
|
|
115
|
final String newId = b ? serviceLocator.getService(ISRegistryService.class).validateProfile(id)
|
116
|
: serviceLocator.getService(ISRegistryService.class).invalidateProfile(id);
|
117
|
|
118
|
repoUIJsonCache.removeAll();
|
119
|
|
120
|
return newId;
|
121
|
}
|
122
|
|
123
|
@RequestMapping(value = "/ui/getRepoDetails.do")
|
124
|
public void getRepoDetails(final HttpServletResponse response, @RequestParam(value = "id", required = true) final String id) throws Exception {
|
125
|
String profile;
|
126
|
try {
|
127
|
profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
|
128
|
} catch (final ISLookUpDocumentNotFoundException e) {
|
129
|
profile = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
130
|
"collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//DATASOURCE_ORIGINAL_ID='" + id + "']");
|
131
|
}
|
132
|
|
133
|
final ApplyXslt xslt = new ApplyXslt(IOUtils.toString(getClass().getResourceAsStream(
|
134
|
"/eu/dnetlib/functionality/modular/ui/repositories/xslt/repoDetails.xslt")));
|
135
|
|
136
|
IOUtils.copy(new StringReader(xslt.evaluate(profile)), response.getOutputStream());
|
137
|
}
|
138
|
|
139
|
@RequestMapping("/ui/repoMetaWf.new")
|
140
|
public @ResponseBody String newDataProviderWorkflow(@RequestParam(value = "id", required = true) final String repoId,
|
141
|
@RequestParam(value = "name", required = true) final String repoName,
|
142
|
@RequestParam(value = "iface", required = true) final String ifaceId,
|
143
|
@RequestParam(value = "wf", required = true) final String wfId) throws Exception {
|
144
|
|
145
|
final Map<String, Object> params = Maps.newHashMap();
|
146
|
params.put(WorkflowsConstants.DATAPROVIDER_ID, repoId);
|
147
|
params.put(WorkflowsConstants.DATAPROVIDER_NAME, repoName);
|
148
|
params.put(WorkflowsConstants.DATAPROVIDER_INTERFACE, ifaceId);
|
149
|
|
150
|
return workflowExecutor.startProcess(wfId, params);
|
151
|
}
|
152
|
|
153
|
@RequestMapping("/ui/repoMetaWf.destroy")
|
154
|
public @ResponseBody String destroyDataProviderWorkflow(@RequestParam(value = "destroyWf", required = true) final String destroyWfId)
|
155
|
throws Exception {
|
156
|
return workflowExecutor.startProcess(destroyWfId, null);
|
157
|
}
|
158
|
|
159
|
@RequestMapping("/ui/repoApi.get")
|
160
|
public @ResponseBody RepoInterfaceEntry getRepoApi(@RequestParam(value = "repoId", required = true) final String repoId,
|
161
|
@RequestParam(value = "ifaceId", required = true) final String ifaceId) throws Exception {
|
162
|
return repoUIUtils.getApi(repoId, ifaceId);
|
163
|
}
|
164
|
|
165
|
@SuppressWarnings("unchecked")
|
166
|
@RequestMapping("/ui/repoApi.update")
|
167
|
public @ResponseBody boolean updateRepoApi(
|
168
|
@RequestParam(value = "id", required = true) final String repoId,
|
169
|
@RequestParam(value = "iface", required = true) final String ifaceId,
|
170
|
@RequestParam(value = "accessParams", required = false) final String accessParamsJson,
|
171
|
@RequestParam(value = "mdIdPath", required = false) final String mdIdPath) throws Exception {
|
172
|
|
173
|
final DatasourceManagerService dsManager = serviceLocator.getService(DatasourceManagerService.class);
|
174
|
|
175
|
if (!StringUtils.isEmpty(accessParamsJson)) {
|
176
|
dsManager.bulkUpdateApiAccessParams(repoId, ifaceId, new Gson().fromJson(accessParamsJson, Map.class));
|
177
|
}
|
178
|
if (mdIdPath != null) {
|
179
|
dsManager.updateExtraField(repoId, ifaceId, "metadata_identifier_path", mdIdPath, true);
|
180
|
}
|
181
|
|
182
|
return true;
|
183
|
}
|
184
|
|
185
|
@RequestMapping("/ui/repoApi.delete")
|
186
|
public @ResponseBody boolean updateRepoApi(
|
187
|
@RequestParam(value = "repo", required = true) final String repoId,
|
188
|
@RequestParam(value = "iface", required = true) final String ifaceId) throws Exception {
|
189
|
return serviceLocator.getService(DatasourceManagerService.class).deleteInterface(repoId, ifaceId);
|
190
|
}
|
191
|
|
192
|
@RequestMapping("/ui/repoApiCompliance.update")
|
193
|
public @ResponseBody boolean updateRepoApiCompliance(@RequestParam(value = "id", required = true) final String repoId,
|
194
|
@RequestParam(value = "iface", required = true) final String ifaceId,
|
195
|
@RequestParam(value = "compliance", required = true) final String compliance) throws Exception {
|
196
|
|
197
|
log.debug("SET COMPLIANCE TO " + compliance);
|
198
|
|
199
|
serviceLocator.getService(DatasourceManagerService.class).overrideCompliance(repoId, ifaceId, compliance);
|
200
|
|
201
|
repoUIJsonCache.removeAll();
|
202
|
|
203
|
return true;
|
204
|
}
|
205
|
|
206
|
@RequestMapping("/ui/repoApiCompliance.reset")
|
207
|
public @ResponseBody boolean resetRepoApiCompliance(@RequestParam(value = "id", required = true) final String repoId,
|
208
|
@RequestParam(value = "iface", required = true) final String ifaceId) throws Exception {
|
209
|
|
210
|
log.debug("RESET COMPLIANCE");
|
211
|
|
212
|
serviceLocator.getService(DatasourceManagerService.class).overrideCompliance(repoId, ifaceId, null);
|
213
|
|
214
|
repoUIJsonCache.removeAll();
|
215
|
|
216
|
return true;
|
217
|
}
|
218
|
|
219
|
@RequestMapping("/ui/repos/repoApi.html")
|
220
|
public void resetRepoApiCompliance(final ModelMap map) throws Exception {}
|
221
|
|
222
|
@RequestMapping("/ui/repoApi.new")
|
223
|
public @ResponseBody boolean addRepoApi(@RequestParam(value = "repoId", required = true) final String repoId,
|
224
|
@RequestParam(value = "iface", required = true) final String ifaceJson) throws DatasourceManagerServiceException {
|
225
|
final IfaceDesc iface = new Gson().fromJson(ifaceJson, IfaceDesc.class);
|
226
|
|
227
|
log.info("Adding interface " + iface.getId() + " to repository " + repoId);
|
228
|
|
229
|
final DatasourceManagerService dsManager = serviceLocator.getService(DatasourceManagerService.class);
|
230
|
return dsManager.addInterface(repoId, iface);
|
231
|
}
|
232
|
|
233
|
@RequestMapping("/ui/repo.new")
|
234
|
public @ResponseBody boolean addRepoApi(@RequestParam(value = "repo", required = true) final String repoJson) throws DatasourceManagerServiceException {
|
235
|
final DatasourceDesc ds = new Gson().fromJson(repoJson, DatasourceDesc.class);
|
236
|
ds.setDateOfCollection(new Date());
|
237
|
|
238
|
if (StringUtils.isBlank(ds.getEnglishName())) {
|
239
|
ds.setEnglishName(ds.getOfficialName());
|
240
|
}
|
241
|
|
242
|
log.info("Adding datasource " + ds.getId() + " - name " + ds.getOfficialName());
|
243
|
|
244
|
final DatasourceManagerService dsManager = serviceLocator.getService(DatasourceManagerService.class);
|
245
|
|
246
|
return dsManager.addDatasource(ds);
|
247
|
}
|
248
|
|
249
|
@RequestMapping("/ui/listValidValuesForParam.do")
|
250
|
public @ResponseBody List<ProtocolParameterValue> listValidValuesForParam(
|
251
|
@RequestParam(value = "protocol", required = true) final String protocol,
|
252
|
@RequestParam(value = "param", required = true) final String param,
|
253
|
@RequestParam(value = "baseUrl", required = true) final String baseUrl) throws CollectorServiceException {
|
254
|
|
255
|
return serviceLocator.getService(CollectorService.class).listValidValuesForParam(protocol, baseUrl, param, null);
|
256
|
}
|
257
|
|
258
|
@ExceptionHandler(Exception.class)
|
259
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
260
|
public @ResponseBody ErrorMessage handleException(final Exception e) {
|
261
|
return new ErrorMessage(e);
|
262
|
}
|
263
|
|
264
|
public class ErrorMessage {
|
265
|
|
266
|
private final String message;
|
267
|
private final String stacktrace;
|
268
|
|
269
|
public ErrorMessage(final Exception e) {
|
270
|
this(e.getMessage(), ExceptionUtils.getStackTrace(e));
|
271
|
}
|
272
|
|
273
|
public ErrorMessage(final String message, final String stacktrace) {
|
274
|
this.message = message;
|
275
|
this.stacktrace = stacktrace;
|
276
|
}
|
277
|
|
278
|
public String getMessage() {
|
279
|
return this.message;
|
280
|
}
|
281
|
|
282
|
public String getStacktrace() {
|
283
|
return this.stacktrace;
|
284
|
}
|
285
|
|
286
|
}
|
287
|
|
288
|
}
|