From d4a6a842baeba91f1374a8eb4958e3cce4afc32b Mon Sep 17 00:00:00 2001 From: Tuna Aksoy Date: Fri, 16 Nov 2012 15:05:06 +0000 Subject: [PATCH] RM-550 (JS Error while declaring a record) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/V2.0-BUG-FIX@43661 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../action/impl/DeclareRecordAction.java | 338 +++++++++--------- .../script/RmActionPost.java | 328 +++++++++-------- 2 files changed, 325 insertions(+), 341 deletions(-) diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java index d37078ad1c..47eb99bbeb 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/action/impl/DeclareRecordAction.java @@ -29,6 +29,7 @@ import java.util.Set; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase; +import org.alfresco.repo.action.executer.ActionExecuterAbstractBase; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.dictionary.AspectDefinition; @@ -48,189 +49,174 @@ import org.springframework.extensions.surf.util.I18NUtil; */ public class DeclareRecordAction extends RMActionExecuterAbstractBase { - /** I18N */ - private static final String MSG_UNDECLARED_ONLY_RECORDS = "rm.action.undeclared-only-records"; - private static final String MSG_NO_DECLARE_MAND_PROP = "rm.action.no-declare-mand-prop"; - - /** Logger */ - private static Log logger = LogFactory.getLog(DeclareRecordAction.class); + /** I18N */ + private static final String MSG_UNDECLARED_ONLY_RECORDS = "rm.action.undeclared-only-records"; + private static final String MSG_NO_DECLARE_MAND_PROP = "rm.action.no-declare-mand-prop"; - /** - * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, - * org.alfresco.service.cmr.repository.NodeRef) - */ - @Override - protected void executeImpl(Action action, NodeRef actionedUponNodeRef) - { - if (recordsManagementService.isRecord(actionedUponNodeRef) == true) - { - if (recordsManagementService.isRecordDeclared(actionedUponNodeRef) == false) + /** Logger */ + private static Log logger = LogFactory.getLog(DeclareRecordAction.class); + + /** + * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, org.alfresco.service.cmr.repository.NodeRef) + */ + @Override + protected void executeImpl(Action action, NodeRef actionedUponNodeRef) + { + if (recordsManagementService.isRecord(actionedUponNodeRef) == true) + { + if (recordsManagementService.isRecordDeclared(actionedUponNodeRef) == false) + { + List missingProperties = new ArrayList(5); + // Aspect not already defined - check mandatory properties then add + if (mandatoryPropertiesSet(actionedUponNodeRef, missingProperties) == true) { - List missingProperties = new ArrayList(5); - // Aspect not already defined - check mandatory properties then add - if (mandatoryPropertiesSet(actionedUponNodeRef, missingProperties) == true) - { - // Add the declared aspect - Map declaredProps = new HashMap(2); - declaredProps.put(PROP_DECLARED_AT, new Date()); - declaredProps.put(PROP_DECLARED_BY, AuthenticationUtil.getRunAsUser()); - this.nodeService.addAspect(actionedUponNodeRef, ASPECT_DECLARED_RECORD, declaredProps); - - // remove all owner related rights - this.ownableService.setOwner(actionedUponNodeRef, OwnableService.NO_OWNER); - } - else - { - throw new AlfrescoRuntimeException(buildMissingPropertiesErrorString(missingProperties)); - } - } - } - else - { - throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, actionedUponNodeRef.toString())); - } - } - - private String buildMissingPropertiesErrorString(List missingProperties) - { - StringBuilder builder = new StringBuilder(255); - builder.append(I18NUtil.getMessage(MSG_NO_DECLARE_MAND_PROP)); - builder.append(" "); - for (String missingProperty : missingProperties) - { - builder.append(missingProperty) - .append(", "); - } - return builder.toString(); - } + // Add the declared aspect + Map declaredProps = new HashMap(2); + declaredProps.put(PROP_DECLARED_AT, new Date()); + declaredProps.put(PROP_DECLARED_BY, AuthenticationUtil.getRunAsUser()); + this.nodeService.addAspect(actionedUponNodeRef, ASPECT_DECLARED_RECORD, declaredProps); - /** - * Helper method to check whether all the mandatory properties of the node have been set - * - * @param nodeRef - * node reference - * @return boolean true if all mandatory properties are set, false otherwise - */ - private boolean mandatoryPropertiesSet(NodeRef nodeRef, List missingProperties) - { - boolean result = true; - - Map nodeRefProps = this.nodeService.getProperties(nodeRef); - - QName nodeRefType = this.nodeService.getType(nodeRef); - - TypeDefinition typeDef = this.dictionaryService.getType(nodeRefType); - for (PropertyDefinition propDef : typeDef.getProperties().values()) - { - if (propDef.isMandatory() == true) - { - if (nodeRefProps.get(propDef.getName()) == null) - { - logMissingProperty(propDef, missingProperties); - - result = false; - break; - } - } - } - - if (result != false) - { - Set aspects = this.nodeService.getAspects(nodeRef); - for (QName aspect : aspects) - { - AspectDefinition aspectDef = this.dictionaryService.getAspect(aspect); - for (PropertyDefinition propDef : aspectDef.getProperties().values()) - { - if (propDef.isMandatory() == true) - { - if (nodeRefProps.get(propDef.getName()) == null) - { - logMissingProperty(propDef, missingProperties); - - result = false; - break; - } - } - } - } - } - - return result; - } - - /** - * Log information about missing properties. - * - * @param propDef property definition - * @param missingProperties missing properties - */ - private void logMissingProperty(PropertyDefinition propDef, List missingProperties) - { - if (logger.isWarnEnabled()) - { - StringBuilder msg = new StringBuilder(); - msg.append("Mandatory property missing: ").append(propDef.getName()); - logger.warn(msg.toString()); - } - missingProperties.add(propDef.getName().toString()); - } - - /** - * @see org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase#getProtectedAspects() - */ - @Override - public Set getProtectedAspects() - { - HashSet qnames = new HashSet(); - qnames.add(ASPECT_DECLARED_RECORD); - return qnames; - } - - /** - * @see org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase#isExecutableImpl(org.alfresco.service.cmr.repository.NodeRef, java.util.Map, boolean) - */ - @Override - protected boolean isExecutableImpl(NodeRef filePlanComponent, Map parameters, boolean throwException) - { - if (recordsManagementService.isRecord(filePlanComponent) == true) - { - if (recordsManagementService.isRecordDeclared(filePlanComponent) == false) - { - // Aspect not already defined - check mandatory properties then add - List missingProperties = new ArrayList(10); - if (mandatoryPropertiesSet(filePlanComponent, missingProperties) == true) - { - return true; - } - else - { - if (throwException) - { - throw new AlfrescoRuntimeException(buildMissingPropertiesErrorString(missingProperties)); - } - else - { - return false; - } - } + // remove all owner related rights + this.ownableService.setOwner(actionedUponNodeRef, OwnableService.NO_OWNER); } else { - return false; + logger.debug(buildMissingPropertiesErrorString(missingProperties)); + action.setParameterValue(ActionExecuterAbstractBase.PARAM_RESULT, "missingProperties"); } - } - else - { - if (throwException) - { - throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, filePlanComponent.toString())); - } - else - { - return false; - } - } + } + } + else + { + throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, actionedUponNodeRef.toString())); + } + } + + private String buildMissingPropertiesErrorString(List missingProperties) + { + StringBuilder builder = new StringBuilder(255); + builder.append(I18NUtil.getMessage(MSG_NO_DECLARE_MAND_PROP)); + builder.append(" "); + for (String missingProperty : missingProperties) + { + builder.append(missingProperty) + .append(", "); + } + return builder.toString(); } + /** + * Helper method to check whether all the mandatory properties of the node have been set + * + * @param nodeRef node reference + * @return boolean true if all mandatory properties are set, false otherwise + */ + private boolean mandatoryPropertiesSet(NodeRef nodeRef, List missingProperties) + { + boolean result = true; + + Map nodeRefProps = this.nodeService.getProperties(nodeRef); + + QName nodeRefType = this.nodeService.getType(nodeRef); + + TypeDefinition typeDef = this.dictionaryService.getType(nodeRefType); + for (PropertyDefinition propDef : typeDef.getProperties().values()) + { + if (propDef.isMandatory() == true) + { + if (nodeRefProps.get(propDef.getName()) == null) + { + logMissingProperty(propDef, missingProperties); + + result = false; + break; + } + } + } + + if (result != false) + { + Set aspects = this.nodeService.getAspects(nodeRef); + for (QName aspect : aspects) + { + AspectDefinition aspectDef = this.dictionaryService.getAspect(aspect); + for (PropertyDefinition propDef : aspectDef.getProperties().values()) + { + if (propDef.isMandatory() == true) + { + if (nodeRefProps.get(propDef.getName()) == null) + { + logMissingProperty(propDef, missingProperties); + + result = false; + break; + } + } + } + } + } + + return result; + } + + /** + * Log information about missing properties. + * + * @param propDef property definition + * @param missingProperties missing properties + */ + private void logMissingProperty(PropertyDefinition propDef, List missingProperties) + { + if (logger.isWarnEnabled()) + { + StringBuilder msg = new StringBuilder(); + msg.append("Mandatory property missing: ").append(propDef.getName()); + logger.warn(msg.toString()); + } + missingProperties.add(propDef.getName().toString()); + } + + /** + * @see org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase#getProtectedAspects() + */ + @Override + public Set getProtectedAspects() + { + HashSet qnames = new HashSet(); + qnames.add(ASPECT_DECLARED_RECORD); + return qnames; + } + + /** + * @see org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase#isExecutableImpl(org.alfresco.service.cmr.repository.NodeRef, java.util.Map, boolean) + */ + @Override + protected boolean isExecutableImpl(NodeRef filePlanComponent, Map parameters, boolean throwException) + { + if (recordsManagementService.isRecord(filePlanComponent) == true) + { + if (recordsManagementService.isRecordDeclared(filePlanComponent) == false) + { + // Aspect not already defined - check mandatory properties then add + List missingProperties = new ArrayList(10); + if (mandatoryPropertiesSet(filePlanComponent, missingProperties) == true) + { + return true; + } + } + return false; + } + else + { + if (throwException) + { + throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, filePlanComponent.toString())); + } + else + { + return false; + } + } + } + } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RmActionPost.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RmActionPost.java index 616410a690..3c16f5d5d7 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RmActionPost.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/script/RmActionPost.java @@ -50,175 +50,173 @@ import org.json.JSONTokener; */ public class RmActionPost extends DeclarativeWebScript { - private static Log logger = LogFactory.getLog(RmActionPost.class); - - private static final String PARAM_NAME = "name"; - private static final String PARAM_NODE_REF = "nodeRef"; - private static final String PARAM_NODE_REFS = "nodeRefs"; - private static final String PARAM_PARAMS = "params"; - - private NodeService nodeService; - private RecordsManagementActionService rmActionService; - - public void setNodeService(NodeService nodeService) - { - this.nodeService = nodeService; - } + private static Log logger = LogFactory.getLog(RmActionPost.class); - public void setRecordsManagementActionService(RecordsManagementActionService rmActionService) - { - this.rmActionService = rmActionService; - } + private static final String PARAM_NAME = "name"; + private static final String PARAM_NODE_REF = "nodeRef"; + private static final String PARAM_NODE_REFS = "nodeRefs"; + private static final String PARAM_PARAMS = "params"; - @SuppressWarnings("unchecked") - @Override - public Map executeImpl(WebScriptRequest req, Status status, Cache cache) - { - String reqContentAsString; - try - { - reqContentAsString = req.getContent().getContent(); - } - catch (IOException iox) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, - "Could not read content from req.", iox); - } + private NodeService nodeService; + private RecordsManagementActionService rmActionService; - String actionName = null; - List targetNodeRefs = null; - Map actionParams = new HashMap(3); - - try - { - JSONObject jsonObj = new JSONObject(new JSONTokener(reqContentAsString)); - - // Get the action name - if (jsonObj.has(PARAM_NAME) == true) - { - actionName = jsonObj.getString(PARAM_NAME); - } - - // Get the target references - if (jsonObj.has(PARAM_NODE_REF) == true) - { - NodeRef nodeRef = new NodeRef(jsonObj.getString(PARAM_NODE_REF)); - targetNodeRefs = new ArrayList(1); - targetNodeRefs.add(nodeRef); - } - if (jsonObj.has(PARAM_NODE_REFS) == true) - { - JSONArray jsonArray = jsonObj.getJSONArray(PARAM_NODE_REFS); - if (jsonArray.length() != 0) - { - targetNodeRefs = new ArrayList(jsonArray.length()); - for (int i = 0; i < jsonArray.length(); i++) - { - NodeRef nodeRef = new NodeRef(jsonArray.getString(i)); - targetNodeRefs.add(nodeRef); - } - } - } - - // params are optional. - if (jsonObj.has(PARAM_PARAMS)) - { - JSONObject paramsObj = jsonObj.getJSONObject(PARAM_PARAMS); - for (Iterator iter = paramsObj.keys(); iter.hasNext(); ) - { - Object nextKey = iter.next(); - String nextKeyString = (String)nextKey; - Object nextValue = paramsObj.get(nextKeyString); - - // Check for date values - if (nextValue instanceof JSONObject) - { - if (((JSONObject)nextValue).has("iso8601") == true) - { - String dateStringValue = ((JSONObject)nextValue).getString("iso8601"); - nextValue = ISO8601DateFormat.parse(dateStringValue); - } - } - - actionParams.put(nextKeyString, (Serializable)nextValue); - } - } - } - catch (JSONException exception) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse request JSON."); - } - - // validate input: check for mandatory params. - // Some RM actions can be posted without a nodeRef. - if (actionName == null) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, - "A mandatory parameter has not been provided in URL"); - } + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + } - // Check that all the nodes provided exist and build report string - StringBuffer targetNodeRefsString = new StringBuffer(30); - boolean firstTime = true; - for (NodeRef targetNodeRef : targetNodeRefs) - { - if (nodeService.exists(targetNodeRef) == false) - { - throw new WebScriptException(Status.STATUS_NOT_FOUND, - "The targetNode does not exist (" + targetNodeRef.toString() + ")"); - } - - // Build the string - if (firstTime == true) - { - firstTime = false; - } - else - { - targetNodeRefsString.append(", "); - } - targetNodeRefsString.append(targetNodeRef.toString()); - } + public void setRecordsManagementActionService(RecordsManagementActionService rmActionService) + { + this.rmActionService = rmActionService; + } - // Proceed to execute the specified action on the specified node. - if (logger.isDebugEnabled()) - { - StringBuilder msg = new StringBuilder(); - msg.append("Executing Record Action ") - .append(actionName) - .append(", (") - .append(targetNodeRefsString.toString()) - .append("), ") - .append(actionParams); - logger.debug(msg.toString()); - } - - Map model = new HashMap(); - if (targetNodeRefs.isEmpty()) - { - RecordsManagementActionResult result = this.rmActionService.executeRecordsManagementAction(actionName, actionParams); - if (result.getValue() != null) - { - model.put("result", result.getValue().toString()); - } - } - else - { - Map resultMap = this.rmActionService.executeRecordsManagementAction(targetNodeRefs, actionName, actionParams); - Map results = new HashMap(resultMap.size()); - for (NodeRef nodeRef : resultMap.keySet()) - { - Object value = resultMap.get(nodeRef).getValue(); - if (value != null) - { - results.put(nodeRef.toString(), resultMap.get(nodeRef).getValue().toString()); - } - } - model.put("results", results); - } - - model.put("message", "Successfully queued action [" + actionName + "] on " + targetNodeRefsString.toString()); + @SuppressWarnings("unchecked") + @Override + public Map executeImpl(WebScriptRequest req, Status status, Cache cache) + { + String reqContentAsString; + try + { + reqContentAsString = req.getContent().getContent(); + } + catch (IOException iox) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, + "Could not read content from req.", iox); + } - return model; - } + String actionName = null; + List targetNodeRefs = new ArrayList(1); + Map actionParams = new HashMap(3); + + try + { + JSONObject jsonObj = new JSONObject(new JSONTokener(reqContentAsString)); + + // Get the action name + if (jsonObj.has(PARAM_NAME) == true) + { + actionName = jsonObj.getString(PARAM_NAME); + } + + // Get the target references + if (jsonObj.has(PARAM_NODE_REF) == true) + { + NodeRef nodeRef = new NodeRef(jsonObj.getString(PARAM_NODE_REF)); + targetNodeRefs.add(nodeRef); + } + if (jsonObj.has(PARAM_NODE_REFS) == true) + { + JSONArray jsonArray = jsonObj.getJSONArray(PARAM_NODE_REFS); + if (jsonArray.length() != 0) + { + targetNodeRefs = new ArrayList(jsonArray.length()); + for (int i = 0; i < jsonArray.length(); i++) + { + NodeRef nodeRef = new NodeRef(jsonArray.getString(i)); + targetNodeRefs.add(nodeRef); + } + } + } + + // params are optional. + if (jsonObj.has(PARAM_PARAMS)) + { + JSONObject paramsObj = jsonObj.getJSONObject(PARAM_PARAMS); + for (Iterator iter = paramsObj.keys(); iter.hasNext(); ) + { + String nextKeyString = iter.next(); + Object nextValue = paramsObj.get(nextKeyString); + + // Check for date values + if (nextValue instanceof JSONObject) + { + if (((JSONObject)nextValue).has("iso8601") == true) + { + String dateStringValue = ((JSONObject)nextValue).getString("iso8601"); + nextValue = ISO8601DateFormat.parse(dateStringValue); + } + } + + actionParams.put(nextKeyString, (Serializable)nextValue); + } + } + } + catch (JSONException exception) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse request JSON."); + } + + // validate input: check for mandatory params. + // Some RM actions can be posted without a nodeRef. + if (actionName == null) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, + "A mandatory parameter has not been provided in URL"); + } + + // Check that all the nodes provided exist and build report string + StringBuffer targetNodeRefsString = new StringBuffer(30); + boolean firstTime = true; + for (NodeRef targetNodeRef : targetNodeRefs) + { + if (nodeService.exists(targetNodeRef) == false) + { + throw new WebScriptException(Status.STATUS_NOT_FOUND, + "The targetNode does not exist (" + targetNodeRef.toString() + ")"); + } + + // Build the string + if (firstTime == true) + { + firstTime = false; + } + else + { + targetNodeRefsString.append(", "); + } + targetNodeRefsString.append(targetNodeRef.toString()); + } + + // Proceed to execute the specified action on the specified node. + if (logger.isDebugEnabled()) + { + StringBuilder msg = new StringBuilder(); + msg.append("Executing Record Action ") + .append(actionName) + .append(", (") + .append(targetNodeRefsString.toString()) + .append("), ") + .append(actionParams); + logger.debug(msg.toString()); + } + + Map model = new HashMap(); + if (targetNodeRefs.isEmpty()) + { + RecordsManagementActionResult result = this.rmActionService.executeRecordsManagementAction(actionName, actionParams); + if (result.getValue() != null) + { + model.put("result", result.getValue().toString()); + } + } + else + { + Map resultMap = this.rmActionService.executeRecordsManagementAction(targetNodeRefs, actionName, actionParams); + Map results = new HashMap(resultMap.size()); + for (NodeRef nodeRef : resultMap.keySet()) + { + Object value = resultMap.get(nodeRef).getValue(); + if (value != null) + { + results.put(nodeRef.toString(), resultMap.get(nodeRef).getValue().toString()); + } + } + model.put("results", results); + } + + model.put("message", "Successfully queued action [" + actionName + "] on " + targetNodeRefsString.toString()); + + return model; + } } \ No newline at end of file