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