- 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
This commit is contained in:
Gavin Cornwell
2009-06-12 15:15:19 +00:00
parent 0dcda8c629
commit e9dfb3f64b
6 changed files with 149 additions and 111 deletions

View File

@@ -188,7 +188,7 @@ public class Form
this.data = new FormData(); this.data = new FormData();
} }
this.data.addData(fieldName, fieldData); this.data.addFieldData(fieldName, fieldData);
} }
/* /*

View File

@@ -25,8 +25,10 @@
package org.alfresco.repo.forms; package org.alfresco.repo.forms;
import java.io.InputStream; import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.NotImplementedException;
@@ -35,8 +37,11 @@ import org.apache.commons.lang.NotImplementedException;
* *
* @author Gavin Cornwell * @author Gavin Cornwell
*/ */
public class FormData public class FormData implements Iterable<FormData.FieldData>
{ {
// TODO: Once we fully support file based FieldData add other methods
// in here to retrieve just file fields, just data fields etc.
protected Map<String, FieldData> data; protected Map<String, FieldData> 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<String, FieldData> 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<String, FieldData> 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 fieldName The name of the field
* @param value The value of the data * @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); FieldData item = new FieldData(fieldName, fieldValue, false);
this.data.put(name, item); 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<String> 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<FormData.FieldData> iterator()
{
return this.data.values().iterator();
} }
/* /*

View File

@@ -350,18 +350,16 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
// check the form data // check the form data
FormData data = form.getFormData(); FormData data = form.getFormData();
assertNotNull("Expecting form data", data); assertNotNull("Expecting form data", data);
Map<String, FormData.FieldData> fieldData = data.getData(); assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue());
assertNotNull("Expecting field data", fieldData); assertEquals(VALUE_DESCRIPTION, data.getFieldData(descField.getDataKeyName()).getValue());
assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); assertEquals(VALUE_MIMETYPE, data.getFieldData(mimetypeField.getDataKeyName()).getValue());
assertEquals(VALUE_DESCRIPTION, fieldData.get(descField.getDataKeyName()).getValue()); assertEquals(VALUE_ENCODING, data.getFieldData(encodingField.getDataKeyName()).getValue());
assertEquals(VALUE_MIMETYPE, fieldData.get(mimetypeField.getDataKeyName()).getValue()); assertEquals(VALUE_ORIGINATOR, data.getFieldData(originatorField.getDataKeyName()).getValue());
assertEquals(VALUE_ENCODING, fieldData.get(encodingField.getDataKeyName()).getValue()); assertEquals(VALUE_ADDRESSEE, data.getFieldData(addresseeField.getDataKeyName()).getValue());
assertEquals(VALUE_ORIGINATOR, fieldData.get(originatorField.getDataKeyName()).getValue()); assertEquals(VALUE_SUBJECT, data.getFieldData(subjectField.getDataKeyName()).getValue());
assertEquals(VALUE_ADDRESSEE, fieldData.get(addresseeField.getDataKeyName()).getValue()); assertTrue("Expecting size to be > 0", ((Long)data.getFieldData(sizeField.getDataKeyName()).getValue()).longValue() > 0);
assertEquals(VALUE_SUBJECT, fieldData.get(subjectField.getDataKeyName()).getValue());
assertTrue("Expecting size to be > 0", ((Long)fieldData.get(sizeField.getDataKeyName()).getValue()).longValue() > 0);
String addressees = (String)fieldData.get(addresseesField.getDataKeyName()).getValue(); String addressees = (String)data.getFieldData(addresseesField.getDataKeyName()).getValue();
assertNotNull(addressees); assertNotNull(addressees);
assertTrue("Expecting the addressees value to have at least 1 comma", addressees.indexOf(",") != -1); assertTrue("Expecting the addressees value to have at least 1 comma", addressees.indexOf(",") != -1);
String[] addresseesArr = StringUtils.delimitedListToStringArray(addressees, ","); String[] addresseesArr = StringUtils.delimitedListToStringArray(addressees, ",");
@@ -372,10 +370,10 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
Calendar calTestValue = Calendar.getInstance(); Calendar calTestValue = Calendar.getInstance();
calTestValue.setTime(VALUE_SENT_DATE); calTestValue.setTime(VALUE_SENT_DATE);
Calendar calServiceValue = Calendar.getInstance(); 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()); assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis());
List<String> targets = (List<String>)fieldData.get(referencesField.getDataKeyName()).getValue(); List<String> targets = (List<String>)data.getFieldData(referencesField.getDataKeyName()).getValue();
assertEquals("Expecting 1 target", 1, targets.size()); assertEquals("Expecting 1 target", 1, targets.size());
assertEquals(this.associatedDoc.toString(), targets.get(0)); assertEquals(this.associatedDoc.toString(), targets.get(0));
} }
@@ -481,25 +479,23 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
// check the form data // check the form data
FormData data = form.getFormData(); FormData data = form.getFormData();
assertNotNull("Expecting form data", data); assertNotNull("Expecting form data", data);
Map<String, FormData.FieldData> fieldData = data.getData(); assertEquals(this.documentName, data.getFieldData(nameField.getDataKeyName()).getValue());
assertNotNull("Expecting field data", fieldData); assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue());
assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue()); assertEquals(VALUE_MIMETYPE, data.getFieldData(mimetypeField.getDataKeyName()).getValue());
assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); assertEquals(VALUE_SUBJECT, data.getFieldData(subjectField.getDataKeyName()).getValue());
assertEquals(VALUE_MIMETYPE, fieldData.get(mimetypeField.getDataKeyName()).getValue()); assertEquals(USER_ONE, data.getFieldData(modifierField.getDataKeyName()).getValue());
assertEquals(VALUE_SUBJECT, fieldData.get(subjectField.getDataKeyName()).getValue());
assertEquals(USER_ONE, fieldData.get(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); assertNotNull("Expecting to find modified date", modifiedDate);
assertTrue("Expecting modified field to return a Date", (modifiedDate instanceof Date)); assertTrue("Expecting modified field to return a Date", (modifiedDate instanceof Date));
Calendar calTestValue = Calendar.getInstance(); Calendar calTestValue = Calendar.getInstance();
calTestValue.setTime(VALUE_SENT_DATE); calTestValue.setTime(VALUE_SENT_DATE);
Calendar calServiceValue = Calendar.getInstance(); 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()); assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis());
List<String> targets = (List<String>)fieldData.get(referencesField.getDataKeyName()).getValue(); List<String> targets = (List<String>)data.getFieldData(referencesField.getDataKeyName()).getValue();
assertEquals("Expecting 1 target", 1, targets.size()); assertEquals("Expecting 1 target", 1, targets.size());
assertEquals(this.associatedDoc.toString(), targets.get(0)); assertEquals(this.associatedDoc.toString(), targets.get(0));
} }
@@ -553,10 +549,8 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
// check the form data // check the form data
FormData data = form.getFormData(); FormData data = form.getFormData();
assertNotNull("Expecting form data", data); assertNotNull("Expecting form data", data);
Map<String, FormData.FieldData> fieldData = data.getData(); assertEquals(this.documentName, data.getFieldData(nameField.getDataKeyName()).getValue());
assertNotNull("Expecting field data", fieldData); assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue());
assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue());
assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue());
} }
public void testForcedFieldsDocForm() throws Exception public void testForcedFieldsDocForm() throws Exception
@@ -621,11 +615,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
// check the form data // check the form data
FormData data = form.getFormData(); FormData data = form.getFormData();
assertNotNull("Expecting form data", data); assertNotNull("Expecting form data", data);
Map<String, FormData.FieldData> fieldData = data.getData(); assertEquals(this.documentName, data.getFieldData(nameField.getDataKeyName()).getValue());
assertNotNull("Expecting field data", fieldData); assertEquals(VALUE_TITLE, data.getFieldData(titleField.getDataKeyName()).getValue());
assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue()); assertNull("Didn't expect to find a value for cm:author", data.getFieldData(authorField.getDataKeyName()));
assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue());
assertNull("Didn't expect to find a value for cm:author", fieldData.get(authorField.getDataKeyName()));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -694,11 +686,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
// check the form data // check the form data
FormData data = form.getFormData(); FormData data = form.getFormData();
assertNotNull("Expecting form data", data); assertNotNull("Expecting form data", data);
Map<String, FormData.FieldData> fieldData = data.getData(); assertEquals(this.folderName, data.getFieldData(nameField.getDataKeyName()).getValue());
assertNotNull("Expecting field data", fieldData);
assertEquals(this.folderName, fieldData.get(nameField.getDataKeyName()).getValue());
List<String> children = (List<String>)fieldData.get(containsField.getDataKeyName()).getValue(); List<String> children = (List<String>)data.getFieldData(containsField.getDataKeyName()).getValue();
assertEquals("Expecting 3 children", 3, children.size()); assertEquals("Expecting 3 children", 3, children.size());
assertEquals(this.document.toString(), children.get(0)); assertEquals(this.document.toString(), children.get(0));
assertEquals(this.associatedDoc.toString(), children.get(1)); assertEquals(this.associatedDoc.toString(), children.get(1));
@@ -720,37 +710,37 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
// update the name // update the name
String newName = "new-" + this.documentName; String newName = "new-" + this.documentName;
data.addData("prop_cm_name", newName); data.addFieldData("prop_cm_name", newName);
// update the title property // update the title property
String newTitle = "This is the new 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 // update the mimetype
String newMimetype = MimetypeMap.MIMETYPE_HTML; 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) // update the author property (this is on an aspect not applied)
String newAuthor = "Gavin Cornwell"; String newAuthor = "Gavin Cornwell";
data.addData("prop_cm_author", newAuthor); data.addFieldData("prop_cm_author", newAuthor);
// update the originator // update the originator
String newOriginator = "jane@example.com"; String newOriginator = "jane@example.com";
data.addData("prop_cm_originator", newOriginator); data.addFieldData("prop_cm_originator", newOriginator);
// update the adressees, add another // update the adressees, add another
String newAddressees = VALUE_ADDRESSEES1 + "," + VALUE_ADDRESSEES2 + "," + VALUE_ADDRESSEES3; 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) // 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) // 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) // try and update non-existent properties (make sure there are no exceptions)
data.addData("prop_cm_wrong", "This should not be persisted"); data.addFieldData("prop_cm_wrong", "This should not be persisted");
data.addData("cm_wrong", "This should not be persisted"); data.addFieldData("cm_wrong", "This should not be persisted");
// persist the data // persist the data
this.formService.saveForm(new Item(NODE_FORM_ITEM_KIND, this.document.toString()), 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 // supply the name
String name = "new-" + this.documentName; String name = "new-" + this.documentName;
data.addData("prop_cm_name", name); data.addFieldData("prop_cm_name", name);
// supply the title property // supply the title property
String title = "This is 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) // persist the data (without a destination and make sure it fails)
try try
@@ -931,7 +921,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
} }
// supply the destination // supply the destination
data.addData("destination", this.folder.toString()); data.addFieldData("destination", this.folder.toString());
// persist the data // persist the data
NodeRef newNode = (NodeRef)this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "cm:content"), data); NodeRef newNode = (NodeRef)this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "cm:content"), data);

View File

@@ -900,12 +900,11 @@ public class NodeFormProcessor extends FilteredFormProcessor
Map<QName, ChildAssociationDefinition> childAssocDefs = typeDef.getChildAssociations(); Map<QName, ChildAssociationDefinition> childAssocDefs = typeDef.getChildAssociations();
Map<QName, PropertyDefinition> propDefs = typeDef.getProperties(); Map<QName, PropertyDefinition> propDefs = typeDef.getProperties();
Map<QName, Serializable> propsToPersist = new HashMap<QName, Serializable>(data.getData().size()); Map<QName, Serializable> propsToPersist = new HashMap<QName, Serializable>(data.getNumberOfFields());
List<AbstractAssocCommand> assocsToPersist = new ArrayList<AbstractAssocCommand>(); List<AbstractAssocCommand> assocsToPersist = new ArrayList<AbstractAssocCommand>();
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! // NOTE: ignore file fields for now, not supported yet!
if (fieldData.isFile() == false) if (fieldData.isFile() == false)
{ {

View File

@@ -238,13 +238,10 @@ public class TypeFormProcessor extends NodeFormProcessor
NodeRef nodeRef = null; NodeRef nodeRef = null;
if (data != null) if (data != null)
{
Map<String, FieldData> fieldData = data.getData();
if (fieldData != null)
{ {
// firstly, ensure we have a destination to create the node in // firstly, ensure we have a destination to create the node in
NodeRef parentRef = null; NodeRef parentRef = null;
FieldData destination = fieldData.get(DESTINATION); FieldData destination = data.getFieldData(DESTINATION);
if (destination == null) if (destination == null)
{ {
throw new FormException("Failed to persist form for '" + throw new FormException("Failed to persist form for '" +
@@ -261,13 +258,13 @@ public class TypeFormProcessor extends NodeFormProcessor
// if a name property is present in the form data use it as the node name, // if a name property is present in the form data use it as the node name,
// otherwise generate a guid // otherwise generate a guid
String nodeName = null; String nodeName = null;
FieldData nameData = fieldData.get(NAME_PROP_DATA); FieldData nameData = data.getFieldData(NAME_PROP_DATA);
if (nameData != null) if (nameData != null)
{ {
nodeName = (String)nameData.getValue(); nodeName = (String)nameData.getValue();
// remove the name data otherwise 'rename' gets called in persistNode // remove the name data otherwise 'rename' gets called in persistNode
fieldData.remove(NAME_PROP_DATA); data.removeFieldData(NAME_PROP_DATA);
} }
if (nodeName == null || nodeName.length() == 0) if (nodeName == null || nodeName.length() == 0)
{ {
@@ -281,7 +278,6 @@ public class TypeFormProcessor extends NodeFormProcessor
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(nodeName)), QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(nodeName)),
typeDef.getName(), nodeProps).getChildRef(); typeDef.getName(), nodeProps).getChildRef();
} }
}
return nodeRef; return nodeRef;
} }

View File

@@ -53,10 +53,10 @@ public class ScriptFormData implements Serializable
ScriptableHashMap<String, ScriptFieldData> result = new ScriptableHashMap<String, ScriptFieldData>(); ScriptableHashMap<String, ScriptFieldData> result = new ScriptableHashMap<String, ScriptFieldData>();
if (this.formData != null) if (this.formData != null)
{ {
for (String k : formData.getData().keySet()) for (FieldData fieldData : formData)
{ {
ScriptFieldData wrappedFieldData = new ScriptFieldData(formData.getData().get(k)); ScriptFieldData wrappedFieldData = new ScriptFieldData(fieldData);
result.put(k, wrappedFieldData); result.put(fieldData.getName(), wrappedFieldData);
} }
} }
return result; return result;