1
|
package eu.dnetlib.enabling.dsmanager;
|
2
|
|
3
|
import java.text.ParseException;
|
4
|
import java.util.Date;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
|
8
|
import javax.annotation.Resource;
|
9
|
|
10
|
import org.apache.commons.lang.StringUtils;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.quartz.CronExpression;
|
14
|
|
15
|
import com.google.common.base.Function;
|
16
|
import com.google.common.collect.Lists;
|
17
|
|
18
|
import eu.dnetlib.common.services.AbstractBaseService;
|
19
|
import eu.dnetlib.enabling.datastructures.Datasource;
|
20
|
import eu.dnetlib.enabling.utils.DnetAnnotationUtils;
|
21
|
import eu.dnetlib.enabling.utils.DnetResourceFactory;
|
22
|
import eu.dnetlib.rmi.objects.dsmanager.BrowsableField;
|
23
|
import eu.dnetlib.rmi.objects.dsmanager.BrowseTerm;
|
24
|
import eu.dnetlib.rmi.objects.dsmanager.DatasourceConstants;
|
25
|
import eu.dnetlib.rmi.objects.dsmanager.IfaceDesc;
|
26
|
import eu.dnetlib.rmi.objects.dsmanager.RepositoryMapEntry;
|
27
|
import eu.dnetlib.rmi.objects.dsmanager.SearchInterfacesEntry;
|
28
|
import eu.dnetlib.rmi.objects.dsmanager.SimpleDatasourceDesc;
|
29
|
import eu.dnetlib.rmi.objects.is.DnetDataStructure;
|
30
|
import eu.dnetlib.rmi.soap.DatasourceManagerService;
|
31
|
import eu.dnetlib.rmi.soap.InformationService;
|
32
|
import eu.dnetlib.rmi.soap.exceptions.DatasourceManagerServiceException;
|
33
|
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
|
34
|
|
35
|
public class DatasourceManagerServiceImpl extends AbstractBaseService implements DatasourceManagerService {
|
36
|
|
37
|
@Resource
|
38
|
private InformationService informationService;
|
39
|
|
40
|
private static final Log log = LogFactory.getLog(DatasourceManagerServiceImpl.class);
|
41
|
|
42
|
@Override
|
43
|
public boolean addDatasource(final Datasource desc) throws DatasourceManagerServiceException {
|
44
|
try {
|
45
|
final DnetDataStructure ds = DnetResourceFactory.createDatastructure(desc.getId(), desc.getOfficialName(), desc);
|
46
|
final String id = informationService.registerDs(ds);
|
47
|
log.info("A new datasaource as been registered (assigned id: " + id + ")");
|
48
|
return true;
|
49
|
} catch (InformationServiceException e) {
|
50
|
throw new DatasourceManagerServiceException(e);
|
51
|
}
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public boolean deleteDatasource(final String code) throws DatasourceManagerServiceException {
|
56
|
try {
|
57
|
return informationService.deleteDsByCode(code, DatasourceConstants.DATASOURCE_TYPE);
|
58
|
} catch (InformationServiceException e) {
|
59
|
throw new DatasourceManagerServiceException(e);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public Datasource getDatasource(final String code) throws DatasourceManagerServiceException {
|
65
|
try {
|
66
|
final DnetDataStructure ds = informationService.getDsByCode(code, DatasourceConstants.DATASOURCE_TYPE);
|
67
|
return ds.getContentAsObject(Datasource.class);
|
68
|
} catch (InformationServiceException e) {
|
69
|
throw new DatasourceManagerServiceException(e);
|
70
|
}
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public List<Datasource> listAllDatasources() throws DatasourceManagerServiceException {
|
75
|
|
76
|
try {
|
77
|
return Lists.transform(informationService.listDatastructures(DatasourceConstants.DATASOURCE_TYPE), new Function<DnetDataStructure, Datasource>() {
|
78
|
|
79
|
@Override
|
80
|
public Datasource apply(final DnetDataStructure ds) {
|
81
|
try {
|
82
|
return ds.getContentAsObject(Datasource.class);
|
83
|
} catch (InformationServiceException e) {
|
84
|
log.error("Error decoding datasource " + ds, e);
|
85
|
return null;
|
86
|
}
|
87
|
}
|
88
|
});
|
89
|
} catch (InformationServiceException e) {
|
90
|
throw new DatasourceManagerServiceException(e);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
@Override
|
95
|
public List<Datasource> listDatasourcesUsingFilter(final String compliance,
|
96
|
final String contentDescription,
|
97
|
final String iisProcessingWorkflow,
|
98
|
final String collectedFrom)
|
99
|
throws DatasourceManagerServiceException {
|
100
|
|
101
|
if (StringUtils.isNotBlank(iisProcessingWorkflow) || StringUtils.isBlank(collectedFrom)) {
|
102
|
throw new DatasourceManagerServiceException("Filter not implemented for 'iisProcessingWorkflow' and 'collectedFrom'");
|
103
|
} else if (StringUtils.isBlank(compliance) && StringUtils.isBlank(contentDescription)) {
|
104
|
return listAllDatasources();
|
105
|
} else {
|
106
|
final List<Datasource> list = Lists.newArrayList();
|
107
|
for (Datasource ds : listAllDatasources()) {
|
108
|
for (IfaceDesc ifc : ds.getInterfaces()) {
|
109
|
boolean b = true;
|
110
|
b &= compliance == null || compliance.equalsIgnoreCase(ifc.getCompliance());
|
111
|
b &= contentDescription == null || contentDescription.equalsIgnoreCase(ifc.getContentDescription());
|
112
|
if (b) {
|
113
|
list.add(ds);
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
return list;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
private boolean updateInterfaceField(final String dsId, final String ifaceId, final Function<IfaceDesc, IfaceDesc> func)
|
122
|
throws DatasourceManagerServiceException {
|
123
|
try {
|
124
|
final DnetDataStructure dds = informationService.getDsByCode(dsId, DatasourceConstants.DATASOURCE_TYPE);
|
125
|
final Datasource ds = dds.getContentAsObject(Datasource.class);
|
126
|
|
127
|
for (IfaceDesc ifc : ds.getInterfaces()) {
|
128
|
if (ifc.getId().equals(ifaceId)) {
|
129
|
dds.setContentFromObject(func.apply(ifc));
|
130
|
informationService.registerDs(dds);
|
131
|
return true;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
return false;
|
136
|
} catch (InformationServiceException e) {
|
137
|
throw new DatasourceManagerServiceException(e);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public boolean updateLevelOfCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException {
|
143
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
144
|
|
145
|
@Override
|
146
|
public IfaceDesc apply(final IfaceDesc desc) {
|
147
|
desc.setCompliance(level);
|
148
|
return desc;
|
149
|
}
|
150
|
});
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public boolean overrideCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException {
|
155
|
// TODO Auto-generated method stub
|
156
|
return false;
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
public boolean updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) throws DatasourceManagerServiceException {
|
161
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
162
|
|
163
|
@Override
|
164
|
public IfaceDesc apply(final IfaceDesc desc) {
|
165
|
desc.setBaseUrl(baseUrl);
|
166
|
return desc;
|
167
|
}
|
168
|
});
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public boolean updateActivationStatus(final String dsId, final String ifaceId, final boolean active) throws DatasourceManagerServiceException {
|
173
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
174
|
|
175
|
@Override
|
176
|
public IfaceDesc apply(final IfaceDesc desc) {
|
177
|
desc.setActive(active);
|
178
|
return desc;
|
179
|
}
|
180
|
});
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public boolean updateContentDescription(final String dsId, final String ifaceId, final String contentDescription) throws DatasourceManagerServiceException {
|
185
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
186
|
|
187
|
@Override
|
188
|
public IfaceDesc apply(final IfaceDesc desc) {
|
189
|
desc.setContentDescription(contentDescription);
|
190
|
return desc;
|
191
|
}
|
192
|
});
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
public boolean setIisProcessingWorkflow(final String dsId, final String ifaceId, final String wf) throws DatasourceManagerServiceException {
|
197
|
throw new DatasourceManagerServiceException("NOT IMPLEMENTED");
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public boolean updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
202
|
throws DatasourceManagerServiceException {
|
203
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
204
|
|
205
|
@Override
|
206
|
public IfaceDesc apply(final IfaceDesc desc) {
|
207
|
desc.getExtraFields().put(field, value);
|
208
|
return desc;
|
209
|
}
|
210
|
});
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public boolean updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
215
|
throws DatasourceManagerServiceException {
|
216
|
|
217
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
218
|
|
219
|
@Override
|
220
|
public IfaceDesc apply(final IfaceDesc desc) {
|
221
|
desc.getAccessParams().put(field, value);
|
222
|
return desc;
|
223
|
}
|
224
|
});
|
225
|
}
|
226
|
|
227
|
@Override
|
228
|
public boolean deleteAccessParamOrExtraField(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException {
|
229
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
230
|
|
231
|
@Override
|
232
|
public IfaceDesc apply(final IfaceDesc desc) {
|
233
|
desc.getAccessParams().remove(field);
|
234
|
desc.getExtraFields().remove(field);
|
235
|
return desc;
|
236
|
}
|
237
|
});
|
238
|
}
|
239
|
|
240
|
@Override
|
241
|
public boolean addInterface(final String dsId, final IfaceDesc iface) throws DatasourceManagerServiceException {
|
242
|
try {
|
243
|
final DnetDataStructure dds = informationService.getDsByCode(dsId, DatasourceConstants.DATASOURCE_TYPE);
|
244
|
final Datasource ds = dds.getContentAsObject(Datasource.class);
|
245
|
|
246
|
for (int i = 0; i < ds.getInterfaces().size(); i++) {
|
247
|
if (ds.getInterfaces().get(0).getId().equals(iface.getId())) {
|
248
|
ds.getInterfaces().add(i, iface);
|
249
|
informationService.registerDs(dds);
|
250
|
return true;
|
251
|
}
|
252
|
}
|
253
|
ds.getInterfaces().add(iface);
|
254
|
informationService.registerDs(dds);
|
255
|
|
256
|
return true;
|
257
|
} catch (InformationServiceException e) {
|
258
|
throw new DatasourceManagerServiceException(e);
|
259
|
}
|
260
|
}
|
261
|
|
262
|
@Override
|
263
|
public boolean deleteInterface(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
264
|
try {
|
265
|
final DnetDataStructure dds = informationService.getDsByCode(dsId, DatasourceConstants.DATASOURCE_TYPE);
|
266
|
final Datasource ds = dds.getContentAsObject(Datasource.class);
|
267
|
|
268
|
for (int i = 0; i < ds.getInterfaces().size(); i++) {
|
269
|
if (ds.getInterfaces().get(0).getId().equals(ifaceId)) {
|
270
|
ds.getInterfaces().remove(i);
|
271
|
informationService.registerDs(dds);
|
272
|
return true;
|
273
|
}
|
274
|
}
|
275
|
return false;
|
276
|
} catch (InformationServiceException e) {
|
277
|
throw new DatasourceManagerServiceException(e);
|
278
|
}
|
279
|
}
|
280
|
|
281
|
@Override
|
282
|
public boolean updateSQL(final String dsId, final String sql, final boolean delete) throws DatasourceManagerServiceException {
|
283
|
throw new DatasourceManagerServiceException("NOT IMPLEMENTED");
|
284
|
}
|
285
|
|
286
|
@Override
|
287
|
public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
288
|
// final String xquery = "/*[.//DATAPROVIDER/@interface='" + ifaceId + "' and .//SCHEDULING/@enabled='true']//CRON/text()";
|
289
|
// TODO
|
290
|
try {
|
291
|
final String cronExpression = "";
|
292
|
final CronExpression cron = new CronExpression(cronExpression);
|
293
|
return cron.getNextValidTimeAfter(new Date());
|
294
|
} catch (ParseException e) {
|
295
|
log.error("Error parsing cron expression", e);
|
296
|
throw new DatasourceManagerServiceException("Error parsing cron expression", e);
|
297
|
}
|
298
|
}
|
299
|
|
300
|
@Override
|
301
|
public boolean bulkUpdateApiExtraFields(final String dsId, final String ifaceId, final Map<String, String> fields) throws DatasourceManagerServiceException {
|
302
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
303
|
|
304
|
@Override
|
305
|
public IfaceDesc apply(final IfaceDesc desc) {
|
306
|
desc.setExtraFields(fields);
|
307
|
return desc;
|
308
|
}
|
309
|
});
|
310
|
}
|
311
|
|
312
|
@Override
|
313
|
public boolean bulkUpdateApiAccessParams(final String dsId, final String ifaceId, final Map<String, String> params)
|
314
|
throws DatasourceManagerServiceException {
|
315
|
|
316
|
return updateInterfaceField(dsId, ifaceId, new Function<IfaceDesc, IfaceDesc>() {
|
317
|
|
318
|
@Override
|
319
|
public IfaceDesc apply(final IfaceDesc desc) {
|
320
|
desc.setAccessParams(params);
|
321
|
return desc;
|
322
|
}
|
323
|
});
|
324
|
}
|
325
|
|
326
|
@Override
|
327
|
public List<BrowsableField> listBrowsableFields() throws DatasourceManagerServiceException {
|
328
|
final List<BrowsableField> list = Lists.newArrayList();
|
329
|
|
330
|
for (Map.Entry<String, String> e : DnetAnnotationUtils.describeIndexedFields(Datasource.class).entrySet()) {
|
331
|
list.add(new BrowsableField(e.getKey(), e.getValue()));
|
332
|
}
|
333
|
return list;
|
334
|
}
|
335
|
|
336
|
@Override
|
337
|
public List<BrowseTerm> browseField(final String field) throws DatasourceManagerServiceException {
|
338
|
final List<BrowseTerm> list = Lists.newArrayList();
|
339
|
|
340
|
// TODO
|
341
|
|
342
|
return list;
|
343
|
}
|
344
|
|
345
|
@Override
|
346
|
public List<SearchInterfacesEntry> searchInterface(final String field, final String value) throws DatasourceManagerServiceException {
|
347
|
// TODO Auto-generated method stub
|
348
|
return null;
|
349
|
}
|
350
|
|
351
|
@Override
|
352
|
public List<RepositoryMapEntry> getRepositoryMap() throws DatasourceManagerServiceException {
|
353
|
final List<RepositoryMapEntry> list = Lists.newArrayList();
|
354
|
|
355
|
for (Datasource ds : listAllDatasources()) {
|
356
|
final Double lat = ds.getLatitude();
|
357
|
final Double lng = ds.getLatitude();
|
358
|
|
359
|
if (lat != null && lng != null && !(lat == 0 && lng == 0)) {
|
360
|
list.add(new RepositoryMapEntry(ds.getId(), ds.getOfficialName(), lat.floatValue(), lng.floatValue()));
|
361
|
}
|
362
|
}
|
363
|
|
364
|
return list;
|
365
|
}
|
366
|
|
367
|
@Override
|
368
|
public List<SimpleDatasourceDesc> simpleListDatasourcesByType(final String type) throws DatasourceManagerServiceException {
|
369
|
final List<SimpleDatasourceDesc> list = Lists.newArrayList();
|
370
|
try {
|
371
|
for (DnetDataStructure res : informationService.listDatastructures(DatasourceConstants.DATASOURCE_TYPE)) {
|
372
|
final Datasource ds = res.getContentAsObject(Datasource.class);
|
373
|
|
374
|
boolean b = false;
|
375
|
for (IfaceDesc ifc : ds.getInterfaces()) {
|
376
|
if (ifc.getTypology().equalsIgnoreCase(type)) {
|
377
|
b = true;
|
378
|
}
|
379
|
}
|
380
|
|
381
|
if (b) {
|
382
|
final SimpleDatasourceDesc s = new SimpleDatasourceDesc();
|
383
|
s.setId(ds.getId());
|
384
|
s.setOrigId(ds.getId());
|
385
|
s.setName(ds.getOfficialName());
|
386
|
s.setTypology(type);
|
387
|
s.setValid(res.isValid());
|
388
|
for (IfaceDesc ifc : ds.getInterfaces()) {
|
389
|
s.getApis().add(ifc.getId());
|
390
|
}
|
391
|
list.add(s);
|
392
|
}
|
393
|
}
|
394
|
|
395
|
return list;
|
396
|
} catch (InformationServiceException e) {
|
397
|
throw new DatasourceManagerServiceException(e);
|
398
|
}
|
399
|
}
|
400
|
}
|