67 lines
3.4 KiB
Java
67 lines
3.4 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.commons.enums.ReferenceType;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.UserEntity;
|
|
import eu.eudat.service.reference.external.criteria.ExternalReferenceCriteria;
|
|
import eu.eudat.logic.utilities.helpers.ListHelper;
|
|
import eu.eudat.models.data.external.ProjectsExternalSourcesModel;
|
|
import eu.eudat.models.data.project.Project;
|
|
import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest;
|
|
import eu.eudat.logic.builders.model.models.ProjectBuilder;
|
|
import eu.eudat.commons.exceptions.HugeResultSetException;
|
|
import eu.eudat.service.reference.external.RemoteFetcher;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.models.data.external.ExternalSourcesItemModel;
|
|
import eu.eudat.queryable.QueryableList;
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class ProjectManager {
|
|
|
|
private ApiContext apiContext;
|
|
private RemoteFetcher remoteFetcher;
|
|
private ListHelper listHelper;
|
|
private final UserScope userScope;
|
|
|
|
public ProjectManager(ApiContext apiContext, ListHelper listHelper, UserScope userScope) {
|
|
this.apiContext = apiContext;
|
|
this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher();
|
|
this.listHelper = listHelper;
|
|
this.userScope = userScope;
|
|
}
|
|
|
|
public List<Project> getCriteriaWithExternal(ProjectCriteriaRequest projectCriteria) throws HugeResultSetException, MyNotFoundException, InvalidApplicationException {
|
|
UserEntity userInfo = new UserEntity();
|
|
userInfo.setId(this.userScope.getUserId());
|
|
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));
|
|
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria(projectCriteria.getCriteria().getLike());
|
|
List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().getReferences(ReferenceType.DataRepositories, externalReferenceCriteria, null);
|
|
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos);
|
|
for (ExternalSourcesItemModel externalListingItem : projectsExternalSourcesModel) {
|
|
eu.eudat.models.data.project.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;
|
|
}
|
|
}
|