1
|
package eu.dnetlib.openaire.dsm.dao;
|
2
|
|
3
|
import java.sql.Date;
|
4
|
import java.util.HashSet;
|
5
|
import java.util.List;
|
6
|
import java.util.Objects;
|
7
|
import java.util.Set;
|
8
|
import java.util.stream.Collectors;
|
9
|
|
10
|
import com.google.common.collect.Lists;
|
11
|
import eu.dnetlib.OpenaireExporterConfig;
|
12
|
import eu.dnetlib.enabling.datasources.common.DsmException;
|
13
|
import eu.dnetlib.openaire.dsm.domain.RequestFilter;
|
14
|
import eu.dnetlib.openaire.dsm.domain.RequestSort;
|
15
|
import eu.dnetlib.openaire.dsm.domain.RequestSortOrder;
|
16
|
import eu.dnetlib.openaire.dsm.domain.db.ApiDbEntry;
|
17
|
import eu.dnetlib.openaire.dsm.domain.db.ApiParamDbEntry;
|
18
|
import eu.dnetlib.openaire.dsm.domain.db.DatasourceApiDbEntry;
|
19
|
import eu.dnetlib.openaire.dsm.domain.db.DatasourceDbEntry;
|
20
|
import eu.dnetlib.openaire.vocabularies.Country;
|
21
|
import eu.dnetlib.openaire.vocabularies.Vocabulary;
|
22
|
import org.apache.commons.lang.StringUtils;
|
23
|
import org.apache.commons.logging.Log;
|
24
|
import org.apache.commons.logging.LogFactory;
|
25
|
import org.apache.http.HttpStatus;
|
26
|
import org.springframework.beans.factory.annotation.Autowired;
|
27
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
28
|
import org.springframework.data.domain.Page;
|
29
|
import org.springframework.data.domain.PageRequest;
|
30
|
import org.springframework.data.jpa.domain.Specification;
|
31
|
import org.springframework.stereotype.Component;
|
32
|
import org.springframework.transaction.annotation.Transactional;
|
33
|
|
34
|
import static eu.dnetlib.openaire.dsm.dao.DatasourceSpecs.apiSpec;
|
35
|
import static eu.dnetlib.openaire.dsm.dao.DatasourceSpecs.dsSpec;
|
36
|
|
37
|
import static eu.dnetlib.openaire.common.ExporterConstants.*;
|
38
|
|
39
|
/**
|
40
|
* Created by claudio on 20/10/2016.
|
41
|
*/
|
42
|
@Component
|
43
|
@ConditionalOnProperty(value = "openaire.exporter.enable.dsm", havingValue = "true")
|
44
|
public class DatasourceDaoImpl implements DatasourceDao<DatasourceDbEntry, ApiDbEntry> {
|
45
|
|
46
|
private static final Log log = LogFactory.getLog(DatasourceDao.class);
|
47
|
|
48
|
@Autowired
|
49
|
private OpenaireExporterConfig config;
|
50
|
|
51
|
@Autowired
|
52
|
private CountryTermRepository countryTermRepository;
|
53
|
|
54
|
@Autowired
|
55
|
private DatasourceDbEntryRepository dsRepository;
|
56
|
|
57
|
@Autowired
|
58
|
private ApiDbEntryRepository apiRepository;
|
59
|
|
60
|
@Autowired
|
61
|
private DatasourceApiDbEntryRepository dsApiRepository;
|
62
|
|
63
|
@Autowired
|
64
|
private VocabularyClient vocabularyClient;
|
65
|
|
66
|
@Override
|
67
|
public List<Country> listCountries() throws DsmException {
|
68
|
final List<Country> countries = Lists.newArrayList();
|
69
|
final Vocabulary v = vocabularyClient.getCountries();
|
70
|
countries.addAll(countryTermRepository.findAll().stream()
|
71
|
.filter(Objects::nonNull)
|
72
|
.map(t -> new Country(t.getTerm(), v.getEnglishName(t.getTerm())))
|
73
|
.collect(Collectors.toList()));
|
74
|
return countries;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public Page<DatasourceDbEntry> search(final RequestSort requestSortBy, RequestSortOrder order, RequestFilter requestFilter, final int page, final int size)
|
79
|
throws DsmException {
|
80
|
|
81
|
final Specification<DatasourceDbEntry> spec = dsSpec(requestSortBy, order, requestFilter);
|
82
|
return dsRepository.findAll(spec, PageRequest.of(page, size));
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public DatasourceDbEntry getDs(final String dsId) throws DsmException {
|
87
|
return dsRepository.getOne(dsId);
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public void setManaged(final String id, final boolean managed) {
|
92
|
log.info(String.format("setting managed = '%s' for ds '%s'", managed, id));
|
93
|
dsRepository.setManaged(id, managed);
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
public boolean isManaged(final String id) {
|
98
|
return dsRepository.isManaged(id);
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
public void updateCompliance(String dsId, String apiId, String compliance, boolean override) {
|
103
|
log.info(String.format("setting compatibility = '%s' for ds '%s'", compliance, apiId));
|
104
|
apiRepository.updateCompatibility(apiId, compliance);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public List<ApiDbEntry> getApis(final String dsId) {
|
109
|
return apiRepository.findByDatasource(dsId);
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public void deleteApi(final String dsId, final String apiId) throws DsmException {
|
114
|
final ApiDbEntry api = apiRepository.getOne(apiId);
|
115
|
if (!api.getRemovable()) {
|
116
|
throw new DsmException(HttpStatus.SC_UNAUTHORIZED, "api is not removable");
|
117
|
}
|
118
|
|
119
|
apiRepository.deleteById(apiId);
|
120
|
log.info(String.format("deleted api '%s'", apiId));
|
121
|
}
|
122
|
|
123
|
@Override
|
124
|
public void addApi(final ApiDbEntry api) {
|
125
|
apiRepository.save(api);
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public boolean existDs(final String dsId) throws DsmException {
|
130
|
return dsRepository.existsById(dsId);
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
public void saveDs(final DatasourceDbEntry d) {
|
135
|
log.info(String.format("saving datasource '%s'", d.getId()));
|
136
|
|
137
|
if (StringUtils.isNotBlank(d.getRegisteredby())) {
|
138
|
ensureRegistrationDate(d.getId());
|
139
|
}
|
140
|
|
141
|
final DatasourceDbEntry datasource = dsRepository.save(d);
|
142
|
log.info(String.format("saved datasource '%s'", datasource.getId()));
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
public void deleteDs(final String dsId) {
|
147
|
dsRepository.deleteById(dsId);
|
148
|
log.info(String.format("deleted datasource '%s'", dsId));
|
149
|
}
|
150
|
|
151
|
@Override
|
152
|
public void updateName(final String dsId, final String officialname, final String englishname) {
|
153
|
//TODO what if one of the two names is null or empty?
|
154
|
dsRepository.setDatasourcename(dsId, officialname, englishname);
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public void updateLogoUrl(final String dsId, final String logourl) throws DsmException {
|
159
|
dsRepository.setLogoUrl(dsId, logourl);
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
public void updateCoordinates(final String dsId, final Double latitude, final Double longitude) {
|
164
|
dsRepository.setCoordinates(dsId, latitude, longitude);
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public void updateApiBaseUrl(final String apiId, final String baseurl) {
|
169
|
apiRepository.setBaseurl(apiId, baseurl);
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
@Transactional
|
174
|
public boolean upsertApiOaiSet(final String apiId, final String oaiSet) throws DsmException {
|
175
|
final ApiDbEntry api = apiRepository.getOne(apiId);
|
176
|
if (OAI.equalsIgnoreCase(api.getProtocol())) {
|
177
|
final Set<ApiParamDbEntry> apiParams = api.getApiParams();
|
178
|
|
179
|
if (!apiParams.stream().anyMatch(ap -> SET.equals(ap.getParam()))) {
|
180
|
apiRepository.addApiParam(apiId, SET, oaiSet);
|
181
|
log.info(String.format("added api '%s' oai set with '%s'", apiId, oaiSet));
|
182
|
return true;
|
183
|
} else {
|
184
|
apiRepository.updateOaiSet(apiId, oaiSet);
|
185
|
log.info(String.format("updated api '%s' oai set with '%s'", apiId, oaiSet));
|
186
|
return false;
|
187
|
}
|
188
|
} else {
|
189
|
throw new DsmException(String.format("won't add OAI set to a non OAI interface: '%s' has protocol '%s'", apiId, api.getProtocol()));
|
190
|
}
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
public List<String> findApiBaseURLs(final RequestFilter requestFilter, final int page, final int size) throws DsmException {
|
195
|
final PageRequest pageable = PageRequest.of(page, size);
|
196
|
final Specification<DatasourceApiDbEntry> spec = apiSpec(requestFilter);
|
197
|
final Set<String> set = dsApiRepository.findAll(spec, pageable).getContent().stream()
|
198
|
.map(DatasourceApiDbEntry::getBaseurl)
|
199
|
.filter(StringUtils::isNotBlank)
|
200
|
.collect(Collectors.toCollection(HashSet::new));
|
201
|
return Lists.newArrayList(set);
|
202
|
}
|
203
|
|
204
|
@Override
|
205
|
public void updateTimezone(final String dsId, final String timezone) {
|
206
|
dsRepository.setTimezone(dsId, timezone);
|
207
|
}
|
208
|
|
209
|
@Override
|
210
|
public void updateTypology(final String dsId, final String typology) throws DsmException {
|
211
|
final Vocabulary typologies = vocabularyClient.getDatasourceTypologies();
|
212
|
if (!typologies.hasCode(typology)) {
|
213
|
throw new DsmException(
|
214
|
HttpStatus.SC_BAD_REQUEST,
|
215
|
String.format(
|
216
|
"invalid datasource typology '%s', provide one according to vocabulary %s",
|
217
|
typology,
|
218
|
config.getVocabularies().getDatasourceTypologiesEndpoint()));
|
219
|
}
|
220
|
dsRepository.setTypology(dsId, typology);
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public void updateRegisteringUser(final String dsId, final String registeredBy) throws DsmException {
|
225
|
|
226
|
ensureRegistrationDate(dsId);
|
227
|
|
228
|
dsRepository.setRegisteringUser(dsId, registeredBy);
|
229
|
|
230
|
}
|
231
|
|
232
|
@Override
|
233
|
public void updatePlatform(final String dsId, final String platform) throws DsmException {
|
234
|
dsRepository.setPlatform(dsId, platform);
|
235
|
}
|
236
|
|
237
|
//HELPER
|
238
|
private void ensureRegistrationDate(String dsId) {
|
239
|
if (!dsRepository.hasRegistrationdate(dsId)) {
|
240
|
log.info("setting registration date for datasource: " + dsId);
|
241
|
dsRepository.setRegistrationDate(dsId, new Date(System.currentTimeMillis()));
|
242
|
}
|
243
|
}
|
244
|
|
245
|
}
|