From 53f29f8fdf806f69b1319f014e8a54ac88bfdf2d Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Tue, 24 Mar 2009 12:06:47 +0000 Subject: [PATCH] Rendering associations in the repo-tier webscript git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13733 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repo/forms/FormServiceImplTest.java | 31 +++---------- .../repo/forms/script/ScriptFormData.java | 44 ++++++++++++++++++- .../repo/forms/script/test_formService.js | 14 ++---- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java index 171a70f824..5158d53b1c 100644 --- a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java +++ b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java @@ -83,6 +83,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest private static String VALUE_MIMETYPE = MimetypeMap.MIMETYPE_TEXT_PLAIN; private static String VALUE_ENCODING = "UTF-8"; private static String VALUE_CONTENT = "This is the content for the test document"; + private static String VALUE_ASSOC_CONTENT = "This is the content for the associated document"; private static Date VALUE_SENT_DATE = new Date(); private static String LABEL_NAME = "Name"; @@ -157,12 +158,17 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest ContentModel.TYPE_CONTENT, docProps).getChildRef(); - // add some content to the node + // add some content to the nodes ContentWriter writer = this.contentService.getWriter(this.document, ContentModel.PROP_CONTENT, true); writer.setMimetype(VALUE_MIMETYPE); writer.setEncoding(VALUE_ENCODING); writer.putContent(VALUE_CONTENT); + ContentWriter writer2 = this.contentService.getWriter(this.associatedDoc, ContentModel.PROP_CONTENT, true); + writer2.setMimetype(VALUE_MIMETYPE); + writer2.setEncoding(VALUE_ENCODING); + writer2.putContent(VALUE_ASSOC_CONTENT); + // add standard titled aspect Map aspectProps = new HashMap(2); aspectProps.put(ContentModel.PROP_TITLE, VALUE_TITLE); @@ -462,31 +468,8 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest } } - public void off_testSaveUpdatedForm() throws Exception - { - fail("Form persistence not yet impl'd."); - - Form originalForm = this.formService.getForm(this.document.toString()); - FormData formData = originalForm.getFormData(); - - FieldData fd = originalForm.getFormData().getData().get("foo"); - assertNull(fd); - - formData.addData("foo", "bar"); - - formService.saveForm(document.toString(), formData); - - Form updatedForm = this.formService.getForm(this.document.toString()); - assertFalse("Expected form instance to have changed.", originalForm == updatedForm); - - fd = updatedForm.getFormData().getData().get("foo"); - assertNotNull(fd); - } - public void testJavascriptAPI() throws Exception { - //TODO Form saving is not yet implemented. - Map model = new HashMap(); model.put("testDoc", this.document.toString()); model.put("testAssociatedDoc", this.associatedDoc.toString()); diff --git a/source/java/org/alfresco/repo/forms/script/ScriptFormData.java b/source/java/org/alfresco/repo/forms/script/ScriptFormData.java index d9712fc193..cb0e87eaba 100644 --- a/source/java/org/alfresco/repo/forms/script/ScriptFormData.java +++ b/source/java/org/alfresco/repo/forms/script/ScriptFormData.java @@ -25,6 +25,7 @@ package org.alfresco.repo.forms.script; import java.io.Serializable; +import java.util.Iterator; import java.util.List; import org.alfresco.repo.forms.FormData; @@ -86,9 +87,50 @@ public class ScriptFormData implements Serializable public Object getValue() { Object rawResult = wrappedFieldData.getValue(); + + // An implementation decision was taken in handling sequence values here. + // + // Background: + // (1) if this method returns instances of java.util.List as is, the data + // webscript that renders the REST JSON in the repo tier will render a + // List.toString() for these field values, which works - but see below. + // + // However the JavaScript API will then not offer sequences as sequences. + // Instead they will be Strings (e.g. field.value == "[foo, bar]") + // and JavaScript client code will have to parse these strings. + // + // (2) if this method instead returns listObject.toArray(), then the + // JavaScript API will see true sequence objects and can easily consume + // them like so field.value[1] == "bar" + // + // However with arrays returned from this method, the webscript that renders + // the REST JSON in the repo tier cannot handle the array type. (I should say + // that I am unable at this point to make it handle it.) + // + // + // + // So should we return List instances or Object[] instances? + // In order to allow the JSON to be easily rendered, and also to make it + // easier for client JavaScript code to use sequence values, we are + // returning our own toString implementation for List instances. + // + // So instead of "[foo, bar]" we'll have "foo,bar" + // This should be very easy for the JavaScript to parse: value.split(",") + // and it works for the webscript too. + if (rawResult instanceof List) { - return ((List) rawResult).toArray(); + List listValue = (List)rawResult; + StringBuilder result = new StringBuilder(); + for (Iterator iter = listValue.iterator(); iter.hasNext(); ) + { + result.append(iter.next()); + if (iter.hasNext()) + { + result.append(","); + } + } + return result.toString(); } else { return rawResult; 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 9ad4f89cb8..d4e043e793 100644 --- a/source/java/org/alfresco/repo/forms/script/test_formService.js +++ b/source/java/org/alfresco/repo/forms/script/test_formService.js @@ -16,12 +16,10 @@ function testGetFormForContentNode() test.assertEquals(testDoc, form.item); test.assertEquals('cm:content', form.type); - //TODO Do we want this to be null or an empty array? test.assertNull(form.fieldGroups, "form.fieldGroups should be null."); var fieldDefs = form.fieldDefinitions; test.assertNotNull(fieldDefs, "field definitions should not be null."); - test.assertEquals(22, fieldDefs.length); // This dataHash is now an integer-keyed hash of the field definition data objects. @@ -49,10 +47,6 @@ function testGetFormForContentNode() test.assertNotNull(sentDateField, "Expecting to find the cm:sentdate field"); test.assertNotNull(referencesField, "Expecting to find the cm:references field"); - //TODO All these checked values will only work for the hard-coded node we're using. - // The hard-coded node should be replaced with a temporary one created within - // this test case. - // check the labels of all the fields test.assertEquals("Name", nameField.label); test.assertEquals("Title", titleField.label); @@ -87,9 +81,6 @@ function testGetFormForContentNode() // Expecting cm:addressees to be multi-valued. test.assertTrue(addresseesField.repeating); - // TODO The below test is failing for some reason. -// test.assertNull(addresseesField.constraints, "addresseesField.constraints should be null."); - // check the details of the association field test.assertEquals("cm:content", referencesField.endpointType); @@ -125,8 +116,9 @@ function testGetFormForContentNode() test.assertFalse(isNaN(Date.parse(sentDate))); var targets = fieldData["assoc:cm:references"].value; - test.assertEquals(1, targets.length); - test.assertEquals(testAssociatedDoc, targets[0]); + + test.assertNotNull(targets, "targets should not be null."); + test.assertEquals(testAssociatedDoc, targets); } function getFieldDefnFromMap(name, fieldDefnDataHash)