Implemented localization of list constraint values on Workflow Task properties in the workflow REST API.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@32197 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
N Smith
2011-11-22 17:01:00 +00:00
parent 886a83ac30
commit 8e790151c3
3 changed files with 74 additions and 26 deletions

View File

@@ -28,6 +28,8 @@
</#if> </#if>
"properties": "properties":
<@propertiesJSON properties=task.properties />, <@propertiesJSON properties=task.properties />,
"propertyLabels":
<@propertyLabelsJSON propertyLabels=task.propertyLabels />,
"workflowInstance": "workflowInstance":
<@workflowInstanceJSON workflowInstance=task.workflowInstance/><#if detailed>, <@workflowInstanceJSON workflowInstance=task.workflowInstance/><#if detailed>,
"definition": "definition":
@@ -96,6 +98,22 @@
</#escape> </#escape>
</#macro> </#macro>
<#-- Renders a map of property labels -->
<#macro propertyLabelsJSON propertyLabels>
<#escape x as jsonUtils.encodeJSONString(x)>
{
<#list propertyLabels?keys as key>
"${key}":
<#if propertyLabels[key]??>
"${propertyLabels[key]}"
<#else>
null
</#if><#if (key_has_next)>,</#if>
</#list>
}
</#escape>
</#macro>
<#-- Renders a workflow instance. --> <#-- Renders a workflow instance. -->
<#macro workflowInstanceJSON workflowInstance detailed=false> <#macro workflowInstanceJSON workflowInstance detailed=false>
<#escape x as jsonUtils.encodeJSONString(x)> <#escape x as jsonUtils.encodeJSONString(x)>

View File

@@ -28,7 +28,6 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.security.authentication.AuthenticationComponent;
@@ -45,9 +44,7 @@ import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.SearchService; import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthorityService; import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.MutableAuthenticationService; import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.workflow.WorkflowDefinition; import org.alfresco.service.cmr.workflow.WorkflowDefinition;
import org.alfresco.service.cmr.workflow.WorkflowInstance; import org.alfresco.service.cmr.workflow.WorkflowInstance;
@@ -478,6 +475,10 @@ public abstract class AbstractWorkflowRestApiTest extends BaseWebScriptTest
assertTrue(properties.has("bpm_priority")); assertTrue(properties.has("bpm_priority"));
assertTrue(properties.has("bpm_description")); assertTrue(properties.has("bpm_description"));
assertTrue(properties.has("bpm_reassignable")); assertTrue(properties.has("bpm_reassignable"));
JSONObject labels =taskJson.getJSONObject("propertyLabels");
assertNotNull(labels);
assertTrue(labels.has("bpm_status"));
} }
private void checkWorkflowTaskReadOnly(JSONObject taskJson) throws Exception private void checkWorkflowTaskReadOnly(JSONObject taskJson) throws Exception

View File

