diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPProfileController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPProfileController.java index b3edb0287..60e9d5213 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPProfileController.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPProfileController.java @@ -3,7 +3,9 @@ package eu.eudat.controllers; import eu.eudat.data.entities.DMPProfile; import eu.eudat.data.query.items.item.dmpprofile.DataManagementPlanProfileCriteriaRequest; import eu.eudat.data.query.items.table.dmpprofile.DataManagementPlanProfileTableRequest; +import eu.eudat.logic.managers.AdminManager; import eu.eudat.logic.managers.DataManagementProfileManager; +import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.services.ApiContext; import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.helpers.responses.ResponseItem; @@ -15,10 +17,14 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; +import java.io.IOException; import java.util.List; +import static eu.eudat.types.Authorities.ADMIN; + /** * Created by ikalyvas on 3/21/2018. */ @@ -56,4 +62,26 @@ public class DMPProfileController extends BaseController { DataTableData dataTable = this.dataManagementProfileManager.getPaged(this.getApiContext(), dataManagementPlanProfileTableRequest, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().status(ApiMessageCode.NO_MESSAGE).payload(dataTable)); } + + @RequestMapping(method = RequestMethod.GET, value = {"/getXml/{id}"}, produces = "application/json") + public @ResponseBody + ResponseEntity getXml( @RequestHeader("Content-Type") String contentType, @PathVariable String id, Principal principal) throws IllegalAccessException, InstantiationException, IOException { + if (contentType.equals("application/xml")) { + DataManagementPlanProfileListingModel dataManagementPlanProfileListingModel = this.dataManagementProfileManager.getSingle(this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpProfileDao(), id, principal); + return this.dataManagementProfileManager.getDocument(dataManagementPlanProfileListingModel,dataManagementPlanProfileListingModel.getLabel()); + }else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message("NOT AUTHORIZE")); + } + } + + @RequestMapping(method = RequestMethod.POST, value = {"/upload"}) + public ResponseEntity setDatasetProfileXml(@RequestParam("file") MultipartFile file, + @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws IllegalAccessException,IOException,Exception{ + eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel.DmpProfile dmpProfileModel = this.dataManagementProfileManager.createDmpProfileFromXml(file); + DataManagementPlanProfileListingModel dataManagementPlan = dmpProfileModel.toDmpProfileCompositeModel(file.getOriginalFilename()); + this.dataManagementProfileManager.createOrUpdate(this.getApiContext(), dataManagementPlan, principal); + return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>() + .status(ApiMessageCode.SUCCESS_MESSAGE).message("")); + } + } diff --git a/dmp-backend/web/src/main/java/eu/eudat/exceptions/datasetprofile/DatasetProfileWithDatasetsExeption.java b/dmp-backend/web/src/main/java/eu/eudat/exceptions/datasetprofile/DatasetProfileWithDatasetsExeption.java new file mode 100644 index 000000000..1d5654017 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/exceptions/datasetprofile/DatasetProfileWithDatasetsExeption.java @@ -0,0 +1,19 @@ +package eu.eudat.exceptions.datasetprofile; + +public class DatasetProfileWithDatasetsExeption extends RuntimeException { + + public DatasetProfileWithDatasetsExeption() { + } + + public DatasetProfileWithDatasetsExeption(String message) { + super(message); + } + + public DatasetProfileWithDatasetsExeption(String message, Throwable cause) { + super(message, cause); + } + + public DatasetProfileWithDatasetsExeption(Throwable cause) { + super(cause); + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementProfileManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementProfileManager.java index 2afb85cd5..ea63edae9 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementProfileManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementProfileManager.java @@ -4,17 +4,27 @@ import eu.eudat.data.dao.entities.DMPProfileDao; import eu.eudat.data.entities.DMPProfile; import eu.eudat.data.query.items.item.dmpprofile.DataManagementPlanProfileCriteriaRequest; import eu.eudat.data.query.items.table.dmpprofile.DataManagementPlanProfileTableRequest; +import eu.eudat.logic.utilities.documents.helpers.FileEnvelope; +import eu.eudat.logic.utilities.documents.xml.dmpXml.ExportXmlBuilderDmpProfile; +import eu.eudat.logic.utilities.documents.xml.dmpXml.ImportXmlBuilderDmpProfile; import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel; import eu.eudat.models.data.security.Principal; import eu.eudat.queryable.QueryableList; import eu.eudat.logic.services.ApiContext; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import javax.activation.MimetypesFileTypeMap; +import java.io.*; +import java.nio.file.Files; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import org.springframework.http.*; +import org.springframework.web.multipart.MultipartFile; + /** * Created by ikalyvas on 3/21/2018. */ @@ -57,4 +67,57 @@ public class DataManagementProfileManager { DMPProfile dmpProfile = dataManagementPlanProfileListingModel.toDataModel(); apiContext.getOperationsContext().getDatabaseRepository().getDmpProfileDao().createOrUpdate(dmpProfile); } + + + + public ResponseEntity getDocument(DataManagementPlanProfileListingModel dmpProfile, String label) throws IllegalAccessException, IOException, InstantiationException { + + + FileEnvelope envelope = getXmlDocument(dmpProfile, label); + InputStream resource = new FileInputStream(envelope.getFile()); + System.out.println("Mime Type of " + envelope.getFilename() + " is " + + new MimetypesFileTypeMap().getContentType(envelope.getFile())); + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setContentLength(envelope.getFile().length()); + responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); + responseHeaders.set("Content-Disposition", "attachment;filename=" + envelope.getFilename() + ".xml"); + 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(envelope.getFile().toPath()); + return new ResponseEntity<>(content, + responseHeaders, + HttpStatus.OK); + } + + public FileEnvelope getXmlDocument(DataManagementPlanProfileListingModel dmpProfile, String label) throws InstantiationException, IllegalAccessException, IOException { + ExportXmlBuilderDmpProfile xmlBuilder = new ExportXmlBuilderDmpProfile(); + File file = xmlBuilder.build(dmpProfile); + FileEnvelope fileEnvelope = new FileEnvelope(); + fileEnvelope.setFile(file); + fileEnvelope.setFilename(label); + return fileEnvelope; + } + + + public eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel.DmpProfile createDmpProfileFromXml(MultipartFile multiPartFile) { + ImportXmlBuilderDmpProfile xmlBuilder = new ImportXmlBuilderDmpProfile(); + try { + return xmlBuilder.build(convert(multiPartFile)); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private static File convert(MultipartFile file) throws IOException { + File convFile = new File(file.getOriginalFilename()); + convFile.createNewFile(); + FileOutputStream fos = new FileOutputStream(convFile); + fos.write(file.getBytes()); + fos.close(); + return convFile; + } + } diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/ExportXmlBuilderDmpProfile.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/ExportXmlBuilderDmpProfile.java new file mode 100644 index 000000000..344d4924a --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/ExportXmlBuilderDmpProfile.java @@ -0,0 +1,55 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml; + +import eu.eudat.logic.utilities.builders.XmlBuilder; + +import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile; +import eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.UUID; + +public class ExportXmlBuilderDmpProfile { + + + public File build(DataManagementPlanProfileListingModel dmpProfile) throws IOException { + + File xmlFile = new File(UUID.randomUUID() + ".xml"); + BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true)); + Document xmlDoc = XmlBuilder.getDocument(); + Element root = xmlDoc.createElement("root"); + Element definition = xmlDoc.createElement("definition"); +// Element root = xmlDoc.createElement(dmpProfile.getLabel()); + definition.appendChild(createDefinition(dmpProfile.getDefinition(), xmlDoc)); + root.appendChild(definition); + xmlDoc.appendChild(root); + String xml = XmlBuilder.generateXml(xmlDoc); + writer.write(xml); + writer.close(); + return xmlFile; + } + + public Element createDefinition(DataManagementPlanProfile dmpDefinition, Document element) { + Element fields = element.createElement("fieldSets"); + dmpDefinition.getFields().forEach(item -> { + Element field = element.createElement("field"); + field.setAttribute("id", "" + item.getId()); + field.setAttribute("type", "" + item.getType()); + field.setAttribute("dataType", "" + item.getDataType()); + field.setAttribute("required", "" + item.getRequired()); + field.setAttribute("label", "" + item.getLabel()); + if(item.getValue()!=null) { + Element value = element.createElement("value"); + value.setAttribute("value", ""+item.getValue()); + field.appendChild(value); + } + fields.appendChild(field); + }); + return fields; + } + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/ImportXmlBuilderDmpProfile.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/ImportXmlBuilderDmpProfile.java new file mode 100644 index 000000000..717b6ac2b --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/ImportXmlBuilderDmpProfile.java @@ -0,0 +1,27 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml; + +import eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel.DmpProfile; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.io.IOException; + +public class ImportXmlBuilderDmpProfile { + + public DmpProfile build(File xmlFile) throws IOException { + DmpProfile dmpProfile = new DmpProfile(); + JAXBContext jaxbContext = null; + try { + jaxbContext = JAXBContext.newInstance(DmpProfile.class); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + dmpProfile = (DmpProfile) unmarshaller.unmarshal(xmlFile); + } catch (JAXBException e) { + e.printStackTrace(); + } + + return dmpProfile; + } + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DMPProfileFieldDataType.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DMPProfileFieldDataType.java new file mode 100644 index 000000000..e5d685162 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DMPProfileFieldDataType.java @@ -0,0 +1,23 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "datatype") +public class DMPProfileFieldDataType { + + private int datatype; + + @XmlAttribute(name = "datatype") + public int getDatatype() { + return datatype; + } + + public void setDatatype(int datatype) { + this.datatype = datatype; + } + + public eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.DMPProfileFieldDataType toDmpProfileCompositeModel() { + return eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.DMPProfileFieldDataType.fromInteger(datatype); + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DMPProfileType.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DMPProfileType.java new file mode 100644 index 000000000..a13117dce --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DMPProfileType.java @@ -0,0 +1,26 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel; + + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "type") +public class DMPProfileType { + + private int type; + + @XmlAttribute(name = "type") + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.DMPProfileType toDmpProfileCompositeModel() { + return eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.DMPProfileType.fromInteger(type); + } + + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DmpProfile.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DmpProfile.java new file mode 100644 index 000000000..3e9138a46 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DmpProfile.java @@ -0,0 +1,33 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel; + +import eu.eudat.data.entities.DMPProfile; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Date; +import java.util.UUID; + +@XmlRootElement(name = "root") +public class DmpProfile { + + private DmpProfileDefinition dmpProfileDefinition; + + @XmlElement(name = "definition") + public DmpProfileDefinition getDmpProfileDefinition() { + return dmpProfileDefinition; + } + + public void setDmpProfileDefinition(DmpProfileDefinition dmpProfileDefinition) { + this.dmpProfileDefinition = dmpProfileDefinition; + } + + public eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel toDmpProfileCompositeModel(String label) { + eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel dmpProfileModel = new eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel(); + dmpProfileModel.setLabel(label); + dmpProfileModel.setStatus(DMPProfile.Status.SAVED.getValue()); + dmpProfileModel.setCreated(new Date()); + dmpProfileModel.setModified(new Date()); + dmpProfileModel.setDefinition(this.dmpProfileDefinition.toDmpProfileCompositeModel()); + return dmpProfileModel; + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DmpProfileDefinition.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DmpProfileDefinition.java new file mode 100644 index 000000000..4cc4977e4 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/DmpProfileDefinition.java @@ -0,0 +1,36 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel; + + +import eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel.FieldSets; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.LinkedList; +import java.util.List; + +@XmlRootElement(name = "definition") +public class DmpProfileDefinition { + + private FieldSets fieldSets; + + @XmlElement(name = "fieldSets") + public FieldSets getFieldSets() { + return fieldSets; + } + + public void setFieldSets(FieldSets fieldSets) { + this.fieldSets = fieldSets; + } + + public eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile toDmpProfileCompositeModel() { + eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile dmpProfileDefinitionModel = new eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile(); + List dmpProfileDefinitionFields = new LinkedList<>(); + for (Field field:this.fieldSets.fields) { + dmpProfileDefinitionFields.add(field.toDmpProfileCompositeModel()); + } + dmpProfileDefinitionModel.setFields(dmpProfileDefinitionFields); + return dmpProfileDefinitionModel; + } + + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/Field.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/Field.java new file mode 100644 index 000000000..28ca0a0f6 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/Field.java @@ -0,0 +1,90 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel; + + + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; +import java.util.UUID; + +@XmlRootElement(name = "field") +public class Field { + + private String id; + + private String label; + + private int dataType; + + private boolean required; + + private int type; + +// private Object value; + + @XmlAttribute(name = "id") + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @XmlAttribute(name = "label") + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + @XmlAttribute(name = "dataType") + public int getDataType() { + return dataType; + } + + public void setDataType(int dataType) { + this.dataType = dataType; + } + + @XmlAttribute(name = "required") + public boolean isRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + + @XmlAttribute(name = "type") + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + +// @XmlValue +// public Object getValue() { +// return value; +// } +// +// public void setValue(Object value) { +// this.value = value; +// } + + public eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.Field toDmpProfileCompositeModel() { + eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.Field field = new eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.Field(); + field.setId(UUID.fromString(this.id)); + field.setDataType(eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.DMPProfileFieldDataType.fromInteger(this.dataType)); + field.setLabel(this.label); + field.setRequired(this.required); + field.setType(eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.types.DMPProfileType.fromInteger(this.type)); + // field.setValue(this.value); + return field; + } + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/FieldSets.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/FieldSets.java new file mode 100644 index 000000000..b8f56fd70 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/xml/dmpXml/dmpProfileModel/FieldSets.java @@ -0,0 +1,22 @@ +package eu.eudat.logic.utilities.documents.xml.dmpXml.dmpProfileModel; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.LinkedList; +import java.util.List; + +@XmlRootElement(name = "fieldSets") +public class FieldSets { + + List fields; + + @XmlElement(name = "field") + public List getField() { + return fields; + } + + public void setField(List fields) { + this.fields = fields; + } + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/listingmodels/DataManagementPlanProfileListingModel.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/listingmodels/DataManagementPlanProfileListingModel.java index a672de65c..9f1426a34 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/data/listingmodels/DataManagementPlanProfileListingModel.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/data/listingmodels/DataManagementPlanProfileListingModel.java @@ -81,6 +81,7 @@ public class DataManagementPlanProfileListingModel implements DataModel
- - {{formGroup.get('label').getError('backendError').message}} - {{'GENERAL.VALIDATION.REQUIRED' | translate}} + + + {{formGroup.get('label').getError('backendError').message}} + + {{'GENERAL.VALIDATION.REQUIRED' | translate}}

{{'DMP-PROFILE-EDITOR.FIELDS.TITLE' | translate}}

-
+
- - {{'GENERAL.VALIDATION.REQUIRED' | translate}} - {{fieldFormGroup.get('label').getError('backendError').message}} + + + {{'GENERAL.VALIDATION.REQUIRED' | translate}} + + {{fieldFormGroup.get('label').getError('backendError').message}} - - {{ + + {{ getDMPProfileFieldTypeWithLanguage(fieldType) | translate}} - {{'GENERAL.VALIDATION.REQUIRED' | translate}} - {{fieldFormGroup.get('type').getError('backendError').message}} + + {{'GENERAL.VALIDATION.REQUIRED' | translate}} + + {{fieldFormGroup.get('type').getError('backendError').message}} - - {{ + + {{ getDMPProfileFieldDataTypeWithLanguage(fieldDataType) | translate}} - {{'GENERAL.VALIDATION.REQUIRED' | translate}} - {{fieldFormGroup.get('dataType').getError('backendError').message}} + + {{'GENERAL.VALIDATION.REQUIRED' | translate}} + + {{fieldFormGroup.get('dataType').getError('backendError').message}}
- {{'DMP-PROFILE-EDITOR.FIELDS.REQUIRED' | translate}} + + {{'DMP-PROFILE-EDITOR.FIELDS.REQUIRED' | translate}}
-
-
+
-
-
-
+
+ + +
-
+ \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/dmp-profile/editor/dmp-profile-editor.component.ts b/dmp-frontend/src/app/ui/admin/dmp-profile/editor/dmp-profile-editor.component.ts index 04261650e..d76f07c83 100644 --- a/dmp-frontend/src/app/ui/admin/dmp-profile/editor/dmp-profile-editor.component.ts +++ b/dmp-frontend/src/app/ui/admin/dmp-profile/editor/dmp-profile-editor.component.ts @@ -2,18 +2,19 @@ import { AfterViewInit, Component } from '@angular/core'; import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; +import * as FileSaver from 'file-saver'; import { takeUntil } from 'rxjs/operators'; import { environment } from '../../../../../environments/environment'; import { ValidationErrorModel } from '../../../../common/forms/validation/error-model/validation-error-model'; import { BaseComponent } from '../../../../core/common/base/base.component'; import { DmpProfileFieldDataType } from '../../../../core/common/enum/dmp-profile-field-type'; +import { DmpProfileStatus } from '../../../../core/common/enum/dmp-profile-status'; import { DmpProfileType } from '../../../../core/common/enum/dmp-profile-type'; import { DmpProfile } from '../../../../core/model/dmp-profile/dmp-profile'; import { DmpProfileService } from '../../../../core/services/dmp/dmp-profile.service'; import { SnackBarNotificationLevel, UiNotificationService } from '../../../../core/services/notification/ui-notification-service'; import { EnumUtils } from '../../../../core/services/utilities/enum-utils.service'; import { DmpProfileEditorModel, DmpProfileFieldEditorModel } from './dmp-profile-editor.model'; -import { DmpProfileStatus } from '../../../../core/common/enum/dmp-profile-status'; @Component({ selector: 'app-dmp-profile-editor-component', @@ -23,9 +24,11 @@ import { DmpProfileStatus } from '../../../../core/common/enum/dmp-profile-statu export class DmpProfileEditorComponent extends BaseComponent implements AfterViewInit { isNew = true; + viewOnly = false; dmpProfileModel: DmpProfileEditorModel; formGroup: FormGroup = null; host = environment.Server; + dmpProfileId: string; constructor( private dmpProfileService: DmpProfileService, @@ -42,15 +45,19 @@ export class DmpProfileEditorComponent extends BaseComponent implements AfterVie this.route.params .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { - const itemId = params['id']; + this.dmpProfileId = params['id']; - if (itemId != null) { + if (this.dmpProfileId != null) { this.isNew = false; - this.dmpProfileService.getSingle(itemId).map(data => data as DmpProfile) + this.dmpProfileService.getSingle(this.dmpProfileId).map(data => data as DmpProfile) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dmpProfileModel = new DmpProfileEditorModel().fromModel(data); this.formGroup = this.dmpProfileModel.buildForm(); + if (this.dmpProfileModel.status == DmpProfileStatus.Finalized) { + this.formGroup.disable(); + this.viewOnly = true + } }); } else { this.dmpProfileModel = new DmpProfileEditorModel(); @@ -176,10 +183,47 @@ export class DmpProfileEditorComponent extends BaseComponent implements AfterVie delete() { this.formGroup.get('status').setValue(DmpProfileStatus.Deleted); this.dmpProfileService.createDmp(this.formGroup.value) - .pipe(takeUntil(this._destroyed)) - .subscribe( - complete => this.onCallbackSuccess(), - error => this.onCallbackError(error) - ); + .pipe(takeUntil(this._destroyed)) + .subscribe( + complete => this.onCallbackSuccess(), + error => this.onCallbackError(error) + ); + } + + finalize() { + //const data = this.form.value; + this.formGroup.get('status').setValue(DmpProfileStatus.Finalized); + + this.onSubmit(); + } + + downloadXML(): void { + this.dmpProfileService.downloadXML(this.dmpProfileId) + .pipe(takeUntil(this._destroyed)) + .subscribe(response => { + const blob = new Blob([response.body], { type: 'application/xml' }); + const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition')); + + FileSaver.saveAs(blob, filename); + }); + } + + + getFilenameFromContentDispositionHeader(header: string): string { + const regex: RegExp = new RegExp(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/g); + + const matches = header.match(regex); + let filename: string; + for (let i = 0; i < matches.length; i++) { + const match = matches[i]; + if (match.includes('filename="')) { + filename = match.substring(10, match.length - 1); + break; + } else if (match.includes('filename=')) { + filename = match.substring(9); + break; + } + } + return filename; } } \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.html b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.html new file mode 100644 index 000000000..24d21c5dc --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.html @@ -0,0 +1,23 @@ +
+
+
+

{{ data.message }}

+
+
+ + +
+
+
+ + + +
+
+
+
+
\ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.scss b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.scss new file mode 100644 index 000000000..226db6ce8 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.scss @@ -0,0 +1,3 @@ +.hidden { + display: none; +} \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.ts b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.ts new file mode 100644 index 000000000..0846e2896 --- /dev/null +++ b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component.ts @@ -0,0 +1,53 @@ +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; +import { Inject, Component } from '@angular/core'; + + +@Component({ + selector: 'app-dialog-confirmation-upload', + templateUrl: './dialog-confirmation-upload-profiles.component.html', + styleUrls: ['./dialog-confirmation-upload-profiles.component.scss'] +}) +export class DialodConfirmationUploadDmpProfiles { + + sizeError = false; + btnColore:String="primary"; + selectFile =false; + maxFileSize: number = 1048576; + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any + ) { } + + + selectXML(event) { + const file: FileList = event.target.files; + const size: number = file[0].size; // Get file size. + this.sizeError = size > this.maxFileSize; // Checks if file size is valid. + const formdata: FormData = new FormData(); + if (!this.sizeError) { + this.data.file = file; + this.selectFile=true; + this.btnColore="primary"; + }else{ + this.btnColore="warn"; + } + this.data.name = file[0].name; + } + + cancel() { + this.data.sucsess = false; + this.dialogRef.close(this.data); + } + + confirm() { + this.data.name = this.data.name; + this.data.sucsess = true; + this.dialogRef.close(this.data); + } + + hasProfile():boolean{ + return (this.selectFile && !this.sizeError); + } + +} diff --git a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.html b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.html index 59c344f18..3e496f325 100644 --- a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.html +++ b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.html @@ -1,9 +1,18 @@
+ - + + +
+
+ + +
-
+ \ No newline at end of file diff --git a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.ts b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.ts index 94a6e514b..9bb4ec2f2 100644 --- a/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.ts +++ b/dmp-frontend/src/app/ui/admin/dmp-profile/listing/criteria/dmp-profile-criteria.component.ts @@ -4,6 +4,11 @@ import { ProjectListingModel } from '../../../../../core/model/project/project-l import { DmpCriteria } from '../../../../../core/query/dmp/dmp-criteria'; import { DmpProfileCriteria } from '../../../../../core/query/dmp/dmp-profile-criteria'; import { BaseCriteriaComponent } from '../../../../misc/criteria/base-criteria.component'; +import { DialodConfirmationUploadDmpProfiles } from './dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component'; +import { MatDialog } from '@angular/material'; +import { TranslateService } from '@ngx-translate/core'; +import { takeUntil } from 'rxjs/operators'; +import { DmpProfileService } from '../../../../../core/services/dmp/dmp-profile.service'; @Component({ selector: 'app-dmp-profile-criteria-component', @@ -17,8 +22,10 @@ export class DmpProfileCriteriaComponent extends BaseCriteriaComponent implement public criteria: DmpProfileCriteria = new DmpProfileCriteria(); filteringProjectsAsync = false; filteredProjects: ProjectListingModel[]; - constructor( + private dmpProfileService: DmpProfileService, + private dialog: MatDialog, + private language: TranslateService ) { super(new ValidationErrorModel()); } @@ -44,4 +51,25 @@ export class DmpProfileCriteriaComponent extends BaseCriteriaComponent implement this.refreshCallback(); } } + + + openDialog(): void { + const dialogRef = this.dialog.open(DialodConfirmationUploadDmpProfiles, { + data: { + message: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML-FILE-TITLE'), + confirmButton: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML'), + cancelButton: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML-FILE-CANCEL'), + name: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML-NAME'), + file: FileList, + sucsess: false + } + }); + dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(data => { + if (data.sucsess && data.name != null && data.file != null) { + this.dmpProfileService.uploadFile(data.file, data.name) + .pipe(takeUntil(this._destroyed)) + .subscribe(); + } + }); + } }