From 6b54bd082f7f0cbb3c26ada31f66b84d57909889 Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Mon, 25 Sep 2017 22:45:32 +0300 Subject: [PATCH 1/5] create relationships with v0 API requests --- .../org/alfresco/rest/core/v0/BaseAPI.java | 27 +++- .../rest/v0/CustomDefinitionsAPI.java | 137 ++++++++++++++++++ 2 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java index 9e6e90063e..19a5db90ac 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java @@ -87,7 +87,7 @@ public abstract class BaseAPI @Autowired private ContentService contentService; - private static final String NODE_REF_WORKSPACE_SPACES_STORE = "workspace://SpacesStore/"; + protected static final String NODE_REF_WORKSPACE_SPACES_STORE = "workspace://SpacesStore/"; private static final String FILE_PLAN_PATH = "/Sites/rm/documentLibrary"; /** @@ -640,4 +640,29 @@ public abstract class BaseAPI { return FILE_PLAN_PATH; } + + /** + * Used to set RM items properties + * including records, categories and folders + */ + public enum CUSTOM_DEFINITIONS + { + ATTACHMENT("Attachment"), + MESSAGE("Message"), + NEXT_VERSION("Next Version"), + RENDITION("Rendition"); + String definition; + + private CUSTOM_DEFINITIONS(String definition) + { + this.definition = definition; + } + + public String getDefinition() + { + return definition; + } + + } + } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java new file mode 100644 index 0000000000..6a29ae5b09 --- /dev/null +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java @@ -0,0 +1,137 @@ +/* + * #%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.v0; + +import java.text.MessageFormat; + +import org.alfresco.rest.core.v0.BaseAPI; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * Methods to make API requests using v0 API on Records Management Custom Model Reference Definitions + * + * @author Rodica Sutu + * @since 2.6 + */ +@Component +public class CustomDefinitionsAPI extends BaseAPI +{ + /** + * custom references endpoint + */ + private static final String CUSTOM_REFERENCE_API_ENDPOINT = "{0}rma/admin/customreferencedefinitions"; + + /** + * create reference endpoint + */ + private static final String CREATE_RELATIONSHIP_API_ENDPOINT = "{0}node/{1}/customreferences"; + + // logger + private static final Logger LOGGER = LoggerFactory.getLogger(CustomDefinitionsAPI.class); + + /** + * Helper method to get custom references definitions + * + * @param adminUser user with administrative privileges + * @param adminPassword password for adminUser + * @param customDefinition custom reference definition name + */ + + public String getCustomReferencesId(String adminUser, String adminPassword, String customDefinition) + { + + JSONObject getResponse = doGetRequest(adminUser, adminPassword, CUSTOM_REFERENCE_API_ENDPOINT); + if (getResponse != null) + try + { + JSONArray customDefinitions = getResponse.getJSONObject("data").getJSONArray("customReferences"); + for (int i = 0; i < customDefinitions.length(); i++) + { + JSONObject item = customDefinitions.getJSONObject(i); + if (customDefinition.equalsIgnoreCase(item.has("source") ? item.getString("source") : null) || + customDefinition.equalsIgnoreCase(item.has("target") ? item.getString("target") : null) || + customDefinition.equalsIgnoreCase(item.has("label") ? item.getString("label") : null) + ) + { + return item.getString("refId"); + } + } + + } + catch (JSONException error) + { + LOGGER.error("Unable to get the refId for the custom reference definition " + customDefinition); + } + return null; + } + + /** + * Helper method to add custom reference instance to the specified record node + * + * @param adminUser user with administrative privileges + * @param adminPassword password for adminUser + * @param recordNodeIdFrom node ref to set a custom reference + * @param recordNodeIdto node ref of the to record + * @return true if creating relationship was successful, + * falseotherwise + */ + public boolean createRelationship( + String adminUser, + String adminPassword, + String recordNodeIdFrom, + String recordNodeIdto, + CUSTOM_DEFINITIONS relationshipType) + { + try + { + //create the request body + JSONObject requestParams = new JSONObject(); + requestParams.put("recordNodeIdto", NODE_REF_WORKSPACE_SPACES_STORE + recordNodeIdto); + requestParams.put("refId", getCustomReferencesId(adminUser, adminPassword, relationshipType + .getDefinition())); + //send the API request to create the relationship + JSONObject setRelationshipStatus = doPostRequest(adminUser, adminPassword, requestParams, + MessageFormat.format(CREATE_RELATIONSHIP_API_ENDPOINT, "{0}", NODE_PREFIX + recordNodeIdFrom)); + //check the response + if (setRelationshipStatus != null) + { + return setRelationshipStatus.getBoolean("success"); + } + } + catch (JSONException error) + { + LOGGER.error("Unable to extract response parameter", error); + } + return false; + } + +} From da50858cfc8a38ef30d4d588c203d5dc6637b33d Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Tue, 26 Sep 2017 09:51:15 +0300 Subject: [PATCH 2/5] fix the java docs --- .../src/main/java/org/alfresco/rest/core/v0/BaseAPI.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java index 19a5db90ac..125fba650f 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java @@ -642,8 +642,7 @@ public abstract class BaseAPI } /** - * Used to set RM items properties - * including records, categories and folders + * List of existing records management custom references. */ public enum CUSTOM_DEFINITIONS { @@ -662,7 +661,5 @@ public abstract class BaseAPI { return definition; } - } - } From 30959017ee4cd644267348ed7354b398b7e53024 Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Tue, 26 Sep 2017 16:41:16 +0300 Subject: [PATCH 3/5] fix the request body sent when creating relationships --- .../main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java index 6a29ae5b09..257ac05bb7 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java @@ -115,7 +115,7 @@ public class CustomDefinitionsAPI extends BaseAPI { //create the request body JSONObject requestParams = new JSONObject(); - requestParams.put("recordNodeIdto", NODE_REF_WORKSPACE_SPACES_STORE + recordNodeIdto); + requestParams.put("toNode", NODE_REF_WORKSPACE_SPACES_STORE + recordNodeIdto); requestParams.put("refId", getCustomReferencesId(adminUser, adminPassword, relationshipType .getDefinition())); //send the API request to create the relationship From 9e5f53bd09a76df1c3ffa9b3e0ea194cf3aafead Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Wed, 27 Sep 2017 09:17:54 +0300 Subject: [PATCH 4/5] review comments changes --- .../org/alfresco/rest/core/v0/BaseAPI.java | 22 ------- .../model/custom/CustomDefinitions.java | 63 +++++++++++++++++++ .../rest/v0/CustomDefinitionsAPI.java | 50 ++++++++++----- 3 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/model/custom/CustomDefinitions.java diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java index 125fba650f..c4844156aa 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java @@ -640,26 +640,4 @@ public abstract class BaseAPI { return FILE_PLAN_PATH; } - - /** - * List of existing records management custom references. - */ - public enum CUSTOM_DEFINITIONS - { - ATTACHMENT("Attachment"), - MESSAGE("Message"), - NEXT_VERSION("Next Version"), - RENDITION("Rendition"); - String definition; - - private CUSTOM_DEFINITIONS(String definition) - { - this.definition = definition; - } - - public String getDefinition() - { - return definition; - } - } } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/model/custom/CustomDefinitions.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/model/custom/CustomDefinitions.java new file mode 100644 index 0000000000..5e94f25d91 --- /dev/null +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/model/custom/CustomDefinitions.java @@ -0,0 +1,63 @@ +/* + * #%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.model.custom; + +/** + * List of existing records management custom references. + * + * @author Rodica Sutu + * @since 2.6 + */ +public enum CustomDefinitions +{ + ATTACHMENT("Attachment"), + MESSAGE("Message"), + NEXT_VERSION("Next Version"), + RENDITION("Rendition"); + /** + * The name of custom reference. + */ + private String definition; + + /** + * Private constructor. + */ + CustomDefinitions(String definition) + { + this.definition = definition; + } + + /** + * Get the name of the custom reference. + * + * @return The value of custom reference. + */ + public String getDefinition() + { + return definition; + } +} diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java index 257ac05bb7..fd2ab72a1b 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java @@ -29,6 +29,7 @@ package org.alfresco.rest.v0; import java.text.MessageFormat; import org.alfresco.rest.core.v0.BaseAPI; +import org.alfresco.rest.rm.community.model.custom.CustomDefinitions; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -55,35 +56,52 @@ public class CustomDefinitionsAPI extends BaseAPI */ private static final String CREATE_RELATIONSHIP_API_ENDPOINT = "{0}node/{1}/customreferences"; - // logger + /** + * logger + */ private static final Logger LOGGER = LoggerFactory.getLogger(CustomDefinitionsAPI.class); /** - * Helper method to get custom references definitions + * Helper method to get the reference id for a custom reference * * @param adminUser user with administrative privileges * @param adminPassword password for adminUser * @param customDefinition custom reference definition name + * @return reference id if the customDefinition is found + * null otherwise + * */ - - public String getCustomReferencesId(String adminUser, String adminPassword, String customDefinition) + public String getCustomReferenceId(String adminUser, String adminPassword, String customDefinition) { JSONObject getResponse = doGetRequest(adminUser, adminPassword, CUSTOM_REFERENCE_API_ENDPOINT); if (getResponse != null) + { try { JSONArray customDefinitions = getResponse.getJSONObject("data").getJSONArray("customReferences"); for (int i = 0; i < customDefinitions.length(); i++) { JSONObject item = customDefinitions.getJSONObject(i); - if (customDefinition.equalsIgnoreCase(item.has("source") ? item.getString("source") : null) || - customDefinition.equalsIgnoreCase(item.has("target") ? item.getString("target") : null) || - customDefinition.equalsIgnoreCase(item.has("label") ? item.getString("label") : null) - ) + boolean hasSource = customDefinition.equalsIgnoreCase( + item.has("source") ? item.getString("source") : null + ); + + boolean hasTarget = customDefinition.equalsIgnoreCase( + item.has("target") ? item.getString("target") : null + ); + + boolean hasLabel = customDefinition.equalsIgnoreCase( + item.has("label") ? item.getString("label") : null + ); + if ( hasSource || hasTarget || hasLabel) { return item.getString("refId"); } + else + { + return null; + } } } @@ -91,6 +109,7 @@ public class CustomDefinitionsAPI extends BaseAPI { LOGGER.error("Unable to get the refId for the custom reference definition " + customDefinition); } + } return null; } @@ -100,23 +119,24 @@ public class CustomDefinitionsAPI extends BaseAPI * @param adminUser user with administrative privileges * @param adminPassword password for adminUser * @param recordNodeIdFrom node ref to set a custom reference - * @param recordNodeIdto node ref of the to record - * @return true if creating relationship was successful, - * falseotherwise + * @param recordNodeIdTo node ref of the to record + * @param relationshipType relation type to be created + * @return true if creating relationship was successful, + * false otherwise */ public boolean createRelationship( String adminUser, String adminPassword, String recordNodeIdFrom, - String recordNodeIdto, - CUSTOM_DEFINITIONS relationshipType) + String recordNodeIdTo, + CustomDefinitions relationshipType) { try { //create the request body JSONObject requestParams = new JSONObject(); - requestParams.put("toNode", NODE_REF_WORKSPACE_SPACES_STORE + recordNodeIdto); - requestParams.put("refId", getCustomReferencesId(adminUser, adminPassword, relationshipType + requestParams.put("toNode", NODE_REF_WORKSPACE_SPACES_STORE + recordNodeIdTo); + requestParams.put("refId", getCustomReferenceId(adminUser, adminPassword, relationshipType .getDefinition())); //send the API request to create the relationship JSONObject setRelationshipStatus = doPostRequest(adminUser, adminPassword, requestParams, From 9e18aa7f0c7d8b25d10996d1f3e456a4b090623b Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Wed, 27 Sep 2017 15:59:29 +0300 Subject: [PATCH 5/5] fix the failure introduced with the last commit --- .../main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java index fd2ab72a1b..0529374f83 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CustomDefinitionsAPI.java @@ -98,10 +98,6 @@ public class CustomDefinitionsAPI extends BaseAPI { return item.getString("refId"); } - else - { - return null; - } } }