From e9dfb3f64be2a9af75dd5570cd1165787131746e Mon Sep 17 00:00:00 2001 From: Gavin Cornwell Date: Fri, 12 Jun 2009 15:15:19 +0000 Subject: [PATCH] - Refactored FormData class to remove unecessary API layer and fixed up fallout - Renamed 'definition' webscript to 'formdefiniton' (no change to URL) - Simplified and improved formdefinition webscript JS/FTL git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14698 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- source/java/org/alfresco/repo/forms/Form.java | 2 +- .../org/alfresco/repo/forms/FormData.java | 83 +++++++++++++---- .../repo/forms/FormServiceImplTest.java | 88 ++++++++----------- .../processor/node/NodeFormProcessor.java | 5 +- .../processor/node/TypeFormProcessor.java | 76 ++++++++-------- .../repo/forms/script/ScriptFormData.java | 6 +- 6 files changed, 149 insertions(+), 111 deletions(-) diff --git a/source/java/org/alfresco/repo/forms/Form.java b/source/java/org/alfresco/repo/forms/Form.java index d1e6783b87..030d455d7f 100644 --- a/source/java/org/alfresco/repo/forms/Form.java +++ b/source/java/org/alfresco/repo/forms/Form.java @@ -188,7 +188,7 @@ public class Form this.data = new FormData(); } - this.data.addData(fieldName, fieldData); + this.data.addFieldData(fieldName, fieldData); } /* diff --git a/source/java/org/alfresco/repo/forms/FormData.java b/source/java/org/alfresco/repo/forms/FormData.java index b8d6560a3a..5d7b1de845 100644 --- a/source/java/org/alfresco/repo/forms/FormData.java +++ b/source/java/org/alfresco/repo/forms/FormData.java @@ -25,8 +25,10 @@ package org.alfresco.repo.forms; import java.io.InputStream; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; import org.apache.commons.lang.NotImplementedException; @@ -35,8 +37,11 @@ import org.apache.commons.lang.NotImplementedException; * * @author Gavin Cornwell */ -public class FormData +public class FormData implements Iterable { + // TODO: Once we fully support file based FieldData add other methods + // in here to retrieve just file fields, just data fields etc. + protected Map data; /** @@ -48,35 +53,83 @@ public class FormData } /** - * Returns the data + * Determines whether field data for the given item exists. * - * @return Map of DataItem objects representing the data + * @param fieldName Name of field to look for + * @return true if the field exists, false otherwise */ - public Map getData() + public boolean hasFieldData(String fieldName) { - return this.data; + return this.data.containsKey(fieldName); } /** - * Sets the form data + * Returns the data for the given field. * - * @param data Map of DataItem objects representing the data + * @param fieldName Name of field to look for + * @return FieldData object representing the data for + * the field or null if it doesn't exist */ - public void setData(Map data) + public FieldData getFieldData(String fieldName) { - this.data = data; + return this.data.get(fieldName); } /** - * Adds the given data to the form + * Adds the given data to the form. + * If data for the given field is already present it will be + * overwritten. * - * @param name The name of the data - * @param value The value of the data + * @param fieldName The name of the field + * @param fieldValue The value of the data */ - public void addData(String name, Object value) + public void addFieldData(String fieldName, Object fieldValue) { - FieldData item = new FieldData(name, value, false); - this.data.put(name, item); + FieldData item = new FieldData(fieldName, fieldValue, false); + this.data.put(fieldName, item); + } + + /** + * Removes the data associated with the given field + * if it exists. + * + * @param fieldName Name of the field to remove + */ + public void removeFieldData(String fieldName) + { + this.data.remove(fieldName); + } + + /** + * Returns a list of the names of the fields held by this + * object. + * + * @return List of String objects + */ + public Set getFieldNames() + { + return this.data.keySet(); + } + + /** + * Returns the number of fields data is being held for. + * + * @return Number of fields + */ + public int getNumberOfFields() + { + return this.data.size(); + } + + /** + * Returns an Iterator over the FieldData objects + * held by this object. + * + * @return Iterator of FieldData + */ + public Iterator iterator() + { + return this.data.values().iterator(); } /* diff --git a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java index 1e422634ac..97dd98ba13 100644 --- a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java +++ b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java @@ -350,18 +350,16 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the form data FormData data = form.getFormData(); assertNotNull("Expecting form data", data); - Map fieldData = data.getData(); - assertNotNull("Expecting field data", fieldData); - assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); - assertEquals(VALUE_DESCRIPTION, fieldData.get(descField.getDataKeyName()).getValue()); - assertEquals(VALUE_MIMETYPE, fieldData.get(mimetypeField.getDataKeyName()).getValue()); - assertEquals(VALUE_ENCODING, fieldData.get(encodingField.getDataKeyName()).getValue()); - assertEquals(VALUE_ORIGINATOR, fieldData.get(originatorField.getDataKeyName()).getValue()); - assertEquals(VALUE_ADDRESSEE, fieldData.get(addresseeField.getDataKeyName()).getValue()); - assertEquals(VALUE_SUBJECT, fieldData.get(subjectField.getDataKeyName()).getValue()); - assertTrue("Expecting size to be > 0", ((Long)fieldData.get(sizeField.getDataKeyName()).getValue()).longValue() > 0); + assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue()); + assertEquals(VALUE_DESCRIPTION, data.getFieldData(descField.getDataKeyName()).getValue()); + assertEquals(VALUE_MIMETYPE, data.getFieldData(mimetypeField.getDataKeyName()).getValue()); + assertEquals(VALUE_ENCODING, data.getFieldData(encodingField.getDataKeyName()).getValue()); + assertEquals(VALUE_ORIGINATOR, data.getFieldData(originatorField.getDataKeyName()).getValue()); + assertEquals(VALUE_ADDRESSEE, data.getFieldData(addresseeField.getDataKeyName()).getValue()); + assertEquals(VALUE_SUBJECT, data.getFieldData(subjectField.getDataKeyName()).getValue()); + assertTrue("Expecting size to be > 0", ((Long)data.getFieldData(sizeField.getDataKeyName()).getValue()).longValue() > 0); - String addressees = (String)fieldData.get(addresseesField.getDataKeyName()).getValue(); + String addressees = (String)data.getFieldData(addresseesField.getDataKeyName()).getValue(); assertNotNull(addressees); assertTrue("Expecting the addressees value to have at least 1 comma", addressees.indexOf(",") != -1); String[] addresseesArr = StringUtils.delimitedListToStringArray(addressees, ","); @@ -372,10 +370,10 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest Calendar calTestValue = Calendar.getInstance(); calTestValue.setTime(VALUE_SENT_DATE); Calendar calServiceValue = Calendar.getInstance(); - calServiceValue.setTime((Date)fieldData.get(sentDateField.getDataKeyName()).getValue()); + calServiceValue.setTime((Date)data.getFieldData(sentDateField.getDataKeyName()).getValue()); assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis()); - List targets = (List)fieldData.get(referencesField.getDataKeyName()).getValue(); + List targets = (List)data.getFieldData(referencesField.getDataKeyName()).getValue(); assertEquals("Expecting 1 target", 1, targets.size()); assertEquals(this.associatedDoc.toString(), targets.get(0)); } @@ -481,25 +479,23 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the form data FormData data = form.getFormData(); assertNotNull("Expecting form data", data); - Map fieldData = data.getData(); - assertNotNull("Expecting field data", fieldData); - assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue()); - assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); - assertEquals(VALUE_MIMETYPE, fieldData.get(mimetypeField.getDataKeyName()).getValue()); - assertEquals(VALUE_SUBJECT, fieldData.get(subjectField.getDataKeyName()).getValue()); - assertEquals(USER_ONE, fieldData.get(modifierField.getDataKeyName()).getValue()); + assertEquals(this.documentName, data.getFieldData(nameField.getDataKeyName()).getValue()); + assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue()); + assertEquals(VALUE_MIMETYPE, data.getFieldData(mimetypeField.getDataKeyName()).getValue()); + assertEquals(VALUE_SUBJECT, data.getFieldData(subjectField.getDataKeyName()).getValue()); + assertEquals(USER_ONE, data.getFieldData(modifierField.getDataKeyName()).getValue()); - Date modifiedDate = (Date)fieldData.get(modifiedField.getDataKeyName()).getValue(); + Date modifiedDate = (Date)data.getFieldData(modifiedField.getDataKeyName()).getValue(); assertNotNull("Expecting to find modified date", modifiedDate); assertTrue("Expecting modified field to return a Date", (modifiedDate instanceof Date)); Calendar calTestValue = Calendar.getInstance(); calTestValue.setTime(VALUE_SENT_DATE); Calendar calServiceValue = Calendar.getInstance(); - calServiceValue.setTime((Date)fieldData.get(sentDateField.getDataKeyName()).getValue()); + calServiceValue.setTime((Date)data.getFieldData(sentDateField.getDataKeyName()).getValue()); assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis()); - List targets = (List)fieldData.get(referencesField.getDataKeyName()).getValue(); + List targets = (List)data.getFieldData(referencesField.getDataKeyName()).getValue(); assertEquals("Expecting 1 target", 1, targets.size()); assertEquals(this.associatedDoc.toString(), targets.get(0)); } @@ -553,10 +549,8 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the form data FormData data = form.getFormData(); assertNotNull("Expecting form data", data); - Map fieldData = data.getData(); - assertNotNull("Expecting field data", fieldData); - assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue()); - assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); + assertEquals(this.documentName, data.getFieldData(nameField.getDataKeyName()).getValue()); + assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue()); } public void testForcedFieldsDocForm() throws Exception @@ -621,11 +615,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the form data FormData data = form.getFormData(); assertNotNull("Expecting form data", data); - Map fieldData = data.getData(); - assertNotNull("Expecting field data", fieldData); - assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue()); - assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); - assertNull("Didn't expect to find a value for cm:author", fieldData.get(authorField.getDataKeyName())); + assertEquals(this.documentName, data.getFieldData(nameField.getDataKeyName()).getValue()); + assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue()); + assertNull("Didn't expect to find a value for cm:author", data.getFieldData(authorField.getDataKeyName())); } @SuppressWarnings("unchecked") @@ -694,11 +686,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the form data FormData data = form.getFormData(); assertNotNull("Expecting form data", data); - Map fieldData = data.getData(); - assertNotNull("Expecting field data", fieldData); - assertEquals(this.folderName, fieldData.get(nameField.getDataKeyName()).getValue()); + assertEquals(this.folderName, data.getFieldData(nameField.getDataKeyName()).getValue()); - List children = (List)fieldData.get(containsField.getDataKeyName()).getValue(); + List children = (List)data.getFieldData(containsField.getDataKeyName()).getValue(); assertEquals("Expecting 3 children", 3, children.size()); assertEquals(this.document.toString(), children.get(0)); assertEquals(this.associatedDoc.toString(), children.get(1)); @@ -720,37 +710,37 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // update the name String newName = "new-" + this.documentName; - data.addData("prop_cm_name", newName); + data.addFieldData("prop_cm_name", newName); // update the title property String newTitle = "This is the new title property"; - data.addData("prop_cm_title", newTitle); + data.addFieldData("prop_cm_title", newTitle); // update the mimetype String newMimetype = MimetypeMap.MIMETYPE_HTML; - data.addData("prop_mimetype", newMimetype); + data.addFieldData("prop_mimetype", newMimetype); // update the author property (this is on an aspect not applied) String newAuthor = "Gavin Cornwell"; - data.addData("prop_cm_author", newAuthor); + data.addFieldData("prop_cm_author", newAuthor); // update the originator String newOriginator = "jane@example.com"; - data.addData("prop_cm_originator", newOriginator); + data.addFieldData("prop_cm_originator", newOriginator); // update the adressees, add another String newAddressees = VALUE_ADDRESSEES1 + "," + VALUE_ADDRESSEES2 + "," + VALUE_ADDRESSEES3; - data.addData("prop_cm_addressees", newAddressees); + data.addFieldData("prop_cm_addressees", newAddressees); // set the date to null (using an empty string) - data.addData("prop_cm_sentdate", ""); + data.addFieldData("prop_cm_sentdate", ""); // add an association to the child doc (as an attachment which is defined on an aspect not applied) - //data.addData("assoc_cm_attachments_added", this.childDoc.toString()); + //data.addField("assoc_cm_attachments_added", this.childDoc.toString()); // try and update non-existent properties (make sure there are no exceptions) - data.addData("prop_cm_wrong", "This should not be persisted"); - data.addData("cm_wrong", "This should not be persisted"); + data.addFieldData("prop_cm_wrong", "This should not be persisted"); + data.addFieldData("cm_wrong", "This should not be persisted"); // persist the data this.formService.saveForm(new Item(NODE_FORM_ITEM_KIND, this.document.toString()), data); @@ -912,11 +902,11 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // supply the name String name = "new-" + this.documentName; - data.addData("prop_cm_name", name); + data.addFieldData("prop_cm_name", name); // supply the title property String title = "This is the title property"; - data.addData("prop_cm_title", title); + data.addFieldData("prop_cm_title", title); // persist the data (without a destination and make sure it fails) try @@ -931,7 +921,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest } // supply the destination - data.addData("destination", this.folder.toString()); + data.addFieldData("destination", this.folder.toString()); // persist the data NodeRef newNode = (NodeRef)this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "cm:content"), data); diff --git a/source/java/org/alfresco/repo/forms/processor/node/NodeFormProcessor.java b/source/java/org/alfresco/repo/forms/processor/node/NodeFormProcessor.java index c426317298..b31d7e956c 100644 --- a/source/java/org/alfresco/repo/forms/processor/node/NodeFormProcessor.java +++ b/source/java/org/alfresco/repo/forms/processor/node/NodeFormProcessor.java @@ -900,12 +900,11 @@ public class NodeFormProcessor extends FilteredFormProcessor Map childAssocDefs = typeDef.getChildAssociations(); Map propDefs = typeDef.getProperties(); - Map propsToPersist = new HashMap(data.getData().size()); + Map propsToPersist = new HashMap(data.getNumberOfFields()); List assocsToPersist = new ArrayList(); - for (String dataKey : data.getData().keySet()) + for (FieldData fieldData : data) { - FieldData fieldData = data.getData().get(dataKey); // NOTE: ignore file fields for now, not supported yet! if (fieldData.isFile() == false) { diff --git a/source/java/org/alfresco/repo/forms/processor/node/TypeFormProcessor.java b/source/java/org/alfresco/repo/forms/processor/node/TypeFormProcessor.java index a2f0ed41b1..5e5fda5e07 100644 --- a/source/java/org/alfresco/repo/forms/processor/node/TypeFormProcessor.java +++ b/source/java/org/alfresco/repo/forms/processor/node/TypeFormProcessor.java @@ -239,48 +239,44 @@ public class TypeFormProcessor extends NodeFormProcessor if (data != null) { - Map fieldData = data.getData(); - if (fieldData != null) + // firstly, ensure we have a destination to create the node in + NodeRef parentRef = null; + FieldData destination = data.getFieldData(DESTINATION); + if (destination == null) { - // firstly, ensure we have a destination to create the node in - NodeRef parentRef = null; - FieldData destination = fieldData.get(DESTINATION); - if (destination == null) - { - throw new FormException("Failed to persist form for '" + - typeDef.getName().toPrefixString(this.namespaceService) + - "' as destination data was not present."); - } - - // create the parent NodeRef - parentRef = new NodeRef((String)destination.getValue()); - - // TODO: determine what association to use when creating the node in the destination, - // defaults to ContentModel.ASSOC_CONTAINS - - // if a name property is present in the form data use it as the node name, - // otherwise generate a guid - String nodeName = null; - FieldData nameData = fieldData.get(NAME_PROP_DATA); - if (nameData != null) - { - nodeName = (String)nameData.getValue(); - - // remove the name data otherwise 'rename' gets called in persistNode - fieldData.remove(NAME_PROP_DATA); - } - if (nodeName == null || nodeName.length() == 0) - { - nodeName = GUID.generate(); - } - - // create the node - Map nodeProps = new HashMap(1); - nodeProps.put(ContentModel.PROP_NAME, nodeName); - nodeRef = this.nodeService.createNode(parentRef, ContentModel.ASSOC_CONTAINS, - QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(nodeName)), - typeDef.getName(), nodeProps).getChildRef(); + throw new FormException("Failed to persist form for '" + + typeDef.getName().toPrefixString(this.namespaceService) + + "' as destination data was not present."); } + + // create the parent NodeRef + parentRef = new NodeRef((String)destination.getValue()); + + // TODO: determine what association to use when creating the node in the destination, + // defaults to ContentModel.ASSOC_CONTAINS + + // if a name property is present in the form data use it as the node name, + // otherwise generate a guid + String nodeName = null; + FieldData nameData = data.getFieldData(NAME_PROP_DATA); + if (nameData != null) + { + nodeName = (String)nameData.getValue(); + + // remove the name data otherwise 'rename' gets called in persistNode + data.removeFieldData(NAME_PROP_DATA); + } + if (nodeName == null || nodeName.length() == 0) + { + nodeName = GUID.generate(); + } + + // create the node + Map nodeProps = new HashMap(1); + nodeProps.put(ContentModel.PROP_NAME, nodeName); + nodeRef = this.nodeService.createNode(parentRef, ContentModel.ASSOC_CONTAINS, + QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(nodeName)), + typeDef.getName(), nodeProps).getChildRef(); } return nodeRef; diff --git a/source/java/org/alfresco/repo/forms/script/ScriptFormData.java b/source/java/org/alfresco/repo/forms/script/ScriptFormData.java index 2665d04e65..26f0df6225 100644 --- a/source/java/org/alfresco/repo/forms/script/ScriptFormData.java +++ b/source/java/org/alfresco/repo/forms/script/ScriptFormData.java @@ -53,10 +53,10 @@ public class ScriptFormData implements Serializable ScriptableHashMap result = new ScriptableHashMap(); if (this.formData != null) { - for (String k : formData.getData().keySet()) + for (FieldData fieldData : formData) { - ScriptFieldData wrappedFieldData = new ScriptFieldData(formData.getData().get(k)); - result.put(k, wrappedFieldData); + ScriptFieldData wrappedFieldData = new ScriptFieldData(fieldData); + result.put(fieldData.getName(), wrappedFieldData); } } return result;