196 lines
10 KiB
Java
196 lines
10 KiB
Java
package eu.eudat.logic.services.externalreferences;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import eu.eudat.commons.enums.ExternalReferencesType;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.dao.criteria.DataRepositoryCriteria;
|
|
import eu.eudat.data.dao.criteria.RegistryCriteria;
|
|
import eu.eudat.data.dao.criteria.ServiceCriteria;
|
|
|
|
import eu.eudat.data.old.DataRepository;
|
|
import eu.eudat.data.old.Registry;
|
|
import eu.eudat.data.old.Service;
|
|
import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
|
|
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet;
|
|
import eu.eudat.logic.proxy.config.exceptions.NoURLFound;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.models.data.ExternalReference;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.util.Comparator;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@org.springframework.stereotype.Service
|
|
public class ExternalReferencesService {//implements ExternalReferencesService{
|
|
|
|
private final ApiContext apiContext;
|
|
private final UserScope userScope;
|
|
|
|
public ExternalReferencesService(ApiContext apiContext, UserScope userScope) {
|
|
this.apiContext = apiContext;
|
|
this.userScope = userScope;
|
|
}
|
|
|
|
// external references:
|
|
// taxonomies,
|
|
// licenses,
|
|
// publications,
|
|
// journals,
|
|
// pubRepositories,
|
|
// dataRepositories
|
|
|
|
// public ExternalReference createDataRepo(ExternalReference externalReference) throws Exception {
|
|
// // only dataRepositories, pubRepositories, journals
|
|
// DataRepository dataRepository = externalReference.toDataModel();
|
|
// dataRepository.getCreationUser().setId(this.userScope.getUserId());
|
|
//
|
|
// dataRepository = apiContext.getOperationsContext().getDatabaseRepository().getDataRepositoryDao().createOrUpdate(dataRepository);
|
|
// return new ExternalReference().fromDataModel(dataRepository);
|
|
// }
|
|
|
|
// public List<ExternalReference> getExternalReference(String externalType, String query, String type) throws HugeResultSet, NoURLFound, InvalidApplicationException {
|
|
// ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(query);
|
|
// List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().get(externalType, externalUrlCriteria, type);
|
|
//
|
|
// DataRepositoryCriteria criteria = new DataRepositoryCriteria();
|
|
// if (!query.isEmpty()) criteria.setLike(query);
|
|
//
|
|
// List<ExternalReference> list = new LinkedList<>();
|
|
// if((externalType.equals("dataRepositories") || externalType.equals("pubRepositories") || externalType.equals("journals"))){
|
|
// criteria.setCreationUserId(this.userScope.getUserId());
|
|
// if (type.equals("")) {
|
|
// List<DataRepository> dataRepositoryList = (this.apiContext.getOperationsContext().getDatabaseRepository().getDataRepositoryDao().getWithCriteria(criteria)).toList();
|
|
// list = dataRepositoryList.stream().map(item -> new ExternalReference().fromDataModel(item)).collect(Collectors.toList());
|
|
// }
|
|
// }
|
|
//
|
|
// ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
// list.addAll(remoteRepos.stream().map(item -> mapper.convertValue(item, ExternalReference.class)).collect(Collectors.toList()));
|
|
// list = list.stream().filter(x -> x.getName().toLowerCase().contains(query.toLowerCase())).collect(Collectors.toList());
|
|
// return list;
|
|
// }
|
|
|
|
|
|
|
|
// external references2:
|
|
// registries,
|
|
// services
|
|
|
|
// public ExternalReference2 create(ExternalReference2 externalReference) throws Exception {
|
|
// if (externalReference.getLabel() == null || externalReference.getAbbreviation() == null || externalReference.getUri() == null) {
|
|
// throw new Exception("Missing mandatory entity.");
|
|
// }
|
|
//
|
|
// ExternalReference2 newExternalReference = null;
|
|
// if(externalReference.getExternalType().equals("registries")){
|
|
// Registry registry = externalReference.toDataModelRegistry();
|
|
// registry.getCreationUser().setId(this.userScope.getUserId());
|
|
// registry = apiContext.getOperationsContext().getDatabaseRepository().getRegistryDao().createOrUpdate(registry);
|
|
// newExternalReference = new ExternalReference2().fromDataModel(registry);
|
|
// } else if (externalReference.getExternalType().equals("services")) {
|
|
// Service service = externalReference.toDataModelService();
|
|
// service.getCreationUser().setId(this.userScope.getUserId());
|
|
// service = apiContext.getOperationsContext().getDatabaseRepository().getServiceDao().createOrUpdate(service);
|
|
// newExternalReference = new ExternalReference2().fromDataModel(service);
|
|
// }
|
|
//
|
|
// return newExternalReference;
|
|
// }
|
|
//
|
|
//
|
|
// public List<ExternalReference2> getExternalReference2(String externalType, String query, String type) throws HugeResultSet, NoURLFound, InvalidApplicationException {
|
|
// ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(query);
|
|
// List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().get(externalType,externalUrlCriteria, type);
|
|
//
|
|
// List<ExternalReference2> list = new LinkedList<>();
|
|
//
|
|
// if (externalType.equals("registries")){
|
|
// RegistryCriteria criteria = new RegistryCriteria();
|
|
//
|
|
// if (!query.isEmpty()) criteria.setLike(query);
|
|
// criteria.setCreationUserId(this.userScope.getUserId());
|
|
//
|
|
// if (type.equals("")) {
|
|
// List<Registry> registryList = (this.apiContext.getOperationsContext().getDatabaseRepository().getRegistryDao().getWithCriteria(criteria)).toList();
|
|
// list = registryList.stream().map(item -> new ExternalReference2().fromDataModel(item)).collect(Collectors.toList());
|
|
// }
|
|
// } else if (externalType.equals("services")) {
|
|
// ServiceCriteria criteria = new ServiceCriteria();
|
|
//
|
|
// if (!query.isEmpty()) criteria.setLike(query);
|
|
// criteria.setCreationUserId(this.userScope.getUserId());
|
|
//
|
|
// if (type.equals("")) {
|
|
// List<Service> serviceList = (this.apiContext.getOperationsContext().getDatabaseRepository().getServiceDao().getWithCriteria(criteria)).toList();
|
|
// list = serviceList.stream().map(item -> new ExternalReference2().fromDataModel(item)).collect(Collectors.toList());
|
|
// }
|
|
// }
|
|
//
|
|
// ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
// list.addAll(remoteRepos.stream().map(item -> mapper.convertValue(item, ExternalReference2.class)).collect(Collectors.toList()));
|
|
//
|
|
// return list;
|
|
// }
|
|
|
|
public List<ExternalReference> getExternalReference(ExternalReferencesType externalType, String query, String type) throws HugeResultSet, NoURLFound, InvalidApplicationException {
|
|
ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(query);
|
|
List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().get(externalType, externalUrlCriteria, type);
|
|
|
|
List<ExternalReference> list = this.fetchFromDb(externalType, query, type);
|
|
|
|
list.addAll(remoteRepos.stream().map(ExternalReference::fromRemoteModel).toList());
|
|
list = list.stream().filter(x -> x.getName().toLowerCase().contains(query.toLowerCase())).collect(Collectors.toList());
|
|
list.sort(Comparator.comparing(ExternalReference::getName));
|
|
return list;
|
|
}
|
|
|
|
private List<ExternalReference> fetchFromDb(ExternalReferencesType externalType, String query, String type) throws InvalidApplicationException {
|
|
List<ExternalReference> list = new LinkedList<>();
|
|
switch (externalType) {
|
|
case DataRepositories:
|
|
case PubRepositories:
|
|
case Journals: {
|
|
DataRepositoryCriteria criteria = new DataRepositoryCriteria();
|
|
if (!query.isEmpty()) criteria.setLike(query);
|
|
criteria.setCreationUserId(this.userScope.getUserId());
|
|
if (type.equals("")) {
|
|
List<DataRepository> dataRepositoryList = (this.apiContext.getOperationsContext().getDatabaseRepository().getDataRepositoryDao().getWithCriteria(criteria)).toList();
|
|
list = dataRepositoryList.stream().map(item -> new ExternalReference().fromDataRepository(item)).collect(Collectors.toList());
|
|
}
|
|
}
|
|
case Registries: {
|
|
RegistryCriteria criteria = new RegistryCriteria();
|
|
if (!query.isEmpty()) criteria.setLike(query);
|
|
criteria.setCreationUserId(this.userScope.getUserId());
|
|
if (type.equals("")) {
|
|
List<Registry> registryList = (this.apiContext.getOperationsContext().getDatabaseRepository().getRegistryDao().getWithCriteria(criteria)).toList();
|
|
list = registryList.stream().map(item -> new ExternalReference().fromRegistry(item)).collect(Collectors.toList());
|
|
}
|
|
}
|
|
case Services:
|
|
{
|
|
ServiceCriteria criteria = new ServiceCriteria();
|
|
|
|
if (!query.isEmpty()) criteria.setLike(query);
|
|
criteria.setCreationUserId(this.userScope.getUserId());
|
|
|
|
if (type.equals("")) {
|
|
List<Service> serviceList = (this.apiContext.getOperationsContext().getDatabaseRepository().getServiceDao().getWithCriteria(criteria)).toList();
|
|
list = serviceList.stream().map(item -> new ExternalReference().fromService(item)).collect(Collectors.toList());
|
|
}
|
|
}
|
|
case Taxonomies:
|
|
case Publications:
|
|
case Licenses:
|
|
break;
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
}
|