mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
- 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:
@@ -188,7 +188,7 @@ public class Form
|
||||
this.data = new FormData();
|
||||
}
|
||||
|
||||
this.data.addData(fieldName, fieldData);
|
||||
this.data.addFieldData(fieldName, fieldData);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -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<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;
|
||||
|
||||
/**
|
||||
@@ -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 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<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();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -350,18 +350,16 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
|
||||
// check the form data
|
||||
FormData data = form.getFormData();
|
||||
assertNotNull("Expecting form data", data);
|
||||
Map<String, FormData.FieldData> 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<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(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<String, FormData.FieldData> 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<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(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<String, FormData.FieldData> 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<String, FormData.FieldData> 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<String, FormData.FieldData> 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<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(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);
|
||||
|
@@ -900,12 +900,11 @@ public class NodeFormProcessor extends FilteredFormProcessor
|
||||
Map<QName, ChildAssociationDefinition> childAssocDefs = typeDef.getChildAssociations();
|
||||
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>();
|
||||
|
||||
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)
|
||||
{
|
||||
|
@@ -239,48 +239,44 @@ public class TypeFormProcessor extends NodeFormProcessor
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
Map<String, FieldData> 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<QName, Serializable> nodeProps = new HashMap<QName, Serializable>(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<QName, Serializable> nodeProps = new HashMap<QName, Serializable>(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;
|
||||
|
@@ -53,10 +53,10 @@ public class ScriptFormData implements Serializable
|
||||
ScriptableHashMap<String, ScriptFieldData> result = new ScriptableHashMap<String, ScriptFieldData>();
|
||||
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;
|
||||
|
Reference in New Issue
Block a user