mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
RM-1764 (Simplified Relationship View)
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@90782 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>(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<String> getRelationships(NodeRef nodeRef)
|
||||
{
|
||||
List<String> relationships = new ArrayList<String>();
|
||||
|
||||
Set<Relationship> relationshipsFrom = getRelationshipService().getRelationshipsFrom(nodeRef);
|
||||
relationships.addAll(buildRelationshipData(relationshipsFrom, RelationshipEndPoint.TARGET));
|
||||
|
||||
Set<Relationship> 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<String> buildRelationshipData(Set<Relationship> relationships, RelationshipEndPoint relationshipEndPoint)
|
||||
{
|
||||
List<String> result = new ArrayList<String>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user