package eu.eudat.logic.managers; import eu.eudat.data.dao.entities.DMPProfileDao; import eu.eudat.data.entities.DMPProfile; import eu.eudat.data.query.items.item.dmpprofile.DataManagementPlanProfileCriteriaRequest; import eu.eudat.data.query.items.table.dmpprofile.DataManagementPlanProfileTableRequest; import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel; import eu.eudat.models.data.security.Principal; import eu.eudat.queryable.QueryableList; import eu.eudat.logic.services.ApiContext; import org.springframework.stereotype.Component; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; /** * Created by ikalyvas on 3/21/2018. */ @Component public class DataManagementProfileManager { public DataTableData getPaged(ApiContext apiContext, DataManagementPlanProfileTableRequest dataManagementPlanProfileTableRequest, Principal principal) throws Exception { QueryableList items = apiContext.getOperationsContext().getDatabaseRepository().getDmpProfileDao().getWithCriteria(dataManagementPlanProfileTableRequest.getCriteria()); QueryableList pagedItems = PaginationManager.applyPaging(items, dataManagementPlanProfileTableRequest); DataTableData dataTable = new DataTableData(); CompletableFuture itemsFuture = pagedItems .selectAsync(item -> new DataManagementPlanProfileListingModel().fromDataModel(item)).whenComplete((resultList, throwable) -> { dataTable.setData(resultList); }); CompletableFuture countFuture = items.countAsync().whenComplete((count, throwable) -> { dataTable.setTotalCount(count); }); CompletableFuture.allOf(itemsFuture, countFuture).join(); return dataTable; } public DataManagementPlanProfileListingModel getSingle(DMPProfileDao dmpProfileDao, String id, Principal principal) throws InstantiationException, IllegalAccessException { DMPProfile dmpProfile = dmpProfileDao.find(UUID.fromString(id)); DataManagementPlanProfileListingModel dataManagementPlanProfileListingModel = new DataManagementPlanProfileListingModel(); dataManagementPlanProfileListingModel.fromDataModel(dmpProfile); return dataManagementPlanProfileListingModel; } public List getWithCriteria(DMPProfileDao dmpProfileDao, DataManagementPlanProfileCriteriaRequest dataManagementPlanProfileCriteriaRequest) throws IllegalAccessException, InstantiationException { QueryableList items = dmpProfileDao.getWithCriteria(dataManagementPlanProfileCriteriaRequest.getCriteria()); List datamanagementPlans = items.select(item -> new DataManagementPlanProfileListingModel().fromDataModel(item)); return datamanagementPlans; } public void createOrUpdate(ApiContext apiContext, DataManagementPlanProfileListingModel dataManagementPlanProfileListingModel, Principal principal) throws Exception { DMPProfile dmpProfile = dataManagementPlanProfileListingModel.toDataModel(); apiContext.getOperationsContext().getDatabaseRepository().getDmpProfileDao().createOrUpdate(dmpProfile); } }