package eu.eudat.logic.managers; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import eu.eudat.commons.XmlHandlingService; import eu.eudat.commons.enums.*; import eu.eudat.commons.scope.user.UserScope; import eu.eudat.commons.types.dmpblueprint.*; import eu.eudat.data.*; import eu.eudat.data.dao.criteria.*; import eu.eudat.data.dao.entities.*; import eu.eudat.data.old.*; import eu.eudat.data.query.items.table.dataset.DatasetTableRequest; import eu.eudat.data.query.items.table.datasetprofile.DatasetProfileTableRequestItem; import eu.eudat.data.query.items.table.dmp.DataManagementPlanTableRequest; //import eu.eudat.depositinterface.models.DmpDepositModel; import eu.eudat.exceptions.datamanagementplan.DMPNewVersionException; import eu.eudat.exceptions.datamanagementplan.DMPWithDatasetsDeleteException; import eu.eudat.exceptions.security.ForbiddenException; import eu.eudat.exceptions.security.UnauthorisedException; import eu.eudat.logic.builders.entity.UserInfoBuilder; import eu.eudat.logic.proxy.config.configloaders.ConfigLoader; import eu.eudat.model.DmpUser; import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleServiceImpl; import eu.eudat.logic.services.operations.DatabaseRepository; import eu.eudat.commons.types.xml.XmlBuilder; import eu.eudat.model.EntityDoi; import eu.eudat.model.file.FileEnvelope; import eu.eudat.query.*; import eu.eudat.logic.utilities.documents.types.ParagraphStyle; import eu.eudat.logic.utilities.documents.word.WordBuilder; import eu.eudat.logic.utilities.documents.xml.ExportXmlBuilder; import eu.eudat.model.persist.deposit.DepositRequest; import eu.eudat.models.HintedModelFactory; import eu.eudat.models.data.dataset.DatasetOverviewModel; import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel; import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.datasetwizard.DatasetsToBeFinalized; import eu.eudat.models.data.dmp.*; import eu.eudat.models.data.dynamicfields.DynamicFieldWithValue; import eu.eudat.models.data.helpermodels.Tuple; import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.listingmodels.*; import eu.eudat.models.data.user.composite.PagedDatasetProfile; import eu.eudat.queryable.QueryableList; import eu.eudat.service.deposit.DepositService; import eu.eudat.service.dmpblueprint.DmpBlueprintService; import eu.eudat.types.MetricNames; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.data.query.QueryFactory; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import org.w3c.dom.Document; import org.w3c.dom.Element; import jakarta.transaction.Transactional; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Unmarshaller; import javax.management.InvalidApplicationException; import java.io.*; import java.math.BigInteger; import java.nio.file.Files; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Component public class DataManagementPlanManager { private static final Logger logger = LoggerFactory.getLogger(DataManagementPlanManager.class); private final ObjectMapper objectMapper; // private final Map notificationPaths = Stream.of(new Object[][] { // {NotificationType.DMP_MODIFIED, "/plans/edit"}, // {NotificationType.DMP_PUBLISH, "/plans/publicEdit"}, // {NotificationType.DMP_FINALISED, "/plans/edit"}, // {NotificationType.DMP_MODIFIED_FINALISED, "/plans/edit"} // }).collect(Collectors.toMap(data -> (NotificationType) data[0], data -> (String) data[1])); private ApiContext apiContext; private DatasetManager datasetManager; private DatabaseRepository databaseRepository; private Environment environment; private RDAManager rdaManager; private final MetricsManager metricsManager; private final ConfigLoader configLoader; private DepositService repositoriesDeposit; private final UserScope userScope; private final AuthorizationService authorizationService; private final DmpBlueprintService dmpBlueprintService; private final QueryFactory queryFactory; private final XmlHandlingService xmlHandlingService; @Autowired public DataManagementPlanManager(XmlHandlingService xmlHandlingService, ApiContext apiContext, DatasetManager datasetManager, Environment environment, RDAManager rdaManager, MetricsManager metricsManager, ConfigLoader configLoader, DepositService repositoriesDeposit, UserScope userScope, AuthorizationService authorizationService, DmpBlueprintService dmpBlueprintService, QueryFactory queryFactory) { this.xmlHandlingService = xmlHandlingService; this.apiContext = apiContext; this.datasetManager = datasetManager; this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.environment = environment; this.rdaManager = rdaManager; this.metricsManager = metricsManager; this.configLoader = configLoader; this.userScope = userScope; this.authorizationService = authorizationService; this.dmpBlueprintService = dmpBlueprintService; this.queryFactory = queryFactory; this.objectMapper = new ObjectMapper(); this.repositoriesDeposit = repositoriesDeposit; } /* * Data Retrieval * */ public DataTableData getPaged(DataManagementPlanTableRequest dataManagementPlanTableRequest, String fieldsGroup) throws Exception { UUID principalID = userScope.getUserIdSafe(); // List dmps = null; QueryableList items = null; QueryableList authItems = null; Long totalData = 0L; // if (apiContext.getOperationsContext().getElasticRepository().getDmpRepository() != null) { //TODO // try { // DmpCriteria criteria = DmpCriteriaMapper.toElasticCriteria(dataManagementPlanTableRequest.getCriteria(), principalID); // criteria.setOffset(dataManagementPlanTableRequest.getOffset()); // criteria.setSize(dataManagementPlanTableRequest.getLength()); // criteria.setSortCriteria(DmpCriteriaMapper.toElasticSorting(dataManagementPlanTableRequest.getOrderings())); // // dmps = apiContext.getOperationsContext().getElasticRepository().getDmpRepository().query(criteria); // if (dmps != null && !dmps.isEmpty()) { // List finalDmps = dmps; // items = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable().where((builder, root) -> root.get("id").in(finalDmps.stream().map(Dmp::getId).collect(Collectors.toList()))); // PaginationManager.applyOrder(items, dataManagementPlanTableRequest); // totalData = apiContext.getOperationsContext().getElasticRepository().getDmpRepository().count(criteria); // } // } catch (Exception ex) { // logger.warn(ex.getMessage(), ex); // items = null; // } // } if (items == null) { items = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getWithCriteria(dataManagementPlanTableRequest.getCriteria()); } List roles = new LinkedList<>(); if (!dataManagementPlanTableRequest.getCriteria().isOnlyPublic()) { if (dataManagementPlanTableRequest.getCriteria().getRole() != null) roles.add(dataManagementPlanTableRequest.getCriteria().getRole()); authItems = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getAuthenticated(items, principalID, roles); } else { authItems = items; } // if (dmps == null) { totalData = authItems.count(); items = PaginationManager.applyPaging(authItems, dataManagementPlanTableRequest); // } else { // items = authItems; // } DataTableData dataTable = new DataTableData<>(); if (fieldsGroup.equals("listing")) { if (!dataManagementPlanTableRequest.getCriteria().isOnlyPublic()) { List dmps1 = items.withHint(HintedModelFactory.getHint(DataManagementPlanListingModel.class)) .distinct().toList(); dataTable.setData(dmps1.stream().map(dmp -> { DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(Collections.singletonList(dmp.getId())); datasetCriteria.setAllVersions(dataManagementPlanTableRequest.getCriteria().getAllVersions()); datasetCriteria.setIsPublic(dataManagementPlanTableRequest.getCriteria().getIsPublic()); datasetCriteria.setGroupIds(Collections.singletonList(dmp.getGroupId())); // try { //TODO // dmp.setDataset(retrieveRelevantDatasets(datasetCriteria, principalID)); // } catch (InvalidApplicationException e) { // throw new RuntimeException(e); // } return new DataManagementPlanListingModel().fromDataModelDatasets(dmp); }).collect(Collectors.toList())); /*.selectAsync(item -> { item.setDataset( item.getDataset().stream() .filter(dataset -> !dataset.getStatus().equals(Dataset.Status.DELETED.getValue()) && !dataset.getStatus().equals(Dataset.Status.CANCELED.getValue())).collect(Collectors.toList()).stream() *//*.filter(dataset -> dataset.getDmp().getUsers().stream() .filter(x -> x.getUser().getId().equals(principalID)) .collect(Collectors.toList()).size() > 0)*//* .collect(Collectors.toSet())); return new DataManagementPlanListingModel().fromDataModelDatasets(item); }) .whenComplete((resultList, throwable) -> dataTable.setData(resultList));*/ } else { List dmps1 = items.withHint(HintedModelFactory.getHint(DataManagementPlanListingModel.class)) .distinct().toList(); dataTable.setData(dmps1.stream().map(dmp -> { DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(Collections.singletonList(dmp.getId())); datasetCriteria.setIsPublic(true); datasetCriteria.setAllVersions(dataManagementPlanTableRequest.getCriteria().getAllVersions()); datasetCriteria.setGroupIds(Collections.singletonList(dmp.getGroupId())); // try { //TODO // dmp.setDataset(retrieveRelevantDatasets(datasetCriteria)); // } catch (InvalidApplicationException e) { // throw new RuntimeException(e); // } return new DataManagementPlanListingModel().fromDataModelDatasets(dmp); }).collect(Collectors.toList())); /*.selectAsync(item -> { item.setDataset( item.getDataset().stream() .filter(dataset -> dataset.getStatus().equals(Dataset.Status.FINALISED.getValue())).collect(Collectors.toSet())); return new DataManagementPlanListingModel().fromDataModelDatasets(item); }) .whenComplete((resultList, throwable) -> dataTable.setData(resultList));*/ } } else if (fieldsGroup.equals("autocomplete")) { dataTable.setData(items .distinct() .select(item -> new DataManagementPlanListingModel().fromDataModelAutoComplete(item))); } else { dataTable.setData(items .distinct() .select(item -> new DataManagementPlanListingModel().fromDataModelAssociatedProfiles(item))); } //CompletableFuture countFuture = authItems.distinct().countAsync().whenComplete((count, throwable) -> dataTable.setTotalCount(count)); dataTable.setTotalCount(totalData); //CompletableFuture.allOf(itemsFuture).join(); return dataTable; } private Set retrieveRelevantDatasets(DatasetCriteria datasetCriteria) throws InvalidApplicationException { return retrieveRelevantDatasets(datasetCriteria, null); } private Set retrieveRelevantDatasets (DatasetCriteria datasetCriteria, UUID principal) throws InvalidApplicationException { QueryableList datasetItems = apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao().getWithCriteria(datasetCriteria) .orderBy((builder, root) -> builder.desc(root.get("modified"))); if (principal != null) { UserEntity userInfo = this.queryFactory.query(UserQuery.class).ids(principal).first(); List roles = new ArrayList<>(); roles.add(0); roles.add(1); datasetItems = apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao().getAuthenticated(datasetItems, userInfo, roles); } Long maxDatasets = datasetItems.distinct().count(); DatasetTableRequest datasetTableRequest = new DatasetTableRequest(); datasetTableRequest.setOffset(0); datasetTableRequest.setLength(3); Set datasetsSet = new LinkedHashSet<>(); try { datasetItems = PaginationManager.applyPaging(datasetItems, datasetTableRequest); List descriptionEntities = datasetItems.distinct().toList(); datasetsSet.addAll(descriptionEntities); for (int i = 0; i < maxDatasets - descriptionEntities.size(); i++) { DescriptionEntity fakedataset = new DescriptionEntity(); fakedataset.setId(UUID.randomUUID()); datasetsSet.add(fakedataset); } } catch (Exception e) { logger.error(e.getMessage(), e); } return datasetsSet; } public eu.eudat.models.data.dmp.DataManagementPlan getSingle(String id, boolean isPublic, boolean includeDatasets) throws Exception { eu.eudat.models.data.dmp.DataManagementPlan dataManagementPlan = new eu.eudat.models.data.dmp.DataManagementPlan(); DmpEntity dataManagementPlanEntity = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (!isPublic && !this.userScope.isSet()) { throw new UnauthorisedException(); //TODO } else if (!isPublic /*&& (dataManagementPlanEntity.getUsers().stream().noneMatch(userInfo -> this.userScope.getUserIdSafe().equals(userInfo.getUser().getId())))*/ ) { if (!dataManagementPlanEntity.getAccessType().equals(DmpAccessType.Public)) { throw new UnauthorisedException(); } } else if (isPublic && !dataManagementPlanEntity.getAccessType().equals(DmpAccessType.Public)) { throw new ForbiddenException("Selected DMP is not public"); } if (includeDatasets) { dataManagementPlan.fromDataModel(dataManagementPlanEntity); dataManagementPlan.getDatasets().forEach(datasetWizardModel -> datasetWizardModel.setDescription(null)); dataManagementPlan.setDatasets(dataManagementPlan.getDatasets().stream().sorted(Comparator.comparing(DatasetWizardModel::getCreated).reversed()).collect(Collectors.toList())); //List datasetEnities = new ArrayList<>(dataManagementPlanEntity.getDataset()); /*for (int i = 0; i < datasetEnities.size(); i++) { for (int j = i; j < dataManagementPlan.getDatasets().size(); j++) { if (dataManagementPlan.getDatasets().get(j).getId().equals(datasetEnities.get(i).getId())) { dataManagementPlan.getDatasets().get(j).setDatasetProfileDefinition(datasetManager.getPagedProfile(dataManagementPlan.getDatasets().get(j), datasetEnities.get(i))); break; } } }*/ /*datasetEnities.stream() .filter(dataset -> !dataset.getStatus().equals(Dataset.Status.DELETED) && !dataset.getStatus().equals(Dataset.Status.CANCELED)) .forEach(dataset -> { dataManagementPlan.getDatasets().stream().filter(datasetWizardModel -> datasetWizardModel.getId().equals(dataset.getId())).forEach(datasetWizardModel -> { DatasetWizardModel wizardModel = datasetManager.getSingle(datasetWizardModel.getId().toString(), principal); datasetWizardModel.setDatasetProfileDefinition(wizardModel.getDatasetProfileDefinition()); datasetWizardModel.setTags(wizardModel.getTags()); }); });*/ if (isPublic) { dataManagementPlan.setDatasets(dataManagementPlan.getDatasets().stream().filter(dataset -> dataset.getStatus() == DescriptionStatus.Finalized).collect(Collectors.toList())); } } else { dataManagementPlan.fromDataModelNoDatasets(dataManagementPlanEntity); } Map dmpProperties = new HashMap();//TODO dataManagementPlanEntity.getDmpProperties() != null ? new org.json.JSONObject(dataManagementPlanEntity.getDmpProperties()).toMap() : null; if (dmpProperties != null && dataManagementPlan.getDynamicFields() != null) dataManagementPlan.getDynamicFields().forEach(item -> { Map properties = (Map) dmpProperties.get(item.getId()); if (properties != null) item.setValue(new Tuple<>(properties.get("id"), properties.get("label"))); }); return dataManagementPlan; } public DataManagementPlanOverviewModel getOverviewSingle(String id, boolean isPublic) throws Exception { DmpEntity dataManagementPlanEntity = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (dataManagementPlanEntity.getIsActive().equals(IsActive.Inactive)) { throw new Exception("DMP is deleted."); } if (!isPublic && !this.userScope.isSet()) { throw new UnauthorisedException(); } else if (!isPublic //&& dataManagementPlanEntity.getUsers() //TODO // .stream().noneMatch(userInfo -> userInfo.getUser().getId().equals(this.userScope.getUserIdSafe())) ) { throw new UnauthorisedException(); } else if (isPublic && !dataManagementPlanEntity.getAccessType().equals(DmpAccessType.Public)) { throw new ForbiddenException("Selected DMP is not public"); } DataManagementPlanOverviewModel datamanagementPlan = new DataManagementPlanOverviewModel(); datamanagementPlan.fromDataModelDatasets(dataManagementPlanEntity); datamanagementPlan.setDatasets(datamanagementPlan.getDatasets().stream().sorted(Comparator.comparing(DatasetOverviewModel::getCreated).reversed()).collect(Collectors.toList())); if (isPublic) { datamanagementPlan.setDatasets(datamanagementPlan.getDatasets().stream().filter(dataset -> dataset.getStatus() == DescriptionStatus.Finalized).collect(Collectors.toList())); } return datamanagementPlan; } // public List> getDynamicFields(String id, DynamicGrantConfiguration dynamicGrantConfiguration, DynamicFieldsCriteria criteria) throws IllegalAccessException, InstantiationException { // List> result = new LinkedList<>(); // RestTemplate restTemplate = new RestTemplate(); // HttpHeaders headers = new HttpHeaders(); // headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); // headers.setContentType(MediaType.APPLICATION_JSON); // HttpEntity entity = new HttpEntity<>("parameters", headers); // // Property property = dynamicGrantConfiguration.getConfiguration().getConfigurationProperties().stream() // .filter(item -> item.getId().equals(id)).findFirst().orElse(null); // StringBuilder stringBuilder = new StringBuilder(); // if (criteria.getLike() != null) stringBuilder.append("?search=" + criteria.getLike()); // if (property.getDependencies() != null && !property.getDependencies().isEmpty() && criteria.getDynamicFields() != null && !criteria.getDynamicFields().isEmpty()) { // property.getDependencies().stream().forEach(item -> { // DynamicFieldsCriteria.DynamicFieldDependencyCriteria dependencyCriteria = criteria.getDynamicFields().stream().filter(dfield -> dfield.getProperty().equals(item.getId())) // .findFirst().orElse(null); // if (dependencyCriteria != null) { // if (criteria.getLike() != null || property.getDependencies().indexOf(item) > 0) // stringBuilder.append("&"); // stringBuilder.append(item.getQueryProperty() + "=" + dependencyCriteria.getValue()); // } // }); // // ResponseEntity response = restTemplate.exchange(property.getSourceUrl() + stringBuilder.toString(), HttpMethod.GET, entity, ArrayList.class); // response.getBody().forEach(item -> { // Tuple tuple = new Tuple<>(); // tuple.setId((String) (((Map) item).get(property.getExternalFieldId()))); // tuple.setLabel((String) (((Map) item).get(property.getExternalFieldLabel()))); // result.add(tuple); // }); // // } else { // ResponseEntity response = restTemplate.exchange(property.getSourceUrl() + stringBuilder.toString(), HttpMethod.GET, entity, ArrayList.class); // response.getBody().forEach(item -> { // Tuple tuple = new Tuple<>(); // tuple.setId((String) (((Map) item).get(property.getExternalFieldId()))); // tuple.setLabel((String) (((Map) item).get(property.getExternalFieldLabel()))); // result.add(tuple); // }); // } // return result; // } public DataTableData getDatasetProfilesUsedByDMP(DatasetProfileTableRequestItem datasetProfileTableRequestItem) throws InvalidApplicationException { datasetProfileTableRequestItem.getCriteria().setFilter(DatasetProfileCriteria.DatasetProfileFilter.DMPs.getValue()); datasetProfileTableRequestItem.getCriteria().setUserId(this.userScope.getUserId()); QueryableList items = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().getWithCriteria(datasetProfileTableRequestItem.getCriteria()); List listingModels = items.select(item -> new DatasetProfileListingModel().fromDataModel(item)); DataTableData data = new DataTableData<>(); data.setData(listingModels); data.setTotalCount((long) listingModels.size()); return data; } public List getAllVersions(String groupId, Boolean isPublic) throws InvalidApplicationException { UUID principalId = this.userScope.getUserIdSafe(); List versions = new ArrayList<>(); QueryableList items = null; QueryableList authItems = null; List roles = new LinkedList<>(); DataManagementPlanCriteria criteria = new DataManagementPlanCriteria(); criteria.setGroupIds(Collections.singletonList(UUID.fromString(groupId))); criteria.setAllVersions(true); criteria.setIsPublic(isPublic); criteria.setOnlyPublic(isPublic); items = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getWithCriteria(criteria); if (!isPublic) { authItems = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getAuthenticated(items, principalId, roles); } else { authItems = items; } CompletableFuture> versionFuture = authItems.withHint(HintedModelFactory.getHint(VersionListingModel.class)) .orderBy(((builder, root) -> builder.desc(root.get("version")))) .selectAsync(item -> new VersionListingModel().fromDataModel(item)) .whenComplete(((versionListingModels, throwable) -> versions.addAll(versionListingModels))); CompletableFuture.allOf(versionFuture).join(); return versions; } /* * Data Management * */ @Transactional public DmpEntity createOrUpdate(DataManagementPlanEditorModel dataManagementPlan) throws Exception { boolean setNotification = false; if (dataManagementPlan.getId() != null) { DmpEntity dmp1 = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(dataManagementPlan.getId()); Instant dbTime = dmp1.getUpdatedAt().truncatedTo(ChronoUnit.SECONDS); Instant modelTime = dmp1.getUpdatedAt().truncatedTo(ChronoUnit.SECONDS); if (!isUserOwnerOfDmp(dmp1)) { throw new Exception("User not being the creator is not authorized to edit this DMP."); } if (dbTime.toEpochMilli() != modelTime.toEpochMilli()) { throw new Exception("Another user have already edit that DMP."); } DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(List.of(dmp1.getId())); List descriptionEntityList = this.databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).toList().stream().filter(dataset -> dataset.getIsActive() != IsActive.Inactive).collect(Collectors.toList()); for (DescriptionEntity descriptionEntity : descriptionEntityList) { if (dataManagementPlan.getProfiles().stream().filter(associatedProfile -> descriptionEntity.getDescriptionTemplateId().equals(associatedProfile.getDescriptionTemplateId())).findAny().orElse(null) == null) throw new Exception("Dataset Template for Dataset Description is missing from the DMP."); } if (dataManagementPlan.getStatus() == DmpStatus.Finalized.getValue() && dmp1.getStatus().equals(DmpStatus.Finalized.getValue() )) throw new Exception("DMP is finalized, therefore cannot be edited."); setNotification = true; } else { metricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT); } DmpEntity newDmp = dataManagementPlan.toDataModel(); if(dataManagementPlan.getProfile() != null){ newDmp.setBlueprintId(dataManagementPlan.getProfile().getId()); } if (newDmp.getStatus().equals(DmpStatus.Finalized)) { checkDmpValidationRules(newDmp); } UserEntity user = this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first(); newDmp.setCreatorId(user.getId()); DmpBlueprintEntity dmpBlueprint = this.queryFactory.query(DmpBlueprintQuery.class).ids(newDmp.getBlueprintId()).first(); // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Organizations)) { // createOrganisationsIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getOrganisationDao()); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Researchers)) { // createResearchersIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao(), user); // } //TODO // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { // createFunderIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getFunderDao()); // }/TODO // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { // createGrantIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getGrantDao()); // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Project)) { //// if (newDmp.getProject() != null && newDmp.getGrant() != null && (newDmp.getProject().getLabel() == null || newDmp.getProject().getLabel().trim().isEmpty())) { //TODO //// newDmp.setProject(newDmp.getProject().projectFromGrant(newDmp.getGrant())); //// } //// createProjectIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getProjectDao()); //TODO // } // } DmpEntity dmp; if (dataManagementPlan.getId() != null) { dmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(dataManagementPlan.getId()); } else dmp = new DmpEntity(); newDmp.setCreatedAt(dmp.getCreatedAt() == null ? Instant.now() : dmp.getCreatedAt()); DmpEntity finalNewDmp = newDmp; if (this.queryFactory.query(DmpUserQuery.class).dmpIds(finalNewDmp.getId()).collect() != null && this.queryFactory.query(DmpUserQuery.class).dmpIds(finalNewDmp.getId()).collect().stream().filter(userInfo -> this.userScope.getUserIdSafe().equals(userInfo.getUserId())) .collect(Collectors.toList()).size() == 0) { List userDMPList = new ArrayList<>(this.queryFactory.query(DmpUserQuery.class).dmpIds(finalNewDmp.getId()).collect()); for (DmpUser userInfoListingModel : dataManagementPlan.getUsers()) { for (DmpUserEntity userDMP : userDMPList) { if (!(userDMP.getUserId().equals(userInfoListingModel.getId()))) { // apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().delete(userDMP); } } } } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { // if (newDmp.getGrant() != null && newDmp.getGrant().getType().equals(Grant.GrantType.INTERNAL.getValue())) { // checkIfUserCanEditGrant(newDmp, user); // }//TODO assignGrandUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { assignFunderUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Project)) { assignProjectUserIfInternal(newDmp, user); } if(newDmp.getId() != null){ // for(DMPDatasetProfile dmpDatasetProfile : newDmp.getAssociatedDmps()){ //TODO // apiContext.getOperationsContext().getDatabaseRepository().getDmpDatasetProfileDao().createOrUpdate(dmpDatasetProfile); // } } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { // if (newDmp.getGrant() != null) { //TODO // apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().createOrUpdate(newDmp.getGrant()); // } } newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp); // for(DMPDatasetProfile dmpDatasetProfile : newDmp.getAssociatedDmps()){ //TODO // apiContext.getOperationsContext().getDatabaseRepository().getDmpDatasetProfileDao().createOrUpdate(dmpDatasetProfile); // } if (dataManagementPlan.getUsers() != null && !dataManagementPlan.getUsers().isEmpty()) { clearUsers(newDmp); for (DmpUser userListing : dataManagementPlan.getUsers()) { UserEntity tempUser = this.queryFactory.query(UserQuery.class).ids(userListing.getId()).first(); assignUser(dmp, tempUser, userListing.getRole()); } } // Dataset manipulation for when the DMP is set to be finalized. if (dataManagementPlan.getStatus() == DmpStatus.Finalized.getValue()) { if (dataManagementPlan.getDatasetsToBeFinalized() != null && !dataManagementPlan.getDatasetsToBeFinalized().isEmpty()) { apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(dataManagementPlan.getDatasetsToBeFinalized())) .update(root -> root.get("status"), DescriptionStatus.Finalized); apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(dataManagementPlan.getDatasetsToBeFinalized())) .update(root -> root.get("finalizedat"), new Date()); List datasetsToBeCanceled = new LinkedList<>(); for (DatasetWizardModel dataset : dataManagementPlan.getDatasets()) { if (!(dataset.getStatus() == DescriptionStatus.Finalized) && !dataManagementPlan.getDatasetsToBeFinalized().contains(dataset.getId())) { datasetsToBeCanceled.add(UUID.fromString(dataset.getId().toString())); } } if (!datasetsToBeCanceled.isEmpty()) apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled)) .update(root -> root.get("status"), DescriptionStatus.Canceled); } else { List datasetsToBeCanceled = new LinkedList<>(); for (DatasetWizardModel dataset : dataManagementPlan.getDatasets()) { if (!(dataset.getStatus() == DescriptionStatus.Finalized)) { datasetsToBeCanceled.add(dataset.getId()); } } if (!datasetsToBeCanceled.isEmpty()) apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled)) .update(root -> root.get("status"), DescriptionStatus.Canceled); } } // if (dataManagementPlan.getAssociatedUsers().size() == 0) // assignUser(newDmp, user); UUID dmpId = newDmp.getId(); //TODO // newDmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //this.updateIndex(newDmp); //TODO if (setNotification) { if (newDmp.getStatus().equals(DmpStatus.Finalized.getValue())) { this.sendNotification(newDmp, user/*, NotificationType.DMP_MODIFIED*/); } else { this.sendNotification(newDmp, user/*, , NotificationType.DMP_MODIFIED_FINALISED*/); } } return newDmp; } public DmpEntity createOrUpdateWithDatasets(DataManagementPlanEditorModel dataManagementPlan) throws Exception { if (dataManagementPlan.getId() != null) { DmpEntity dmp1 = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(dataManagementPlan.getId()); Instant dbTime = dmp1.getUpdatedAt().truncatedTo(ChronoUnit.SECONDS); Instant modelTime = Instant.ofEpochMilli(dataManagementPlan.getModified().getTime()).truncatedTo(ChronoUnit.SECONDS); if (!isUserOwnerOfDmp(dmp1)) { throw new Exception("User not being the creator is not authorized to edit this DMP."); } if (dbTime.toEpochMilli() != modelTime.toEpochMilli()) { throw new Exception("Another user have already edit that DMP."); } for (DatasetWizardModel dataset : dataManagementPlan.getDatasets()) { if (dataManagementPlan.getProfiles().stream().filter(associatedProfile -> dataset.getProfile().getId().equals(associatedProfile.getDescriptionTemplateId())).findAny().orElse(null) == null) throw new Exception("Dataset Template for Dataset Description is missing from the DMP."); } if (dataManagementPlan.getStatus() == (int) DmpStatus.Finalized.getValue() && dmp1.getStatus().equals(DmpStatus.Finalized.getValue())) throw new Exception("DMP is finalized, therefore cannot be edited."); } else { metricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT); } List descriptionEntities = new ArrayList<>(); DmpEntity tempDMP = dataManagementPlan.toDataModel(); if (tempDMP.getStatus().equals(DmpStatus.Finalized)) { checkDmpValidationRules(tempDMP); } UserEntity user = this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first(); DmpBlueprintEntity dmpBlueprint = this.queryFactory.query(DmpBlueprintQuery.class).ids(tempDMP.getBlueprintId()).first(); // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Organizations)) { // createOrganisationsIfTheyDontExist(tempDMP, apiContext.getOperationsContext().getDatabaseRepository().getOrganisationDao()); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Researchers)) { // createResearchersIfTheyDontExist(tempDMP, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao(), user); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { // createFunderIfItDoesntExist(tempDMP, apiContext.getOperationsContext().getDatabaseRepository().getFunderDao()); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { // createGrantIfItDoesntExist(tempDMP, apiContext.getOperationsContext().getDatabaseRepository().getGrantDao()); // }//TODO DmpEntity result = createOrUpdate(dataManagementPlan); for (DatasetWizardModel datasetWizardModel: dataManagementPlan.getDatasets()) { datasetWizardModel.setDmp(new DataManagementPlan().fromDataModel(result)); DescriptionEntity descriptionEntity = datasetManager.createOrUpdate(datasetWizardModel); descriptionEntities.add(descriptionEntity); } UUID dmpId = result.getId(); //result.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //TODO //this.updateIndex(result); //TODO return result; } public UUID newVersion(UUID uuid, DataManagementPlanNewVersionModel dataManagementPlan) throws Exception { DmpEntity oldDmp = databaseRepository.getDmpDao().find(uuid); if (!isUserOwnerOfDmp(oldDmp)) { throw new Exception("User not being the creator is not authorized to perform this action."); } DataManagementPlanCriteria criteria = new DataManagementPlanCriteria(); LinkedList list = new LinkedList<>(); list.push(oldDmp.getGroupId()); criteria.setGroupIds(list); criteria.setAllVersions(false); QueryableList dataManagementPlanQueryableList = databaseRepository.getDmpDao().getWithCriteria(criteria); List latestVersionDMP = dataManagementPlanQueryableList.toList(); if (latestVersionDMP.get(0).getVersion().equals(oldDmp.getVersion())) { DmpEntity newDmp = dataManagementPlan.toDataModel(); newDmp.setBlueprintId(oldDmp.getBlueprintId()); newDmp.setProperties(oldDmp.getProperties()); newDmp.setProperties(oldDmp.getProperties()); UserEntity user = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserInfoBuilder.class).id(this.userScope.getUserId()).build(); newDmp.setCreatorId(user.getId()); DmpBlueprintEntity dmpBlueprint = this.queryFactory.query(DmpBlueprintQuery.class).ids(newDmp.getBlueprintId()).first(); // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Organizations)) { // createOrganisationsIfTheyDontExist(newDmp, databaseRepository.getOrganisationDao()); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Researchers)) { // createResearchersIfTheyDontExist(newDmp, databaseRepository.getResearcherDao(), user); // }//TODO // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { // createFunderIfItDoesntExist(newDmp, databaseRepository.getFunderDao()); // }//TODO // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { // createGrantIfItDoesntExist(newDmp, databaseRepository.getGrantDao()); // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Project)) { // //TODO //// if (newDmp.getProject() != null && newDmp.getGrant() != null && (newDmp.getProject().getLabel() == null || newDmp.getProject().getLabel().trim().isEmpty())) { //// newDmp.setProject(newDmp.getProject().projectFromGrant(newDmp.getGrant())); //// } //// createProjectIfItDoesntExist(newDmp, databaseRepository.getProjectDao()); //TODO // } // }//TODO newDmp.setGroupId(oldDmp.getGroupId()); newDmp.setVersion((short)(oldDmp.getVersion() + 1)); newDmp.setId(null); if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { //TODO // if (newDmp.getGrant() != null && newDmp.getGrant().getType().equals(Grant.GrantType.INTERNAL.getValue())) { // checkIfUserCanEditGrant(newDmp, user); // } assignGrandUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { assignFunderUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Project)) { assignProjectUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { //TODO // if (newDmp.getGrant() != null) { // if (newDmp.getGrant().getStartdate() == null) { // newDmp.getGrant().setStartdate(new Date()); // } // if (newDmp.getGrant().getEnddate() == null) { // newDmp.getGrant().setEnddate(Date.from(Instant.now().plus(365, ChronoUnit.DAYS))); // } // databaseRepository.getGrantDao().createOrUpdate(newDmp.getGrant()); // } } DmpEntity tempDmp = databaseRepository.getDmpDao().createOrUpdate(newDmp); newDmp.setId(tempDmp.getId()); //TODO // for(DMPDatasetProfile dmpDatasetProfile : newDmp.getAssociatedDmps()){ // apiContext.getOperationsContext().getDatabaseRepository().getDmpDatasetProfileDao().createOrUpdate(dmpDatasetProfile); // } // Assign creator. //assignUser(newDmp, user); copyDatasets(newDmp, databaseRepository.getDatasetDao()); this.queryFactory.query(DmpUserQuery.class).dmpIds(oldDmp.getId()).collect() .stream().forEach(userDMP -> { DmpUserEntity temp = new DmpUserEntity(); temp.setUserId(userDMP.getUserId()); temp.setRole(userDMP.getRole()); temp.setDmpId(newDmp.getId()); // apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(temp); }); //TODO //newDmp.setUsers(new HashSet<>(databaseRepository.getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), newDmp.getId())).toList())); DatasetCriteria criteria1 = new DatasetCriteria(); criteria1.setDmpIds(Collections.singletonList(newDmp.getId())); //TODO //newDmp.setDataset(new HashSet<>(databaseRepository.getDatasetDao().getWithCriteria(criteria1).toList())); this.updateGroupIndex(newDmp.getGroupId()); return newDmp.getId(); } else { throw new DMPNewVersionException("Version to update not the latest."); } } public UUID clone(UUID uuid, DataManagementPlanNewVersionModel dataManagementPlan) throws Exception { DmpEntity oldDmp = databaseRepository.getDmpDao().find(uuid); DmpEntity newDmp = dataManagementPlan.toDataModel(); newDmp.setBlueprintId(oldDmp.getBlueprintId()); newDmp.setProperties(oldDmp.getProperties()); newDmp.setProperties(oldDmp.getProperties()); UserEntity user = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserInfoBuilder.class).id(this.userScope.getUserId()).build(); newDmp.setCreatorId(user.getId()); DmpBlueprintEntity dmpBlueprint = this.queryFactory.query(DmpBlueprintQuery.class).ids(newDmp.getBlueprintId()).first(); // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Organizations)) { // createOrganisationsIfTheyDontExist(newDmp, databaseRepository.getOrganisationDao()); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Researchers)) { // createResearchersIfTheyDontExist(newDmp, databaseRepository.getResearcherDao(), user); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { // createFunderIfItDoesntExist(newDmp, databaseRepository.getFunderDao()); // } // if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { // createGrantIfItDoesntExist(newDmp, databaseRepository.getGrantDao()); // //TODO //// if(this.dmpBlueprintService.fieldInBlueprint(newDmp.getProfile(), DmpBlueprintSystemFieldType.Project)) { //// if (newDmp.getProject() != null && newDmp.getGrant() != null && (newDmp.getProject().getLabel() == null || newDmp.getProject().getLabel().trim().isEmpty())) { //// newDmp.setProject(newDmp.getProject().projectFromGrant(newDmp.getGrant())); //// } //// createProjectIfItDoesntExist(newDmp, databaseRepository.getProjectDao()); //// } // }//TODO newDmp.setGroupId(UUID.randomUUID()); newDmp.setVersion((short)0); newDmp.setId(null); if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { //TODO // if (newDmp.getGrant() != null && newDmp.getGrant().getType().equals(Grant.GrantType.INTERNAL.getValue())) { // checkIfUserCanEditGrant(newDmp, user); // } assignGrandUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Funder)) { assignFunderUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Project)) { assignProjectUserIfInternal(newDmp, user); } if(this.dmpBlueprintService.fieldInBlueprint(dmpBlueprint, DmpBlueprintSystemFieldType.Grant)) { //TODO // if (newDmp.getGrant() != null) { // databaseRepository.getGrantDao().createOrUpdate(newDmp.getGrant()); // } } DmpEntity tempDmp = databaseRepository.getDmpDao().createOrUpdate(newDmp); newDmp.setId(tempDmp.getId()); //TODO // for(DMPDatasetProfile dmpDatasetProfile : newDmp.getAssociatedDmps()){ // apiContext.getOperationsContext().getDatabaseRepository().getDmpDatasetProfileDao().createOrUpdate(dmpDatasetProfile); // } assignUser(newDmp, user); copyDatasets(newDmp, databaseRepository.getDatasetDao()); //TODO // newDmp.setUsers(new HashSet<>(databaseRepository.getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), newDmp.getId())).toList())); DatasetCriteria criteria1 = new DatasetCriteria(); criteria1.setDmpIds(Collections.singletonList(newDmp.getId())); //TODO // newDmp.setDataset(new HashSet<>(databaseRepository.getDatasetDao().getWithCriteria(criteria1).toList())); UUID dmpId = newDmp.getId(); //TODO // newDmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //this.updateIndex(newDmp); //TODO metricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT); return newDmp.getId(); } public void delete(UUID uuid) throws DMPWithDatasetsDeleteException, IOException, InvalidApplicationException { DatasetCriteria criteria = new DatasetCriteria(); List dmpIds = Collections.singletonList(uuid); criteria.setDmpIds(dmpIds); if (apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao().getWithCriteria(criteria).toList().size() > 0) throw new DMPWithDatasetsDeleteException("You cannot Remove Datamanagement Plan with Datasets"); DmpEntity oldDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(uuid); switch (oldDmp.getStatus()) { case Draft: metricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.DRAFT); break; case Finalized: //TODO // if (oldDmp.getDois() != null && !oldDmp.getDois().isEmpty()) { // metricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.DOIED); // } // if (oldDmp.isPublic()) { // metricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.PUBLISHED); // } metricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.FINALIZED); break; } oldDmp.setIsActive(IsActive.Inactive); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(oldDmp); UUID dmpId = oldDmp.getId(); // oldDmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //TODO // this.updateIndex(oldDmp); //TODO DataManagementPlanCriteria criteria1 = new DataManagementPlanCriteria(); criteria1.setAllVersions(true); criteria1.setGroupIds(Collections.singletonList(oldDmp.getGroupId())); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getWithCriteria(criteria1).toList().forEach(dmp -> { //TODO // try { // UUID tdmpId = dmp.getId(); // //TODO // //dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), tdmpId)).toList())); // //this.updateIndex(dmp); //TODO // } catch (InvalidApplicationException e) { // logger.error(e.getMessage(), e); // } // for (DescriptionEntity descriptionEntity : dmp.getDataset()) { // // try { // List tags = new ArrayList<>(); // eu.eudat.elastic.entities.Dataset elastic = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(descriptionEntity.getId().toString()); // if (elastic != null) { // tags = elastic.getTags(); // } // //descriptionEntity.setDmpId(dmp.getId()); //TODO // this.datasetManager.updateTags(descriptionEntity, tags); // } catch (Exception e) { // logger.error(e.getMessage(), e); // } // } }); } // private void createResearchersIfTheyDontExist(DmpEntity newDmp, ResearcherDao researcherRepository, UserEntity user) throws InvalidApplicationException { // if (newDmp.getResearchers() != null && !newDmp.getResearchers().isEmpty()) { //TODO // for (eu.eudat.data.old.Researcher researcher : newDmp.getResearchers()) { // ResearcherCriteria criteria = new ResearcherCriteria(); // criteria.setReference(researcher.getReference()); // List entries = researcherRepository.getWithCriteria(criteria).toList(); // if (entries != null && !entries.isEmpty()) researcher.setId(entries.get(0).getId()); // else { // researcher.setCreationUser(user); // researcherRepository.createOrUpdate(researcher); // metricsManager.increaseValue(MetricNames.RESEARCHER, 1, null); // } // } // } // } // private void createOrganisationsIfTheyDontExist(DmpEntity newDmp, OrganisationDao organisationRepository) throws InvalidApplicationException { // if (newDmp.getOrganisations() != null && !newDmp.getOrganisations().isEmpty()) { //TODO // for (Organisation organisation : newDmp.getOrganisations()) { // boolean createNew = false; // if (organisation.getReference() != null) { // OrganisationCriteria criteria = new OrganisationCriteria(); // criteria.setLike(organisation.getReference()); // List entries = organisationRepository.getWithCriteria(criteria).toList(); // if (entries != null && !entries.isEmpty()) organisation.setId(entries.get(0).getId()); // else createNew = true; // } else { // createNew = true; // } // // if (createNew) { // if (organisation.getReference() == null) { // organisation.setReference("Internal:" + UUID.randomUUID().toString()); // } // organisationRepository.createOrUpdate(organisation); // } // } // } // } // private void createGrantIfItDoesntExist(DmpEntity newDmp, GrantDao grantDao) throws InvalidApplicationException { // if (newDmp.getGrant() != null) { //TODO // Grant grant = newDmp.getGrant(); // GrantCriteria criteria = new GrantCriteria(); // if (grant.getReference() != null) { // criteria.setReference(grant.getReference()); // Grant grantEntity = grantDao.getWithCriteria(criteria).toList().stream().max(Comparator.comparing(grant1 -> grant1.getModified().getTime())).orElse(null); // if (grantEntity != null) grant.setId(grantEntity.getId()); // else { // grant.setType(Grant.GrantType.EXTERNAL.getValue()); // grant.setCreationUser(null); // if (grant.getStartdate() == null) { // grant.setStartdate(new Date()); // } // if (grant.getEnddate() == null) { // grant.setEnddate(Date.from(Instant.now().plus(365, ChronoUnit.DAYS))); // } // grantDao.createOrUpdate(grant); // } // } // else { // grant.setType(Grant.GrantType.EXTERNAL.getValue()); // grant.setCreationUser(null); // grantDao.createOrUpdate(grant); // } // } // } // private void createFunderIfItDoesntExist(DmpEntity newDmp, FunderDao funderDao) throws InvalidApplicationException { // if (newDmp.getGrant() != null && newDmp.getGrant().getFunder() != null) { //TODO // Funder funder = newDmp.getGrant().getFunder(); // FunderCriteria criteria = new FunderCriteria(); // if (funder.getReference() != null) { // criteria.setReference(funder.getReference()); // Funder funderEntity = funderDao.getWithCritetia(criteria).toList().stream().max(Comparator.comparing(funder1 -> funder1.getModified().getTime())).orElse(null); // if (funderEntity != null) funder.setId(funderEntity.getId()); // else { // funderDao.createOrUpdate(funder); // } // } // else { // funderDao.createOrUpdate(funder); // } // } // } // private void createProjectIfItDoesntExist(DmpEntity newDmp, ProjectDao projectDao) throws InvalidApplicationException { //TODO // if (newDmp.getProject() != null) { // Project project = newDmp.getProject(); // ProjectCriteria criteria = new ProjectCriteria(); // if (project.getReference() != null) { // criteria.setReference(project.getReference()); // Project projectEntity = projectDao.getWithCritetia(criteria).toList().stream().max(Comparator.comparing(project1 -> project1.getModified().getTime())).orElse(null); // if (projectEntity != null) project.setId(projectEntity.getId()); // else { // createExternalProject(project, projectDao); // } // } // else { // createExternalProject(project, projectDao); // } // } // } // // private void createExternalProject(Project project, ProjectDao projectDao) { // if (project.getStartdate() == null) project.setStartdate(new Date()); // if (project.getEnddate() == null) project.setEnddate(new Date()); // project.setType(Project.ProjectType.EXTERNAL.getValue()); // if (project.getId() == null) project.setId(UUID.randomUUID()); // projectDao.createOrUpdate(project); // metricsManager.increaseValue(MetricNames.PROJECT, 1, null); // } private void copyDatasets(DmpEntity newDmp, DatasetDao datasetDao) throws InvalidApplicationException { List> futures = new LinkedList<>(); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(List.of(newDmp.getId())); for (DescriptionEntity descriptionEntity : this.databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).toList()) { DescriptionEntity tempDescriptionEntity = datasetDao.find(descriptionEntity.getId()); //TODO // try { // List tags = new ArrayList<>(); // eu.eudat.elastic.entities.Dataset elastic = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(descriptionEntity.getId().toString()); // if (elastic != null) { // tags = elastic.getTags(); // } // //TODO // //UUID dmpId = tempDescriptionEntity.getDmpId(); // //tempDescriptionEntity.getDmp().setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); // this.datasetManager.updateTags(tempDescriptionEntity, tags); // } catch (Exception e) { // logger.error(e.getMessage(), e); // } datasetDao.asQueryable().withHint(HintedModelFactory.getHint(DatasetListingModel.class)).where((builder, root) -> builder.equal(root.get("id"), descriptionEntity.getId())).getSingleAsync() .thenApplyAsync(entityDataset -> { DescriptionEntity newDescriptionEntity = new DescriptionEntity(); newDescriptionEntity.update(entityDataset); newDescriptionEntity.setStatus(DescriptionStatus.Draft); //TODO //newDescriptionEntity.setDmpId(newDmp.getId()); // if (newDescriptionEntity.getDatasetDataRepositories() != null) { // newDescriptionEntity.setDatasetDataRepositories(newDescriptionEntity.getDatasetDataRepositories().stream().map(item -> { // DataRepository dataRepository = new DataRepository(); // dataRepository.setId(item.getDataRepository().getId()); // // DatasetDataRepository datasetDataRepository = new DatasetDataRepository(); // datasetDataRepository.setDataRepository(dataRepository); // datasetDataRepository.setDataset(newDescriptionEntity); // datasetDataRepository.setData(item.getData()); // return datasetDataRepository; // }).collect(Collectors.toSet())); // } // // if (newDescriptionEntity.getDatasetExternalDatasets() != null) { // newDescriptionEntity.setDatasetExternalDatasets(newDescriptionEntity.getDatasetExternalDatasets().stream().map(item -> { // ExternalDataset externalDataset = new ExternalDataset(); // externalDataset.setId(item.getExternalDataset().getId()); // DatasetExternalDataset datasetExternalDataset = new DatasetExternalDataset(); // datasetExternalDataset.setExternalDataset(externalDataset); // datasetExternalDataset.setDataset(newDescriptionEntity); // datasetExternalDataset.setData(item.getData()); // return datasetExternalDataset; // }).collect(Collectors.toSet())); // } // // if (newDescriptionEntity.getRegistries() != null) { // newDescriptionEntity.setRegistries(newDescriptionEntity.getRegistries().stream().map(item -> { // Registry registry = new Registry(); // registry.setId(item.getId()); // return registry; // }).collect(Collectors.toSet())); // } // // if (newDescriptionEntity.getServices() != null) { // newDescriptionEntity.setServices(newDescriptionEntity.getServices().stream().map(item -> { // Service service = new Service(); // service.setId(item.getId()); // DatasetService datasetService = new DatasetService(); // datasetService.setService(service); // datasetService.setDataset(newDescriptionEntity); // datasetService.setData(item.getData()); // return datasetService; // }).collect(Collectors.toSet())); // } newDescriptionEntity.setCreatedAt(Instant.now()); return newDescriptionEntity; }).thenApplyAsync(item -> { futures.add(datasetDao.createOrUpdateAsync(item).whenComplete(((dataset1, throwable) -> { DmpEntity dmp = null; try { dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(this.queryFactory.query(DmpDescriptionTemplateQuery.class).ids(dataset1.getDmpDescriptionTemplateId()).isActive(IsActive.Active).first().getDmpId()); } catch (InvalidApplicationException e) { throw new RuntimeException(e); } metricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.DRAFT); //TODO // eu.eudat.elastic.entities.Dataset datasetElastic = new eu.eudat.elastic.entities.Dataset(); // datasetElastic.setId(dataset1.getId().toString()); // datasetElastic.setLabel(dataset1.getLabel()); // datasetElastic.setDescription(dataset1.getDescription()); // datasetElastic.setTemplate(this.queryFactory.query(DmpDescriptionTemplateQuery.class).ids(dataset1.getDmpDescriptionTemplateId()).isActive(IsActive.Active).first().getDescriptionTemplateId()); // datasetElastic.setDmp(dmp.getId()); // datasetElastic.setStatus(dataset1.getStatus().getValue()); // datasetElastic.setGroup(dmp.getGroupId()); // if(this.dmpBlueprintService.fieldInBlueprint(dmp.getProfile(), DmpBlueprintSystemFieldType.Grant)) { // datasetElastic.setGrant(dmp.getGrant().getId()); // } // if (dmp.getUsers() != null) { // datasetElastic.setCollaborators(dmp.getUsers().stream().map(user -> { // Collaborator collaborator = new Collaborator(); // collaborator.setId(user.getId().toString()); // collaborator.setRole(user.getRole()); // // collaborator.setName(user.getUser().getName()); // return collaborator; // }).collect(Collectors.toList())); // } // datasetElastic.setLastVersion(true); // datasetElastic.setLastPublicVersion(false); // if (dmp.getOrganisations() != null) { // datasetElastic.setOrganizations(dmp.getOrganisations().stream().map(org -> { // Organization organization = new Organization(); // organization.setId(org.getId().toString()); // organization.setName(org.getLabel()); // return organization; // }).collect(Collectors.toList())); // } // datasetElastic.setPublic(dmp.isPublic()); // if(this.dmpBlueprintService.fieldInBlueprint(dmp.getProfile(), DmpBlueprintSystemFieldType.Grant)) { // datasetElastic.setGrantStatus(dmp.getGrant().getStatus()); // } // // try { // eu.eudat.elastic.entities.Dataset oldDatasetElastic = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(descriptionEntity.getId().toString()); // if (oldDatasetElastic != null) { // datasetElastic.setTags(oldDatasetElastic.getTags()); // } // datasetElastic.setFormData(this.datasetManager.getWordDocumentText(dataset1)); // apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().createOrUpdate(datasetElastic); // } catch (Exception e) { // logger.error(e.getMessage(), e); // } }))); return futures; }).join(); } } public void makePublic(UUID id) throws Exception { DmpEntity dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id); // Check if dmp is finalized and if user is owner. if (!isUserOwnerOfDmp(dmp)) throw new Exception("User does not have the privilege to do this action."); if (!dmp.getStatus().equals(DmpStatus.Finalized)) throw new Exception("DMP is not finalized"); dmp.setAccessType(DmpAccessType.Public); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp); UUID dmpId = dmp.getId(); // dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //TODO //this.updateIndex(dmp); //TODO metricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.PUBLISHED); DataManagementPlanCriteria criteria = new DataManagementPlanCriteria(); criteria.setGroupIds(Collections.singletonList(dmp.getGroupId())); criteria.setAllVersions(true); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getWithCriteria(criteria).toList().stream().forEach(dmp1 -> { // dmp1.getDataset().forEach(dataset -> { //TODO // try { // List tags = new ArrayList<>(); // eu.eudat.elastic.entities.Dataset elastic = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(dataset.getId().toString()); // if (elastic != null) { // tags = elastic.getTags(); // } // //TODO // //UUID tmdmpId = dataset.getDmpId(); // //dataset.getDmp().setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), tmdmpId)).toList())); // this.datasetManager.updateTags(dataset, tags); // metricsManager.increaseValue(MetricNames.DATASET, 1, MetricNames.PUBLISHED); // } catch (Exception e) { // logger.error(e.getMessage(), e); // } // }); }); UserEntity user = this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first(); sendNotification(dmp, user/*, , NotificationType.DMP_PUBLISH*/); } @Transactional public void makeFinalize(UUID id, DatasetsToBeFinalized datasetsToBeFinalized) throws Exception { DmpEntity dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id); if (!isUserOwnerOfDmp(dmp)) throw new Exception("User does not have the privilege to do this action."); if (dmp.getStatus().equals(DmpStatus.Finalized)) throw new Exception("DMP is already finalized"); List indexDescriptionEntities = new ArrayList<>(); if (datasetsToBeFinalized != null && datasetsToBeFinalized.getUuids() != null && !datasetsToBeFinalized.getUuids().isEmpty()) { List finalizedDescriptionEntities = apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeFinalized.getUuids())) .toList(); for (DescriptionEntity descriptionEntity : finalizedDescriptionEntities) { DescriptionStatus status = descriptionEntity.getStatus(); Instant finalizedDate = descriptionEntity.getFinalizedAt(); descriptionEntity.setStatus(DescriptionStatus.Finalized); descriptionEntity.setFinalizedAt(Instant.now()); DatasetWizardModel wizardModel = new DatasetWizardModel(); wizardModel = wizardModel.fromDataModel(descriptionEntity); wizardModel.setDatasetProfileDefinition(this.datasetManager.getPagedProfile(wizardModel, descriptionEntity)); try { datasetManager.createOrUpdate(wizardModel); } catch (Exception e) { descriptionEntity.setStatus(status); descriptionEntity.setFinalizedAt(finalizedDate); throw e; } descriptionEntity.setUpdatedAt(Instant.now()); } /*apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeFinalized.getUuids())) .update(root -> root.get("finalizedAt"), new Date());*/ //List finalizedDatasets = dmp.getDataset().stream().filter(dataset -> datasetsToBeFinalized.getUuids().contains(dataset.getId())).collect(Collectors.toList()); /* finalizedDatasets.forEach(dataset ->{ dataset.setStatus(Dataset.Status.FINALISED.getValue()); dataset.setFinalizedAt(new Date()); dataset.setModified(new Date()); } );*/ indexDescriptionEntities.addAll(finalizedDescriptionEntities); List datasetsToBeCanceled = new LinkedList<>(); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(List.of(dmp.getId())); List descriptionEntityList = this.databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).toList().stream().collect(Collectors.toList()); for (DescriptionEntity descriptionEntity : descriptionEntityList) { if (!descriptionEntity.getStatus().equals(DescriptionStatus.Finalized) && !datasetsToBeFinalized.getUuids().contains(descriptionEntity.getId())) { datasetsToBeCanceled.add(descriptionEntity.getId()); } } if (!datasetsToBeCanceled.isEmpty()) { apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled)) .update(root -> root.get("status"), DescriptionStatus.Canceled); List cancelledDescriptionEntities = descriptionEntityList.stream().filter(dataset -> datasetsToBeCanceled.contains(dataset.getId())).collect(Collectors.toList()); cancelledDescriptionEntities.forEach(dataset -> dataset.setStatus(DescriptionStatus.Canceled)); indexDescriptionEntities.addAll(cancelledDescriptionEntities); } } else { List datasetsToBeCanceled = new LinkedList<>(); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(List.of(dmp.getId())); List descriptionEntityList = this.databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).toList().stream().collect(Collectors.toList()); for (DescriptionEntity descriptionEntity : descriptionEntityList) { if (!descriptionEntity.getStatus().equals(DescriptionStatus.Finalized)) { datasetsToBeCanceled.add(descriptionEntity.getId()); } } if (!datasetsToBeCanceled.isEmpty()) { apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao() .asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled)) .update(root -> root.get("status"), DescriptionStatus.Canceled); List cancelledDescriptionEntities = descriptionEntityList.stream().filter(dataset -> datasetsToBeCanceled.contains(dataset.getId())).collect(Collectors.toList()); cancelledDescriptionEntities.forEach(dataset -> dataset.setStatus(DescriptionStatus.Canceled)); indexDescriptionEntities.addAll(cancelledDescriptionEntities); } } dmp.setStatus(DmpStatus.Finalized); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp); UUID dmpId = dmp.getId(); // dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //TODO //this.updateIndex(dmp); //TODO UserEntity user = this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first(); sendNotification(dmp, user/*, , NotificationType.DMP_FINALISED*/); metricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.DRAFT); metricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.FINALIZED); //this.updateDatasetsIndex(indexDescriptionEntities); //TODO metricsManager.decreaseValue(MetricNames.DATASET, indexDescriptionEntities.size(), MetricNames.DRAFT); metricsManager.increaseValue(MetricNames.DATASET, indexDescriptionEntities.size(), MetricNames.FINALIZED); } public void undoFinalize(UUID id) throws Exception { DmpEntity dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id); if (!isUserOwnerOfDmp(dmp)) throw new Exception("User does not have the privilege to do this action."); if (dmp.getIsActive().equals(IsActive.Inactive)) throw new Exception("DMP is already Active"); if (dmp.getAccessType().equals(DmpAccessType.Public)) throw new Exception("DMP is publicly available"); // if (!dmp.getDois().isEmpty())//TODO // throw new Exception("DMP is deposited"); dmp.setStatus(DmpStatus.Draft); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp); UUID dmpId = dmp.getId(); // dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //TODO //this.updateIndex(dmp); //TODO metricsManager.decreaseValue(MetricNames.DMP, 1, MetricNames.FINALIZED); metricsManager.increaseValue(MetricNames.DMP, 1, MetricNames.DRAFT); } public void updateUsers(UUID id, List users) throws Exception { DmpEntity dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id); if (!isUserOwnerOfDmp(dmp)) throw new Exception("User does not have the privilege to do this action."); clearUsers(dmp); for (DmpUser userListing : users) { UserEntity tempUser = this.queryFactory.query(UserQuery.class).ids(userListing.getId()).first(); assignUser(dmp, tempUser, userListing.getRole()); } } /* * Export Data * */ public FileEnvelope getWordDocument(String id, ConfigLoader configLoader) throws IOException, InvalidApplicationException { return this.getWordDocument(id, configLoader, true); } public FileEnvelope getWordDocument(String id, ConfigLoader configLoader, Boolean versioned) throws IOException, InvalidApplicationException { WordBuilder wordBuilder = new WordBuilder(this.environment, configLoader); VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl(); DatasetWizardModel dataset = new DatasetWizardModel(); XWPFDocument document = configLoader.getDocument(); DmpEntity dmpEntity = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (!dmpEntity.getAccessType().equals(DmpAccessType.Public) //TODO && dmpEntity.getUsers().stream().filter(userInfo -> userInfo.getUser().getId().equals(this.userScope.getUserIdSafe())).collect(Collectors.toList()).size() == 0 ) throw new UnauthorisedException(); wordBuilder.fillFirstPage(dmpEntity, null, document, false); // int powered_pos = document.getParagraphs().size() - 3; int powered_pos = wordBuilder.findPosOfPoweredBy(document); XWPFParagraph powered_par = null; XWPFParagraph argos_img_par = null; if(powered_pos != -1) { powered_par = document.getParagraphArray(powered_pos); argos_img_par = document.getParagraphArray(powered_pos + 1); } // // DMP info on top of the document. // wordBuilder.addParagraphContent("Data Management Plan Information", document, ParagraphStyle.HEADER1, BigInteger.ZERO); // // DMP title custom style. // wordBuilder.addParagraphContent(dmpEntity.getLabel(), document, ParagraphStyle.HEADER2, BigInteger.ZERO); // wordBuilder.addParagraphContent(dmpEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO); // // wordBuilder.addParagraphContent("Funder", document, ParagraphStyle.HEADER3, BigInteger.ZERO); // if (dmpEntity.getGrant().getFunder() != null) // wordBuilder.addParagraphContent(dmpEntity.getGrant().getFunder().getLabel(), document, ParagraphStyle.TEXT, BigInteger.ZERO); // // wordBuilder.addParagraphContent("Grant", document, ParagraphStyle.HEADER3, BigInteger.ZERO); // wordBuilder.addParagraphContent(dmpEntity.getGrant().getLabel(), document, ParagraphStyle.TEXT, BigInteger.ZERO); // // wordBuilder.addParagraphContent("Organisations", document, ParagraphStyle.HEADER3, BigInteger.ZERO); // if (dmpEntity.getOrganisations().size() > 0) { // wordBuilder.addParagraphContent(dmpEntity.getOrganisations().stream().map(Organisation::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.TEXT, BigInteger.ZERO); // } // // wordBuilder.addParagraphContent("Researchers", document, ParagraphStyle.HEADER3, BigInteger.ZERO); // if (dmpEntity.getResearchers().size() > 0) { // wordBuilder.addParagraphContent(dmpEntity.getResearchers().stream().map(Researcher::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.TEXT, BigInteger.ZERO); // } // // /*wordBuilder.addParagraphContent("DMP Profile", document, ParagraphStyle.HEADER2, BigInteger.ZERO); // if (dmpEntity.getProfile() != null){ // wordBuilder.addParagraphContent(dmpEntity.getProfile().getLabel(), document, ParagraphStyle.TEXT, BigInteger.ZERO); // }*/ // // // Page break at the end of the DMP title. // XWPFParagraph parBreakDMP = document.createParagraph(); // parBreakDMP.setPageBreak(true); // // wordBuilder.addParagraphContent("Datasets", document, ParagraphStyle.HEADER1, BigInteger.ZERO); // // Space below Datasets. // XWPFParagraph parBreakDatasets = document.createParagraph(); DefinitionEntity dmpBlueprint = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, this.queryFactory.query(DmpBlueprintQuery.class).ids(dmpEntity.getBlueprintId()).first().getDefinition()); for(SectionEntity section: dmpBlueprint.getSections()){ wordBuilder.addParagraphContent("Section " + section.getOrdinal(), document, ParagraphStyle.HEADER1, BigInteger.ZERO, 0); XWPFParagraph sectionInfoParagraph = document.createParagraph(); sectionInfoParagraph.setSpacingBetween(1.0); XWPFRun runSectionTitle = sectionInfoParagraph.createRun(); runSectionTitle.setText("Title: "); runSectionTitle.setColor("000000"); XWPFRun runSectionTitleText = sectionInfoParagraph.createRun(); runSectionTitleText.setText(section.getLabel()); runSectionTitleText.setColor("116a78"); XWPFParagraph sectionDescriptionParagraph = document.createParagraph(); XWPFRun runSectionDescription = sectionDescriptionParagraph.createRun(); runSectionDescription.setText("Description: "); runSectionDescription.setColor("000000"); XWPFRun runSectionDescriptionText = sectionDescriptionParagraph.createRun(); runSectionDescriptionText.setText(section.getDescription()); runSectionDescriptionText.setColor("116a78"); wordBuilder.addParagraphContent("Section Fields", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0); section.getFields().sort(Comparator.comparingInt(FieldEntity::getOrdinal)); for(FieldEntity field: section.getFields()){ if(field.getCategory() == DmpBlueprintFieldCategory.System){ SystemFieldEntity systemField = (SystemFieldEntity) field; XWPFParagraph systemFieldParagraph = document.createParagraph(); systemFieldParagraph.setSpacingBetween(1.0); XWPFRun runSyStemFieldTitle = systemFieldParagraph.createRun(); runSyStemFieldTitle.setText("Title: "); runSyStemFieldTitle.setColor("000000"); XWPFRun runSystemFieldTitleText = systemFieldParagraph.createRun(); runSystemFieldTitleText.setText(systemField.getLabel()); runSystemFieldTitleText.setColor("116a78"); if(systemField.getDescription() != null && !systemField.getDescription().isEmpty()){ XWPFParagraph systemFieldDescription = document.createParagraph(); systemFieldDescription.setSpacingBetween(1.0); XWPFRun runSyStemFieldDescription = systemFieldDescription.createRun(); runSyStemFieldDescription.setText("Description: "); runSyStemFieldDescription.setColor("000000"); XWPFRun runSystemFieldDescriptionText = systemFieldDescription.createRun(); runSystemFieldDescriptionText.setText(systemField.getDescription()); runSystemFieldDescriptionText.setColor("116a78"); } XWPFParagraph systemFieldInput = document.createParagraph(); systemFieldInput.setSpacingBetween(1.0); XWPFRun runInput = systemFieldInput.createRun(); runInput.setText("Input: "); runInput.setColor("000000"); switch (systemField.getType()) { case Text: XWPFRun runTitle = systemFieldInput.createRun(); runTitle.setText(dmpEntity.getLabel()); runTitle.setColor("116a78"); break; case HtmlText: XWPFRun runDescription = systemFieldInput.createRun(); runDescription.setText(dmpEntity.getDescription()); runDescription.setColor("116a78"); break; case Researchers: //TODO // for(Researcher researcher: dmpEntity.getResearchers()){ // XWPFRun runResearcher = systemFieldInput.createRun(); // runResearcher.setText("• " + researcher.getLabel()); // runResearcher.setColor("116a78"); // } break; //TODO // case Organizations: // // for(Organisation organisation: dmpEntity.getOrganisations()){ // XWPFRun runOrganisation = systemFieldInput.createRun(); // runOrganisation.setText("• " + organisation.getLabel()); // runOrganisation.setColor("116a78"); // } // break; // case Language: // XWPFRun runLanguage = systemFieldInput.createRun(); // runLanguage.setText(objectMapper.readValue(dmpEntity.getExtraProperties(), HashMap.class).get("language").toString()); // runLanguage.setColor("116a78"); // break; // case Contact: // XWPFRun runContact = systemFieldInput.createRun(); // runContact.setText(dmpEntity.getCreator().getName()); // runContact.setColor("116a78"); // break; // case Funder: // if (dmpEntity.getGrant() != null && dmpEntity.getGrant().getFunder() != null) { // XWPFRun runFunder = systemFieldInput.createRun(); // runFunder.setText(dmpEntity.getGrant().getFunder().getLabel()); // runFunder.setColor("116a78"); // } // break; // case Grant: // if (dmpEntity.getGrant() != null) { // XWPFRun runGrant = systemFieldInput.createRun(); // runGrant.setText(dmpEntity.getGrant().getLabel()); // runGrant.setColor("116a78"); // } // break; // case Project: // if (dmpEntity.getProject() != null ) { // XWPFRun runProject = systemFieldInput.createRun(); // runProject.setText(dmpEntity.getProject().getLabel()); // runProject.setColor("116a78"); // } // break; // case License: // Map extraProperties = objectMapper.readValue(dmpEntity.getExtraProperties(), HashMap.class); // if (extraProperties.containsKey("license")) { // XWPFRun runLicense = systemFieldInput.createRun(); // runLicense.setText(extraProperties.get("license").toString()); // runLicense.setColor("116a78"); // } // break; // case AccessRights: // Map extraPropertiesMap = objectMapper.readValue(dmpEntity.getExtraProperties(), HashMap.class); // if (extraPropertiesMap.containsKey("visible")) { // XWPFRun runAccessRights = systemFieldInput.createRun(); // runAccessRights.setText(extraPropertiesMap.get("visible").toString()); // runAccessRights.setColor("116a78"); // } // break; } document.createParagraph(); } else if(field.getCategory() == DmpBlueprintFieldCategory.Extra){ ExtraFieldEntity extraField = (ExtraFieldEntity)field; XWPFParagraph extraFieldParagraph = document.createParagraph(); extraFieldParagraph.setSpacingBetween(1.0); XWPFRun runExtraFieldLabel = extraFieldParagraph.createRun(); runExtraFieldLabel.setText(extraField.getLabel()); runExtraFieldLabel.setColor("116a78"); if(extraField.getDescription() != null && !extraField.getDescription().isEmpty()){ XWPFRun runExtraFieldDescription = extraFieldParagraph.createRun(); runExtraFieldDescription.setText(extraField.getDescription()); runExtraFieldDescription.setColor("116a78"); } XWPFRun runExtraFieldInput = extraFieldParagraph.createRun(); Map dmpProperties = objectMapper.readValue(dmpEntity.getProperties(), HashMap.class); if (dmpProperties.containsKey(field.getId()) && dmpProperties.get(field.getId()) != null) { runExtraFieldInput.setText((String) dmpProperties.get(field.getId())); } runExtraFieldInput.setColor("116a78"); } } if(!section.getDescriptionTemplates().isEmpty()){ wordBuilder.addParagraphContent("Section descriptions", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0); wordBuilder.addParagraphContent("Description Templates", document, ParagraphStyle.HEADER4, BigInteger.ZERO, 0); for(eu.eudat.commons.types.dmpblueprint.DescriptionTemplateEntity descriptionTemplateEntity : section.getDescriptionTemplates()){ XWPFParagraph templateParagraph = document.createParagraph(); XWPFRun runTemplateLabel = templateParagraph.createRun(); runTemplateLabel.setText("• " + descriptionTemplateEntity.getLabel()); runTemplateLabel.setColor("116a78"); } final Boolean isFinalized = dmpEntity.getStatus().equals(DmpStatus.Finalized); final Boolean isPublic = dmpEntity.getAccessType().equals(DmpAccessType.Public); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(Collections.singletonList(dmpEntity.getId())); List descriptionEntityList = this.databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).toList().stream().collect(Collectors.toList()); descriptionEntityList.stream() .filter(item -> item.getStatus() != DescriptionStatus.Canceled) .filter(item -> item.getIsActive() != IsActive.Inactive) .filter(item -> !isPublic && !isFinalized || item.getStatus() == DescriptionStatus.Finalized) //.filter(item -> item.getDmpSectionIndex().equals(section.getOrdinal() - 1)) //TODO .sorted(Comparator.comparing(DescriptionEntity::getCreatedAt)) .forEach(datasetEntity -> { Map properties = new HashMap<>(); if (datasetEntity.getProperties() != null) { //ObjectMapper objectMapper = new ObjectMapper(); try { properties = objectMapper.readValue(datasetEntity.getProperties(), LinkedHashMap.class); } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); } /*JSONObject jObject = new JSONObject(datasetEntity.getProperties()); properties = jObject.toMap();*/ } // Dataset Description custom style. XWPFParagraph datasetDescriptionParagraph = document.createParagraph(); datasetDescriptionParagraph.setStyle("Heading4"); datasetDescriptionParagraph.setSpacingBetween(1.5); XWPFRun datasetDescriptionRun = datasetDescriptionParagraph.createRun(); datasetDescriptionRun.setText("Dataset Description"); //datasetDescriptionRun.setColor("2E75B6"); //datasetDescriptionRun.setBold(true); datasetDescriptionRun.setFontSize(15); // Custom style for the Dataset title. //wordBuilder.addParagraphContent("Title: " + datasetEntity.getLabel(), document, ParagraphStyle.HEADER1, BigInteger.ZERO); XWPFParagraph datasetLabelParagraph = document.createParagraph(); // datasetLabelParagraph.setStyle("Heading2"); datasetLabelParagraph.setSpacingBetween(1.0); XWPFRun runDatasetTitle1 = datasetLabelParagraph.createRun(); runDatasetTitle1.setText("Title: "); runDatasetTitle1.setColor("000000"); //runDatasetTitle1.setBold(true); //runDatasetTitle1.setFontSize(12); XWPFRun runDatasetTitle = datasetLabelParagraph.createRun(); runDatasetTitle.setText(datasetEntity.getLabel()); runDatasetTitle.setColor("116a78"); //runDatasetTitle.setBold(true); //runDatasetTitle.setFontSize(12); XWPFParagraph datasetTemplateParagraph = document.createParagraph(); // datasetTemplateParagraph.setStyle("Heading3"); XWPFRun runDatasetTemplate1 = datasetTemplateParagraph.createRun(); runDatasetTemplate1.setText("Template: "); runDatasetTemplate1.setColor("000000"); //runDatasetTemplate1.setBold(true); //runDatasetTemplate1.setFontSize(12); XWPFRun runDatasetTemplate = datasetTemplateParagraph.createRun(); runDatasetTemplate.setText(this.queryFactory.query(DescriptionTemplateQuery.class).ids(datasetEntity.getDescriptionTemplateId()).first().getLabel()); runDatasetTemplate.setColor("116a78"); //runDatasetTemplate.setBold(true); //runDatasetTemplate.setFontSize(12); // /*XWPFParagraph externalReferencesParagraph = document.createParagraph(); // externalReferencesParagraph.setStyle("Heading3"); // XWPFRun externalReferencesRun = externalReferencesParagraph.createRun(); // externalReferencesRun.setText("External References"); // externalReferencesRun.setColor("2E75B6"); // externalReferencesRun.setBold(true); // externalReferencesRun.setFontSize(12); // // wordBuilder.addParagraphContent("Data Repositories", document, ParagraphStyle.HEADER4, BigInteger.ZERO); // if (datasetEntity.getDatasetDataRepositories().size() > 0) { // wordBuilder.addParagraphContent(datasetEntity.getDatasetDataRepositories().stream().map(DatasetDataRepository::getDataRepository).map(DataRepository::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.TEXT, BigInteger.ZERO); // } // wordBuilder.addParagraphContent("External Datasets", document, ParagraphStyle.HEADER4, BigInteger.ZERO); // if (datasetEntity.getDatasetExternalDatasets().size() > 0) { // wordBuilder.addParagraphContent(datasetEntity.getDatasetExternalDatasets().stream().map(DatasetExternalDataset::getExternalDataset).map(ExternalDataset::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.TEXT, BigInteger.ZERO); // } // wordBuilder.addParagraphContent("Registries", document, ParagraphStyle.HEADER4, BigInteger.ZERO); // if (datasetEntity.getRegistries().size() > 0) { // wordBuilder.addParagraphContent(datasetEntity.getRegistries().stream().map(Registry::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.TEXT, BigInteger.ZERO); // } // wordBuilder.addParagraphContent("Services", document, ParagraphStyle.HEADER4, BigInteger.ZERO); // if (datasetEntity.getServices().size() > 0) { // wordBuilder.addParagraphContent(datasetEntity.getServices().stream().map(DatasetService::getService).map(Service::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.TEXT, BigInteger.ZERO); // } // *//*wordBuilder.addParagraphContent("Tags", document, ParagraphStyle.HEADER3, BigInteger.ZERO); // if (datasetEntity.().size() > 0) { // wordBuilder.addParagraphContent(datasetEntity.getServices().stream().map(DatasetService::getService).map(Service::getLabel).collect(Collectors.joining(", ")) // , document, ParagraphStyle.HEADER4, BigInteger.ZERO); // }*/ // // XWPFParagraph datasetDescParagraph = document.createParagraph(); XWPFRun runDatasetDescription1 = datasetDescParagraph.createRun(); runDatasetDescription1.setText("Description: "); runDatasetDescription1.setColor("000000"); XWPFRun runDatasetDescription = datasetDescParagraph.createRun(); runDatasetDescription.setText(this.queryFactory.query(DescriptionTemplateQuery.class).ids(datasetEntity.getDescriptionTemplateId()).first().getLabel()); runDatasetDescription.setColor("116a78"); //wordBuilder.addParagraphContent(datasetEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO, 0); document.createParagraph(); PagedDatasetProfile pagedDatasetProfile = datasetManager.getPagedProfile(dataset, datasetEntity); visibilityRuleService.setProperties(properties); visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules()); try { wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService); } catch (IOException e) { logger.error(e.getMessage(), e); } // Page break at the end of the Dataset. XWPFParagraph parBreakDataset = document.createParagraph(); parBreakDataset.setPageBreak(true); }); } } // // Removes the top empty headings. // for (int i = 0; i < 6; i++) { // document.removeBodyElement(0); // } if(powered_pos != -1) { document.getLastParagraph().setPageBreak(false); document.createParagraph(); document.setParagraph(powered_par, document.getParagraphs().size() - 1); document.createParagraph(); document.setParagraph(argos_img_par, document.getParagraphs().size() - 1); document.removeBodyElement(powered_pos + 1); document.removeBodyElement(powered_pos + 1); } wordBuilder.fillFooter(dmpEntity, null, document, false); String fileName =""; //TODO // if (dmpEntity.getGrant() != null && dmpEntity.getGrant().getLabel() != null) { // fileName = "DMP_" + dmpEntity.getGrant().getLabel(); // } // else { // fileName = "DMP_" + dmpEntity.getLabel(); // } // if (versioned) { // fileName += "_" + dmpEntity.getVersion(); // } // fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", ""); FileEnvelope exportEnvelope = new FileEnvelope(); exportEnvelope.setFilename(fileName + ".docx"); String uuid = UUID.randomUUID().toString(); File exportFile = new File(this.environment.getProperty("temp.temp") + uuid + ".docx"); FileOutputStream out = new FileOutputStream(exportFile); document.write(out); out.close(); exportEnvelope.setFile(exportFile); return exportEnvelope; } private FileEnvelope getXmlDocument(String id) throws IOException, InvalidApplicationException { ExportXmlBuilder xmlBuilder = new ExportXmlBuilder(); VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl(); DmpEntity dmp = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (!dmp.getAccessType().equals(DmpAccessType.Public) //TODO && dmp.getUsers().stream().filter(userInfo -> this.userScope.getUserIdSafe().equals(userInfo.getUser().getId())).collect(Collectors.toList()).size() == 0 ) throw new UnauthorisedException(); final Boolean isFinalized = dmp.getStatus().equals(DmpStatus.Finalized); final Boolean isPublic = dmp.getAccessType().equals(DmpAccessType.Public); DatasetCriteria datasetCriteria = new DatasetCriteria(); datasetCriteria.setDmpIds(List.of(dmp.getId())); List descriptionEntityList = this.databaseRepository.getDatasetDao().getWithCriteria(datasetCriteria).toList().stream().filter(dataset -> dataset.getIsActive() != IsActive.Inactive).collect(Collectors.toList()); List descriptionEntities = descriptionEntityList.stream() .filter(dataset -> dataset.getIsActive() != IsActive.Inactive && dataset.getStatus() != DescriptionStatus.Canceled) .filter(dataset -> !isPublic && !isFinalized || dataset.getStatus() == DescriptionStatus.Finalized) .collect(Collectors.toList()); /*String fileName = dmp.getLabel(); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");*/ String uuid = UUID.randomUUID().toString(); File xmlFile = new File(this.environment.getProperty("temp.temp") + uuid + ".xml"); BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true)); Document xmlDoc = XmlBuilder.getDocument(); Element dmpElement = xmlDoc.createElement("dmp"); Element dmpDescription = xmlDoc.createElement("description"); dmpDescription.setTextContent(dmp.getDescription()); dmpElement.appendChild(dmpDescription); Element dmpName = xmlDoc.createElement("dmpName"); dmpName.setTextContent(dmp.getLabel()); dmpElement.appendChild(dmpName); //TODO // if (dmp.getExtraProperties() != null && !dmp.getExtraProperties().isEmpty()) { // Map extraProperties = new ObjectMapper().readValue(dmp.getExtraProperties(), HashMap.class); // Element language = xmlDoc.createElement("language"); // language.setTextContent(extraProperties.get("language") != null ? extraProperties.get("language").toString() : null); // dmpElement.appendChild(language); // Element visibility = xmlDoc.createElement("visibility"); // visibility.setTextContent(extraProperties.get("visible") != null ? extraProperties.get("visible").toString() : null); // dmpElement.appendChild(visibility); // Element publicDate = xmlDoc.createElement("publicDate"); // publicDate.setTextContent(extraProperties.get("publicDate") != null ? extraProperties.get("publicDate").toString() : null); // dmpElement.appendChild(publicDate); // Element costs = xmlDoc.createElement("costs"); // costs.setTextContent(extraProperties.get("costs") != null ? extraProperties.get("costs").toString() : null); // dmpElement.appendChild(costs); // } DmpBlueprintEntity dmpBlueprint = this.queryFactory.query(DmpBlueprintQuery.class).ids(dmp.getBlueprintId()).first(); Element dmpProfileElement = xmlDoc.createElement("dmpProfile"); Element dmpProfileName = xmlDoc.createElement("dmpProfileName"); if (!(dmpBlueprint == null)) { dmpProfileName.setTextContent(dmpBlueprint.getLabel()); dmpProfileElement.appendChild(dmpProfileName); Element dmpProfileId = xmlDoc.createElement("dmpProfileId"); dmpProfileId.setTextContent(dmpBlueprint.getId().toString()); dmpProfileElement.appendChild(dmpProfileId); // Element values = xmlDoc.createElement("values"); // values.setTextContent(dmpProfile.getDefinition()); // dmpProfileElement.appendChild(values); } dmpElement.appendChild(dmpProfileElement); Element dmpContactElement = xmlDoc.createElement("contact"); Element dmpContactName = xmlDoc.createElement("name"); Element dmpContactEmail = xmlDoc.createElement("email"); if(dmp.getCreatorId() != null){ dmpContactName.setTextContent(this.queryFactory.query(UserQuery.class).ids(dmp.getCreatorId()).first().getName()); //TODO dmpContactEmail.setTextContent(this.databaseRepository.getUserInfoDao().find(dmp.getCreator()).getEmail()); } else{ Iterator users = new ArrayList().iterator(); //TODO dmp.getUsers().iterator(); if(users.hasNext()){ DmpUserEntity creator = users.next(); dmpContactName.setTextContent(this.queryFactory.query(UserQuery.class).ids(creator.getUserId()).first().getName()); //TODO .setTextContent(this.databaseRepository.getUserInfoDao().find(creator.getUser()).getEmail()); } } dmpContactElement.appendChild(dmpContactName); dmpContactElement.appendChild(dmpContactEmail); dmpElement.appendChild(dmpContactElement); Element dmpContributorsElement = xmlDoc.createElement("contributors"); Iterator users = new ArrayList().iterator(); //TODO dmp.getUsers().iterator(); while(users.hasNext()){ Element dmpContributorElement = xmlDoc.createElement("contributor"); Element dmpContributorName = xmlDoc.createElement("name"); Element dmpContributorEmail= xmlDoc.createElement("email"); DmpUserEntity contributor = users.next(); dmpContributorName.setTextContent(this.queryFactory.query(UserQuery.class).ids(contributor.getUserId()).first().getName()); //TODO dmpContributorEmail.setTextContent(this.databaseRepository.getUserInfoDao().find(contributor.getUser()).getEmail()); dmpContributorElement.appendChild(dmpContributorName); dmpContributorElement.appendChild(dmpContributorEmail); dmpContributorsElement.appendChild(dmpContributorElement); } dmpElement.appendChild(dmpContributorsElement); // Funder. Element funder = xmlDoc.createElement("funder"); if (this.dmpBlueprintService.fieldInBlueprint(this.queryFactory.query(DmpBlueprintQuery.class).ids(dmp.getBlueprintId()).first(), DmpBlueprintSystemFieldType.Grant) && this.dmpBlueprintService.fieldInBlueprint(this.queryFactory.query(DmpBlueprintQuery.class).ids(dmp.getBlueprintId()).first(), DmpBlueprintSystemFieldType.Funder)) { Element funderLabel = xmlDoc.createElement("label"); Element funderId = xmlDoc.createElement("id"); //TODO // funderLabel.setTextContent(dmp.getGrant().getFunder().getLabel()); // funderId.setTextContent(dmp.getGrant().getFunder().getId().toString()); // funder.appendChild(funderLabel); // funder.appendChild(funderId); // if (dmp.getGrant().getFunder().getReference() != null) { // String referencePrefix = dmp.getGrant().getFunder().getReference().split(":")[0]; // String shortReference = dmp.getGrant().getFunder().getReference().substring(referencePrefix.length() + 1); // Element funderReference = xmlDoc.createElement("reference"); // funderReference.setTextContent(shortReference); // funder.appendChild(funderReference); // } } dmpElement.appendChild(funder); // Grant. Element grant = xmlDoc.createElement("grant"); if (this.dmpBlueprintService.fieldInBlueprint(this.queryFactory.query(DmpBlueprintQuery.class).ids(dmp.getBlueprintId()).first(), DmpBlueprintSystemFieldType.Grant)) { Element grantLabel = xmlDoc.createElement("label"); Element grantId = xmlDoc.createElement("id"); //TODO // grantLabel.setTextContent(dmp.getGrant().getLabel()); // grantId.setTextContent(dmp.getGrant().getId().toString()); // grant.appendChild(grantLabel); // grant.appendChild(grantId); // if (dmp.getGrant().getReference() != null) { // String referencePrefix = dmp.getGrant().getReference().split(":")[0]; // String shortReference = dmp.getGrant().getReference().substring(referencePrefix.length() + 1); // Element grantReference = xmlDoc.createElement("reference"); // grantReference.setTextContent(shortReference); // grant.appendChild(grantReference); // } } dmpElement.appendChild(grant); // Project. Element project = xmlDoc.createElement("project"); if (this.dmpBlueprintService.fieldInBlueprint(this.queryFactory.query(DmpBlueprintQuery.class).ids(dmp.getBlueprintId()).first(), DmpBlueprintSystemFieldType.Project)) { Element projectId = xmlDoc.createElement("id"); Element projectLabel = xmlDoc.createElement("label"); Element projectDescription = xmlDoc.createElement("description"); Element projectStartDate = xmlDoc.createElement("start"); Element projectEndDate = xmlDoc.createElement("end"); //TODO // projectId.setTextContent(dmp.getProject().getId().toString()); // projectLabel.setTextContent(dmp.getProject().getLabel()); // projectDescription.setTextContent(dmp.getProject().getDescription()); // projectStartDate.setTextContent(dmp.getProject().getStartdate().toString()); // projectEndDate.setTextContent(dmp.getProject().getEnddate().toString()); project.appendChild(projectId); project.appendChild(projectLabel); project.appendChild(projectDescription); project.appendChild(projectStartDate); project.appendChild(projectEndDate); } dmpElement.appendChild(project); //TODO // Element organisationsElement = xmlDoc.createElement("organisations"); // for (Organisation organisation : dmp.getOrganisations()) { // Element organisationElement = xmlDoc.createElement("organisation"); // Element organisationNameElement = xmlDoc.createElement("name"); // organisationNameElement.setTextContent(organisation.getLabel()); // Element organisationReferenceElement = xmlDoc.createElement("reference"); // organisationReferenceElement.setTextContent(organisation.getReference()); // organisationElement.appendChild(organisationNameElement); // organisationElement.appendChild(organisationReferenceElement); // organisationsElement.appendChild(organisationElement); // } // dmpElement.appendChild(organisationsElement); // // Element researchersElement = xmlDoc.createElement("researchers"); // for (Researcher researcher : dmp.getResearchers()) { // Element researcherElement = xmlDoc.createElement("researcher"); // Element researcherNameElement = xmlDoc.createElement("name"); // researcherNameElement.setTextContent(researcher.getLabel()); // Element researcherReferenceElement = xmlDoc.createElement("reference"); // researcherReferenceElement.setTextContent(researcher.getReference()); // researcherElement.appendChild(researcherNameElement); // researcherElement.appendChild(researcherReferenceElement); // researchersElement.appendChild(researcherElement); // } // dmpElement.appendChild(researchersElement); Element extraFields = xmlDoc.createElement("extraFields"); Map dmpProperties = new ObjectMapper().readValue(dmp.getProperties(), new TypeReference>() {}); DefinitionEntity blueprint = new DefinitionEntity(); //new DefinitionEntity().fromXml(XmlBuilder.fromXml(this.queryFactory.query(DmpBlueprintQuery.class).ids(dmp.getBlueprintId()).first().getDefinition()).getDocumentElement()); blueprint.getSections().forEach(section -> { section.getFields().forEach(fieldModel -> { if (fieldModel.getCategory() == DmpBlueprintFieldCategory.Extra) { Element extraField = xmlDoc.createElement("extraField"); Element extraFieldId = xmlDoc.createElement("id"); extraFieldId.setTextContent(fieldModel.getId().toString()); Element extraFieldLabel = xmlDoc.createElement("label"); extraFieldLabel.setTextContent(fieldModel.getLabel()); Element extraFieldValue = xmlDoc.createElement("value"); Object value = dmpProperties.get(fieldModel.getId().toString()); if (value != null) { extraFieldValue.setTextContent((String) value); } extraField.appendChild(extraFieldId); extraField.appendChild(extraFieldLabel); extraField.appendChild(extraFieldValue); extraFields.appendChild(extraField); } }); }); dmpElement.appendChild(extraFields); Element datasetsElement = xmlDoc.createElement("datasets"); for (DescriptionEntity descriptionEntity : descriptionEntities) { Element datasetElement = xmlDoc.createElement("dataset"); datasetElement.setAttribute("name", descriptionEntity.getLabel()); Element datasetDescriptionElement = xmlDoc.createElement("description"); datasetElement.appendChild(datasetDescriptionElement); datasetDescriptionElement.setTextContent(descriptionEntity.getDescription()); //TODO // Element datasetDmpSectionIndex = xmlDoc.createElement("dmpSectionIndex"); // datasetElement.appendChild(datasetDmpSectionIndex); // datasetDmpSectionIndex.setTextContent(String.valueOf(descriptionEntity.getDmpSectionIndex())); Element datsetProfileElement = xmlDoc.createElement("profile-id"); datasetElement.appendChild(datsetProfileElement); datsetProfileElement.setTextContent(descriptionEntity.getDescriptionTemplateId().toString()); Element datsetProfileLabelElement = xmlDoc.createElement("profile-label"); datasetElement.appendChild(datsetProfileLabelElement); datsetProfileLabelElement.setTextContent(this.queryFactory.query(DescriptionTemplateQuery.class).ids(descriptionEntity.getDescriptionTemplateId()).first().getLabel()); DatasetWizardModel datasetWizardModel = new DatasetWizardModel(); Map properties = new HashMap<>(); if (descriptionEntity.getProperties() != null) { JSONObject jobject = new JSONObject(descriptionEntity.getProperties()); properties = jobject.toMap(); } PagedDatasetProfile pagedDatasetProfile = datasetManager.getPagedProfile(datasetWizardModel, descriptionEntity); visibilityRuleService.setProperties(properties); visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules()); datasetElement.appendChild(xmlBuilder.createPages(pagedDatasetProfile.getPages(), visibilityRuleService, xmlDoc)); datasetsElement.appendChild(datasetElement); } Element profiles = xmlDoc.createElement("profiles"); // Get DatasetProfiles from DMP to add to XML. //TODO // for (DMPDatasetProfile dmpDescriptionProfile : dmp.getAssociatedDmps()) { // DescriptionTemplateEntity descriptionTemplateEntity = dmpDescriptionProfile.getDatasetprofile(); // Element profile = xmlDoc.createElement("profile"); // Element profileId = xmlDoc.createElement("profileId"); // profileId.setTextContent(descriptionTemplateEntity.getId().toString()); // profile.appendChild(profileId); // Element profileGroupId = xmlDoc.createElement("profileGroupId"); // profileGroupId.setTextContent(descriptionTemplateEntity.getGroupId().toString()); // profile.appendChild(profileGroupId); // Element profileLabel = xmlDoc.createElement("profileLabel"); // profileLabel.setTextContent(descriptionTemplateEntity.getLabel()); // profile.appendChild(profileLabel); // Element profileVersion = xmlDoc.createElement("profileVersion"); // profileVersion.setTextContent(String.valueOf(descriptionTemplateEntity.getVersion())); // profile.appendChild(profileVersion); // Element profileInSections = xmlDoc.createElement("profileInSections"); // Map data = new ObjectMapper().readValue(dmpDescriptionProfile.getData(), new TypeReference>() {}); // List sections = (List) data.get("dmpSectionIndex"); // for(int section: sections) { // Element profileInSection = xmlDoc.createElement("section"); // profileInSection.setTextContent(String.valueOf(section)); // profileInSections.appendChild(profileInSection); // } // profile.appendChild(profileInSections); // profiles.appendChild(profile); // } dmpElement.appendChild(profiles); dmpElement.appendChild(datasetsElement); xmlDoc.appendChild(dmpElement); String xml = XmlBuilder.generateXml(xmlDoc); writer.write(xml); writer.close(); FileEnvelope fileEnvelope = new FileEnvelope(); fileEnvelope.setFile(xmlFile); //TODO // if (dmp.getGrant() != null && dmp.getGrant().getLabel() != null) { // fileEnvelope.setFilename("DMP_" + dmp.getGrant().getLabel() + "_" + dmp.getVersion() + ".xml"); // } // else { // fileEnvelope.setFilename("DMP_" + dmp.getLabel() + "_" + dmp.getVersion() + ".xml"); // } return fileEnvelope; } @Transactional public FileEnvelope getRDAJsonDocument(String id) throws Exception { DmpEntity dmp = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (!dmp.getAccessType().equals(DmpAccessType.Public) //TODO && dmp.getUsers().stream().noneMatch(userInfo -> this.userScope.getUserIdSafe().equals(userInfo.getUser().getId())) ) throw new UnauthorisedException(); final boolean isFinalized = dmp.getStatus() == DmpStatus.Finalized; final boolean isPublic = dmp.getAccessType().equals(DmpAccessType.Public); //TODO // dmp.setDataset(dmp.getDataset().stream() // .filter(dataset -> dataset.getIsActive() != IsActive.Inactive && // dataset.getStatus() != DescriptionStatus.Canceled) // .filter(dataset -> !isPublic && !isFinalized || dataset.getStatus() == DescriptionStatus.Finalized) // .collect(Collectors.toSet())); String result = rdaManager.convertToRDA(dmp); String fileName = ""; //TODO // if (dmp.getGrant() != null && dmp.getGrant().getLabel() != null) { // fileName = "DMP_" + dmp.getGrant().getLabel() + "_" + dmp.getVersion(); // } // else { // fileName = "DMP_" + dmp.getLabel() + "_" + dmp.getVersion(); // } fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "").replace(" ", "_").replace(",", "_"); String uuid = UUID.randomUUID().toString(); File file = new File(this.environment.getProperty("temp.temp") + uuid + ".json"); OutputStream output = new FileOutputStream(file); try { output.write(result.getBytes()); output.flush(); output.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } FileEnvelope rdaJsonDocument = new FileEnvelope(); rdaJsonDocument.setFilename(fileName + ".json"); rdaJsonDocument.setFile(file); return rdaJsonDocument; } public ResponseEntity getDocument(String id, String contentType, ConfigLoader configLoader) throws InstantiationException, IllegalAccessException, IOException, InvalidApplicationException { FileEnvelope file; switch (contentType) { case "application/xml": file = getXmlDocument(id); break; case "application/msword": file = getWordDocument(id, configLoader); break; default: file = getXmlDocument(id); } String fileName = file.getFilename().replace(" ", "_").replace(",", "_"); InputStream resource = new FileInputStream(file.getFile()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(file.getFile().length()); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); responseHeaders.set("Content-Disposition", "attachment;filename=" + fileName); responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource); resource.close(); Files.deleteIfExists(file.getFile().toPath()); return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK); } /* * Data Import * */ public List createDmpFromXml(MultipartFile[] files, String[] profiles) throws IOException, JAXBException, Exception { List dataManagementPlans = new ArrayList<>(); // Jaxb approach. JAXBContext jaxbContext; for (MultipartFile multipartFile : Arrays.asList(files)) { // Gets one item from the array. try { InputStream in = multipartFile.getInputStream(); // Transforms item to InputStream. jaxbContext = JAXBContext.newInstance(DmpImportModel.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); DmpImportModel dmpImportModel = (DmpImportModel) jaxbUnmarshaller.unmarshal(in); dataManagementPlans.add(dmpImportModel); } catch (IOException | JAXBException ex) { logger.error(ex.getMessage(), ex); } // TODO Iterate through the list of dataManagementPlans. // Creates new dataManagementPlan to fill it with the data model that was parsed from the xml. // Creates properties. DataManagementPlanEditorModel dm = new DataManagementPlanEditorModel(); DmpProfileImportModel dmpProfileImportModel = dataManagementPlans.get(0).getDmpProfile(); Tuple tupleProfile = new Tuple<>(); if (dmpProfileImportModel != null) { tupleProfile.setId(dmpProfileImportModel.getDmpProfileId()); tupleProfile.setLabel(dmpProfileImportModel.getDmpProfileName()); } else { tupleProfile.setId(UUID.fromString("86635178-36a6-484f-9057-a934e4eeecd5")); tupleProfile.setLabel("Dmp Default Blueprint"); } dm.setProfile(tupleProfile); Map dmpPropertiesMap = new HashMap<>(); if (dataManagementPlans.get(0).getExtraFieldsImportModels() != null) { for (ExtraFieldsImportModels extraField: dataManagementPlans.get(0).getExtraFieldsImportModels()) { dmpPropertiesMap.put(extraField.getId(), extraField.getValue()); } } dm.setProperties(dmpPropertiesMap); //TODO // if (this.dmpBlueprintService.fieldInBlueprint(dmpProfileImportModel.getDmpProfileId(), DmpBlueprintSystemFieldType.Funder)) { // eu.eudat.models.data.funder.Funder funder = new eu.eudat.models.data.funder.Funder(); // FunderImportModels funderImport = dataManagementPlans.get(0).getFunderImportModels(); // funder.setId(funderImport.getId()); // funder.setLabel(funderImport.getLabel()); // FunderDMPEditorModel funderEditor = new FunderDMPEditorModel(); // funderEditor.setExistFunder(funder); // // dm.setFunder(funderEditor); // } //TODO // if (this.dmpBlueprintService.fieldInBlueprint(dmpProfileImportModel.getDmpProfileId(), DmpBlueprintSystemFieldType.Grant)) { // eu.eudat.models.data.grant.Grant grant = new eu.eudat.models.data.grant.Grant(); // GrantImportModels grantImport = dataManagementPlans.get(0).getGrantImport(); // grant.setId(grantImport.getId()); // grant.setLabel(grantImport.getLabel()); // grant.setAbbreviation(grantImport.getAbbreviation()); // grant.setDescription(grantImport.getDescription()); // GrantDMPEditorModel grantEditor = new GrantDMPEditorModel(); // grantEditor.setExistGrant(grant); // // dm.setGrant(grantEditor); // } //TODO // if (this.dmpBlueprintService.fieldInBlueprint(dmpProfileImportModel.getDmpProfileId(), DmpBlueprintSystemFieldType.Project)) { // eu.eudat.models.data.project.Project project = new eu.eudat.models.data.project.Project(); // ProjectImportModels projectImport = dataManagementPlans.get(0).getProjectImportModels(); // project.setId(projectImport.getId()); // project.setLabel(projectImport.getLabel()); // ProjectDMPEditorModel projectEditor = new ProjectDMPEditorModel(); // projectEditor.setExistProject(project); // // dm.setProject(projectEditor); // } List associatedProfiles = new LinkedList<>(); // if (profiles != null && profiles.length > 0) { // for (String profile : profiles) { // DescriptionTemplate exProfile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(profile)); // AssociatedProfile associatedProfile = new AssociatedProfile().fromData(exProfile); // associatedProfiles.add(associatedProfile); // } // } for (AssociatedProfileImportModels a : dataManagementPlans.get(0).getProfilesImportModels()) { try { eu.eudat.data.DescriptionTemplateEntity exProfile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(a.getId()); AssociatedProfile associatedProfile = new AssociatedProfile().fromData(exProfile); Map data = new HashMap<>(); List sections = new ArrayList<>(a.getSection()); data.put("dmpSectionIndex", sections); associatedProfile.setData(data); associatedProfiles.add(associatedProfile); } catch (Exception ignored) { } } // List organisations = new ArrayList<>(); // for (OrganisationImportModel org : dataManagementPlans.get(0).getOrganisationImportModels()) { // eu.eudat.models.data.dmp.Organisation organisation = new eu.eudat.models.data.dmp.Organisation(); // organisation.setName(org.getOrganaisationNameImport()); // organisation.setReference(org.getOrganaisationReferenceImport()); // organisation.setKey(organisation.getReference().split(":")[0]); // organisations.add(organisation); // }//TODO // List researchers = new LinkedList<>(); // for (ResearcherImportModels res : dataManagementPlans.get(0).getResearchersImportModels()) { // eu.eudat.models.data.dmp.Researcher researcher = new eu.eudat.models.data.dmp.Researcher(); // researcher.setLabel(res.getResearcherImportName()); // researcher.setName(res.getResearcherImportName()); // researcher.setReference(res.getResearcherImportReference()); // researcher.setKey(researcher.getReference().split(":")[0]); // researchers.add(researcher); // } //TODO //List associatedUsers = new LinkedList<>(); List dynamicFields = new LinkedList<>(); // Sets properties. dm.setLabel(files[0].getOriginalFilename()); // Sets label. dm.setDescription(dataManagementPlans.get(0).getDescriptionImport()); // Sets description property. dm.setProfiles(associatedProfiles); // dm.setOrganisations(organisations); // Sets organisations property. // dm.setResearchers(researchers); // Sets researchers property. //dm.setAssociatedUsers(associatedUsers); // Sets associatedUsers property. dm.setDynamicFields(dynamicFields); // Sets dynamicFields property. //dm.setDefinition(dmpProfile); //ObjectMapper mapper = new ObjectMapper(); Map extraPropertiesMap = new HashMap<>(); if (dataManagementPlans.get(0).getLanguage() != null) { extraPropertiesMap.put("language", dataManagementPlans.get(0).getLanguage()); } if (dataManagementPlans.get(0).getVisibility() != null) { extraPropertiesMap.put("visible", dataManagementPlans.get(0).getVisibility()); } if (dataManagementPlans.get(0).getPublicDate() != null) { extraPropertiesMap.put("publicDate", dataManagementPlans.get(0).getPublicDate()); } if (dataManagementPlans.get(0).getCosts() != null && !dataManagementPlans.get(0).getCosts().isEmpty()) { extraPropertiesMap.put("costs", objectMapper.readValue(dataManagementPlans.get(0).getCosts(), ArrayList.class)); } dm.setExtraProperties(extraPropertiesMap); //createOrUpdate(apiContext, dm, principal); DmpEntity dmp = this.createOrUpdate(dm); // if (dmp.getOrganisations() == null) { //TODO // dmp.setOrganisations(new HashSet<>()); // } // if (dmp.getResearchers() == null) { // dmp.setResearchers(new HashSet<>()); // } // if (dmp.getDataset() == null) { // dmp.setDataset(new HashSet<>()); // } // if (dmp.getUsers() == null) { // dmp.setUsers(new HashSet<>()); // } // if (dmp.getAssociatedDmps() == null) { // dmp.setAssociatedDmps(new HashSet<>()); // } List datasets = new LinkedList<>(); for (DatasetImportModels das: dataManagementPlans.get(0).getDatasetImportModels()) { DescriptionEntity descriptionEntity = new DescriptionEntity(); descriptionEntity.setLabel(das.getName()); //TODO // descriptionEntity.setDmpSectionIndex(das.getDmpSectionIndex()); // try { // descriptionEntity.setDescriptionTemplateId(das.getProfile()); // } catch (Exception ignored) { // descriptionEntity.setDescriptionTemplateId(associatedProfiles.get(0).getDescriptionTemplateId()); // } // descriptionEntity.setDmpId(dmp.getId()); descriptionEntity.setProperties(objectMapper.writeValueAsString(das.getFieldImportModels())); descriptionEntity.setStatus(DescriptionStatus.Draft); // descriptionEntity.setRegistries(new HashSet<>()); // descriptionEntity.setDatasetDataRepositories(new HashSet<>()); // descriptionEntity.setServices(new HashSet<>()); // descriptionEntity.setDatasetExternalDatasets(new HashSet<>()); DatasetWizardModel datasetWizard = new DatasetWizardModel(); datasetWizard.setDatasetProfileDefinition(this.datasetManager.getPagedProfile(datasetWizard, descriptionEntity)); datasetWizard.fromDataModel(descriptionEntity); this.datasetManager.createOrUpdate(datasetWizard); // datasets.add(new DatasetListingModel().fromDataModel(dataset)); } } return dataManagementPlans; } public List createFromRDA(MultipartFile[] files, String[] profiles) throws IOException, InvalidApplicationException { if (!this.userScope.isSet()) { throw new UnauthorisedException("No user is logged in"); } List result = new ArrayList<>(); for (MultipartFile file: files) { DmpEntity dmp = rdaManager.convertToEntity(new String(file.getBytes(), "UTF-8"), profiles); dmp.setLabel(file.getOriginalFilename()); UserEntity me = this.queryFactory.query(UserQuery.class).ids(dmp.getCreatorId()).first(); dmp.setUpdatedAt(Instant.now()); dmp.setCreatorId(me.getId()); //TODO // Map extraProperties = objectMapper.readValue(dmp.getExtraProperties(), HashMap.class); // extraProperties.put("contact", me.getId().toString()); // dmp.setExtraProperties(objectMapper.writeValueAsString(extraProperties)); // dmp.setVersion(0); // dmp.setStatus((short)0); // dmp.setGroupId(UUID.randomUUID()); // if (dmp.getResearchers() != null && !dmp.getResearchers().isEmpty()) { // dmp.getResearchers().stream().filter(Objects::nonNull).forEach(researcher -> { // researcher.setId(UUID.randomUUID()); // researcher.setCreated(new Date()); // researcher.setModified(new Date()); // researcher.setStatus((short) 0); // apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao().createOrUpdate(researcher); // }); // } // databaseRepository.getDmpDao().createOrUpdate(dmp); // assignUser(dmp, me); // //TODO //// if (this.apiContext.getOperationsContext().getElasticRepository().getDmpRepository().getClient() != null) { //// UUID dmpId = dmp.getId(); //// dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); //// this.updateIndex(dmp); //// } // dmp.getDataset().forEach(dataset -> { // dataset.setStatus(DescriptionStatus.Draft); // dataset.setCreatedAt(Instant.now()); // dataset.setUpdatedAt(Instant.now()); // //dataset.setDmpId(dmp.getId()); //TODO // dataset = databaseRepository.getDatasetDao().createOrUpdate(dataset); //// dataset.setRegistries(new HashSet<>()); //// dataset.setDatasetDataRepositories(new HashSet<>()); //// dataset.setDatasetExternalDatasets(new HashSet<>()); //// dataset.setServices(new HashSet<>()); // if (dmp.getOrganisations() == null) { // dmp.setOrganisations(new HashSet<>()); // } // if (dmp.getResearchers() == null) { // dmp.setResearchers(new HashSet<>()); // } // try { // dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmp.getId())).toList())); // } catch (InvalidApplicationException e) { // throw new RuntimeException(e); // } //TODO // try { // List tags = new ArrayList<>(); // eu.eudat.elastic.entities.Dataset elastic = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(dataset.getId().toString()); // DatasetWizardModel datasetWizardModel = new DatasetWizardModel().fromDataModel(dataset); // if (elastic != null) { // tags = elastic.getTags(); // datasetWizardModel.setTags(tags); // } // datasetManager.getTagsFromProfile(datasetWizardModel, dataset); // datasetManager.updateTags(dataset, datasetWizardModel.getTags()); // } catch (Exception e) { // logger.error(e.getMessage(), e); // } // }); result.add(dmp); } return result; } /* * Data assignments * */ private void assignUser(DmpEntity dmp, UserEntity userInfo) { this.assignUser(dmp, userInfo, DmpUserRole.Owner); } private void assignUser(DmpEntity dmp, UserEntity userInfo, DmpUserRole role) { DmpUserEntity userDMP = new DmpUserEntity(); userDMP.setDmpId(dmp.getId()); userDMP.setUserId(userInfo.getId()); userDMP.setRole(role); // databaseRepository.getUserDmpDao().createOrUpdate(userDMP); } private void clearUsers(DmpEntity dmp) throws InvalidApplicationException { List userDMPs = this.queryFactory.query(DmpUserQuery.class).dmpIds(dmp.getId()).collect(); // userDMPs.forEach(userDMP -> apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().delete(userDMP)); } private void assignGrandUserIfInternal(DmpEntity dmp, UserEntity user) { //TODO // if (dmp.getGrant() != null && dmp.getGrant().getCreationUser() == null && (dmp.getGrant().getReference() != null && dmp.getGrant().getReference().startsWith("dmp:"))) { // dmp.getGrant().setCreationUser(user); // } } private void assignFunderUserIfInternal(DmpEntity dmp, UserEntity user) { //TODO // if (dmp.getGrant() != null && dmp.getGrant().getFunder() != null && dmp.getGrant().getFunder().getCreationUser() == null && ( dmp.getGrant().getFunder().getReference() != null && dmp.getGrant().getFunder().getReference().startsWith("dmp:"))) { // dmp.getGrant().getFunder().setCreationUser(user); // } } private void assignProjectUserIfInternal(DmpEntity dmp, UserEntity user) { //TODO // if (dmp.getProject() != null && dmp.getProject().getCreationUser() == null && (dmp.getProject().getReference() != null && dmp.getProject().getReference().startsWith("dmp:"))) { // dmp.getProject().setCreationUser(user); // } } /* * Data Index * */ private void updateGroupIndex(UUID groupId) throws InvalidApplicationException { DataManagementPlanCriteria criteria = new DataManagementPlanCriteria(); criteria.setGroupIds(Collections.singletonList(groupId)); criteria.setAllVersions(true); List dmps = databaseRepository.getDmpDao().getWithCriteria(criteria).toList(); for (DmpEntity dmp: dmps) { // try { //TODO // if (dmp.getUsers() == null) { // UUID dmpId = dmp.getId(); // dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); // } //// this.updateIndex(dmp); //TODO // } catch (InvalidApplicationException e) { // logger.error(e.getMessage(), e); // } } } // private void updateDatasetsIndex(List descriptionEntities) { // descriptionEntities.forEach(dataset -> { // List tags = new ArrayList<>(); // eu.eudat.elastic.entities.Dataset elastic = null; // try { // elastic = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(dataset.getId().toString()); // if (elastic != null) { // tags = elastic.getTags(); // } // //TODO // //UUID dmpId = dataset.getDmpId(); // //dataset.getDmp().setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); // this.datasetManager.updateTags(dataset, tags); // } catch (Exception e) { // logger.error(e.getMessage(), e); // } // // }); // } // public void updateIndex(DMP dmp) throws IOException, InvalidApplicationException { // DmpMapper mapper = new DmpMapper(apiContext, datasetManager); // Dmp elastic = mapper.toElastic(dmp); // apiContext.getOperationsContext().getElasticRepository().getDmpRepository().createOrUpdate(elastic); // } // // public void generateIndex() throws InvalidApplicationException { // if (this.authorizationService.authorize(Permission.AdminRole)) { // if (apiContext.getOperationsContext().getElasticRepository().getDmpRepository().createIndex()) { // List dmps = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable().toList(); // dmps.forEach(dmp -> { // try { // UUID dmpId = dmp.getId(); // dmp.setUsers(new HashSet<>(apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("dmp").get("id"), dmpId)).toList())); // this.updateIndex(dmp); // } catch (IOException | InvalidApplicationException e) { // logger.error(e.getMessage(), e); // } // }); // } // } // } // public void clearIndex() throws IOException { // if (this.authorizationService.authorize(Permission.AdminRole)) { // apiContext.getOperationsContext().getElasticRepository().getDmpRepository().clear(); // } // } /* * Data Checkup * */ private void checkIfUserCanEditGrant(DmpEntity dmp, UserEntity user) throws Exception{ // if (dmp.getGrant() != null && dmp.getGrant().getId() != null) { //TODO // Grant grant = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().find(dmp.getGrant().getId()); // if (grant.getFunder() != null && dmp.getGrant().getFunder() != null // && !grant.getFunder().getId().equals(dmp.getGrant().getFunder().getId())) { // if (grant.getCreationUser() == null) { // throw new Exception("Grant has no user, therefore, cannot be edited."); // } // if (!grant.getCreationUser().getId().equals(user.getId())) { // throw new Exception("User is not the owner of the Grant, therefore, cannot edit it."); // } // } // } } private void checkDmpValidationRules(DmpEntity dmp) throws Exception { if (dmp.getLabel() == null || dmp.getLabel().trim().isEmpty()) { throw new Exception("DMP has no label."); } //TODO // if (dmp.getAssociatedDmps().size() == 0) { // throw new Exception("DMP does not contain Dataset Templates."); // } // if (dmp.getGrant() == null) { // throw new Exception("DMP has no Grant assigned."); // } } private boolean isUserOwnerOfDmp(DmpEntity dmp) throws InvalidApplicationException { return this.userScope.isSet() ; //TODO && (dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).map(userDMP -> userDMP.getUser().getId())).collect(Collectors.toList()).contains(this.userScope.getUserId(); } /* * DOI Generation * */ private String getPreviousDOI(UUID groupId, UUID selfId, String repositoryId) throws InvalidApplicationException { DataManagementPlanCriteria criteria = new DataManagementPlanCriteria(); List groupIds = new ArrayList<>(); groupIds.add(groupId); criteria.setGroupIds(groupIds); criteria.setAllVersions(true); List dmps = this.databaseRepository.getDmpDao().getWithCriteria(criteria).toList(); dmps.sort((DmpEntity d1, DmpEntity d2) -> d2.getVersion() - d1.getVersion()); for (DmpEntity dmp: dmps) { if (!dmp.getId().equals(selfId)) { // if (dmp.getDois() != null && !dmp.getDois().isEmpty()) { //TODO // for (Iterator it = dmp.getDois().iterator(); it.hasNext(); ) { // EntityDoiEntity entityDoi = it.next(); // if(entityDoi.getRepositoryId().equals(repositoryId)){ // return entityDoi.getDoi(); // } // } // } } } return null; } @Transactional public EntityDoi createDoi(DepositRequest depositRequest) throws Exception { DmpEntity dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(depositRequest.getDmpId()); if (!isUserOwnerOfDmp(dmp)) throw new Exception("User is not authorized to invoke this action"); if (!dmp.getStatus().equals(DmpStatus.Finalized)) throw new Exception("DMP is not finalized"); /*if (dmp.getDoi() != null) throw new Exception("DMP already has a DOI");*/ FileEnvelope file = getWordDocument(depositRequest.getDmpId().toString(), configLoader); String name = file.getFilename().substring(0, file.getFilename().length() - 5).replaceAll("[^a-zA-Z0-9_+ ]", "").replace(" ", "_").replace(",", "_"); byte[] pdfFile = null; //PDFUtils.convertToPDF(file, environment); //TODO // eu.eudat.depositinterface.models.FileEnvelope pdfEnvelope = new eu.eudat.depositinterface.models.FileEnvelope(); // pdfEnvelope.setFile(pdfFile); // pdfEnvelope.setFilename(name + ".pdf"); // eu.eudat.depositinterface.models.FileEnvelope rdaJsonFile = new eu.eudat.depositinterface.models.FileEnvelope(); try { FileEnvelope rdaJsonDocument = getRDAJsonDocument(depositRequest.getDmpId().toString()); //rdaJsonFile.setFile(rdaJsonDocument.getFile()); //TODO // rdaJsonFile.setFilename(rdaJsonDocument.getFilename()); } catch (Exception e) { logger.error(e.getMessage(), e); } String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId(), depositRequest.getRepositoryId()); File supportingFilesZip = this.createSupportingFilesZip(dmp); // DmpDepositModel dmpDepositModel = new DmpDepositModel(); // DMPToDepositMapper.fromDMP(dmp, pdfEnvelope, rdaJsonFile, supportingFilesZip, previousDOI);//TODO String finalDoi = null; try { finalDoi = this.repositoriesDeposit.deposit(depositRequest).getDoi(); } catch (Exception e) { logger.error(e.getMessage(), e); return null; } EntityDoi doiModel = null; if (finalDoi != null) { EntityDoiEntity doiEntity = new EntityDoiEntity(); doiEntity.setId(UUID.randomUUID()); doiEntity.setEntityType(EntityType.DMP); doiEntity.setDoi(finalDoi); doiEntity.setRepositoryId(depositRequest.getRepositoryId()); doiEntity.setCreatedAt(Instant.now()); doiEntity.setUpdatedAt(Instant.now()); doiEntity.setEntityId(dmp.getId()); //TODO: Save doi // apiContext.getOperationsContext().getDatabaseRepository().getEntityDoiDao().createOrUpdate(doiEntity); // dmp.getDois().add(doiEntity); apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp); doiModel = new EntityDoi();//.fromDataModel(doiEntity); //TODO } if(supportingFilesZip != null) { Files.deleteIfExists(supportingFilesZip.toPath()); } // Files.deleteIfExists(rdaJsonFile.getFile().toPath()); //TODO // Files.deleteIfExists(pdfFile.toPath()); // Files.deleteIfExists(file.getFile().toPath()); return doiModel; } private File createSupportingFilesZip(DmpEntity dmp) throws IOException, InvalidApplicationException { FileOutputStream fout = new FileOutputStream(this.environment.getProperty("temp.temp") + "supportingFiles.zip"); ZipOutputStream zout = new ZipOutputStream(fout); boolean hasFileUploaded = false; Set descriptionEntities = new HashSet<>(); //TODO dmp.getDataset(); for (DescriptionEntity descriptionEntity : descriptionEntities) { List files = this.apiContext.getOperationsContext().getDatabaseRepository().getFileUploadDao().getFileUploads(descriptionEntity.getId()); for (FileUpload f : files) { if(!f.getIsDeleted()){ File exportFile = new File(this.environment.getProperty("file.storage") + f.getId()); String filename = f.getName().replace(" ", "_").replace(",", "_"); byte[] content = Files.readAllBytes(exportFile.toPath()); ZipEntry ze = new ZipEntry(filename); zout.putNextEntry(ze); zout.write(content, 0, content.length); zout.closeEntry(); hasFileUploaded = true; } } } zout.close(); if(!hasFileUploaded){ Files.deleteIfExists(new File(this.environment.getProperty("temp.temp") + "supportingFiles.zip").toPath()); } return hasFileUploaded ? new File(this.environment.getProperty("temp.temp") + "supportingFiles.zip") : null; } /* * Misc * */ private void sendNotification(DmpEntity dmp, UserEntity user/*, NotificationType notificationType*/) throws InvalidApplicationException { List userDMPS = this.queryFactory.query(DmpUserQuery.class).dmpIds(dmp.getId()).collect(); for (DmpUserEntity userDMP : userDMPS) { if (!userDMP.getUserId().equals(user.getId())) { // Notification notification = new Notification(); //TODO // notification.setUserId(user); // notification.setType(notificationType); // notification.setNotifyState(NotifyState.PENDING); // notification.setIsActive(ActiveStatus.ACTIVE); // notification.setData("{" + // "\"userId\": \"" + userDMP.getId() + "\"" + //// ", \"id\": \"" + userDMP.getDmp().getId() + "\"" + //TODO //// ", \"name\": \"" + userDMP.getDmp().getLabel() + "\"" + // ", \"path\": \"" + notificationPaths.get(notificationType) +"\"" + // "}"); // notification.setCreatedAt(new Date()); // notification.setUpdatedAt(notification.getCreatedAt()); // notification.setContactTypeHint(ContactType.EMAIL); // //notification.setContactHint(userDMP.getUser().getEmail()); //TODO // databaseRepository.getNotificationDao().createOrUpdate(notification); } } } /*public DataTableData getPublicPaged(DataManagmentPlanPublicTableRequest dataManagementPlanPublicTableRequest, String fieldsGroup, Principal principal) throws Exception { dataManagementPlanPublicTableRequest.setQuery(databaseRepository.getDmpDao().asQueryable().withHint(HintedModelFactory.getHint(DataManagementPlanListingModel.class))); QueryableList items = dataManagementPlanPublicTableRequest.applyCriteria(); if (principal.getId() != null && dataManagementPlanPublicTableRequest.getCriteria().getRole() != null) { items.where((builder, root) -> { Join userJoin = root.join("users", JoinType.LEFT); return builder.and(builder.equal(userJoin.join("user", JoinType.LEFT).get("id"), principal.getId()), builder.equal(userJoin.get("role"), dataManagementPlanPublicTableRequest.getCriteria().getRole())); }); } QueryableList pagedItems = PaginationManager.applyPaging(items, dataManagementPlanPublicTableRequest); DataTableData dataTable = new DataTableData<>(); CompletableFuture itemsFuture; if (fieldsGroup.equals("listing")) { itemsFuture = pagedItems.withHint(HintedModelFactory.getHint(DataManagementPlanListingModel.class)) .selectAsync(item -> { item.setDataset( item.getDataset().stream() .filter(dataset -> dataset.getStatus().equals(Dataset.Status.FINALISED.getValue())).collect(Collectors.toSet())); return new DataManagementPlanListingModel().fromDataModelDatasets(item); }) .whenComplete((resultList, throwable) -> dataTable.setData(resultList)); } else { itemsFuture = pagedItems .selectAsync(item -> new DataManagementPlanListingModel().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 void unlock(UUID uuid) throws Exception { apiContext.getOperationsContext().getDatabaseRepository().getDmpDao() .asQueryable().where((builder, root) -> builder.equal(root.get("id"), uuid)) .update(root -> root.get("status"), DMP.DMPStatus.ACTIVE.getValue()); return; }*/ /*public File getPdfDocument(String id) throws InstantiationException, IllegalAccessException, InterruptedException, IOException { File file = this.getWordDocument(id); String fileName = file.getName(); if (fileName.endsWith(".docx")){ fileName = fileName.substring(0, fileName.length() - 5); } return this.datasetManager.convertToPDF(file, environment, fileName); }*/ /*public eu.eudat.models.data.dmp.DataManagementPlan getSinglePublic(String id, DynamicGrantConfiguration dynamicGrantConfiguration) throws Exception { DMP dataManagementPlanEntity = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (dataManagementPlanEntity != null && dataManagementPlanEntity.getStatus() == 1) { eu.eudat.models.data.dmp.DataManagementPlan datamanagementPlan = new eu.eudat.models.data.dmp.DataManagementPlan(); datamanagementPlan.fromDataModel(dataManagementPlanEntity); datamanagementPlan.setDatasets(datamanagementPlan.getDatasets().stream().filter(dataset -> dataset.getStatus() == Dataset.Status.FINALISED.getValue()).collect(Collectors.toList())); Map dmpProperties = dataManagementPlanEntity.getDmpProperties() != null ? new org.json.JSONObject(dataManagementPlanEntity.getDmpProperties()).toMap() : null; // datamanagementPlan.setDynamicFields(dynamicGrantConfiguration.getFields().stream().map(item -> { // DynamicFieldWithValue fieldWithValue = new DynamicFieldWithValue(); // fieldWithValue.setId(item.getId()); // fieldWithValue.setDependencies(item.getDependencies()); // fieldWithValue.setName(item.getName()); // fieldWithValue.setQueryProperty(item.getQueryProperty()); // fieldWithValue.setRequired(item.getRequired()); // return fieldWithValue; // }).collect(Collectors.toList())); if (dmpProperties != null && datamanagementPlan.getDynamicFields() != null) datamanagementPlan.getDynamicFields().forEach(item -> { Map properties = (Map) dmpProperties.get(item.getId()); if (properties != null) item.setValue(new Tuple<>(properties.get("id"), properties.get("label"))); }); return datamanagementPlan; } else { throw new Exception("Selected DMP is not public"); } } public DataManagementPlanOverviewModel getOverviewSinglePublic(String id) throws Exception { DMP dataManagementPlanEntity = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (dataManagementPlanEntity != null && dataManagementPlanEntity.getStatus() == 1) { DataManagementPlanOverviewModel datamanagementPlan = new DataManagementPlanOverviewModel(); datamanagementPlan.fromDataModelDatasets(dataManagementPlanEntity); datamanagementPlan.setDatasets(datamanagementPlan.getDatasets().stream().filter(dataset -> dataset.getStatus() == Dataset.Status.FINALISED.getValue()).collect(Collectors.toList())); return datamanagementPlan; } else { throw new Exception("Selected DMP is not public"); } }*/ /*public List getWithCriteria(DMPDao dmpsRepository, DataManagementPlanCriteriaRequest dataManagementPlanCriteria, Principal principal) throws IllegalAccessException, InstantiationException { UUID principalID = principal.getId(); QueryableList items = dmpsRepository.getWithCriteria(dataManagementPlanCriteria.getCriteria()).withHint(HintedModelFactory.getHint(DataManagementPlan.class)); List roles = new LinkedList<>(); QueryableList authenticatedItems = dmpsRepository.getAuthenticated(items, principalID, roles); List datamanagementPlans = authenticatedItems.select(item -> new DataManagementPlan().fromDataModel(item)); return datamanagementPlans; }*/ }