package eu.eudat.logic.managers; import eu.eudat.logic.builders.model.models.ResearcherBuilder; import eu.eudat.data.entities.Researcher; import eu.eudat.models.data.external.ExternalSourcesItemModel; import eu.eudat.models.data.external.ResearchersExternalSourcesModel; import eu.eudat.data.query.items.item.researcher.ResearcherCriteriaRequest; 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 eu.eudat.queryable.QueryableList; import eu.eudat.logic.services.ApiContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; /** * Created by ikalyvas on 2/5/2018. */ @Component public class ResearcherManager { private ApiContext apiContext; private RemoteFetcher remoteFetcher; @Autowired public ResearcherManager(ApiContext apiContext) { this.apiContext = apiContext; this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher(); } public Researcher create(eu.eudat.models.data.researcher.Researcher researcher, Principal principal) throws Exception { Researcher researcherEntity = researcher.toDataModel(); researcherEntity.setCreationUser(apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId())); return apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao().createOrUpdate(researcherEntity); } public List getCriteriaWithExternal(ResearcherCriteriaRequest researcherCriteriaRequest, Principal principal) throws HugeResultSet, NoURLFound { QueryableList items = apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao().getWithCriteria(researcherCriteriaRequest.getCriteria()); items.where((builder, root) -> builder.equal(root.get("creationUser").get("id"), principal.getId())); List researchers = items.select(item -> new eu.eudat.models.data.dmp.Researcher().fromDataModel(item)); List> remoteRepos = remoteFetcher.getResearchers(researcherCriteriaRequest.getCriteria().getName(),null); ResearchersExternalSourcesModel researchersExternalSourcesModel = new ResearchersExternalSourcesModel().fromExternalItem(remoteRepos); for (ExternalSourcesItemModel externalListingItem : researchersExternalSourcesModel) { eu.eudat.models.data.dmp.Researcher researcher = apiContext.getOperationsContext().getBuilderFactory().getBuilder(ResearcherBuilder.class) .label(externalListingItem.getAbbreviation()) .id(externalListingItem.getId()) .name(externalListingItem.getName()) .tag(externalListingItem.getTag()) .build(); researchers.add(researcher); } return researchers.stream().distinct().collect(Collectors.toList()); } }