1
|
package eu.dnetlib.enabling.datasources;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Date;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.Optional;
|
9
|
import java.util.stream.Collectors;
|
10
|
|
11
|
import org.apache.commons.lang.StringUtils;
|
12
|
import org.apache.commons.lang.math.NumberUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.springframework.beans.factory.annotation.Required;
|
16
|
import org.springframework.core.io.ClassPathResource;
|
17
|
import org.springframework.core.io.Resource;
|
18
|
|
19
|
import com.google.common.collect.ImmutableMap;
|
20
|
|
21
|
import eu.dnetlib.enabling.datasources.DatasourceManagerClients.AfterSqlUpdate;
|
22
|
import eu.dnetlib.enabling.datasources.common.Api;
|
23
|
import eu.dnetlib.enabling.datasources.common.ApiParam;
|
24
|
import eu.dnetlib.enabling.datasources.common.BrowsableField;
|
25
|
import eu.dnetlib.enabling.datasources.common.BrowseTerm;
|
26
|
import eu.dnetlib.enabling.datasources.common.BrowseTermImpl;
|
27
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
28
|
import eu.dnetlib.enabling.datasources.common.DsmException;
|
29
|
import eu.dnetlib.enabling.datasources.common.Identity;
|
30
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
31
|
import eu.dnetlib.enabling.datasources.common.Organization;
|
32
|
import eu.dnetlib.enabling.datasources.common.SearchApisEntry;
|
33
|
import eu.dnetlib.enabling.datasources.common.SimpleDatasource;
|
34
|
|
35
|
public class LocalOpenaireDatasourceManager implements LocalDatasourceManager<Datasource<Organization<?>, Identity>, Api<ApiParam>> {
|
36
|
|
37
|
private DatasourceManagerClients datasourceManagerClients;
|
38
|
|
39
|
private List<DbBrowsableField> browsableFields;
|
40
|
|
41
|
private static final String QUERY_BASEDIR = "/eu/dnetlib/enabling/datasources/queries/";
|
42
|
private static final Resource searchDsByType = new ClassPathResource(QUERY_BASEDIR + "searchDsByType.sql");
|
43
|
private static final Resource searchApis = new ClassPathResource(QUERY_BASEDIR + "searchApisNormal.sql");
|
44
|
private static final Resource searchApisUsingField = new ClassPathResource(QUERY_BASEDIR + "searchApisUsingField.sql");
|
45
|
private static final Resource addDs = new ClassPathResource(QUERY_BASEDIR + "addDatasource.sql");
|
46
|
private static final Resource addOrg = new ClassPathResource(QUERY_BASEDIR + "addOrganization.sql");
|
47
|
private static final Resource deleteDs = new ClassPathResource(QUERY_BASEDIR + "deleteDatasource.sql");
|
48
|
private static final Resource setActive = new ClassPathResource(QUERY_BASEDIR + "setActive.sql");
|
49
|
private static final Resource setManaged = new ClassPathResource(QUERY_BASEDIR + "setManaged.sql");
|
50
|
private static final Resource setCompliance = new ClassPathResource(QUERY_BASEDIR + "setCompliance.sql");
|
51
|
private static final Resource overrideCompliance = new ClassPathResource(QUERY_BASEDIR + "overrideCompliance.sql");
|
52
|
private static final Resource setLastCollectionInfo = new ClassPathResource(QUERY_BASEDIR + "setLastCollectionInfo.sql");
|
53
|
private static final Resource setLastAggregationInfo = new ClassPathResource(QUERY_BASEDIR + "setLastAggregationInfo.sql");
|
54
|
private static final Resource setLastDownloadInfo = new ClassPathResource(QUERY_BASEDIR + "setLastDownloadInfo.sql");
|
55
|
private static final Resource insertApiParam = new ClassPathResource(QUERY_BASEDIR + "insertApiParam.sql");
|
56
|
private static final Resource insertApi = new ClassPathResource(QUERY_BASEDIR + "insertApi.sql");
|
57
|
|
58
|
private static final Log log = LogFactory.getLog(LocalOpenaireDatasourceManager.class);
|
59
|
|
60
|
@Override
|
61
|
public List<SimpleDatasource> searchDatasourcesByType(final String type) throws DsmException {
|
62
|
return datasourceManagerClients.searchSQL(searchDsByType, ImmutableMap.of("type", type))
|
63
|
.stream()
|
64
|
.map(DatasourceFunctions::mapToSimpleDs)
|
65
|
.collect(Collectors.toList());
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public List<? extends SearchApisEntry> searchApis(final String field, final Object value) throws DsmException {
|
70
|
try {
|
71
|
final Map<String, Object> sqlParams = new HashMap<>();
|
72
|
|
73
|
if (!field.equalsIgnoreCase("__search__")) {
|
74
|
sqlParams.put("field", field);
|
75
|
sqlParams.put("value", value);
|
76
|
} else {
|
77
|
sqlParams.put("value", "%" + value + "%");
|
78
|
}
|
79
|
|
80
|
return datasourceManagerClients.searchSQL(field.equalsIgnoreCase("__search__") ? searchApis : searchApisUsingField, sqlParams)
|
81
|
.stream()
|
82
|
.map(DatasourceFunctions::mapToSearchApisEntry)
|
83
|
.collect(Collectors.toList());
|
84
|
|
85
|
} catch (final Exception e) {
|
86
|
log.error("Error searching field " + field + " - value: " + value, e);
|
87
|
}
|
88
|
return new ArrayList<>();
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void saveDs(final Datasource<Organization<?>, Identity> ds) throws DsmException {
|
93
|
if (StringUtils.isBlank(ds.getAggregator())) {
|
94
|
ds.setAggregator("OPENAIRE");
|
95
|
}
|
96
|
|
97
|
datasourceManagerClients.updateSQL(ds.getId(), addDs, AfterSqlUpdate.NONE, DatasourceFunctions.dsToMap(ds));
|
98
|
|
99
|
if (ds.getOrganizations() != null) {
|
100
|
for (final Organization<?> org : ds.getOrganizations()) {
|
101
|
final Map<String, Object> orgParams = DatasourceFunctions.orgToMap(ds.getId(), org);
|
102
|
datasourceManagerClients.updateSQL(ds.getId(), addOrg, AfterSqlUpdate.NONE, orgParams);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
datasourceManagerClients.regenerateProfile(ds.getId());
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
public void deleteDs(final String dsId) throws DsmException {
|
111
|
datasourceManagerClients.updateSQL(dsId, deleteDs, AfterSqlUpdate.DELETE_DS_PROFILE, ImmutableMap.of("dsId", dsId));
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public Datasource<Organization<?>, Identity> getDs(final String dsId) throws DsmException {
|
116
|
return datasourceManagerClients.getDatasourceById(dsId);
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public List<Api<ApiParam>> getApis(final String dsId) throws DsmException {
|
121
|
return datasourceManagerClients.getApis(dsId);
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public void setManaged(final String dsId, final boolean managed) throws DsmException {
|
126
|
final Map<String, Object> params = new HashMap<>();
|
127
|
params.put("managed", managed);
|
128
|
params.put("dsId", dsId);
|
129
|
|
130
|
datasourceManagerClients.updateSQL(dsId, setManaged, AfterSqlUpdate.UPDATE_DS_PROFILE, params);
|
131
|
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
public boolean isManaged(final String dsId) throws DsmException {
|
136
|
final String q = "SELECT * from dsm_datasources WHERE id = :dsId AND managed = true";
|
137
|
return !datasourceManagerClients.searchSQL(q, ImmutableMap.of("dsId", dsId)).isEmpty();
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public void setActive(final String dsId, final String apiId, final boolean active) throws DsmException {
|
142
|
final Map<String, Object> params = new HashMap<>();
|
143
|
params.put("active", active);
|
144
|
params.put("apiId", apiId);
|
145
|
params.put("dsId", dsId);
|
146
|
|
147
|
datasourceManagerClients.updateSQL(dsId, setActive, AfterSqlUpdate.UPDATE_DS_PROFILE, params);
|
148
|
if (!active) {
|
149
|
setLastCollectionInfo(dsId, apiId, null, null, null);
|
150
|
setLastAggregationInfo(dsId, apiId, null, null, null);
|
151
|
setLastDownloadInfo(dsId, apiId, null, null, null);
|
152
|
}
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
public boolean isActive(final String dsId, final String apiId) throws DsmException {
|
157
|
final String q = "SELECT * from dsm_api WHERE id = :apiId AND datasource = :dsId AND active = true";
|
158
|
return !datasourceManagerClients.searchSQL(q, ImmutableMap.of("dsId", dsId, "apiId", apiId)).isEmpty();
|
159
|
}
|
160
|
|
161
|
@Override
|
162
|
public boolean isRemovable(final String dsId, final String apiId) throws DsmException {
|
163
|
final String q = "SELECT * from dsm_api WHERE id = :apiId AND datasource = :dsId AND active != true AND removable = true";
|
164
|
return !datasourceManagerClients.searchSQL(q, ImmutableMap.of("dsId", dsId, "apiId", apiId)).isEmpty();
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public void updateCompliance(final String dsId, final String apiId, final String level, final boolean override) throws DsmException {
|
169
|
final Map<String, Object> params = ImmutableMap.of("level", level, "apiId", apiId, "dsId", dsId);
|
170
|
datasourceManagerClients.updateSQL(dsId, override ? overrideCompliance : setCompliance, AfterSqlUpdate.UPDATE_DS_PROFILE, params);
|
171
|
}
|
172
|
|
173
|
@Override
|
174
|
public void setLastCollectionInfo(final String dsId, final String apiId, final String mdId, final Integer size, final Date date)
|
175
|
throws DsmException {
|
176
|
setLastOperationInfo(setLastCollectionInfo, dsId, apiId, mdId, size, date);
|
177
|
}
|
178
|
|
179
|
@Override
|
180
|
public void setLastAggregationInfo(final String dsId, final String apiId, final String mdId, final Integer size, final Date date)
|
181
|
throws DsmException {
|
182
|
setLastOperationInfo(setLastAggregationInfo, dsId, apiId, mdId, size, date);
|
183
|
}
|
184
|
|
185
|
@Override
|
186
|
public void setLastDownloadInfo(final String dsId, final String apiId, final String objId, final Integer size, final Date date)
|
187
|
throws DsmException {
|
188
|
setLastOperationInfo(setLastDownloadInfo, dsId, apiId, objId, size, date);
|
189
|
}
|
190
|
|
191
|
private void setLastOperationInfo(final Resource sqlResource, final String dsId, final String apiId, final String mdId, final Integer size, final Date date)
|
192
|
throws DsmException {
|
193
|
final Map<String, Object> params = new HashMap<>();
|
194
|
params.put("dsId", dsId);
|
195
|
params.put("apiId", apiId);
|
196
|
params.put("mdId", mdId);
|
197
|
params.put("size", size);
|
198
|
params.put("date", date);
|
199
|
datasourceManagerClients.updateSQL(dsId, sqlResource, AfterSqlUpdate.UPDATE_DS_PROFILE, params);
|
200
|
}
|
201
|
|
202
|
@Override
|
203
|
public void updateApiDetails(final String dsId,
|
204
|
final String apiId,
|
205
|
final String metadataIdentifierPath,
|
206
|
final String baseUrl,
|
207
|
final Map<String, String> params)
|
208
|
throws DsmException {
|
209
|
// Delete old params
|
210
|
datasourceManagerClients.updateSQL(dsId, "DELETE FROM dsm_apiparams WHERE api = :api", AfterSqlUpdate.NONE, ImmutableMap.of("api", apiId));
|
211
|
|
212
|
// Insert new params
|
213
|
for (final Map.Entry<String, String> e : params.entrySet()) {
|
214
|
final Map<String, Object> sqlParams = ImmutableMap.of("param", e.getKey(), "value", e.getValue(), "api", apiId);
|
215
|
datasourceManagerClients.updateSQL(dsId, insertApiParam, AfterSqlUpdate.NONE, sqlParams);
|
216
|
}
|
217
|
|
218
|
// Update the BaseURL
|
219
|
datasourceManagerClients.updateSQL(dsId,
|
220
|
"UPDATE dsm_api SET baseurl = :baseurl WHERE id = :api",
|
221
|
AfterSqlUpdate.NONE,
|
222
|
ImmutableMap.of("baseurl", baseUrl, "api", apiId));
|
223
|
|
224
|
// Update the metadata_identifier_path
|
225
|
datasourceManagerClients.updateSQL(dsId,
|
226
|
"UPDATE dsm_api SET metadata_identifier_path = :path WHERE id = :api",
|
227
|
AfterSqlUpdate.NONE,
|
228
|
ImmutableMap.of("path", metadataIdentifierPath, "api", apiId));
|
229
|
|
230
|
// Update the IS profile
|
231
|
datasourceManagerClients.regenerateProfile(dsId);
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
public List<? extends BrowsableField> listBrowsableFields() throws DsmException {
|
236
|
return getBrowsableFields();
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public List<BrowseTerm> browseField(final String field) throws DsmException {
|
241
|
final Optional<DbBrowsableField> bf = getBrowsableFields()
|
242
|
.stream()
|
243
|
.filter(f -> f.getId().equals(field))
|
244
|
.findFirst();
|
245
|
|
246
|
if (bf.isPresent()) {
|
247
|
return datasourceManagerClients.searchSQL(bf.get().getSql(), new HashMap<>())
|
248
|
.stream()
|
249
|
.filter(m -> m.get("term") != null)
|
250
|
.filter(m -> m.get("count") != null)
|
251
|
.filter(m -> StringUtils.isNotBlank(m.get("term").toString()))
|
252
|
.map(m -> new BrowseTermImpl(m.get("term").toString(), NumberUtils.toInt(m.get("count").toString(), 0)))
|
253
|
.collect(Collectors.toList());
|
254
|
} else {
|
255
|
log.error("Not browsable field:" + field);
|
256
|
throw new DsmException("Not browsable field:" + field);
|
257
|
}
|
258
|
}
|
259
|
|
260
|
@Override
|
261
|
public void addApi(final Api<ApiParam> api) throws DsmException {
|
262
|
|
263
|
datasourceManagerClients.updateSQL(api.getDatasource(), insertApi, AfterSqlUpdate.NONE, DatasourceFunctions.apiToMap(api));
|
264
|
|
265
|
api.getApiParams().forEach(p -> {
|
266
|
final ImmutableMap<String, Object> sqlParams = ImmutableMap.of("param", p.getParam(), "value", p.getValue(), "api", api.getId());
|
267
|
try {
|
268
|
datasourceManagerClients.updateSQL(api.getDatasource(), insertApiParam, AfterSqlUpdate.NONE, sqlParams);
|
269
|
} catch (final DsmException e) {
|
270
|
throw new RuntimeException(e);
|
271
|
}
|
272
|
});
|
273
|
|
274
|
datasourceManagerClients.regenerateProfile(api.getDatasource());
|
275
|
}
|
276
|
|
277
|
@Override
|
278
|
public void deleteApi(final String dsId, final String apiId) throws DsmException {
|
279
|
if (!isRemovable(dsId, apiId)) { throw new DsmException("The api " + apiId + " can't be deleted"); }
|
280
|
|
281
|
datasourceManagerClients.updateSQL(dsId, "DELETE FROM dsm_apiparams WHERE api = :api", AfterSqlUpdate.NONE, ImmutableMap.of("api", apiId));
|
282
|
datasourceManagerClients.updateSQL(dsId, "DELETE FROM dsm_api WHERE id = :api", AfterSqlUpdate.UPDATE_DS_PROFILE, ImmutableMap.of("api", apiId));
|
283
|
}
|
284
|
|
285
|
public DatasourceManagerClients getDatasourceManagerClients() {
|
286
|
return datasourceManagerClients;
|
287
|
}
|
288
|
|
289
|
@Required
|
290
|
public void setDatasourceManagerClients(final DatasourceManagerClients datasourceManagerClients) {
|
291
|
this.datasourceManagerClients = datasourceManagerClients;
|
292
|
}
|
293
|
|
294
|
public List<DbBrowsableField> getBrowsableFields() {
|
295
|
return browsableFields;
|
296
|
}
|
297
|
|
298
|
@Required
|
299
|
public void setBrowsableFields(final List<DbBrowsableField> browsableFields) {
|
300
|
this.browsableFields = browsableFields;
|
301
|
}
|
302
|
|
303
|
}
|