package eu.eudat.managers; import eu.eudat.builders.model.models.RecentActivityDataBuilder; import eu.eudat.dao.entities.DMPDao; import eu.eudat.dao.entities.DatasetDao; import eu.eudat.dao.entities.ProjectDao; import eu.eudat.entities.UserInfo; import eu.eudat.models.criteria.DataManagementPlanCriteria; import eu.eudat.models.criteria.DatasetCriteria; 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()); List activities = new LinkedList<>(); 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()).type(RecentActivityData.RecentActivityType.DMP.getValue()).build()) .whenComplete((dmpActivities, throwable) -> activities.addAll(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()).type(RecentActivityData.RecentActivityType.DATASET.getValue()).build()) .whenComplete((datasetActivities, throwable) -> activities.addAll(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()).type(RecentActivityData.RecentActivityType.PROJECT.getValue()).build()) .whenComplete((projectActivities, throwable) -> activities.addAll(projectActivities)); CompletableFuture.allOf(projects, dmps, datasets).join(); Collections.sort(activities, Comparator.comparing(RecentActivityData::getTimestamp).reversed()); activity.setRecentActivities(activities.subList(0, numberofactivities)); return activity; } }