2022-02-04 10:12:15 +01:00
|
|
|
package eu.dnetlib.openaire.dsm.dao;
|
|
|
|
|
2022-02-07 10:09:18 +01:00
|
|
|
import static eu.dnetlib.openaire.common.ExporterConstants.OAI;
|
|
|
|
import static eu.dnetlib.openaire.common.ExporterConstants.SET;
|
|
|
|
import static eu.dnetlib.openaire.dsm.dao.DatasourceSpecs.apiSpec;
|
|
|
|
import static eu.dnetlib.openaire.dsm.dao.DatasourceSpecs.dsRegisteredbyNotNullSpec;
|
|
|
|
import static eu.dnetlib.openaire.dsm.dao.DatasourceSpecs.dsSpec;
|
|
|
|
|
|
|
|
import java.sql.Date;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import javax.persistence.EntityNotFoundException;
|
|
|
|
|
2022-02-04 10:12:15 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
2022-02-07 10:09:18 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2022-02-04 10:12:15 +01:00
|
|
|
|
2022-02-07 10:09:18 +01:00
|
|
|
import eu.dnetlib.DnetOpenaireExporterProperties;
|
|
|
|
import eu.dnetlib.openaire.dsm.domain.RequestSort;
|
|
|
|
import eu.dnetlib.openaire.dsm.domain.RequestSortOrder;
|
|
|
|
import eu.dnetlib.openaire.dsm.domain.db.ApiDbEntry;
|
|
|
|
import eu.dnetlib.openaire.dsm.domain.db.ApiParamDbEntry;
|
|
|
|
import eu.dnetlib.openaire.dsm.domain.db.DatasourceApiDbEntry;
|
|
|
|
import eu.dnetlib.openaire.dsm.domain.db.DatasourceDbEntry;
|
2023-03-31 14:53:52 +02:00
|
|
|
import eu.dnetlib.openaire.exporter.exceptions.DsmException;
|
|
|
|
import eu.dnetlib.openaire.exporter.exceptions.DsmForbiddenException;
|
|
|
|
import eu.dnetlib.openaire.exporter.exceptions.DsmNotFoundException;
|
2023-04-03 08:33:39 +02:00
|
|
|
import eu.dnetlib.openaire.exporter.model.dsm.RequestFilter;
|
2023-03-31 14:53:52 +02:00
|
|
|
import eu.dnetlib.openaire.exporter.model.vocabularies.Country;
|
|
|
|
import eu.dnetlib.openaire.exporter.model.vocabularies.Vocabulary;
|
2022-02-04 10:12:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by claudio on 20/10/2016.
|
|
|
|
*/
|
|
|
|
@Component
|
|
|
|
@ConditionalOnProperty(value = "openaire.exporter.enable.dsm", havingValue = "true")
|
|
|
|
public class DatasourceDaoImpl implements DatasourceDao<DatasourceDbEntry, ApiDbEntry> {
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(DatasourceDao.class);
|
|
|
|
|
|
|
|
@Autowired
|
2022-02-07 10:09:18 +01:00
|
|
|
private DnetOpenaireExporterProperties config;
|
2022-02-04 10:12:15 +01:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CountryTermRepository countryTermRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private DatasourceDbEntryRepository dsRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private ApiDbEntryRepository apiRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private DatasourceApiDbEntryRepository dsApiRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private VocabularyClient vocabularyClient;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Country> listCountries() throws DsmException {
|
|
|
|
final List<Country> countries = Lists.newArrayList();
|
|
|
|
final Vocabulary v = vocabularyClient.getCountries();
|
2022-02-07 10:09:18 +01:00
|
|
|
countries.addAll(countryTermRepository.findAll()
|
|
|
|
.stream()
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
.map(t -> new Country(t.getTerm(), v.getEnglishName(t.getTerm())))
|
|
|
|
.collect(Collectors.toList()));
|
2022-02-04 10:12:15 +01:00
|
|
|
return countries;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-02-07 10:09:18 +01:00
|
|
|
public Page<DatasourceDbEntry> search(final RequestSort requestSortBy,
|
|
|
|
final RequestSortOrder order,
|
|
|
|
final RequestFilter requestFilter,
|
|
|
|
final int page,
|
|
|
|
final int size)
|
|
|
|
throws DsmException {
|
2022-02-04 10:12:15 +01:00
|
|
|
|
|
|
|
final Specification<DatasourceDbEntry> spec = dsSpec(requestSortBy, order, requestFilter);
|
|
|
|
return dsRepository.findAll(spec, PageRequest.of(page, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-02-07 10:09:18 +01:00
|
|
|
public Page<DatasourceDbEntry> searchRegistered(final RequestSort requestSortBy,
|
|
|
|
final RequestSortOrder order,
|
|
|
|
final RequestFilter requestFilter,
|
|
|
|
final int page,
|
|
|
|
final int size)
|
|
|
|
throws DsmException {
|
2022-02-04 10:12:15 +01:00
|
|
|
|
|
|
|
final Specification<DatasourceDbEntry> spec = dsSpec(requestSortBy, order, requestFilter).and(dsRegisteredbyNotNullSpec());
|
|
|
|
return dsRepository.findAll(spec, PageRequest.of(page, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DatasourceDbEntry getDs(final String dsId) throws DsmException {
|
2022-02-07 10:09:18 +01:00
|
|
|
return dsRepository.findById(dsId).orElseThrow(() -> new DsmException("Datasource not found. ID: " + dsId));
|
2022-02-04 10:12:15 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:58:20 +01:00
|
|
|
@Override
|
|
|
|
public DatasourceDbEntry getDsByNsPrefix(final String prefix) throws DsmException {
|
|
|
|
return dsRepository.findByNamespaceprefix(prefix).orElseThrow(() -> new DsmException("Datasource not found. NS Prefix: " + prefix));
|
|
|
|
}
|
|
|
|
|
2022-02-04 10:12:15 +01:00
|
|
|
@Override
|
|
|
|
public void setManaged(final String id, final boolean managed) {
|
|
|
|
log.info(String.format("setting managed = '%s' for ds '%s'", managed, id));
|
|
|
|
dsRepository.setManaged(id, managed);
|
|
|
|
apiRepository.setRemovable(id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isManaged(final String id) {
|
|
|
|
return dsRepository.isManaged(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-02-07 10:09:18 +01:00
|
|
|
public void updateCompliance(final String dsId, final String apiId, final String compliance, final boolean override) {
|
2022-02-04 10:12:15 +01:00
|
|
|
log.info(String.format("setting compatibility = '%s' for ds '%s'", compliance, apiId));
|
|
|
|
apiRepository.updateCompatibility(apiId, compliance);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<ApiDbEntry> getApis(final String dsId) {
|
|
|
|
return apiRepository.findByDatasource(dsId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deleteApi(final String dsId, final String apiId) throws DsmForbiddenException, DsmNotFoundException {
|
2022-02-07 10:09:18 +01:00
|
|
|
final ApiDbEntry api = apiRepository.findById(apiId).orElseThrow(() -> new DsmNotFoundException("Api not found. ID: " + apiId));
|
2022-02-04 10:12:15 +01:00
|
|
|
try {
|
2022-02-07 10:09:18 +01:00
|
|
|
if (!api.getRemovable()) { throw new DsmForbiddenException(HttpStatus.SC_UNAUTHORIZED, "api is not removable"); }
|
2022-02-04 10:12:15 +01:00
|
|
|
|
|
|
|
apiRepository.deleteById(apiId);
|
|
|
|
log.info(String.format("deleted api '%s'", apiId));
|
2022-02-07 10:09:18 +01:00
|
|
|
} catch (final EntityNotFoundException e) {
|
2022-02-04 10:12:15 +01:00
|
|
|
throw new DsmNotFoundException(HttpStatus.SC_NOT_FOUND, "api not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addApi(final ApiDbEntry api) {
|
|
|
|
apiRepository.save(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean existDs(final String dsId) throws DsmException {
|
|
|
|
return dsRepository.existsById(dsId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void saveDs(final DatasourceDbEntry d) {
|
|
|
|
log.info(String.format("saving datasource '%s'", d.getId()));
|
|
|
|
|
|
|
|
final DatasourceDbEntry datasource = dsRepository.save(d);
|
|
|
|
log.info(String.format("saved datasource '%s'", datasource.getId()));
|
|
|
|
ensureRegistrationDate(d.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deleteDs(final String dsId) {
|
|
|
|
dsRepository.deleteById(dsId);
|
|
|
|
log.info(String.format("deleted datasource '%s'", dsId));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateName(final String dsId, final String officialname, final String englishname) {
|
2022-02-07 10:09:18 +01:00
|
|
|
// TODO what if one of the two names is null or empty?
|
2022-02-04 10:12:15 +01:00
|
|
|
dsRepository.setDatasourcename(dsId, officialname, englishname);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateLogoUrl(final String dsId, final String logourl) throws DsmException {
|
|
|
|
dsRepository.setLogoUrl(dsId, logourl);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateCoordinates(final String dsId, final Double latitude, final Double longitude) {
|
|
|
|
dsRepository.setCoordinates(dsId, latitude, longitude);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateApiBaseUrl(final String apiId, final String baseurl) {
|
|
|
|
apiRepository.setBaseurl(apiId, baseurl);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
public boolean upsertApiOaiSet(final String apiId, final String oaiSet) throws DsmException {
|
2022-02-07 10:09:18 +01:00
|
|
|
final ApiDbEntry api = apiRepository.findById(apiId).orElseThrow(() -> new DsmNotFoundException("Api not found. ID: " + apiId));
|
2022-02-04 10:12:15 +01:00
|
|
|
if (OAI.equalsIgnoreCase(api.getProtocol())) {
|
|
|
|
final Set<ApiParamDbEntry> apiParams = api.getApiParams();
|
|
|
|
|
|
|
|
if (!apiParams.stream().anyMatch(ap -> SET.equals(ap.getParam()))) {
|
|
|
|
apiRepository.addApiParam(apiId, SET, oaiSet);
|
|
|
|
log.info(String.format("added api '%s' oai set with '%s'", apiId, oaiSet));
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
apiRepository.updateOaiSet(apiId, oaiSet);
|
|
|
|
log.info(String.format("updated api '%s' oai set with '%s'", apiId, oaiSet));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DsmException(String.format("won't add OAI set to a non OAI interface: '%s' has protocol '%s'", apiId, api.getProtocol()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> findApiBaseURLs(final RequestFilter requestFilter, final int page, final int size) throws DsmException {
|
|
|
|
final PageRequest pageable = PageRequest.of(page, size);
|
|
|
|
final Specification<DatasourceApiDbEntry> spec = apiSpec(requestFilter);
|
2022-02-07 10:09:18 +01:00
|
|
|
final Set<String> set = dsApiRepository.findAll(spec, pageable)
|
|
|
|
.getContent()
|
|
|
|
.stream()
|
|
|
|
.map(DatasourceApiDbEntry::getBaseurl)
|
|
|
|
.filter(StringUtils::isNotBlank)
|
|
|
|
.collect(Collectors.toCollection(HashSet::new));
|
2022-02-04 10:12:15 +01:00
|
|
|
return Lists.newArrayList(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateTimezone(final String dsId, final String timezone) {
|
|
|
|
dsRepository.setTimezone(dsId, timezone);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-03-24 12:26:14 +01:00
|
|
|
public void updateEoscDatasourceType(final String dsId, final String type) throws DsmException {
|
2022-02-04 10:12:15 +01:00
|
|
|
final Vocabulary typologies = vocabularyClient.getDatasourceTypologies();
|
2022-03-24 12:26:14 +01:00
|
|
|
if (!typologies.hasCode(type)) {
|
2022-02-04 10:12:15 +01:00
|
|
|
throw new DsmException(
|
2022-02-07 10:09:18 +01:00
|
|
|
HttpStatus.SC_BAD_REQUEST,
|
2022-03-24 12:26:14 +01:00
|
|
|
String.format("invalid datasource type '%s', provide one according to vocabulary %s", type, config.getVocabularies()
|
2022-02-07 10:09:18 +01:00
|
|
|
.getDatasourceTypologiesEndpoint()));
|
2022-02-04 10:12:15 +01:00
|
|
|
}
|
2022-03-24 12:26:14 +01:00
|
|
|
dsRepository.setEoscDatasourceType(dsId, type);
|
2022-02-04 10:12:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateRegisteringUser(final String dsId, final String registeredBy) throws DsmException {
|
|
|
|
|
|
|
|
ensureRegistrationDate(dsId);
|
|
|
|
|
|
|
|
dsRepository.setRegisteringUser(dsId, registeredBy);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updatePlatform(final String dsId, final String platform) throws DsmException {
|
|
|
|
dsRepository.setPlatform(dsId, platform);
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:09:18 +01:00
|
|
|
// HELPER
|
|
|
|
private void ensureRegistrationDate(final String dsId) {
|
2022-02-04 10:12:15 +01:00
|
|
|
if (!dsRepository.hasRegistrationdate(dsId)) {
|
|
|
|
log.info("setting registration date for datasource: " + dsId);
|
|
|
|
dsRepository.setRegistrationDate(dsId, new Date(System.currentTimeMillis()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|