diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/custompropdefinitions.get.json.ftl b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/custompropdefinitions.get.json.ftl index bbdee48371..083a4cc902 100644 --- a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/custompropdefinitions.get.json.ftl +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/custompropdefinitions.get.json.ftl @@ -1,43 +1,43 @@ <#escape x as jsonUtils.encodeJSONString(x)> { - "data": - { - "customProperties": - { - <#list customProps as prop> - "${prop.name.toPrefixString()}": - { - "dataType": "<#if prop.dataType??>${prop.dataType.name.toPrefixString()}", - "label": "${prop.title!""}", - "description": "${prop.description!""}", - "mandatory": ${prop.mandatory?string}, - "multiValued": ${prop.multiValued?string}, - "defaultValue": "${prop.defaultValue!""}", - "protected": ${prop.protected?string}, - "propId": "${prop.name.localName}", - "constraintRefs": - [ - <#list prop.constraints as con> - { - "name": "${con.constraint.shortName!""}", - "title": "${con.title!""}", - "type": "${con.constraint.type!""}", - "parameters": - { - <#-- Basic implementation. Only providing 2 hardcoded parameters. --> - <#assign lov = con.constraint.parameters["allowedValues"]> - "caseSensitive": ${con.constraint.parameters["caseSensitive"]?string}, - "listOfValues" : - [ - <#list lov as val>"${val}"<#if val_has_next>, - ] - } - }<#if con_has_next>, - - ] - }<#if prop_has_next>, - - } - } + "data": + { + "customProperties": + { + <#list customProps as prop> + "${prop.name.toPrefixString()}": + { + "dataType": "<#if prop.dataType??>${prop.dataType.name.toPrefixString()}", + "label": "${prop.title!""}", + "description": "${prop.description!""}", + "mandatory": ${prop.mandatory?string}, + "multiValued": ${prop.multiValued?string}, + "defaultValue": "${prop.defaultValue!""}", + "protected": ${prop.protected?string}, + "propId": "${prop.name.localName}", + "constraintRefs": + [ + <#list prop.constraints as con> + { + "name": "${con.constraint.shortName!""}", + "title": "${con.constraint.title!""}", + "type": "${con.constraint.type!""}", + "parameters": + { + <#-- Basic implementation. Only providing 2 hardcoded parameters. --> + <#assign lov = con.constraint.parameters["allowedValues"]> + "caseSensitive": ${con.constraint.parameters["caseSensitive"]?string}, + "listOfValues" : + [ + <#list lov as val>"${val}"<#if val_has_next>, + ] + } + }<#if con_has_next>, + + ] + }<#if prop_has_next>, + + } + } } \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/CustomPropertyDefinitionPut.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/CustomPropertyDefinitionPut.java index 71f11e7c68..9567e4b5b0 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/CustomPropertyDefinitionPut.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/CustomPropertyDefinitionPut.java @@ -22,24 +22,27 @@ import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import org.alfresco.module.org_alfresco_module_rm.CustomMetadataException; +import org.alfresco.module.org_alfresco_module_rm.PropertyAlreadyExistsMetadataException; import org.alfresco.module.org_alfresco_module_rm.RecordsManagementAdminService; +import org.alfresco.service.cmr.dictionary.ConstraintDefinition; import org.alfresco.service.namespace.QName; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONTokener; import org.springframework.extensions.surf.util.ParameterCheck; import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptRequest; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONTokener; /** * Implementation for Java backed webscript to update RM custom property definitions * in the custom model. - * + * * @author Neil McErlean */ public class CustomPropertyDefinitionPut extends BaseCustomPropertyWebScript @@ -86,30 +89,30 @@ public class CustomPropertyDefinitionPut extends BaseCustomPropertyWebScript throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je); } - + return ftlModel; } /** * Applies custom properties. - * @throws CustomMetadataException + * @throws CustomMetadataException */ protected Map handlePropertyDefinitionUpdate(WebScriptRequest req, JSONObject json) throws JSONException, CustomMetadataException { Map result = new HashMap(); - + Map params = getParamsFromUrlAndJson(req, json); - + QName propertyQName; propertyQName = updatePropertyDefinition(params); String localName = propertyQName.getLocalName(); - + result.put(PROP_ID, localName); - + String urlResult = req.getServicePath(); result.put(URL, urlResult); - + return result; } @@ -117,15 +120,16 @@ public class CustomPropertyDefinitionPut extends BaseCustomPropertyWebScript * If label has a non-null value, it is set on the property def. * If constraintRef has a non-null value, it is set on this propDef. * If constraintRef has a null value, all constraints for that propDef are removed. - * + * * @param params * @return - * @throws CustomMetadataException + * @throws CustomMetadataException */ protected QName updatePropertyDefinition(Map params) throws CustomMetadataException { QName result = null; - + boolean updated = false; + String propId = (String)params.get(PROP_ID); ParameterCheck.mandatoryString("propId", propId); @@ -135,45 +139,71 @@ public class CustomPropertyDefinitionPut extends BaseCustomPropertyWebScript throw new WebScriptException(Status.STATUS_NOT_FOUND, "Could not find property definition for: " + propId); } - + if (params.containsKey(PARAM_CONSTRAINT_REF)) { - String constraintRef = (String)params.get(PARAM_CONSTRAINT_REF); - - if (constraintRef == null) - { - result = rmAdminService.removeCustomPropertyDefinitionConstraints(propQName); - } - else - { - QName constraintRefQName = QName.createQName(constraintRef, namespaceService); - result = rmAdminService.setCustomPropertyDefinitionConstraint(propQName, constraintRefQName); - } + String constraintRef = (String)params.get(PARAM_CONSTRAINT_REF); + List constraints = rmAdminService.getCustomPropertyDefinitions().get(propQName).getConstraints(); + + if (constraintRef == null) + { + result = rmAdminService.removeCustomPropertyDefinitionConstraints(propQName); + updated = constraints.isEmpty() ? false : true; + } + else + { + boolean exists = false; + for (ConstraintDefinition constraintDefinition : constraints) + { + if (constraintDefinition.getConstraint().getShortName().equalsIgnoreCase(constraintRef)) + { + exists = true; + break; + } + } + if (exists == false) + { + QName constraintRefQName = QName.createQName(constraintRef, namespaceService); + result = rmAdminService.setCustomPropertyDefinitionConstraint(propQName, constraintRefQName); + updated = true; + } + } } if (params.containsKey(PARAM_LABEL)) { String label = (String)params.get(PARAM_LABEL); - result = rmAdminService.updateCustomPropertyDefinitionName(propQName, label); + try + { + result = rmAdminService.updateCustomPropertyDefinitionName(propQName, label); + } + catch (PropertyAlreadyExistsMetadataException ex) + { + if (updated == false) + { + String propIdAsString = rmAdminService.getQNameForClientId(label).toPrefixString(namespaceService); + throw new PropertyAlreadyExistsMetadataException(propIdAsString); + } + } } return result; } - @SuppressWarnings("unchecked") + @SuppressWarnings("rawtypes") protected Map getParamsFromUrlAndJson(WebScriptRequest req, JSONObject json) throws JSONException { Map params; params = new HashMap(); - + Map templateVars = req.getServiceMatch().getTemplateVars(); String propId = templateVars.get(PROP_ID); if (propId != null) { params.put(PROP_ID, (Serializable)propId); } - + for (Iterator iter = json.keys(); iter.hasNext(); ) { String nextKeyString = (String)iter.next(); @@ -182,10 +212,10 @@ public class CustomPropertyDefinitionPut extends BaseCustomPropertyWebScript { nextValueString = json.getString(nextKeyString); } - + params.put(nextKeyString, nextValueString); } - + return params; } } \ No newline at end of file