add external references service

This commit is contained in:
amentis 2023-10-18 18:05:39 +03:00
parent a38342e534
commit 7cba7b2346
11 changed files with 872 additions and 0 deletions

View File

@ -0,0 +1,103 @@
package eu.eudat.controllers.v2;
import eu.eudat.controllers.BaseController;
import eu.eudat.data.old.DataRepository;
import eu.eudat.data.old.Registry;
import eu.eudat.data.query.items.item.funder.FunderCriteriaRequest;
import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest;
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.logic.services.externalreferences.ExternalReferencesService;
import eu.eudat.logic.services.externalreferences.FunderService;
import eu.eudat.logic.services.externalreferences.ProjectService;
import eu.eudat.models.data.ExternalReference;
import eu.eudat.models.data.ExternalReference2;
import eu.eudat.models.data.datarepository.DataRepositoryModel;
import eu.eudat.models.data.funder.Funder;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.models.data.project.Project;
import eu.eudat.models.data.registries.RegistryModel;
import eu.eudat.models.data.security.Principal;
import eu.eudat.types.ApiMessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping(path = {"api/external-references"})
public class ExternalReferencesController extends BaseController {
private final FunderService funderService;
private final ExternalReferencesService externalReferencesService;
private final ProjectService projectService;
@Autowired
public ExternalReferencesController(
ApiContext apiContext,
FunderService funderService,
ExternalReferencesService externalReferencesService,
ProjectService projectService
) {
super(apiContext);
this.funderService = funderService;
this.externalReferencesService = externalReferencesService;
this.projectService = projectService;
}
@PostMapping(path = {"funders"}, consumes = "application/json", produces = "application/json")
public @ResponseBody ResponseEntity<ResponseItem<List<Funder>>> getWithExternal(@RequestBody FunderCriteriaRequest funderCriteria, Principal principal) throws NoURLFound, InstantiationException, HugeResultSet, IllegalAccessException {
List<Funder> dataTable = this.funderService.getCriteriaWithExternal(funderCriteria, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<Funder>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
}
@PostMapping(path = {"projects"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<List<Project>>> getWithExternal(@RequestBody ProjectCriteriaRequest projectCriteria, Principal principal) throws NoURLFound, InstantiationException, HugeResultSet, IllegalAccessException {
List<Project> dataTable = this.projectService.getCriteriaWithExternal(projectCriteria, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<Project>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
}
@GetMapping(path = {"data-repo/{externalType}"}, produces = "application/json")
public @ResponseBody ResponseEntity<ResponseItem<List<ExternalReference>>> listExternalReferecnes(@RequestParam(value = "externalType") String externalType, @RequestParam(value = "query", required = false) String query,
@RequestParam(value = "type", required = false) String type, Principal principal
) throws HugeResultSet, NoURLFound {
List<ExternalReference> externalReferences = this.externalReferencesService.getExternalReference(externalType, query, type, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<ExternalReference>>().status(ApiMessageCode.NO_MESSAGE).payload(externalReferences));
}
@Transactional
@PostMapping(path = {"data-repo/persist"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<ExternalReference>> createExternalReferecnes(@RequestBody ExternalReference externalReference, Principal principal) throws Exception {
ExternalReference newExternalReference = this.externalReferencesService.createDataRepo(externalReference, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ExternalReference>().payload(newExternalReference).status(ApiMessageCode.SUCCESS_MESSAGE));
}
@GetMapping(path = {"{externalType}"}, produces = "application/json")
public @ResponseBody ResponseEntity<ResponseItem<List<ExternalReference2>>> listExternalReferecnes2(@RequestParam(value = "externalType") String externalType, @RequestParam(value = "query", required = false) String query,
@RequestParam(value = "type", required = false) String type, Principal principal
) throws HugeResultSet, NoURLFound {
List<ExternalReference2> externalReferences = this.externalReferencesService.getExternalReference2(externalType, query, type, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<ExternalReference2>>().status(ApiMessageCode.NO_MESSAGE).payload(externalReferences));
}
@Transactional
@PostMapping(value = {"{externalType}/persist"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<ExternalReference2>> create(@RequestBody ExternalReference2 externalReference, Principal principal) throws Exception {
ExternalReference2 newExternalReference = this.externalReferencesService.create(externalReference, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ExternalReference2>().payload(newExternalReference).status(ApiMessageCode.SUCCESS_MESSAGE));
}
}

View File

@ -0,0 +1,37 @@
package eu.eudat.controllers.v2;
import eu.eudat.controllers.BaseController;
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.logic.services.ValidationService;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.models.data.security.Principal;
import eu.eudat.types.ApiMessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin
@RequestMapping(path = {"api/validation"})
public class ValidationController extends BaseController {
private ValidationService validationService;
@Autowired
public ValidationController(ApiContext apiContext, ValidationService validationService) {
super(apiContext);
this.validationService = validationService;
}
@GetMapping(path = {""}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Boolean>> validate(
@RequestParam(value = "query", required = false) String query, @RequestParam(value = "type", required = false) String type, Principal principal
) throws HugeResultSet, NoURLFound {
Boolean isValid = this.validationService.validateIdentifier(query, type, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Boolean>().payload(isValid).status(ApiMessageCode.NO_MESSAGE));
}
}

View File

@ -50,6 +50,55 @@ public class RemoteFetcher {
).clientConnector(new ReactorClientHttpConnector(HttpClient.create().followRedirect(true))).build(); ).clientConnector(new ReactorClientHttpConnector(HttpClient.create().followRedirect(true))).build();
} }
public List<Map<String, String>> get(String externalType, ExternalUrlCriteria externalUrlCriteria, String key) throws NoURLFound, HugeResultSet {
List<UrlConfiguration> urlConfigs = null;
FetchStrategy fetchStrategy = null;
switch (externalType){
case "taxonomies":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getTaxonomies().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getTaxonomies().getUrls();
fetchStrategy = configLoader.getExternalUrls().getTaxonomies().getFetchMode();
break;
case "licenses":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getLicenses().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getLicenses().getUrls();
fetchStrategy = configLoader.getExternalUrls().getLicenses().getFetchMode();
break;
case "publications":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getPublications().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getPublications().getUrls();
fetchStrategy = configLoader.getExternalUrls().getPublications().getFetchMode();
break;
case "journals":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getJournals().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getJournals().getUrls();
fetchStrategy = configLoader.getExternalUrls().getJournals().getFetchMode();
break;
case "pubRepositories":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getPubRepositories().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getPubRepositories().getUrls();
fetchStrategy = configLoader.getExternalUrls().getPubRepositories().getFetchMode();
break;
case "dataRepositories":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getRepositories().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getRepositories().getUrls();
fetchStrategy = configLoader.getExternalUrls().getRepositories().getFetchMode();
break;
case "registries":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getRegistries().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getRegistries().getUrls();
fetchStrategy = configLoader.getExternalUrls().getRegistries().getFetchMode();
break;
case "services":
urlConfigs = key != null && !key.isEmpty() ? configLoader.getExternalUrls().getServices().getUrls().stream().filter(item -> item.getKey().equals(key)).collect(Collectors.toList())
: configLoader.getExternalUrls().getServices().getUrls();
fetchStrategy = configLoader.getExternalUrls().getServices().getFetchMode();
break;
}
return getAll(urlConfigs, fetchStrategy, externalUrlCriteria);
}
@Cacheable(value = "repositories", keyGenerator = "externalUrlsKeyGenerator") @Cacheable(value = "repositories", keyGenerator = "externalUrlsKeyGenerator")
public List<Map<String, String>> getRepositories(ExternalUrlCriteria externalUrlCriteria, String key) throws NoURLFound, HugeResultSet { public List<Map<String, String>> getRepositories(ExternalUrlCriteria externalUrlCriteria, String key) throws NoURLFound, HugeResultSet {
List<UrlConfiguration> urlConfigs = List<UrlConfiguration> urlConfigs =

View File

@ -0,0 +1,30 @@
package eu.eudat.logic.services;
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.proxy.fetching.RemoteFetcher;
import eu.eudat.models.data.security.Principal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ValidationService {
private RemoteFetcher remoteFetcher;
@Autowired
public ValidationService(RemoteFetcher remoteFetcher) {
super();
this.remoteFetcher = remoteFetcher;
}
public Boolean validateIdentifier(String identifier, String type, Principal principal) throws NoURLFound, HugeResultSet {
ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(identifier);
Integer count = this.remoteFetcher.findEntries(externalUrlCriteria, type);
return principal != null && count > 0;
}
}

View File

@ -0,0 +1,10 @@
package eu.eudat.logic.services.externalreferences;
import gr.cite.tools.cache.CacheOptions;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "cache.external-reference")
public class ExternalReferencesCacheOptions extends CacheOptions {
}

View File

@ -0,0 +1,67 @@
package eu.eudat.logic.services.externalreferences;
import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
import gr.cite.tools.cache.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Locale;
@Service
public class ExternalReferencesCacheService extends CacheService<ExternalReferencesCacheService.ExternalReferencesCacheValue> {
public static class ExternalReferencesCacheValue {
public ExternalReferencesCacheValue() {}
public ExternalReferencesCacheValue(String externalType, ExternalUrlCriteria externalUrlCriteria) {
this.externalType = externalType;
this.externalUrlCriteria = externalUrlCriteria;
}
private String externalType;
private ExternalUrlCriteria externalUrlCriteria;
public String getExternalType() {
return externalType;
}
public void setExternalType(String externalType) {
this.externalType = externalType;
}
public ExternalUrlCriteria getExternalUrlCriteria() {
return externalUrlCriteria;
}
public void setExternalUrlCriteria(ExternalUrlCriteria externalUrlCriteria) {
this.externalUrlCriteria = externalUrlCriteria;
}
}
@Autowired
public ExternalReferencesCacheService(ExternalReferencesCacheOptions options) {
super(options);
}
@Override
protected Class<ExternalReferencesCacheValue> valueClass() {return ExternalReferencesCacheValue.class;}
public String keyOf(ExternalReferencesCacheValue value) {
return this.buildKey(value.getExternalType(), value.getExternalUrlCriteria());
}
public String buildKey(String externalType, ExternalUrlCriteria externalUrlCriteria) {
HashMap<String, String> keyParts = new HashMap<>();
keyParts.put("$type$", externalType.toLowerCase(Locale.ROOT));
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(externalUrlCriteria);
keyParts.put("$criteria$", stringBuffer.toString().toLowerCase(Locale.ROOT));
return this.generateKey(keyParts);
}
}

View File

@ -0,0 +1,135 @@
package eu.eudat.logic.services.externalreferences;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
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 eu.eudat.models.data.ExternalReference2;
import eu.eudat.models.data.security.Principal;
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;
public ExternalReferencesService(ApiContext apiContext) {
this.apiContext = apiContext;
}
// external references:
// taxonomies,
// licenses,
// publications,
// journals,
// pubRepositories,
// dataRepositories
public ExternalReference createDataRepo(ExternalReference externalReference, Principal principal) throws Exception {
// only dataRepositories, pubRepositories, journals
DataRepository dataRepository = externalReference.toDataModel();
dataRepository.getCreationUser().setId(principal.getId());
dataRepository = apiContext.getOperationsContext().getDatabaseRepository().getDataRepositoryDao().createOrUpdate(dataRepository);
return new ExternalReference().fromDataModel(dataRepository);
}
public List<ExternalReference> getExternalReference(String externalType, String query, String type, Principal principal) throws HugeResultSet, NoURLFound {
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(principal.getId());
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, Principal principal) 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(principal.getId());
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(principal.getId());
service = apiContext.getOperationsContext().getDatabaseRepository().getServiceDao().createOrUpdate(service);
newExternalReference = new ExternalReference2().fromDataModel(service);
}
return newExternalReference;
}
public List<ExternalReference2> getExternalReference2(String externalType, String query, String type, Principal principal) throws HugeResultSet,NoURLFound {
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(principal.getId());
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(principal.getId());
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;
}
}

View File

@ -0,0 +1,66 @@
package eu.eudat.logic.services.externalreferences;
import eu.eudat.data.old.UserInfo;
import eu.eudat.data.query.items.item.funder.FunderCriteriaRequest;
import eu.eudat.logic.builders.model.models.FunderBuilder;
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.proxy.fetching.RemoteFetcher;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.helpers.ListHelper;
import eu.eudat.models.data.external.ExternalSourcesItemModel;
import eu.eudat.models.data.external.FundersExternalSourcesModel;
import eu.eudat.models.data.funder.Funder;
import eu.eudat.models.data.security.Principal;
import eu.eudat.queryable.QueryableList;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class FunderService {
private ApiContext apiContext;
private RemoteFetcher remoteFetcher;
private ListHelper listHelper;
public FunderService(ApiContext apiContext, RemoteFetcher remoteFetcher, ListHelper listHelper) {
this.apiContext = apiContext;
this.remoteFetcher = remoteFetcher;
this.listHelper = listHelper;
}
public List<Funder> getCriteriaWithExternal(FunderCriteriaRequest funderCriteria, Principal principal) throws HugeResultSet, NoURLFound {
UserInfo userInfo = new UserInfo();
userInfo.setId(principal.getId());
funderCriteria.getCriteria().setReference("dmp:");
QueryableList<eu.eudat.data.old.Funder> items = apiContext.getOperationsContext().getDatabaseRepository().getFunderDao().getWithCritetia(funderCriteria.getCriteria());
QueryableList<eu.eudat.data.old.Funder> authItems = apiContext.getOperationsContext().getDatabaseRepository().getFunderDao().getAuthenticated(items, userInfo);
List<Funder> funders = authItems.select(item -> new Funder().fromDataModel(item));
ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(funderCriteria.getCriteria().getLike());
List<Map<String, String>> remoteRepos = remoteFetcher.getFunders(externalUrlCriteria);
FundersExternalSourcesModel fundersExternalSourcesModel = new FundersExternalSourcesModel().fromExternalItem(remoteRepos);
for (ExternalSourcesItemModel externalListingItem : fundersExternalSourcesModel) {
Funder funder = apiContext.getOperationsContext().getBuilderFactory().getBuilder(FunderBuilder.class)
.reference(externalListingItem.getRemoteId()).label(externalListingItem.getName())
.status(eu.eudat.data.old.Funder.Status.fromInteger(0))
.key(externalListingItem.getKey())
.source(externalListingItem.getTag())
.build();
if (externalListingItem.getSource() != null) {
funder.setSource(externalListingItem.getSource());
} else {
funder.setSource(externalListingItem.getTag());
}
funders.add(funder);
}
funders.sort(Comparator.comparing(Funder::getLabel));
funders = funders.stream().filter(listHelper.distinctByKey(Funder::getLabel)).collect(Collectors.toList());
return funders;
}
}

View File

@ -0,0 +1,61 @@
package eu.eudat.logic.services.externalreferences;
import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest;
import eu.eudat.logic.builders.model.models.ProjectBuilder;
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.proxy.fetching.RemoteFetcher;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.helpers.ListHelper;
import eu.eudat.models.data.external.ExternalSourcesItemModel;
import eu.eudat.models.data.external.ProjectsExternalSourcesModel;
import eu.eudat.models.data.project.Project;
import eu.eudat.models.data.security.Principal;
import eu.eudat.queryable.QueryableList;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class ProjectService {
private ApiContext apiContext;
private RemoteFetcher remoteFetcher;
private ListHelper listHelper;
public ProjectService(ApiContext apiContext, ListHelper listHelper) {
this.apiContext = apiContext;
this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher();
this.listHelper = listHelper;
}
public List<Project> getCriteriaWithExternal(ProjectCriteriaRequest projectCriteria, Principal principal) throws HugeResultSet, NoURLFound {
eu.eudat.data.old.UserInfo userInfo = new eu.eudat.data.old.UserInfo();
userInfo.setId(principal.getId());
projectCriteria.getCriteria().setReference("dmp:");
QueryableList<eu.eudat.data.old.Project> items = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getWithCritetia(projectCriteria.getCriteria());
QueryableList<eu.eudat.data.old.Project> authItems = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getAuthenticated(items, userInfo);
List<Project> projects = authItems.select(item -> new Project().fromDataModel(item));
ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(projectCriteria.getCriteria().getLike());
List<Map<String, String>> remoteRepos = remoteFetcher.getProjects(externalUrlCriteria);
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos);
for (ExternalSourcesItemModel externalListingItem : projectsExternalSourcesModel) {
Project project = apiContext.getOperationsContext().getBuilderFactory().getBuilder(ProjectBuilder.class)
.reference(externalListingItem.getRemoteId()).label(externalListingItem.getName())
.description(externalListingItem.getDescription()).uri(externalListingItem.getUri())
.abbreviation(externalListingItem.getAbbreviation()).status(eu.eudat.data.old.Project.Status.fromInteger(0))
.key(externalListingItem.getKey())
.source(externalListingItem.getTag())
.build();
projects.add(project);
}
projects.sort(Comparator.comparing(Project::getLabel));
projects = projects.stream().filter(listHelper.distinctByKey(Project::getLabel)).collect(Collectors.toList());
return projects;
}
}

View File

@ -0,0 +1,126 @@
package eu.eudat.models.data;
import eu.eudat.data.old.DataRepository;
import eu.eudat.data.old.UserInfo;
import java.util.Date;
import java.util.UUID;
public class ExternalReference {
private UUID id;
private String name;
private String pid;
private String abbreviation;
private String uri;
private Date created;
private Date modified;
private String tag; // Api fetching the data
private String source; // Actual harvested source
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getAbbreviation() {
return abbreviation;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public ExternalReference fromDataModel(DataRepository entity) {
this.setAbbreviation(entity.getAbbreviation());
this.setName(entity.getLabel());
this.setUri(entity.getUri());
this.setId(entity.getId());
this.setPid(entity.getReference());
String source1 = entity.getReference().substring(0, entity.getReference().indexOf(":"));
if (source1.equals("dmp")) {
this.source = "Internal";
} else {
this.source = source1;
}
return this;
}
public DataRepository toDataModel() throws Exception {
DataRepository dataRepository = new DataRepository();
dataRepository.setId(this.id != null ? this.id : UUID.randomUUID());
dataRepository.setAbbreviation(this.abbreviation);
dataRepository.setCreated(this.created != null ? this.created : new Date());
dataRepository.setModified(new Date());
dataRepository.setLabel(this.name);
if (this.source != null) {
if (this.source.equals("Internal") || this.source.equals(this.id.toString().substring(0, this.source.length()))) {
dataRepository.setReference(this.id.toString());
} else {
dataRepository.setReference(this.source + ":" + dataRepository.getId());
}
} else {
dataRepository.setReference("dmp:" + dataRepository.getId());
}
dataRepository.setUri(this.uri);
dataRepository.setStatus((short) 0);
dataRepository.setCreationUser(new UserInfo());
return dataRepository;
}
public String getHint() {
return null;
}
}

View File

@ -0,0 +1,188 @@
package eu.eudat.models.data;
import eu.eudat.data.old.Registry;
import eu.eudat.data.old.Service;
import eu.eudat.data.old.UserInfo;
import java.util.Date;
import java.util.UUID;
public class ExternalReference2 {
private UUID id;
private String label;
private String name;
private String pid;
private String abbreviation;
private String uri;
private Date created;
private Date modified;
private String reference;
private String tag;
private String source;
private String externalType;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getAbbreviation() {
return abbreviation;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getExternalType() {return externalType;}
public void setExternalType(String externalType) {this.externalType = externalType;}
public ExternalReference2 fromDataModel(Service entity) {
this.abbreviation = entity.getAbbreviation();
this.created = entity.getCreated();
this.id = entity.getId();
this.label = entity.getLabel();
this.name = entity.getLabel();
this.modified = entity.getModified();
this.uri = entity.getUri();
String source = entity.getReference().substring(0, entity.getReference().indexOf(":"));
if (source.equals("dmp")) {
this.source = "Internal";
} else {
this.source = source;
}
this.externalType = "services";
return this;
}
public ExternalReference2 fromDataModel(Registry entity) {
this.id = entity.getId();
this.abbreviation = entity.getAbbreviation();
this.created = entity.getCreated();
this.label = entity.getLabel();
this.name = entity.getLabel();
this.modified = entity.getModified();
this.uri = entity.getUri();
String source1 = entity.getReference().substring(0, entity.getReference().indexOf(":"));
if (source1.equals("dmp")) {
this.source = "Internal";
} else {
this.source = source1;
}
this.reference = entity.getReference();
this.externalType = "registries";
return this;
}
public Service toDataModelService() throws Exception {
Service service = new Service();
service.setId(this.id != null ? this.id : UUID.randomUUID());
service.setAbbreviation(this.abbreviation);
service.setCreated(this.created != null ? this.created : new Date());
service.setLabel(this.label != null ? this.label : this.name);
service.setModified(new Date());
service.setUri(this.uri);
if (this.source == null) this.source = "dmp";
if (this.reference == null) this.reference = service.getId().toString();
if (this.source.equals(this.reference.substring(0, this.source.length()))) {
service.setReference(this.reference);
} else {
service.setReference(this.source + ":" + this.reference);
}
service.setModified(new Date());
service.setStatus((short) 0);
service.setCreationUser(new UserInfo());
return service;
}
public Registry toDataModelRegistry() throws Exception {
Registry registry = new Registry();
registry.setAbbreviation(this.abbreviation);
registry.setCreated(this.created != null ? this.created : new Date());
registry.setId(this.id != null ? this.id : UUID.randomUUID());
registry.setLabel(this.label != null ? this.label : this.name);
registry.setUri(this.uri);
registry.setModified(new Date());
if (this.source == null) this.source = "dmp";
if (this.source.equals(registry.getId().toString().substring(0, this.source.length()))) {
registry.setReference(registry.getId().toString());
} else {
registry.setReference(this.source + ":" + registry.getId());
}
registry.setStatus((short)0);
registry.setCreationUser(new UserInfo());
return registry;
}
public String getHint() {
return null;
}
}