argos/dmp-backend/web/src/main/java/eu/eudat/logic/managers/ProjectManager.java

128 lines
7.7 KiB
Java

package eu.eudat.logic.managers;
import eu.eudat.logic.builders.entity.ContentBuilder;
import eu.eudat.logic.builders.model.models.ProjectBuilder;
import eu.eudat.data.dao.entities.ContentDao;
import eu.eudat.data.dao.entities.ProjectDao;
import eu.eudat.data.dao.entities.UserInfoDao;
import eu.eudat.data.entities.Content;
import eu.eudat.data.entities.DMP;
import eu.eudat.exceptions.files.TempFileNotFoundException;
import eu.eudat.models.HintedModelFactory;
import eu.eudat.models.data.external.ExternalSourcesItemModel;
import eu.eudat.models.data.external.ProjectsExternalSourcesModel;
import eu.eudat.models.data.files.ContentFile;
import eu.eudat.models.data.helpers.common.DataTableData;
import eu.eudat.models.data.project.Project;
import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest;
import eu.eudat.models.data.project.ProjectListingModel;
import eu.eudat.data.query.items.table.project.ProjectTableRequest;
import eu.eudat.models.data.security.Principal;
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.queryable.QueryableList;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.helpers.FileStorageService;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class ProjectManager {
public DataTableData<eu.eudat.models.data.project.ProjectListingModel> getPaged(ProjectDao projectRepository, ProjectTableRequest projectTableRequest, Principal principal) throws Exception {
eu.eudat.data.entities.UserInfo userInfo = new eu.eudat.data.entities.UserInfo();
userInfo.setId(principal.getId());
QueryableList<eu.eudat.data.entities.Project> items = projectRepository.getWithCriteria(projectTableRequest.getCriteria());
QueryableList<eu.eudat.data.entities.Project> authItems = projectRepository.getAuthenticated(items, userInfo);
QueryableList<eu.eudat.data.entities.Project> pagedItems = PaginationManager.applyPaging(authItems, projectTableRequest);
DataTableData<eu.eudat.models.data.project.ProjectListingModel> dataTable = new DataTableData<>();
CompletableFuture projectsFuture = pagedItems.withHint(HintedModelFactory.getHint(ProjectListingModel.class)).selectAsync(item -> {
item.setDmps(item.getDmps().stream().filter(
dmp -> dmp.getCreator().getId().equals(principal.getId()) || dmp.getUsers().stream().filter(user -> user.getId().equals(principal.getId())).collect(Collectors.toList()).size() > 0)
.collect(Collectors.groupingBy(DMP::getGroupId))
.values().stream()
.map(dmps -> dmps.stream().reduce((first, second) -> {
if (first.getVersion() > second.getVersion()) return first;
else return second;
}).get())
.collect(Collectors.toSet()));
return new ProjectListingModel().fromDataModel(item);
}).whenComplete((results, throwable) -> {
dataTable.setData(results);
});
CompletableFuture countFuture = pagedItems.countAsync().whenComplete((count, throwable) -> dataTable.setTotalCount(count));
CompletableFuture.allOf(projectsFuture, countFuture).join();
return dataTable;
}
public eu.eudat.models.data.project.Project getSingle(ProjectDao projectRepository, String id) throws InstantiationException, IllegalAccessException {
eu.eudat.models.data.project.Project project = new eu.eudat.models.data.project.Project();
project.fromDataModel(projectRepository.find(UUID.fromString(id)));
return project;
}
public eu.eudat.data.entities.Project inactivate(ProjectDao projectRepository, String id) throws InstantiationException, IllegalAccessException {
eu.eudat.data.entities.Project project = projectRepository.find(UUID.fromString(id));
project.setStatus(eu.eudat.data.entities.Project.Status.DELETED.getValue());
project = projectRepository.createOrUpdate(project);
return project;
}
public List<eu.eudat.models.data.project.Project> getCriteriaWithExternal(ApiContext apiContext, ProjectCriteriaRequest projectCriteria, RemoteFetcher remoteFetcher) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
QueryableList<eu.eudat.data.entities.Project> items = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getWithCriteria(projectCriteria.getCriteria());
List<eu.eudat.models.data.project.Project> projects = items.select(item -> new Project().fromDataModel(item));
List<Map<String, String>> remoteRepos = remoteFetcher.getProjects(projectCriteria.getCriteria().getLike());
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.entities.Project.Status.fromInteger(0))
.build();
projects.add(project);
}
return projects;
}
public List<eu.eudat.models.data.project.Project> getCriteria(ProjectDao projectRepository, ProjectCriteriaRequest projectCriteria, RemoteFetcher remoteFetcher) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
QueryableList<eu.eudat.data.entities.Project> items = projectRepository.getWithCriteria(projectCriteria.getCriteria());
if(projectCriteria.getLength() != null) items.take(projectCriteria.getLength());
List<eu.eudat.models.data.project.Project> projects = items.select(item -> new Project().fromDataModel(item));
return projects;
}
public static void createOrUpdate(FileStorageService fileStorageService, ProjectDao projectRepository, ContentDao contentRepository, UserInfoDao userInfoRepository, eu.eudat.models.data.project.Project project, Principal principal) throws ParseException, IOException {
eu.eudat.data.entities.Project projectEntity = project.toDataModel();
if(project.getFiles() != null) {
for (ContentFile file : project.getFiles()) {
try {
ContentFile storedFile = fileStorageService.copyFromTempFileSystem(file);
Content content = new ContentBuilder().extension(file.getType())
.label(file.getFilename())
.locationType(Content.LocationType.INTERNAL.getValue())
.parentType(Content.ParentType.PROJECT.getValue())
.uri("LOCAL:" + storedFile.getId())
.build();
projectEntity.setContent(contentRepository.createOrUpdate(content));
} catch (TempFileNotFoundException e) {
continue;
}
}
}
projectEntity.setType(eu.eudat.data.entities.Project.ProjectType.INTERNAL.getValue());
projectEntity.setCreationUser(userInfoRepository.find(principal.getId()));
projectRepository.createOrUpdate(projectEntity);
}
}