@@ -26,12 +26,16 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.repo.workflow.WorkflowModel; import org.alfresco.repo.workflow.WorkflowModel;
import org.alfresco.repo.workflow.WorkflowQNameConverter;
import org.alfresco.service.cmr.dictionary.AssociationDefinition; import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.Constraint;
import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
import org.alfresco.service.cmr.dictionary.PropertyDefinition; import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition; import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.AssociationRef; import org.alfresco.service.cmr.repository.AssociationRef;
@@ -50,9 +54,11 @@ import org.alfresco.service.cmr.workflow.WorkflowTaskDefinition;
import org.alfresco.service.cmr.workflow.WorkflowTaskQuery; import org.alfresco.service.cmr.workflow.WorkflowTaskQuery;
import org.alfresco.service.cmr.workflow.WorkflowTaskState; import org.alfresco.service.cmr.workflow.WorkflowTaskState;
import org.alfresco.service.cmr.workflow.WorkflowTransition; import org.alfresco.service.cmr.workflow.WorkflowTransition;
import org.alfresco.service.namespace.NamespaceException;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.alfresco.util.collections.CollectionUtils;
import org.alfresco.util.collections.Function;
import org.springframework.extensions.surf.util.ISO8601DateFormat; import org.springframework.extensions.surf.util.ISO8601DateFormat;
/** /**
@@ -67,6 +73,7 @@ public class WorkflowModelBuilder
public static final String PERSON_AVATAR = "avatarUrl"; public static final String PERSON_AVATAR = "avatarUrl";
public static final String TASK_PROPERTIES = "properties"; public static final String TASK_PROPERTIES = "properties";
public static final String TASK_PROPERTIY_LABELS = "propertyLabels";
public static final String TASK_OWNER = "owner"; public static final String TASK_OWNER = "owner";
public static final String TASK_STATE = "state"; public static final String TASK_STATE = "state";
public static final String TASK_DESCRIPTION = "description"; public static final String TASK_DESCRIPTION = "description";
@@ -141,23 +148,21 @@ public class WorkflowModelBuilder
public static final String WORKFLOW_DEFINITION_START_TASK_DEFINITION_TYPE = "startTaskDefinitionType"; public static final String WORKFLOW_DEFINITION_START_TASK_DEFINITION_TYPE = "startTaskDefinitionType";
public static final String WORKFLOW_DEFINITION_TASK_DEFINITIONS = "taskDefinitions"; public static final String WORKFLOW_DEFINITION_TASK_DEFINITIONS = "taskDefinitions";
private static final String PREFIX_SEPARATOR = Character.toString(QName.NAMESPACE_PREFIX);
private final NamespaceService namespaceService;
private final NodeService nodeService; private final NodeService nodeService;
private final PersonService personService; private final PersonService personService;
private final WorkflowService workflowService; private final WorkflowService workflowService;
private final AuthenticationService authenticationService; private final AuthenticationService authenticationService;
private final WorkflowQNameConverter qNameConverter;
public WorkflowModelBuilder(NamespaceService namespaceService, NodeService nodeService, public WorkflowModelBuilder(NamespaceService namespaceService, NodeService nodeService,
AuthenticationService authenticationService, PersonService personService, AuthenticationService authenticationService, PersonService personService,
WorkflowService workflowService) WorkflowService workflowService)
{ {
this.namespaceService = namespaceService;
this.nodeService = nodeService; this.nodeService = nodeService;
this.personService = personService; this.personService = personService;
this.workflowService = workflowService; this.workflowService = workflowService;
this.authenticationService = authenticationService; this.authenticationService = authenticationService;
this.qNameConverter = new WorkflowQNameConverter(namespaceService);
} }
/** /**
@@ -190,7 +195,9 @@ public class WorkflowModelBuilder
model.put(TASK_OWNER, getPersonModel(owner)); model.put(TASK_OWNER, getPersonModel(owner));
// task properties // task properties
model.put(TASK_PROPERTIES, buildProperties(task, propertyFilters)); Map<String, Object> propertyModel = buildProperties(task, propertyFilters);
model.put(TASK_PROPERTIES, propertyModel);
model.put(TASK_PROPERTIY_LABELS, buildPropertyLabels(task, propertyModel));
// workflow instance part // workflow instance part
model.put(TASK_WORKFLOW_INSTANCE, buildSimple(task.getPath().getInstance())); model.put(TASK_WORKFLOW_INSTANCE, buildSimple(task.getPath().getInstance()));
@@ -409,14 +416,43 @@ public class WorkflowModelBuilder
return buildQNameProperties(properties, keys); return buildQNameProperties(properties, keys);
} }
private Map<String, String> buildPropertyLabels(WorkflowTask task, Map<String, Object> properties)
{
TypeDefinition taskType = task.getDefinition().getMetadata();
final Map<QName, PropertyDefinition> propDefs = taskType.getProperties();
return CollectionUtils.transform(properties, new Function<Entry<String, Object>, Pair<String, String>>()
{
@Override
public Pair<String, String> apply(Entry<String, Object> entry)
{
String propName = entry.getKey();
PropertyDefinition propDef = propDefs.get(qNameConverter.mapNameToQName(propName));
if(propDef != null )
{
List<ConstraintDefinition> constraints = propDef.getConstraints();
for (ConstraintDefinition constraintDef : constraints)
{
Constraint constraint = constraintDef.getConstraint();
if (constraint instanceof ListOfValuesConstraint)
{
ListOfValuesConstraint listConstraint = (ListOfValuesConstraint) constraint;
String label = listConstraint.getDisplayLabel(entry.getValue().toString());
return new Pair<String, String>(propName, label);
}
}
}
return null;
}
});
}
private Map<String, Object> buildQNameProperties(Map<QName, Serializable> properties, Collection<QName> keys) private Map<String, Object> buildQNameProperties(Map<QName, Serializable> properties, Collection<QName> keys)
{ {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
for (QName key : keys) for (QName key : keys)
{ {
Object value = convertValue(properties.get(key)); Object value = convertValue(properties.get(key));
String prefixedKey = key.toPrefixString(namespaceService); String strKey = qNameConverter.mapQNameToName(key);
String strKey = prefixedKey.replace(PREFIX_SEPARATOR, "_");
model.put(strKey, value); model.put(strKey, value);
} }
return model; return model;
@@ -445,21 +481,14 @@ public class WorkflowModelBuilder
private Collection<QName> buildQNameKeys(Collection<String> keys) private Collection<QName> buildQNameKeys(Collection<String> keys)
{ {
List<QName> qKeys = new ArrayList<QName>(keys.size()); return CollectionUtils.transform(keys, new Function<String, QName>()
for (String key : keys)
{ {
String prefixedName = key.replaceFirst("_", PREFIX_SEPARATOR); @Override
try public QName apply(String name)
{ {
QName qKey = QName.createQName(prefixedName, namespaceService); return qNameConverter.mapNameToQName(name);
qKeys.add(qKey);
} }
catch (NamespaceException e) });
{
throw new AlfrescoRuntimeException("Invalid property key: " + key, e);
}
}
return qKeys;
} }
private Map<String, Object> getPersonModel(Serializable nameSer) private Map<String, Object> getPersonModel(Serializable nameSer)
@@ -622,7 +651,7 @@ public class WorkflowModelBuilder
private String getUrl(TypeDefinition typeDefinition) private String getUrl(TypeDefinition typeDefinition)
{ {
return "api/classes/" + typeDefinition.getName().toPrefixString().replace(PREFIX_SEPARATOR, "_"); return "api/classes/" + qNameConverter.mapQNameToName(typeDefinition.getName());
} }
private String getUrl(WorkflowPath path) private String getUrl(WorkflowPath path)