From 596eb3a93789473434cb61dee776d10bd19a7863 Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Mon, 30 Jan 2017 09:34:34 +0200 Subject: [PATCH] proposed solution for creating electronic records with setting properties inside the FilePlanComponentProperties object using multipart upload --- .../igCoreAPI/FilePlanComponentAPI.java | 6 +-- .../community/util/FilePlanComponentMix.java | 50 +++++++++++++++++++ .../rest/rm/community/util/PojoUtility.java | 35 +++++++++++++ .../ElectronicRecordTests.java | 8 +++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/FilePlanComponentMix.java diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java index 92132e8926..88b3c31c8a 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java @@ -32,6 +32,7 @@ import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanCo import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryObject; import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryString; import static org.alfresco.rest.rm.community.util.PojoUtility.toJson; +import static org.alfresco.rest.rm.community.util.PojoUtility.toJsonElectronicRecord; import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.springframework.http.HttpMethod.DELETE; import static org.springframework.http.HttpMethod.GET; @@ -231,7 +232,7 @@ public class FilePlanComponentAPI extends RMModelRequest /** * Create electronic record from file resource - * + * * @param electronicRecordModel {@link FilePlanComponent} for electronic record to be created * @param recordContent {@link File} pointing to the content of the electronic record to be created * @param parentId parent container id @@ -253,8 +254,7 @@ public class FilePlanComponentAPI extends RMModelRequest * to the request. */ RequestSpecBuilder builder = getRMRestWrapper().configureRequestSpec(); - JsonNode root = new ObjectMapper().readTree(toJson(electronicRecordModel)); - + JsonNode root = new ObjectMapper().readTree(toJsonElectronicRecord(electronicRecordModel)); // add request fields Iterator fieldNames = root.fieldNames(); while (fieldNames.hasNext()) diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/FilePlanComponentMix.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/FilePlanComponentMix.java new file mode 100644 index 0000000000..580fe13049 --- /dev/null +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/FilePlanComponentMix.java @@ -0,0 +1,50 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.rest.rm.community.util; + +import com.fasterxml.jackson.annotation.JsonUnwrapped; + +import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentProperties; + +/** + * Mix class for FilePlanComponent POJO class + * Mix-in annotations are: a way to associate annotations with classes + * without modifying (target) classes themselves. + * + * @author Rodica Sutu + * @since 2.6 + */ +public abstract class FilePlanComponentMix +{ + /** + * Annotation used to indicate that a property should be serialized "unwrapped" + * Its properties are instead included as properties of its containing Object + */ + @JsonUnwrapped + abstract FilePlanComponentProperties getProperties(); + +} diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java index 7653f8ffc0..2d0eb321e7 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java @@ -34,6 +34,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent; + /** * Utility class for creating the json object * @@ -71,4 +73,37 @@ public class PojoUtility return e.toString(); } } + + + /** + * Converting object to JSON string for electronic records + * + * @param model The java object model to convert + * @throws JsonProcessingException Throws exceptions if the given object doesn't match to the POJO class model + */ + public static String toJsonElectronicRecord(Object model) throws JsonProcessingException + { + ObjectMapper mapper = new ObjectMapper(); + //inject the "mix-in" annotations from FilePlanComponentMix to + // FilePlanComponent POJO class when converting to json + mapper.addMixIn(FilePlanComponent.class, FilePlanComponentMix.class); + //include only values that differ from default settings to be included + mapper.setSerializationInclusion(Include.NON_DEFAULT); + try + { + + //return the json object + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model); + + } catch (JsonGenerationException e) + { + return e.toString(); + } catch (JsonMappingException e) + { + return e.toString(); + } catch (IOException e) + { + return e.toString(); + } + } } diff --git a/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/fileplancomponents/ElectronicRecordTests.java b/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/fileplancomponents/ElectronicRecordTests.java index 99fb80b1c6..3322e4b346 100644 --- a/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/fileplancomponents/ElectronicRecordTests.java +++ b/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/fileplancomponents/ElectronicRecordTests.java @@ -49,6 +49,7 @@ import org.alfresco.rest.rm.community.base.BaseRMRestTest; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentContent; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentFields; +import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentProperties; import org.alfresco.rest.rm.community.requests.igCoreAPI.FilePlanComponentAPI; import org.alfresco.utility.report.Bug; import org.testng.annotations.DataProvider; @@ -272,6 +273,11 @@ public class ElectronicRecordTests extends BaseRMRestTest .mimeType("text/plain") .build() ) + .properties(FilePlanComponentProperties + .builder() + .description(ELECTRONIC_RECORD_NAME) + .build() + ) .relativePath(RELATIVE_PATH) .build(); @@ -283,6 +289,8 @@ public class ElectronicRecordTests extends BaseRMRestTest // get newly created electronic record and verify its properties assertTrue(filePlanComponentsAPI.getFilePlanComponent(recordCreated.getId()) .getName().startsWith(ELECTRONIC_RECORD_NAME)); + assertTrue(filePlanComponentsAPI.getFilePlanComponent(recordCreated.getId()) + .getProperties().getDescription().equals(ELECTRONIC_RECORD_NAME)); assertTrue(filePlanComponentsAPI.getFilePlanComponent(recordCreated.getParentId()) .getName().equals(FOLDER_NAME)); //get newly created electronic record using the relativePath