From 966201871cc998baa0d29dcaf18e8822b9ee6c40 Mon Sep 17 00:00:00 2001 From: Gavin Cornwell Date: Mon, 20 Apr 2009 14:53:04 +0000 Subject: [PATCH] FormService improvements - A 'dataKeyName' property has been added to the field definition to point to the corresponding data - FormProcessors can now return a submission url to use on the client - FormProcessors are now responsible for returning a sensible URL to represent the 'item' being processed - Updated all affected tests git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14021 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../forms/AssociationFieldDefinition.java | 1 + .../alfresco/repo/forms/FieldDefinition.java | 21 +++++ source/java/org/alfresco/repo/forms/Form.java | 33 ++++---- .../repo/forms/FormServiceImplTest.java | 77 +++++++++---------- source/java/org/alfresco/repo/forms/Item.java | 64 ++++++++++++++- .../repo/forms/PropertyFieldDefinition.java | 1 + .../repo/forms/processor/NodeHandler.java | 60 ++++++++++----- .../repo/forms/script/ScriptForm.java | 18 +++-- .../repo/forms/script/test_formService.js | 42 +++++----- 9 files changed, 211 insertions(+), 106 deletions(-) diff --git a/source/java/org/alfresco/repo/forms/AssociationFieldDefinition.java b/source/java/org/alfresco/repo/forms/AssociationFieldDefinition.java index a86c8b8427..ce6569613b 100644 --- a/source/java/org/alfresco/repo/forms/AssociationFieldDefinition.java +++ b/source/java/org/alfresco/repo/forms/AssociationFieldDefinition.java @@ -137,6 +137,7 @@ public class AssociationFieldDefinition extends FieldDefinition buffer.append(", description=").append(this.description); buffer.append(", binding=").append(this.binding); buffer.append(", defaultValue=").append(this.defaultValue); + buffer.append(", dataKeyName=").append(this.dataKeyName); buffer.append(", group=").append(this.group); buffer.append(", protectedField=").append(this.protectedField); buffer.append(")"); diff --git a/source/java/org/alfresco/repo/forms/FieldDefinition.java b/source/java/org/alfresco/repo/forms/FieldDefinition.java index 1b9f17bf4d..ee0b944d70 100644 --- a/source/java/org/alfresco/repo/forms/FieldDefinition.java +++ b/source/java/org/alfresco/repo/forms/FieldDefinition.java @@ -36,6 +36,7 @@ public abstract class FieldDefinition protected String description; protected String binding; protected String defaultValue; + protected String dataKeyName; protected FieldGroup group; protected boolean protectedField = false; @@ -141,6 +142,26 @@ public abstract class FieldDefinition this.defaultValue = defaultValue; } + /** + * Returns the name of the key being used to hold the data for the field + * + * @return Name of the key being used to hold the data for the field + */ + public String getDataKeyName() + { + return this.dataKeyName; + } + + /** + * Sets the name of the key to be used to hold the data for the field + * + * @param dataKeyName The name of the key to be used to hold the data for the field + */ + public void setDataKeyName(String dataKeyName) + { + this.dataKeyName = dataKeyName; + } + /** * Returns the group the field may be a part of * diff --git a/source/java/org/alfresco/repo/forms/Form.java b/source/java/org/alfresco/repo/forms/Form.java index ac976f137a..d1e6783b87 100644 --- a/source/java/org/alfresco/repo/forms/Form.java +++ b/source/java/org/alfresco/repo/forms/Form.java @@ -36,7 +36,7 @@ import java.util.List; public class Form { protected Item item; - protected String type; + protected String submissionUrl; protected List fieldDefinitions; protected Collection fieldGroups; protected FormData data; @@ -59,27 +59,26 @@ public class Form public Item getItem() { return this.item; - } - - /** - * Returns the type of the item the form is for, could be a content model type, a - * workflow task type, an XML schema etc. - * - * @return The type of the item - */ - public String getType() - { - return this.type; } /** - * Sets the type of the item this Form represents + * Returns the submission URL to use for the form * - * @param type The type + * @return URL to submit to */ - public void setType(String type) + public String getSubmissionUrl() { - this.type = type; + return this.submissionUrl; + } + + /** + * Sets the submission URL the form should use + * + * @param url URL to submit to + */ + public void setSubmissionUrl(String url) + { + this.submissionUrl = url; } /** @@ -201,7 +200,7 @@ public class Form StringBuilder buffer = new StringBuilder(super.toString()); buffer.append(" ("); buffer.append("item=").append(this.item); - buffer.append(", type=").append(this.type); + buffer.append(", submissionUrl=").append(this.submissionUrl); buffer.append(", fieldGroups=").append(this.fieldGroups); buffer.append("\nfieldDefinitions=").append(this.fieldDefinitions); buffer.append("\nformData=").append(this.data); diff --git a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java index e34c093f13..0ee073d4bb 100644 --- a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java +++ b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java @@ -240,7 +240,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the type is correct assertEquals(ContentModel.TYPE_CONTENT.toPrefixString(this.namespaceService), - form.getType()); + form.getItem().getType()); // check there is no group info assertNull("Expecting the form groups to be null!", form.getFieldGroups()); @@ -351,16 +351,16 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest assertNotNull("Expecting form data", data); Map fieldData = data.getData(); assertNotNull("Expecting field data", fieldData); - assertEquals(VALUE_TITLE, fieldData.get("prop:cm:title").getValue()); - assertEquals(VALUE_DESCRIPTION, fieldData.get("prop:cm:description").getValue()); - assertEquals(VALUE_MIMETYPE, fieldData.get("prop:mimetype").getValue()); - assertEquals(VALUE_ENCODING, fieldData.get("prop:encoding").getValue()); - assertEquals(VALUE_ORIGINATOR, fieldData.get("prop:cm:originator").getValue()); - assertEquals(VALUE_ADDRESSEE, fieldData.get("prop:cm:addressee").getValue()); - assertEquals(VALUE_SUBJECT, fieldData.get("prop:cm:subjectline").getValue()); - assertTrue("Expecting size to be > 0", ((Long)fieldData.get("prop:size").getValue()).longValue() > 0); + 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); - String addressees = (String)fieldData.get("prop:cm:addressees").getValue(); + String addressees = (String)fieldData.get(addresseesField.getDataKeyName()).getValue(); assertNotNull(addressees); assertTrue("Expecting the addressees value to have at least 1 comma", addressees.indexOf(",") != -1); String[] addresseesArr = StringUtils.delimitedListToStringArray(addressees, ","); @@ -371,10 +371,10 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest Calendar calTestValue = Calendar.getInstance(); calTestValue.setTime(VALUE_SENT_DATE); Calendar calServiceValue = Calendar.getInstance(); - calServiceValue.setTime((Date)fieldData.get("prop:cm:sentdate").getValue()); + calServiceValue.setTime((Date)fieldData.get(sentDateField.getDataKeyName()).getValue()); assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis()); - List targets = (List)fieldData.get("assoc:cm:references").getValue(); + List targets = (List)fieldData.get(referencesField.getDataKeyName()).getValue(); assertEquals("Expecting 1 target", 1, targets.size()); assertEquals(this.associatedDoc.toString(), targets.get(0)); } @@ -404,7 +404,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the type is correct assertEquals(ContentModel.TYPE_CONTENT.toPrefixString(this.namespaceService), - form.getType()); + form.getItem().getType()); // check there is no group info assertNull("Expecting the form groups to be null!", form.getFieldGroups()); @@ -482,23 +482,23 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest assertNotNull("Expecting form data", data); Map fieldData = data.getData(); assertNotNull("Expecting field data", fieldData); - assertEquals(this.documentName, fieldData.get("prop:cm:name").getValue()); - assertEquals(VALUE_TITLE, fieldData.get("prop:cm:title").getValue()); - assertEquals(VALUE_MIMETYPE, fieldData.get("prop:mimetype").getValue()); - assertEquals(VALUE_SUBJECT, fieldData.get("prop:cm:subjectline").getValue()); - assertEquals(USER_ONE, fieldData.get("prop:cm:modifier").getValue()); + 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()); - Date modifiedDate = (Date)fieldData.get("prop:cm:modified").getValue(); + Date modifiedDate = (Date)fieldData.get(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("prop:cm:sentdate").getValue()); + calServiceValue.setTime((Date)fieldData.get(sentDateField.getDataKeyName()).getValue()); assertEquals(calTestValue.getTimeInMillis(), calServiceValue.getTimeInMillis()); - List targets = (List)fieldData.get("assoc:cm:references").getValue(); + List targets = (List)fieldData.get(referencesField.getDataKeyName()).getValue(); assertEquals("Expecting 1 target", 1, targets.size()); assertEquals(this.associatedDoc.toString(), targets.get(0)); } @@ -554,8 +554,8 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest assertNotNull("Expecting form data", data); Map fieldData = data.getData(); assertNotNull("Expecting field data", fieldData); - assertEquals(this.documentName, fieldData.get("prop:cm:name").getValue()); - assertEquals(VALUE_TITLE, fieldData.get("prop:cm:title").getValue()); + assertEquals(this.documentName, fieldData.get(nameField.getDataKeyName()).getValue()); + assertEquals(VALUE_TITLE, fieldData.get(titleField.getDataKeyName()).getValue()); } public void testForcedFieldsDocForm() throws Exception @@ -622,9 +622,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest assertNotNull("Expecting form data", data); Map fieldData = data.getData(); assertNotNull("Expecting field data", fieldData); - assertEquals(this.documentName, fieldData.get("prop:cm:name").getValue()); - assertEquals(VALUE_TITLE, fieldData.get("prop:cm:title").getValue()); - assertNull("Didn't expect to find a value for cm:author", fieldData.get("prop:cm:author")); + 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())); } @SuppressWarnings("unchecked") @@ -641,7 +641,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // check the type is correct assertEquals(ContentModel.TYPE_FOLDER.toPrefixString(this.namespaceService), - form.getType()); + form.getItem().getType()); // check there is no group info assertNull("Expecting the form groups to be null!", form.getFieldGroups()); @@ -695,19 +695,18 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest assertNotNull("Expecting form data", data); Map fieldData = data.getData(); assertNotNull("Expecting field data", fieldData); - assertEquals(this.folderName, fieldData.get("prop:cm:name").getValue()); + assertEquals(this.folderName, fieldData.get(nameField.getDataKeyName()).getValue()); - List children = (List)fieldData.get("assoc:cm:contains").getValue(); + List children = (List)fieldData.get(containsField.getDataKeyName()).getValue(); assertEquals("Expecting 3 children", 3, children.size()); assertEquals(this.document.toString(), children.get(0)); assertEquals(this.associatedDoc.toString(), children.get(1)); assertEquals(this.childDoc.toString(), children.get(2)); } - @SuppressWarnings("unchecked") public void testGetSelectedFieldsFolderForm() throws Exception { - // attempt to get a form with fields that are not appropriate + // TODO: attempt to get a form with fields that are not appropriate // for a folder type i.e. mimetype and encoding } @@ -720,30 +719,30 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // update the name String newName = "new-" + this.documentName; - data.addData("prop:cm:name", newName); + data.addData("prop_cm_name", newName); // update the title property String newTitle = "This is the new title property"; - data.addData("prop:cm:title", newTitle); + data.addData("prop_cm_title", newTitle); // update the mimetype String newMimetype = MimetypeMap.MIMETYPE_HTML; - data.addData("prop:mimetype", newMimetype); + data.addData("prop_mimetype", newMimetype); // update the originator String newOriginator = "jane@example.com"; - data.addData("prop:cm:originator", newOriginator); + data.addData("prop_cm_originator", newOriginator); // update the adressees, add another String newAddressees = VALUE_ADDRESSEES1 + "," + VALUE_ADDRESSEES2 + "," + VALUE_ADDRESSEES3; - data.addData("prop:cm:addressees", newAddressees); + data.addData("prop_cm_addressees", newAddressees); // set the date to null (using an empty string) - data.addData("prop:cm:sentdate", ""); + data.addData("prop_cm_sentdate", ""); // 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.addData("prop_cm_wrong", "This should not be persisted"); + data.addData("cm_wrong", "This should not be persisted"); // persist the data this.formService.saveForm(new Item(NODE_FORM_ITEM_KIND, this.document.toString()), data); diff --git a/source/java/org/alfresco/repo/forms/Item.java b/source/java/org/alfresco/repo/forms/Item.java index 27970bf0e4..e7368ce724 100644 --- a/source/java/org/alfresco/repo/forms/Item.java +++ b/source/java/org/alfresco/repo/forms/Item.java @@ -28,13 +28,18 @@ import org.alfresco.util.ParameterCheck; /** * Represents an item a form is generated for. + *

This class can be augmented with the item's type and a + * representational URL by the form processor used to process + * the item.

* * @author Gavin Cornwell */ public class Item { - private String kind; - private String id; + protected String kind; + protected String id; + protected String type; + protected String url; /** * Constructs an item. @@ -71,12 +76,65 @@ public class Item return this.id; } + /** + * Returns the type of the item the form is for, could be a content model type, a + * workflow task type, an XML schema etc. + * + * @return The type of the item + */ + public String getType() + { + return this.type; + } + + /** + * Returns a URL that represents the item + * + * @return A URL representing the item + */ + public String getUrl() + { + return this.url; + } + + /** + * Sets the type of the item + * + * @param type The type + */ + public void setType(String type) + { + this.type = type; + } + + /** + * Sets the URL that represents the item + * + * @param url The URL + */ + public void setUrl(String url) + { + this.url = url; + } + /* * @see java.lang.Object#toString() */ @Override public String toString() { - return "[" + this.kind + "]" + this.id; + StringBuilder builder = new StringBuilder("["); + builder.append(this.kind).append("]").append(this.id); + + if (this.type != null) + { + builder.append(", type=").append(this.type); + } + if (this.url != null) + { + builder.append(", url=").append(this.url); + } + + return builder.toString(); } } diff --git a/source/java/org/alfresco/repo/forms/PropertyFieldDefinition.java b/source/java/org/alfresco/repo/forms/PropertyFieldDefinition.java index 24586343d1..0bdaf8431b 100644 --- a/source/java/org/alfresco/repo/forms/PropertyFieldDefinition.java +++ b/source/java/org/alfresco/repo/forms/PropertyFieldDefinition.java @@ -137,6 +137,7 @@ public class PropertyFieldDefinition extends FieldDefinition buffer.append(", description=").append(this.description); buffer.append(", binding=").append(this.binding); buffer.append(", defaultValue=").append(this.defaultValue); + buffer.append(", dataKeyName=").append(this.dataKeyName); buffer.append(", group=").append(this.group); buffer.append(", protectedField=").append(this.protectedField); buffer.append(", mandatory=").append(this.mandatory); diff --git a/source/java/org/alfresco/repo/forms/processor/NodeHandler.java b/source/java/org/alfresco/repo/forms/processor/NodeHandler.java index 914cc583c0..216eb106f5 100644 --- a/source/java/org/alfresco/repo/forms/processor/NodeHandler.java +++ b/source/java/org/alfresco/repo/forms/processor/NodeHandler.java @@ -78,10 +78,11 @@ public class NodeHandler extends AbstractHandler { private static final Log logger = LogFactory.getLog(NodeHandler.class); - protected static final String PROP_PREFIX = "prop:"; - protected static final String ASSOC_PREFIX = "assoc:"; + protected static final String PROP_PREFIX = "prop_"; + protected static final String ASSOC_PREFIX = "assoc_"; protected static final String ASSOC_ADD_SUFFIX = "_added"; protected static final String ASSOC_REMOVE_SUFFIX = "_removed"; + protected static final String DATA_KEY_SEPARATOR = "_"; protected static final String TRANSIENT_MIMETYPE = "mimetype"; protected static final String TRANSIENT_SIZE = "size"; @@ -105,7 +106,7 @@ public class NodeHandler extends AbstractHandler * These names will look like "prop:cm:name". * The pattern can also be used to extract the "cm" and the "name" parts. */ - protected Pattern propertyNamePattern = Pattern.compile(PROP_PREFIX + "(.*){1}?:(.*){1}?"); + protected Pattern propertyNamePattern = Pattern.compile(PROP_PREFIX + "(.*){1}?_(.*){1}?"); /** * A regular expression which can be used to match tranisent property names. @@ -119,7 +120,7 @@ public class NodeHandler extends AbstractHandler * These names will look like "assoc:cm:references_added". * The pattern can also be used to extract the "cm", the "name" and the suffix parts. */ - protected Pattern associationNamePattern = Pattern.compile(ASSOC_PREFIX + "(.*){1}?:(.*){1}?(_[a-zA-Z]+)"); + protected Pattern associationNamePattern = Pattern.compile(ASSOC_PREFIX + "(.*){1}?_(.*){1}?(_[a-zA-Z]+)"); /** * Sets the node service @@ -191,9 +192,14 @@ public class NodeHandler extends AbstractHandler */ protected void generateNode(NodeRef nodeRef, List fields, List forcedFields, Form form) { - // set the type + // set the type and URL of the item QName type = this.nodeService.getType(nodeRef); - form.setType(type.toPrefixString(this.namespaceService)); + form.getItem().setType(type.toPrefixString(this.namespaceService)); + StringBuilder builder = new StringBuilder("/api/node/"); + builder.append(nodeRef.getStoreRef().getProtocol()).append("/"); + builder.append(nodeRef.getStoreRef().getIdentifier()).append("/"); + builder.append(nodeRef.getId()); + form.getItem().setUrl(builder.toString()); if (fields != null && fields.size() > 0) { @@ -396,6 +402,7 @@ public class NodeHandler extends AbstractHandler protected void generatePropertyField(PropertyDefinition propDef, Serializable propValue, Form form) { String propName = propDef.getName().toPrefixString(this.namespaceService); + String[] nameParts = QName.splitPrefixedQName(propName); PropertyFieldDefinition fieldDef = new PropertyFieldDefinition( propName, propDef.getDataType().getName().toPrefixString( this.namespaceService)); @@ -412,6 +419,10 @@ public class NodeHandler extends AbstractHandler fieldDef.setProtectedField(propDef.isProtected()); fieldDef.setRepeating(propDef.isMultiValued()); + // define the data key name and set + String dataKeyName = PROP_PREFIX + nameParts[0] + DATA_KEY_SEPARATOR + nameParts[1]; + fieldDef.setDataKeyName(dataKeyName); + // setup constraints for the property List constraints = propDef.getConstraints(); if (constraints != null && constraints.size() > 0) @@ -462,7 +473,7 @@ public class NodeHandler extends AbstractHandler propValue = makeListString((List)propValue); } - form.addData(PROP_PREFIX + fieldDef.getName(), propValue); + form.addData(dataKeyName, propValue); } } @@ -502,13 +513,15 @@ public class NodeHandler extends AbstractHandler */ protected void generateMimetypePropertyField(ContentData content, Form form) { + String dataKeyName = PROP_PREFIX + TRANSIENT_MIMETYPE; PropertyFieldDefinition mimetypeField = new PropertyFieldDefinition( TRANSIENT_MIMETYPE, DataTypeDefinition.TEXT.toPrefixString( this.namespaceService)); mimetypeField.setLabel(I18NUtil.getMessage(MSG_MIMETYPE_LABEL)); mimetypeField.setDescription(I18NUtil.getMessage(MSG_MIMETYPE_DESC)); + mimetypeField.setDataKeyName(dataKeyName); form.addFieldDefinition(mimetypeField); - form.addData(PROP_PREFIX + TRANSIENT_MIMETYPE, content.getMimetype()); + form.addData(dataKeyName, content.getMimetype()); } /** @@ -519,13 +532,15 @@ public class NodeHandler extends AbstractHandler */ protected void generateEncodingPropertyField(ContentData content, Form form) { + String dataKeyName = PROP_PREFIX + TRANSIENT_ENCODING; PropertyFieldDefinition encodingField = new PropertyFieldDefinition( TRANSIENT_ENCODING, DataTypeDefinition.TEXT.toPrefixString( this.namespaceService)); encodingField.setLabel(I18NUtil.getMessage(MSG_ENCODING_LABEL)); encodingField.setDescription(I18NUtil.getMessage(MSG_ENCODING_DESC)); + encodingField.setDataKeyName(dataKeyName); form.addFieldDefinition(encodingField); - form.addData(PROP_PREFIX + TRANSIENT_ENCODING, content.getEncoding()); + form.addData(dataKeyName, content.getEncoding()); } /** @@ -536,14 +551,16 @@ public class NodeHandler extends AbstractHandler */ protected void generateSizePropertyField(ContentData content, Form form) { + String dataKeyName = PROP_PREFIX + TRANSIENT_SIZE; PropertyFieldDefinition sizeField = new PropertyFieldDefinition( TRANSIENT_SIZE, DataTypeDefinition.LONG.toPrefixString( this.namespaceService)); sizeField.setLabel(I18NUtil.getMessage(MSG_SIZE_LABEL)); sizeField.setDescription(I18NUtil.getMessage(MSG_SIZE_DESC)); + sizeField.setDataKeyName(dataKeyName); sizeField.setProtectedField(true); form.addFieldDefinition(sizeField); - form.addData(PROP_PREFIX + TRANSIENT_SIZE, new Long(content.getSize())); + form.addData(dataKeyName, new Long(content.getSize())); } /** @@ -575,6 +592,7 @@ public class NodeHandler extends AbstractHandler QName assocType = assoc.getTypeQName(); String assocName = assocType.toPrefixString(this.namespaceService); String assocValue = assoc.getTargetRef().toString(); + String dataKeyName = ASSOC_PREFIX + assocName.replace(":", DATA_KEY_SEPARATOR); // setup the field definition for the association if it hasn't before AssociationFieldDefinition fieldDef = assocFieldDefs.get(assocName); @@ -599,13 +617,12 @@ public class NodeHandler extends AbstractHandler fieldDef.setProtectedField(assocDef.isProtected()); fieldDef.setEndpointMandatory(assocDef.isTargetMandatory()); fieldDef.setEndpointMany(assocDef.isTargetMany()); + fieldDef.setDataKeyName(dataKeyName); // add definition to Form and to internal cache form.addFieldDefinition(fieldDef); assocFieldDefs.put(assocName, fieldDef); } - - String prefixedAssocName = ASSOC_PREFIX + assocName; if (fieldDef.isEndpointMany()) { @@ -613,11 +630,11 @@ public class NodeHandler extends AbstractHandler // add the value as a List (or add to the list if the form data // is already present) - FieldData fieldData = form.getFormData().getData().get(prefixedAssocName); + FieldData fieldData = form.getFormData().getData().get(dataKeyName); if (fieldData == null) { targets = new ArrayList(4); - form.addData(prefixedAssocName, targets); + form.addData(dataKeyName, targets); } else { @@ -630,7 +647,7 @@ public class NodeHandler extends AbstractHandler else { // there should only be one value - form.addData(prefixedAssocName, assocValue); + form.addData(dataKeyName, assocValue); } } } @@ -665,6 +682,7 @@ public class NodeHandler extends AbstractHandler // get the name of the association QName assocTypeName = childAssoc.getTypeQName(); String assocName = assocTypeName.toPrefixString(this.namespaceService); + String dataKeyName = ASSOC_PREFIX + assocName.replace(":", DATA_KEY_SEPARATOR); // setup the field definition for the association if it hasn't before AssociationFieldDefinition fieldDef = childAssocFieldDefs.get(assocName); @@ -690,6 +708,7 @@ public class NodeHandler extends AbstractHandler fieldDef.setProtectedField(assocDef.isProtected()); fieldDef.setEndpointMandatory(assocDef.isTargetMandatory()); fieldDef.setEndpointMany(assocDef.isTargetMany()); + fieldDef.setDataKeyName(dataKeyName); // add definition to Form and to internal cache form.addFieldDefinition(fieldDef); @@ -726,7 +745,7 @@ public class NodeHandler extends AbstractHandler ChildAssociationRef nextChild = iter.next(); nodeRefs.add(nextChild.getChildRef().toString()); } - form.addData(ASSOC_PREFIX + associationName, nodeRefs); + form.addData(ASSOC_PREFIX + associationName.replace(":", DATA_KEY_SEPARATOR), nodeRefs); } } @@ -742,6 +761,7 @@ public class NodeHandler extends AbstractHandler List assocValues, Form form) { String assocName = assocDef.getName().toPrefixString(this.namespaceService); + String[] nameParts = QName.splitPrefixedQName(assocName); AssociationFieldDefinition fieldDef = new AssociationFieldDefinition(assocName, assocDef.getTargetClass().getName().toPrefixString( this.namespaceService), Direction.TARGET); @@ -756,12 +776,14 @@ public class NodeHandler extends AbstractHandler fieldDef.setEndpointMandatory(assocDef.isTargetMandatory()); fieldDef.setEndpointMany(assocDef.isTargetMany()); + // define the data key name and set + String dataKeyName = ASSOC_PREFIX + nameParts[0] + DATA_KEY_SEPARATOR + nameParts[1]; + fieldDef.setDataKeyName(dataKeyName); + // add definition to the form form.addFieldDefinition(fieldDef); // add the association value to the form - String prefixedAssocName = ASSOC_PREFIX + assocName; - // determine the type of association values data and extract accordingly List values = new ArrayList(4); for (Object value : assocValues) @@ -784,7 +806,7 @@ public class NodeHandler extends AbstractHandler // TODO: Do we also return a well known named list of association names // for each noderef so that clients do not have extra work to do // to display the current values to the user - form.addData(prefixedAssocName, values); + form.addData(dataKeyName, values); } /* diff --git a/source/java/org/alfresco/repo/forms/script/ScriptForm.java b/source/java/org/alfresco/repo/forms/script/ScriptForm.java index a600af641d..6093be3646 100644 --- a/source/java/org/alfresco/repo/forms/script/ScriptForm.java +++ b/source/java/org/alfresco/repo/forms/script/ScriptForm.java @@ -25,14 +25,12 @@ package org.alfresco.repo.forms.script; import java.io.Serializable; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.alfresco.repo.forms.FieldDefinition; -import org.alfresco.repo.forms.FieldGroup; import org.alfresco.repo.forms.Form; /** @@ -68,15 +66,19 @@ public class ScriptForm implements Serializable return form.getItem().getId(); } - public String getType() + public String getItemType() { - return form.getType(); + return form.getItem().getType(); } - - //TODO Wrap this type in a script type? - public Collection getFieldGroups() + + public String getItemUrl() { - return form.getFieldGroups(); + return form.getItem().getUrl(); + } + + public String getSubmissionUrl() + { + return form.getSubmissionUrl(); } public FieldDefinition[] getFieldDefinitions() diff --git a/source/java/org/alfresco/repo/forms/script/test_formService.js b/source/java/org/alfresco/repo/forms/script/test_formService.js index 3e2107470b..85df21273c 100644 --- a/source/java/org/alfresco/repo/forms/script/test_formService.js +++ b/source/java/org/alfresco/repo/forms/script/test_formService.js @@ -25,9 +25,10 @@ function testGetFormForContentNode() test.assertEquals("node", form.itemKind); test.assertEquals(testDoc, form.itemId); - test.assertEquals('cm:content', form.type); + test.assertEquals('cm:content', form.itemType); + test.assertEquals('/api/node/' + testDoc.replace(":/", ""), form.itemUrl); - test.assertNull(form.fieldGroups, "form.fieldGroups should be null."); + test.assertNull(form.submissionUrl, "form.submissionUrl should be null."); var fieldDefs = form.fieldDefinitions; test.assertNotNull(fieldDefs, "field definitions should not be null."); @@ -112,14 +113,14 @@ function testGetFormForContentNode() test.assertNotNull(fieldData, "fieldData should not be null."); test.assertNotNull(fieldData.length, "fieldData.length should not be null."); - test.assertEquals(testDocName, fieldData["prop:cm:name"].value); - test.assertEquals("This is the title for the test document", fieldData["prop:cm:title"].value); - test.assertEquals("This is the description for the test document", fieldData["prop:cm:description"].value); - test.assertEquals("fred@customer.com", fieldData["prop:cm:originator"].value); - test.assertEquals("bill@example.com", fieldData["prop:cm:addressee"].value); - test.assertEquals("The subject is...", fieldData["prop:cm:subjectline"].value); + test.assertEquals(testDocName, fieldData[nameField.dataKeyName].value); + test.assertEquals("This is the title for the test document", fieldData[titleField.dataKeyName].value); + test.assertEquals("This is the description for the test document", fieldData[descField.dataKeyName].value); + test.assertEquals("fred@customer.com", fieldData[originatorField.dataKeyName].value); + test.assertEquals("bill@example.com", fieldData[addresseeField.dataKeyName].value); + test.assertEquals("The subject is...", fieldData[subjectField.dataKeyName].value); - var addressees = fieldData["prop:cm:addressees"].value; + var addressees = fieldData[addresseesField.dataKeyName].value; test.assertNotNull(addressees); test.assertTrue(addressees.indexOf(",") != -1); var addresseesArr = addressees.split(","); @@ -127,12 +128,12 @@ function testGetFormForContentNode() test.assertEquals("harry@example.com", addresseesArr[0]); test.assertEquals("jane@example.com", addresseesArr[1]); - var sentDate = fieldData["prop:cm:sentdate"].getValue(); + var sentDate = fieldData[sentDateField.dataKeyName].getValue(); test.assertTrue((typeof sentDate === "object"), "Expecting sentData to be an object"); var month = sentDate.getMonth(); test.assertTrue((month >= 0 && month < 12), "Expecting valid month"); - var targets = fieldData["assoc:cm:references"].value; + var targets = fieldData[referencesField.dataKeyName].value; test.assertNotNull(targets, "targets should not be null."); test.assertEquals(testAssociatedDoc, targets); @@ -146,9 +147,10 @@ function testGetFormForFolderNode() test.assertEquals("node", form.itemKind); test.assertEquals(folder, form.itemId); - test.assertEquals('cm:folder', form.type); + test.assertEquals('cm:folder', form.itemType); + test.assertEquals('/api/node/' + folder.replace(":/", ""), form.itemUrl); - test.assertNull(form.fieldGroups, "form.fieldGroups should be null."); + test.assertNull(form.submissionUrl, "form.submissionUrl should be null."); var fieldDefs = form.fieldDefinitions; test.assertNotNull(fieldDefs, "field definitions should not be null."); @@ -192,8 +194,8 @@ function testGetFormForFolderNode() test.assertNotNull(fieldData, "fieldData should not be null."); test.assertNotNull(fieldData.length, "fieldData.length should not be null."); - test.assertEquals(folderName, fieldData["prop:cm:name"].value); - var children = fieldData["assoc:cm:contains"].value; + test.assertEquals(folderName, fieldData[nameField.dataKeyName].value); + var children = fieldData[containsField.dataKeyName].value; test.assertNotNull(children, "children should not be null."); var childrenArr = children.split(","); test.assertTrue(childrenArr.length == 3, "Expecting there to be 3 children"); @@ -253,8 +255,8 @@ function testGetFormWithSelectedFields() test.assertNotNull(fieldData, "fieldData should not be null."); test.assertNotNull(fieldData.length, "fieldData.length should not be null."); - test.assertEquals(testDocName, fieldData["prop:cm:name"].value); - test.assertEquals("This is the title for the test document", fieldData["prop:cm:title"].value); + test.assertEquals(testDocName, fieldData[nameField.dataKeyName].value); + test.assertEquals("This is the title for the test document", fieldData[titleField.dataKeyName].value); } function testGetFormWithForcedFields() @@ -319,9 +321,9 @@ function testGetFormWithForcedFields() test.assertNotNull(fieldData, "fieldData should not be null."); test.assertNotNull(fieldData.length, "fieldData.length should not be null."); - test.assertEquals(testDocName, fieldData["prop:cm:name"].value); - test.assertEquals("This is the title for the test document", fieldData["prop:cm:title"].value); - test.assertNull(fieldData["prop:cm:author"], "Expecting cm:author to be null"); + test.assertEquals(testDocName, fieldData[nameField.dataKeyName].value); + test.assertEquals("This is the title for the test document", fieldData[titleField.dataKeyName].value); + test.assertNull(fieldData[authorField.dataKeyName], "Expecting cm:author to be null"); } // Execute tests