package eu.eudat.managers; import eu.eudat.builders.entity.ContentBuilder; import eu.eudat.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.external.ExternalSourcesItemModel; import eu.eudat.models.external.ProjectsExternalSourcesModel; import eu.eudat.models.files.ContentFile; import eu.eudat.models.helpers.common.DataTableData; import eu.eudat.models.project.Project; import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest; import eu.eudat.models.project.ProjectListingModel; import eu.eudat.data.query.items.table.project.ProjectTableRequest; import eu.eudat.models.security.Principal; import eu.eudat.proxy.config.exceptions.HugeResultSet; import eu.eudat.proxy.config.exceptions.NoURLFound; import eu.eudat.proxy.fetching.RemoteFetcher; import eu.eudat.queryable.QueryableList; import eu.eudat.services.ApiContext; import eu.eudat.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 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 items = projectRepository.getWithCriteria(projectTableRequest.getCriteria()); QueryableList authItems = projectRepository.getAuthenticated(items, userInfo); QueryableList pagedItems = PaginationManager.applyPaging(authItems, projectTableRequest); DataTableData 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.project.Project getSingle(ProjectDao projectRepository, String id) throws InstantiationException, IllegalAccessException { eu.eudat.models.project.Project project = new eu.eudat.models.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 getCriteriaWithExternal(ApiContext apiContext, ProjectCriteriaRequest projectCriteria, RemoteFetcher remoteFetcher) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound { QueryableList items = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getWithCriteria(projectCriteria.getCriteria()); List projects = items.select(item -> new Project().fromDataModel(item)); List> remoteRepos = remoteFetcher.getProjects(projectCriteria.getCriteria().getLike()); ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos); for (ExternalSourcesItemModel externalListingItem : projectsExternalSourcesModel) { eu.eudat.models.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 getCriteria(ProjectDao projectRepository, ProjectCriteriaRequest projectCriteria, RemoteFetcher remoteFetcher) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound { QueryableList items = projectRepository.getWithCriteria(projectCriteria.getCriteria()); List 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.project.Project project, Principal principal) throws ParseException, IOException { eu.eudat.data.entities.Project projectEntity = project.toDataModel(); 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); } }