You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
16 KiB
Java
260 lines
16 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.data.dao.criteria.*;
|
|
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.*;
|
|
import eu.eudat.data.query.definition.helpers.ColumnOrderings;
|
|
import eu.eudat.data.query.items.table.dataset.DatasetTableRequest;
|
|
import eu.eudat.data.query.items.table.dmp.DataManagementPlanTableRequest;
|
|
import eu.eudat.elastic.entities.Dmp;
|
|
import eu.eudat.logic.builders.model.models.RecentActivityDataBuilder;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.logic.services.operations.DatabaseRepository;
|
|
import eu.eudat.logic.utilities.helpers.StreamDistinctBy;
|
|
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.helpers.common.DataTableData;
|
|
import eu.eudat.models.data.listingmodels.DataManagementPlanListingModel;
|
|
import eu.eudat.models.data.listingmodels.DatasetListingModel;
|
|
import eu.eudat.models.data.security.Principal;
|
|
import eu.eudat.types.searchbar.SearchBarItemType;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class DashBoardManager {
|
|
private static final Logger logger = LoggerFactory.getLogger(DashBoardManager.class);
|
|
|
|
private ApiContext apiContext;
|
|
private DatabaseRepository databaseRepository;
|
|
private DataManagementPlanManager dmpManager;
|
|
private DatasetManager datasetManager;
|
|
|
|
@Autowired
|
|
public DashBoardManager(ApiContext apiContext, DataManagementPlanManager dmpManager, DatasetManager datasetManager) {
|
|
this.apiContext = apiContext;
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
this.dmpManager = dmpManager;
|
|
this.datasetManager = datasetManager;
|
|
}
|
|
|
|
public DashBoardStatistics getStatistics() throws Exception {
|
|
DashBoardStatistics statistics = new DashBoardStatistics();
|
|
AtomicReference<DataTableData<DataManagementPlanListingModel>> dmpData = new AtomicReference<>(new DataTableData<>());
|
|
AtomicReference<DataTableData<DatasetListingModel>> datasetData = new AtomicReference<>(new DataTableData<>());
|
|
LinkedList<Grant> grants = new LinkedList<>();
|
|
|
|
DataManagementPlanTableRequest tableRequest = new DataManagementPlanTableRequest();
|
|
tableRequest.setCriteria(new DataManagementPlanCriteria());
|
|
tableRequest.getCriteria().setAllVersions(false);
|
|
tableRequest.getCriteria().setOnlyPublic(true);
|
|
tableRequest.getCriteria().setIsPublic(true);
|
|
CompletableFuture dmpTask = CompletableFuture.runAsync(() -> {
|
|
try {
|
|
dmpData.set(dmpManager.getPaged(tableRequest, new Principal(), "listing"));
|
|
} catch (Exception e) {
|
|
logger.error(e.getMessage(), e);
|
|
}
|
|
});
|
|
|
|
DatasetTableRequest datasetRequest = new DatasetTableRequest();
|
|
datasetRequest.setCriteria(new DatasetCriteria());
|
|
datasetRequest.getCriteria().setAllVersions(false);
|
|
datasetRequest.getCriteria().setIsPublic(true);
|
|
datasetRequest.setOrderings(new ColumnOrderings());
|
|
CompletableFuture datasetTask = CompletableFuture.runAsync(() -> {
|
|
try {
|
|
datasetData.set(datasetManager.getPaged(datasetRequest, new Principal()));
|
|
} catch (Exception e) {
|
|
logger.error(e.getMessage(), e);
|
|
}
|
|
|
|
});
|
|
|
|
OrganisationCriteria organisationCriteria = new OrganisationCriteria();
|
|
organisationCriteria.setPublic(true);
|
|
|
|
CompletableFuture.allOf(dmpTask, datasetTask).join();
|
|
|
|
List<DMP> dmps = dmpData.get().getData().stream().map(DataManagementPlanListingModel::toDataModel).collect(Collectors.toList());
|
|
long numberOfDatasets = datasetData.get().getTotalCount();
|
|
grants.addAll(dmps.stream().map(DMP::getGrant).collect(Collectors.toList()));
|
|
|
|
statistics.setTotalDataManagementPlanCount(dmpData.get().getTotalCount());
|
|
statistics.setTotalDataSetCount(numberOfDatasets);
|
|
statistics.setTotalGrantCount(grants.stream().filter(StreamDistinctBy.distinctByKey(Grant::getId)).count());
|
|
statistics.setTotalOrganisationCount(databaseRepository.getOrganisationDao().getWithCriteria(organisationCriteria).count());
|
|
|
|
return statistics;
|
|
}
|
|
|
|
public DashBoardStatistics getMeStatistics(Principal principal) throws IOException {
|
|
List<eu.eudat.elastic.entities.Dataset> datasets = null;
|
|
List<eu.eudat.elastic.entities.Dmp> dmps = null;
|
|
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();
|
|
if (apiContext.getOperationsContext().getElasticRepository().getDatasetRepository() != null) {
|
|
try {
|
|
eu.eudat.elastic.criteria.DatasetCriteria datasetElasticCriteria = new eu.eudat.elastic.criteria.DatasetCriteria();
|
|
datasetElasticCriteria.setAllowAllVersions(false);
|
|
datasetElasticCriteria.setPublic(false);
|
|
datasets = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().query(datasetElasticCriteria);
|
|
}catch (Exception e) {
|
|
logger.warn(e.getMessage(), e);
|
|
datasets = null;
|
|
}
|
|
}
|
|
datasetCriteria.setAllVersions(false);
|
|
datasetCriteria.setIsPublic(false);
|
|
DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria();
|
|
if (apiContext.getOperationsContext().getElasticRepository().getDmpRepository() != null) {
|
|
try {
|
|
eu.eudat.elastic.criteria.DmpCriteria dmpElasticCriteria = new eu.eudat.elastic.criteria.DmpCriteria();
|
|
dmpElasticCriteria.setAllowAllVersions(false);
|
|
dmpElasticCriteria.setPublic(false);
|
|
dmps = apiContext.getOperationsContext().getElasticRepository().getDmpRepository().query(dmpElasticCriteria);
|
|
}catch (Exception e) {
|
|
logger.warn(e.getMessage(), e);
|
|
dmps = null;
|
|
}
|
|
}
|
|
dataManagementPlanCriteria.setAllVersions(false);
|
|
GrantCriteria grantCriteria = new GrantCriteria();
|
|
|
|
List<Integer> roles = new LinkedList<>();
|
|
List<Dmp> finalDmps = dmps;
|
|
CompletableFuture dmpFuture = dataManagementPlanRepository.getAuthenticated(dmps != null ? dataManagementPlanRepository.asQueryable().where((builder, root) -> root.get("id").in(finalDmps.stream().map(Dmp::getId).collect(Collectors.toList()))) : dataManagementPlanRepository.getWithCriteria(dataManagementPlanCriteria), principal.getId(), roles).countAsync()
|
|
.whenComplete((dmpsStats, throwable) -> statistics.setTotalDataManagementPlanCount(dmpsStats));
|
|
List<eu.eudat.elastic.entities.Dataset> finalDatasets = datasets;
|
|
CompletableFuture datasetFuture = datasetRepository.getAuthenticated(datasets != null ? datasetRepository.asQueryable().where((builder, root) -> root.get("id").in(finalDatasets.stream().map(x -> UUID.fromString(x.getId())).collect(Collectors.toList()))) : datasetRepository.getWithCriteria(datasetCriteria), user, roles).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);
|
|
|
|
List<Integer> roles = new LinkedList<>();
|
|
CompletableFuture<List<RecentActivityData>> dmps = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.getWithCriteria(dataManagementPlanCriteria), principal.getId(), roles)
|
|
.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<List<RecentActivityData>> datasets = datasetRepository.getAuthenticated(datasetRepository.getWithCriteria(datasetCriteria), user, roles)
|
|
.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<List<RecentActivityData>> 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<SearchBarItem> searchUserData(String like, Principal principal) {
|
|
UserInfo user = new UserInfo();
|
|
user.setId(principal.getId());
|
|
DMPDao dataManagementPlanRepository = databaseRepository.getDmpDao();
|
|
DatasetDao datasetRepository = databaseRepository.getDatasetDao();
|
|
GrantDao grantRepository = databaseRepository.getGrantDao();
|
|
|
|
List<Integer> roles = new LinkedList<>();
|
|
List<SearchBarItem> searchBarItems = new LinkedList<>();
|
|
|
|
CompletableFuture<List<SearchBarItem>> publicDmps = dataManagementPlanRepository.asQueryable()
|
|
.where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%"))
|
|
.where((builder, root) -> builder.notEqual(root.get("status"), DMP.DMPStatus.DELETED.getValue()))
|
|
.where((builder, root) -> builder.equal(root.get("isPublic"), true))
|
|
.orderBy((builder, root) -> builder.desc(root.get("modified")))
|
|
.selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.DMP.getValue(), true))
|
|
.whenComplete((dmpItems, throwable) -> searchBarItems.addAll(dmpItems));
|
|
|
|
CompletableFuture<List<SearchBarItem>> publicDatasets = datasetRepository.asQueryable()
|
|
.where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%"))
|
|
.where((builder, root) -> builder.equal(root.get("status"), Dataset.Status.FINALISED.getValue()))
|
|
.where((builder, root) -> builder.equal(root.get("dmp").get("isPublic"), true))
|
|
.orderBy((builder, root) -> builder.desc(root.get("modified")))
|
|
.selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.DATASET.getValue(), true))
|
|
.whenComplete((dataSetItems, throwable) -> searchBarItems.addAll(dataSetItems));
|
|
|
|
if (principal.getId() != null) {
|
|
CompletableFuture<List<SearchBarItem>> dmps = dataManagementPlanRepository.getAuthenticated(dataManagementPlanRepository.asQueryable(), principal.getId(), roles)
|
|
.withHint("dmpRecentActivity")
|
|
.where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%"))
|
|
.where((builder, root) -> builder.notEqual(root.get("status"), DMP.DMPStatus.DELETED.getValue()))
|
|
.orderBy((builder, root) -> builder.desc(root.get("modified")))
|
|
.selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.DMP.getValue(), false))
|
|
.whenComplete((dmpItems, throwable) -> searchBarItems.addAll(dmpItems));
|
|
|
|
CompletableFuture<List<SearchBarItem>> datasets = datasetRepository.getAuthenticated(datasetRepository.asQueryable(), user, roles)
|
|
.withHint("datasetRecentActivity")
|
|
.where((builder, root) -> builder.like(builder.upper(root.get("label")), "%" + like.toUpperCase() + "%"))
|
|
.where((builder, root) -> builder.notEqual(root.get("status"), Dataset.Status.DELETED.getValue()))
|
|
.where((builder, root) -> builder.notEqual(root.get("status"), Dataset.Status.CANCELED.getValue()))
|
|
.orderBy((builder, root) -> builder.desc(root.get("modified")))
|
|
.selectAsync(item -> new SearchBarItem(item.getId().toString(), item.getLabel(), SearchBarItemType.DATASET.getValue(), false))
|
|
.whenComplete((dataSetItems, throwable) -> searchBarItems.addAll(dataSetItems));
|
|
|
|
CompletableFuture<List<SearchBarItem>> 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(), false))
|
|
.whenComplete((grantItems, throwable) -> searchBarItems.addAll(grantItems));
|
|
|
|
CompletableFuture.allOf(grants, dmps, datasets, publicDmps, publicDatasets).join();
|
|
} else {
|
|
CompletableFuture.allOf(publicDmps, publicDatasets).join();
|
|
}
|
|
|
|
return searchBarItems;
|
|
}
|
|
}
|