diff --git a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml index 6aa2eb595d..616d1ed95c 100644 --- a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml +++ b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml @@ -626,4 +626,12 @@ parent="rmBaseWebscript"> + + + + + + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationships.get.desc.xml b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationships.get.desc.xml new file mode 100644 index 0000000000..2e9204be3a --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationships.get.desc.xml @@ -0,0 +1,9 @@ + + Records Management Relationships + Gets the list of existing relationships on the specified node. + /api/node/{store_type}/{store_id}/{id}/relationships + + user + required + internal + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationships.get.json.ftl b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationships.get.json.ftl new file mode 100644 index 0000000000..ede191397c --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationships.get.json.ftl @@ -0,0 +1,15 @@ +<#escape x as jsonUtils.encodeJSONString(x)> +{ + "data": + { + "items": + [ + <#list relationships as relationship> + { + "node": <#noescape>${relationship} + }<#if relationship_has_next>, + + ] + } +} + \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipsGet.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipsGet.java new file mode 100644 index 0000000000..134cfaf0a0 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipsGet.java @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2005-2014 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * 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 . + */ +package org.alfresco.module.org_alfresco_module_rm.script; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.alfresco.module.org_alfresco_module_rm.jscript.app.JSONConversionComponent; +import org.alfresco.module.org_alfresco_module_rm.relationship.Relationship; +import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDefinition; +import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipService; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.util.WebScriptUtils; +import org.json.JSONObject; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * Implementation for Java backed webscript to get the relationships for a node. + * + * @author Tuna Aksoy + * @since 2.3 + */ +public class RelationshipsGet extends AbstractRmWebScript +{ + /** Constants */ + private static final String RELATIONSHIPS = "relationships"; + private static final String RELATIONSHIP_LABEL = "relationshipLabel"; + + /** The relationship end point */ + private enum RelationshipEndPoint + { + SOURCE, TARGET + }; + + /** Relationship service */ + private RelationshipService relationshipService; + + /** JSON conversion component */ + private JSONConversionComponent jsonConversionComponent; + + /** + * Gets the relationship service + * + * @return The relationship service + */ + protected RelationshipService getRelationshipService() + { + return this.relationshipService; + } + + /** + * Gets the JSON conversion component + * + * @return The JSON conversion component + */ + protected JSONConversionComponent getJsonConversionComponent() + { + return this.jsonConversionComponent; + } + + /** + * Sets the relationship service + * + * @param relationshipService The relationship service + */ + public void setRelationshipService(RelationshipService relationshipService) + { + this.relationshipService = relationshipService; + } + + /** + * Sets the JSON conversion component + * + * @param jsonConversionComponent The JSON conversion component + */ + public void setJsonConversionComponent(JSONConversionComponent jsonConversionComponent) + { + this.jsonConversionComponent = jsonConversionComponent; + } + + /** + * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, + * org.springframework.extensions.webscripts.Status, + * org.springframework.extensions.webscripts.Cache) + */ + @Override + protected Map executeImpl(WebScriptRequest req, Status status, Cache cache) + { + Map model = new HashMap(1); + NodeRef nodeRef = parseRequestForNodeRef(req); + model.put(RELATIONSHIPS, getRelationships(nodeRef)); + return model; + } + + /** + * Gets the relationships of a node + * + * @param nodeRef The node reference + * + * @return The list of relationships of a node + */ + private List getRelationships(NodeRef nodeRef) + { + List relationships = new ArrayList(); + + Set relationshipsFrom = getRelationshipService().getRelationshipsFrom(nodeRef); + relationships.addAll(buildRelationshipData(relationshipsFrom, RelationshipEndPoint.TARGET)); + + Set relationshipsTo = getRelationshipService().getRelationshipsTo(nodeRef); + relationships.addAll(buildRelationshipData(relationshipsTo, RelationshipEndPoint.SOURCE)); + + return relationships; + } + + /** + * Creates the relationship data + * + * @param relationships The {@link Set} of relationships + * @param relationshipEndPoint The end point of the relationship, which is either {@link RelationshipEndpoint#SOURCE} or {@link RelationshipEndpoint#TARGET} + * @return The relationship data as {@link List} + */ + private List buildRelationshipData(Set relationships, RelationshipEndPoint relationshipEndPoint) + { + List result = new ArrayList(); + + for (Relationship relationship : relationships) + { + String uniqueName = relationship.getUniqueName(); + RelationshipDefinition relationshipDefinition = getRelationshipService().getRelationshipDefinition(uniqueName); + if (relationshipDefinition != null) + { + NodeRef node; + String label; + + if (RelationshipEndPoint.SOURCE.equals(relationshipEndPoint)) + { + node = relationship.getSource(); + label = relationshipDefinition.getDisplayName().getSourceText(); + } + else if (RelationshipEndPoint.TARGET.equals(relationshipEndPoint)) + { + node = relationship.getTarget(); + label = relationshipDefinition.getDisplayName().getTargetText(); + } + else + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unknown relationship end point type '" + relationshipEndPoint + "'."); + } + + String nodeDetails = getJsonConversionComponent().toJSON(node, true); + JSONObject jsonObject = WebScriptUtils.createJSONObject(nodeDetails); + WebScriptUtils.putValuetoJSONObject(jsonObject, RELATIONSHIP_LABEL, label); + + result.add(jsonObject.toString()); + } + } + + return result; + } +} diff --git a/rm-server/source/java/org/alfresco/util/WebScriptUtils.java b/rm-server/source/java/org/alfresco/util/WebScriptUtils.java index 4410854534..131687fe7a 100644 --- a/rm-server/source/java/org/alfresco/util/WebScriptUtils.java +++ b/rm-server/source/java/org/alfresco/util/WebScriptUtils.java @@ -295,4 +295,28 @@ public final class WebScriptUtils return value; } + + /** + * Creates a json object from the given {@link String} + * + * @param json The json object as {@link String} + * @return The json object created from the given {@link String} + */ + public static JSONObject createJSONObject(String json) + { + mandatory("json", json); + + JSONObject jsonObject; + + try + { + jsonObject = new JSONObject(json); + } + catch (JSONException error) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Cannot create a json object from the given string '" + json + "'.", error); + } + + return jsonObject; + } }