package eu.eudat.logic.managers; import eu.eudat.data.dao.criteria.DataManagementPlanCriteria; import eu.eudat.data.dao.criteria.DatasetCriteria; import eu.eudat.data.dao.criteria.OrganisationCriteria; import eu.eudat.data.dao.criteria.GrantCriteria; import eu.eudat.data.dao.entities.DMPDao; import eu.eudat.data.dao.entities.DatasetDao; import eu.eudat.data.dao.entities.OrganisationDao; import eu.eudat.data.dao.entities.GrantDao; import eu.eudat.data.entities.UserInfo; import eu.eudat.logic.builders.model.models.RecentActivityDataBuilder; import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.operations.DatabaseRepository; import eu.eudat.models.data.dashboard.recent.RecentActivity; import eu.eudat.models.data.dashboard.recent.RecentActivityData; import eu.eudat.models.data.dashboard.searchbar.SearchBarItem; import eu.eudat.models.data.dashboard.statistics.DashBoardStatistics; import eu.eudat.models.data.security.Principal; import eu.eudat.types.searchbar.SearchBarItemType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.LinkedList; import java.util.List; import java.util.concurrent.CompletableFuture; @Component public class DashBoardManager { private ApiContext apiContext; private DatabaseRepository databaseRepository; @Autowired public DashBoardManager(ApiContext apiContext) { this.apiContext = apiContext; this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); } public DashBoardStatistics getStatistics() { DashBoardStatistics statistics = new DashBoardStatistics(); DatasetCriteria datasetCriteria = new DatasetCriteria(); DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria(); OrganisationCriteria organisationCriteria = new OrganisationCriteria(); datasetCriteria.setAllVersions(false); dataManagementPlanCriteria.setAllVersions(false); organisationCriteria.setPublic(false); CompletableFuture dmpFuture = databaseRepository.getDmpDao().getWithCriteria(dataManagementPlanCriteria).countAsync() .whenComplete((dmpsStats, throwable) -> statistics.setTotalDataManagementPlanCount(dmpsStats)); CompletableFuture datasetFuture = databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).countAsync() .whenComplete((datasetsStats, throwable) -> statistics.setTotalDataSetCount(datasetsStats)); CompletableFuture grantFuture = databaseRepository.getGrantDao().asQueryable().countAsync() .whenComplete((grantsStats, throwable) -> statistics.setTotalGrantCount(grantsStats)); CompletableFuture organisationFuture = databaseRepository.getOrganisationDao().getWithCriteria(organisationCriteria).countAsync() .whenComplete((organisationStats, throwable) -> statistics.setTotalOrganisationCount(organisationStats)); CompletableFuture.allOf(dmpFuture, datasetFuture, grantFuture, organisationFuture).join(); return statistics; } public DashBoardStatistics getMeStatistics(Principal principal) { DashBoardStatistics statistics = new DashBoardStatistics(); DMPDao dataManagementPlanRepository = databaseRepository.getDmpDao(); DatasetDao datasetRepository = databaseRepository.getDatasetDao(); GrantDao grantRepository = databaseRepository.getGrantDao(); OrganisationDao organisationRepository = databaseRepository.getOrganisationDao(); UserInfo user = new UserInfo(); user.setId(principal.getId()); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setAllVersions(false); DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria(); dataManagementPlanCriteria.setAllVersions(false); GrantCriteria grantCriteria = new GrantCriteria(); OrganisationCriteria organisationCriteria = new OrganisationCriteria(); CompletableFuture dmpFuture = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.getWithCriteria(dataManagementPlanCriteria), principal.getId()).countAsync() .whenComplete((dmpsStats, throwable) -> statistics.setTotalDataManagementPlanCount(dmpsStats)); CompletableFuture datasetFuture = datasetRepository.getAuthenticated(datasetRepository.getWithCriteria(datasetCriteria), user).countAsync() .whenComplete((datasetsStats, throwable) -> statistics.setTotalDataSetCount(datasetsStats)); CompletableFuture grantFuture = grantRepository.getAuthenticated(grantRepository.getWithCriteria(grantCriteria), user).countAsync() .whenComplete((grantsStats, throwable) -> statistics.setTotalGrantCount(grantsStats)); CompletableFuture orgnanisationFuture = organisationRepository.getAuthenticated(organisationRepository.asQueryable().withHint("organisationRecentActivity"), user).countAsync() .whenComplete((organisationStats, throwable) -> statistics.setTotalOrganisationCount(organisationStats)); CompletableFuture.allOf(dmpFuture, datasetFuture, grantFuture, orgnanisationFuture).join(); return statistics; } public RecentActivity getRecentActivity(Principal principal, Integer numberofactivities) { RecentActivity activity = new RecentActivity(); DMPDao dataManagementPlanRepository = databaseRepository.getDmpDao(); DatasetDao datasetRepository = databaseRepository.getDatasetDao(); GrantDao grantRepository = databaseRepository.getGrantDao(); UserInfo user = new UserInfo(); user.setId(principal.getId()); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setAllVersions(false); DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria(); dataManagementPlanCriteria.setAllVersions(false); GrantCriteria grantCriteria = new GrantCriteria(); RecentActivityDataBuilder recentActivityDataBuilder = apiContext.getOperationsContext().getBuilderFactory().getBuilder(RecentActivityDataBuilder.class); CompletableFuture> dmps = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.getWithCriteria(dataManagementPlanCriteria), principal.getId()) .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.getWithCriteria(datasetCriteria), 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> grants = grantRepository.getAuthenticated(grantRepository.getWithCriteria(grantCriteria), user) .withHint("grantRecentActivity") .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((grantActivities, throwable) -> activity.setRecentGrantActivities(grantActivities)); CompletableFuture.allOf(grants, dmps, datasets).join(); return activity; } public List searchUserData(String like, Principal principal) { RecentActivity activity = new RecentActivity(); UserInfo user = new UserInfo(); user.setId(principal.getId()); DMPDao dataManagementPlanRepository = databaseRepository.getDmpDao(); DatasetDao datasetRepository = databaseRepository.getDatasetDao(); GrantDao grantRepository = databaseRepository.getGrantDao(); List searchBarItems = new LinkedList<>(); CompletableFuture> dmps = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.asQueryable(), principal.getId()) .withHint("dmpRecentActivity") .where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%")) .orderBy((builder, root) -> builder.desc(root.get("modified"))) .selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.DMP.getValue())) .whenComplete((dmpItems, throwable) -> searchBarItems.addAll(dmpItems)); CompletableFuture> datasets = datasetRepository.getAuthenticated(datasetRepository.asQueryable(), user) .withHint("datasetRecentActivity") .where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%")) .orderBy((builder, root) -> builder.desc(root.get("modified"))) .selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.DATASET.getValue())) .whenComplete((dataSetItems, throwable) -> searchBarItems.addAll(dataSetItems)); CompletableFuture> grants = grantRepository.getAuthenticated(grantRepository.asQueryable(), user) .withHint("grantRecentActivity") .where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%")) .orderBy((builder, root) -> builder.desc(root.get("modified"))) .selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.GRANT.getValue())) .whenComplete((grantItems, throwable) -> searchBarItems.addAll(grantItems)); CompletableFuture.allOf(grants, dmps, datasets).join(); return searchBarItems; } }