package eu.eudat.managers; import eu.eudat.builders.model.models.RecentActivityDataBuilder; import eu.eudat.data.dao.entities.DMPDao; import eu.eudat.data.dao.entities.DatasetDao; import eu.eudat.data.dao.entities.ProjectDao; import eu.eudat.data.entities.DMP; import eu.eudat.data.entities.Dataset; import eu.eudat.data.entities.UserInfo; import eu.eudat.data.dao.criteria.DataManagementPlanCriteria; import eu.eudat.data.dao.criteria.DatasetCriteria; import eu.eudat.data.query.definition.Query; import eu.eudat.data.query.items.table.dataset.DatasetTableRequest; import eu.eudat.models.dashboard.recent.RecentActivity; import eu.eudat.models.dashboard.recent.RecentActivityData; import eu.eudat.models.dashboard.statistics.DashBoardStatistics; import eu.eudat.models.security.Principal; import eu.eudat.services.ApiContext; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.concurrent.CompletableFuture; public class DashBoardManager { public DashBoardStatistics getStatistics(DatasetDao datasetRepository, DMPDao dataManagementPlanRepository, ProjectDao projectRepository) { DashBoardStatistics statistics = new DashBoardStatistics(); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setAllVersions(false); DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria(); dataManagementPlanCriteria.setAllVersions(false); CompletableFuture dmpFuture = dataManagementPlanRepository.getWithCriteria(dataManagementPlanCriteria).countAsync() .whenComplete((dmpsStats, throwable) -> statistics.setTotalDataManagementPlanCount(dmpsStats)); CompletableFuture datasetFuture = datasetRepository.getWithCriteria(datasetCriteria).countAsync() .whenComplete((datasetsStats, throwable) -> statistics.setTotalDataSetCount(datasetsStats)); CompletableFuture projectFuture = projectRepository.asQueryable().countAsync() .whenComplete((projectsStats, throwable) -> statistics.setTotalProjectCount(projectsStats)); CompletableFuture.allOf(dmpFuture, datasetFuture, projectFuture).join(); return statistics; } public DashBoardStatistics getMeStatistics(DatasetDao datasetRepository, DMPDao dataManagementPlanRepository, ProjectDao projectRepository, Principal principal) { DashBoardStatistics statistics = new DashBoardStatistics(); UserInfo user = new UserInfo(); user.setId(principal.getId()); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setAllVersions(false); DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria(); dataManagementPlanCriteria.setAllVersions(false); CompletableFuture dmpFuture = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.getWithCriteria(dataManagementPlanCriteria), user).countAsync() .whenComplete((dmpsStats, throwable) -> statistics.setTotalDataManagementPlanCount(dmpsStats)); CompletableFuture datasetFuture = datasetRepository.getAuthenticated(datasetRepository.getWithCriteria(datasetCriteria), user).countAsync() .whenComplete((datasetsStats, throwable) -> statistics.setTotalDataSetCount(datasetsStats)); CompletableFuture projectFuture = projectRepository.getAuthenticated(projectRepository.asQueryable(), user).countAsync() .whenComplete((projectsStats, throwable) -> statistics.setTotalProjectCount(projectsStats)); CompletableFuture.allOf(dmpFuture, datasetFuture, projectFuture).join(); return statistics; } public RecentActivity getRecentActivity(ApiContext apiContext, DatasetDao datasetRepository, DMPDao dataManagementPlanRepository, ProjectDao projectRepository, Principal principal, Integer numberofactivities) { RecentActivity activity = new RecentActivity(); UserInfo user = new UserInfo(); user.setId(principal.getId()); RecentActivityDataBuilder recentActivityDataBuilder = apiContext.getOperationsContext().getBuilderFactory().getBuilder(RecentActivityDataBuilder.class); CompletableFuture> dmps = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.asQueryable(), user) .withHint("dmpRecentActivity") .orderBy((builder, root) -> builder.desc(root.get("modified"))) .take(numberofactivities) .selectAsync(item -> recentActivityDataBuilder.label(item.getLabel()).timestamp(item.getModified()).id(item.getId().toString()).build()) .whenComplete((dmpActivities, throwable) -> activity.setRecentDmpActivities(dmpActivities)); CompletableFuture> datasets = datasetRepository.getAuthenticated(datasetRepository.asQueryable(), user) .withHint("datasetRecentActivity") .orderBy((builder, root) -> builder.desc(root.get("modified"))) .take(numberofactivities) .selectAsync(item -> recentActivityDataBuilder.label(item.getLabel()).timestamp(item.getModified()).id(item.getId().toString()).build()) .whenComplete((datasetActivities, throwable) -> activity.setRecentDatasetActivities(datasetActivities)); CompletableFuture> projects = projectRepository.getAuthenticated(projectRepository.asQueryable(), user) .withHint("projectRecentActivity") .orderBy((builder, root) -> builder.desc(root.get("modified"))) .take(numberofactivities) .selectAsync(item -> recentActivityDataBuilder.label(item.getLabel()).timestamp(item.getModified()).id(item.getId().toString()).build()) .whenComplete((projectActivities, throwable) -> activity.setRecentProjectActivities(projectActivities)); CompletableFuture.allOf(projects, dmps, datasets).join(); return activity; } }