2017-12-15 00:01:26 +01:00
|
|
|
package eu.eudat.managers;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2018-01-08 12:44:48 +01:00
|
|
|
import java.util.stream.Collectors;
|
2017-12-15 00:01:26 +01:00
|
|
|
|
2017-12-22 09:31:05 +01:00
|
|
|
import eu.eudat.dao.entities.*;
|
2017-12-17 22:34:24 +01:00
|
|
|
import eu.eudat.entities.DMP;
|
2017-12-22 09:31:05 +01:00
|
|
|
import eu.eudat.entities.Project;
|
|
|
|
import eu.eudat.entities.UserInfo;
|
2018-01-08 12:44:48 +01:00
|
|
|
import eu.eudat.exceptions.UnauthorisedException;
|
2017-12-19 09:38:47 +01:00
|
|
|
import eu.eudat.models.criteria.DataManagementPlanCriteria;
|
2017-12-20 15:52:09 +01:00
|
|
|
import eu.eudat.models.criteria.OrganisationCriteria;
|
2017-12-22 09:31:05 +01:00
|
|
|
import eu.eudat.models.criteria.ProjectCriteria;
|
2017-12-20 15:52:09 +01:00
|
|
|
import eu.eudat.models.criteria.ResearcherCriteria;
|
2017-12-19 09:38:47 +01:00
|
|
|
import eu.eudat.models.dmp.DataManagementPlan;
|
|
|
|
import eu.eudat.models.dmp.DataManagementPlanCriteriaRequest;
|
2017-12-15 00:01:26 +01:00
|
|
|
import eu.eudat.models.dmp.DataManagementPlanTableRequest;
|
|
|
|
import eu.eudat.models.helpers.DataTableData;
|
2017-12-19 15:09:49 +01:00
|
|
|
import eu.eudat.models.listingmodels.DataManagementPlanListingModel;
|
2017-12-20 15:52:09 +01:00
|
|
|
import eu.eudat.models.security.Principal;
|
2017-12-17 22:34:24 +01:00
|
|
|
import eu.eudat.queryable.QueryableList;
|
2018-01-04 10:32:39 +01:00
|
|
|
import eu.eudat.services.ApiContext;
|
2017-12-15 00:01:26 +01:00
|
|
|
import eu.eudat.utilities.builders.DomainModelConverter;
|
|
|
|
|
|
|
|
public class DataManagementPlanManager {
|
|
|
|
|
2018-01-08 15:57:21 +01:00
|
|
|
public DataTableData<DataManagementPlanListingModel> getPaged(ApiContext apiContext, DataManagementPlanTableRequest dataManagementPlanTableRequest,Principal principal) throws IllegalAccessException, InstantiationException{
|
|
|
|
UserInfo userInfo = apiContext.getDatabaseRepository().getUserInfoDao().find(principal.getId());
|
|
|
|
QueryableList<DMP> items = apiContext.getDatabaseRepository().getDmpDao().getWithCriteria(dataManagementPlanTableRequest.getCriteria());
|
|
|
|
QueryableList<DMP> authItems = apiContext.getDatabaseRepository().getDmpDao().getAuthenticated(items,userInfo);
|
|
|
|
QueryableList<DMP> pagedItems = PaginationManager.applyPaging(authItems,dataManagementPlanTableRequest);
|
|
|
|
|
2018-01-02 09:36:40 +01:00
|
|
|
if(dataManagementPlanTableRequest.getWithHint())pagedItems.withHint("fullyDetailed");
|
2017-12-19 15:09:49 +01:00
|
|
|
List<DataManagementPlanListingModel> datamanagementPlans = new DomainModelConverter<eu.eudat.entities.DMP, DataManagementPlanListingModel>().fromDataModel( pagedItems.toList(), DataManagementPlanListingModel.class);
|
|
|
|
DataTableData<DataManagementPlanListingModel> dataTable = new DataTableData<DataManagementPlanListingModel>();
|
2017-12-15 00:01:26 +01:00
|
|
|
dataTable.setData(datamanagementPlans);
|
2017-12-19 15:09:49 +01:00
|
|
|
dataTable.setTotalCount(items.count());
|
2017-12-15 00:01:26 +01:00
|
|
|
return dataTable;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:44:48 +01:00
|
|
|
public eu.eudat.models.dmp.DataManagementPlan getSingle(DMPDao dmpsRepository, String id,Principal principal) throws InstantiationException, IllegalAccessException{
|
|
|
|
DMP dataManagementPlanEntity = dmpsRepository.find(UUID.fromString(id));
|
|
|
|
if(dataManagementPlanEntity.getUsers().stream().filter(userInfo -> userInfo.getId() == principal.getId()).collect(Collectors.toList()).size()==0)throw new UnauthorisedException();
|
2017-12-15 00:01:26 +01:00
|
|
|
eu.eudat.models.dmp.DataManagementPlan datamanagementPlan = new eu.eudat.models.dmp.DataManagementPlan();
|
2018-01-08 12:44:48 +01:00
|
|
|
datamanagementPlan.fromDataModel(dataManagementPlanEntity);
|
2017-12-15 00:01:26 +01:00
|
|
|
return datamanagementPlan;
|
|
|
|
}
|
2017-12-19 09:38:47 +01:00
|
|
|
|
|
|
|
public List<DataManagementPlan> getWithCriteria(DMPDao dmpsRepository, DataManagementPlanCriteriaRequest dataManagementPlanCriteria) throws IllegalAccessException, InstantiationException{
|
|
|
|
QueryableList<DMP> items = dmpsRepository.getWithCriteria(dataManagementPlanCriteria.getCriteria());
|
|
|
|
List<eu.eudat.models.dmp.DataManagementPlan> datamanagementPlans = new DomainModelConverter<eu.eudat.entities.DMP, eu.eudat.models.dmp.DataManagementPlan>().fromDataModel( items.toList(), eu.eudat.models.dmp.DataManagementPlan.class);
|
|
|
|
return datamanagementPlans;
|
|
|
|
}
|
2017-12-20 15:52:09 +01:00
|
|
|
|
2018-01-04 10:32:39 +01:00
|
|
|
public static void createOrUpdate(ApiContext apiContext, DataManagementPlan dataManagementPlan, Principal principal){
|
2017-12-20 15:52:09 +01:00
|
|
|
DMP newDmp = dataManagementPlan.toDataModel();
|
2018-01-04 10:32:39 +01:00
|
|
|
createOrganisationsIfTheyDontExist(newDmp,apiContext.getDatabaseRepository().getOrganisationDao());
|
|
|
|
createResearchersIfTheyDontExist(newDmp,apiContext.getDatabaseRepository().getResearcherDao());
|
2018-01-05 08:47:52 +01:00
|
|
|
UserInfo user = apiContext.getDatabaseRepository().getUserInfoDao().find(principal.getId());
|
2018-01-04 10:32:39 +01:00
|
|
|
createProjectIfItDoesntExist(newDmp,apiContext.getDatabaseRepository().getProjectDao(),user);
|
2017-12-22 09:31:05 +01:00
|
|
|
newDmp.setCreator(user);
|
2018-01-04 10:32:39 +01:00
|
|
|
apiContext.getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
|
2017-12-20 15:52:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void createResearchersIfTheyDontExist(DMP newDmp,ResearcherDao researcherRepository){
|
|
|
|
if(newDmp.getResearchers()!=null&&!newDmp.getResearchers().isEmpty()){
|
|
|
|
for(eu.eudat.entities.Researcher researcher : newDmp.getResearchers()){
|
|
|
|
ResearcherCriteria criteria = new ResearcherCriteria();
|
|
|
|
criteria.setLike(researcher.getReference());
|
|
|
|
List<eu.eudat.entities.Researcher> entries = researcherRepository.listBy(criteria);
|
|
|
|
if(entries!=null&&!entries.isEmpty())researcher.setId(entries.get(0).getId());
|
|
|
|
else researcher = researcherRepository.create(researcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void createOrganisationsIfTheyDontExist(DMP newDmp,OrganisationDao organisationRepository){
|
|
|
|
if(newDmp.getOrganisations()!=null&&!newDmp.getOrganisations().isEmpty()){
|
|
|
|
for(eu.eudat.entities.Organisation organisation: newDmp.getOrganisations()){
|
|
|
|
OrganisationCriteria criteria = new OrganisationCriteria();
|
|
|
|
criteria.setLike(organisation.getReference());
|
|
|
|
List<eu.eudat.entities.Organisation> entries = organisationRepository.listBy(criteria);
|
|
|
|
if(entries!=null&&!entries.isEmpty())organisation.setId(entries.get(0).getId());
|
|
|
|
else organisation = organisationRepository.create(organisation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 09:31:05 +01:00
|
|
|
|
|
|
|
private static void createProjectIfItDoesntExist(DMP newDmp,ProjectDao projectDao,UserInfo userInfo){
|
|
|
|
if(newDmp.getProject()!=null){
|
|
|
|
Project project = newDmp.getProject();
|
|
|
|
ProjectCriteria criteria = new ProjectCriteria();
|
|
|
|
criteria.setLike(project.getReference());
|
|
|
|
List<eu.eudat.entities.Project> entries = projectDao.getWithCriteria(criteria).toList();
|
|
|
|
if(entries!=null&&!entries.isEmpty())project.setId(entries.get(0).getId());
|
|
|
|
else{
|
|
|
|
project.setCreationUser(userInfo);
|
|
|
|
projectDao.createOrUpdate(project);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 00:01:26 +01:00
|
|
|
}
|