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