From 6fba4602edcd0ca232875b43d17a18ae56dfebe1 Mon Sep 17 00:00:00 2001 From: Tuna Aksoy Date: Sun, 23 Nov 2014 21:23:21 +0000 Subject: [PATCH] RM-1765 (Simplified Add Relationship Dialog) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@90948 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../rm-webscript-context.xml | 7 + .../rma/relationshiplabels.get.desc.xml | 9 + .../rma/relationshiplabels.get.json.ftl | 16 ++ .../script/RelationshipLabelsGet.java | 218 ++++++++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.desc.xml create mode 100644 rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.json.ftl create mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipLabelsGet.java 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 616d1ed95c..2a005730dc 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 @@ -634,4 +634,11 @@ + + + + + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.desc.xml b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.desc.xml new file mode 100644 index 0000000000..6518ea45a2 --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.desc.xml @@ -0,0 +1,9 @@ + + Records Management Relationship Labels + Gets the list of existing relationship labels. + /api/rma/admin/relationshiplabels + + user + required + internal + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.json.ftl b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.json.ftl new file mode 100644 index 0000000000..b000b08c1d --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/relationshiplabels.get.json.ftl @@ -0,0 +1,16 @@ +<#escape x as jsonUtils.encodeJSONString(x)> +{ + "data": + { + "relationshipLabels": + [ + <#list relationshipLabels as relationshipLabel> + { + "label": "${relationshipLabel.label}", + "uniqueName": "${relationshipLabel.uniqueName}" + }<#if relationshipLabel_has_next>, + + ] + } +} + \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipLabelsGet.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipLabelsGet.java new file mode 100644 index 0000000000..3f52761471 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RelationshipLabelsGet.java @@ -0,0 +1,218 @@ +/* + * 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 static org.alfresco.util.ParameterCheck.mandatoryString; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDefinition; +import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipDisplayName; +import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipService; +import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipType; +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 relationship labels. + * + * @author Tuna Aksoy + * @since 2.3 + */ +public class RelationshipLabelsGet extends AbstractRmWebScript +{ + /** Constants */ + private static final String RELATIONSHIP_LABELS = "relationshipLabels"; + + /** Relationship service */ + private RelationshipService relationshipService; + + /** + * Gets the relationship service + * + * @return The relationship service + */ + protected RelationshipService getRelationshipService() + { + return this.relationshipService; + } + + /** + * Sets the relationship service + * + * @param relationshipService The relationship service + */ + public void setRelationshipService(RelationshipService relationshipService) + { + this.relationshipService = relationshipService; + } + + /** + * @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); + model.put(RELATIONSHIP_LABELS, getRelationshipsLabels()); + return model; + } + + /** + * Gets the list of available relationship labels + * + * @return The list of available relationship labels + */ + private List getRelationshipsLabels() + { + List relationshipLabels = new ArrayList(); + + Set relationshipDefinitions = getRelationshipService().getRelationshipDefinitions(); + for (RelationshipDefinition relationshipDefinition : relationshipDefinitions) + { + RelationshipType type = relationshipDefinition.getType(); + String uniqueName = relationshipDefinition.getUniqueName(); + RelationshipDisplayName displayName = relationshipDefinition.getDisplayName(); + String sourceText = displayName.getSourceText(); + String targetText = displayName.getTargetText(); + + if (RelationshipType.PARENTCHILD.equals(type)) + { + relationshipLabels.add(new RelationshipLabel(sourceText, uniqueName)); + relationshipLabels.add(new RelationshipLabel(targetText, uniqueName)); + } + else if (RelationshipType.BIDIRECTIONAL.equals(type)) + { + if (!sourceText.equals(targetText)) + { + throw new WebScriptException( + Status.STATUS_BAD_REQUEST, + "The source '" + + sourceText + + "' and target text '" + + targetText + + "' must be the same for a bidirectional relationship."); + } + relationshipLabels.add(new RelationshipLabel(sourceText, uniqueName)); + } + else + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unknown relationship type '" + type + "'."); + } + } + + return sortRelationshipLabelsByName(relationshipLabels); + } + + /** + * Helper method to sort the relationship labels by their names + * + * @param relationshipLabels Relationship labels to sort + * @return Sorted list of relationship labels + */ + private List sortRelationshipLabelsByName(List relationshipLabels) + { + Collections.sort(relationshipLabels, new Comparator() + { + @Override + public int compare(RelationshipLabel r1, RelationshipLabel r2) + { + return r1.getLabel().toLowerCase().compareTo(r2.getLabel().toLowerCase()); + } + }); + return relationshipLabels; + } + + /** + * Relationship label helper class + */ + public class RelationshipLabel + { + /** Label of the relationship */ + private String label; + + /** Unique name of the relationship */ + private String uniqueName; + + /** + * Constructor + * + * @param label Label of the relationship + * @param uniqueName Unique name of the relationship + */ + public RelationshipLabel(String label, String uniqueName) + { + mandatoryString("label", label); + mandatoryString("uniqueName", uniqueName); + + setLabel(label); + setUniqueName(uniqueName); + } + + /** + * Gets the label of the relationship + * + * @return The label of the relationship + */ + public String getLabel() + { + return this.label; + } + + /** + * Sets the label of the relationship + * + * @param label The label of the relationship + */ + private void setLabel(String label) + { + this.label = label; + } + + /** + * Gets the unique name of the relationship + * + * @return The unique name of the relationship + */ + public String getUniqueName() + { + return this.uniqueName; + } + + /** + * Sets the unique name of the relationship + * + * @param uniqueName The unique name of the relationship + */ + private void setUniqueName(String uniqueName) + { + this.uniqueName = uniqueName; + } + } +}