From 1df71e1b03fb0aa43aed0a699229fa904aef031e Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Wed, 7 Jan 2009 09:42:07 +0000 Subject: [PATCH] Script layer impl of form.get git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@12584 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- source/java/org/alfresco/repo/forms/Form.java | 11 ++ .../repo/forms/FormServiceImplTest.java | 81 +++++++++++-- .../repo/forms/script/ScriptForm.java | 81 +++++++++++++ .../repo/forms/script/ScriptFormService.java | 71 ++++++++++++ .../repo/forms/script/test_formService.js | 107 ++++++++++++++++++ 5 files changed, 339 insertions(+), 12 deletions(-) create mode 100644 source/java/org/alfresco/repo/forms/script/ScriptForm.java create mode 100644 source/java/org/alfresco/repo/forms/script/ScriptFormService.java create mode 100644 source/java/org/alfresco/repo/forms/script/test_formService.js diff --git a/source/java/org/alfresco/repo/forms/Form.java b/source/java/org/alfresco/repo/forms/Form.java index 6d7012863d..6501de4b9b 100644 --- a/source/java/org/alfresco/repo/forms/Form.java +++ b/source/java/org/alfresco/repo/forms/Form.java @@ -26,6 +26,7 @@ package org.alfresco.repo.forms; import java.util.ArrayList; import java.util.Collection; +import java.util.List; /** * Data representation of a form to be displayed in the UI. @@ -92,6 +93,16 @@ public class Form return this.fieldDefinitions; } + public List getFieldDefinitionNames() + { + List result = new ArrayList(fieldDefinitions.size()); + for (FieldDefinition fieldDefn : fieldDefinitions) + { + result.add(fieldDefn.getName()); + } + return result; + } + /** * Sets the collection of FieldDefintion objects representing the fields the * form is able to display diff --git a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java index df2d3d5024..0e75e3c182 100644 --- a/source/java/org/alfresco/repo/forms/FormServiceImplTest.java +++ b/source/java/org/alfresco/repo/forms/FormServiceImplTest.java @@ -18,7 +18,7 @@ * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's - * FLOSS exception. You should have recieved a copy of the text describing + * FLOSS exception. You should have received a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ @@ -32,19 +32,23 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.alfresco.model.ContentModel; import org.alfresco.repo.forms.AssociationFieldDefinition.Direction; +import org.alfresco.repo.forms.FormData.FieldData; +import org.alfresco.repo.jscript.ClasspathScriptLocation; import org.alfresco.repo.forms.PropertyFieldDefinition.FieldConstraint; import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.ScriptLocation; +import org.alfresco.service.cmr.repository.ScriptService; import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.alfresco.util.BaseAlfrescoSpringTest; import org.alfresco.util.GUID; +import org.alfresco.util.PropertyMap; /** * Form service implementation unit test. @@ -55,6 +59,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest { private FormService formService; private NamespaceService namespaceService; + private ScriptService scriptService; + private PersonService personService; + private NodeRef document; private NodeRef associatedDoc; @@ -77,6 +84,9 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest private static String LABEL_SENT_DATE = "Sent Date"; private static String LABEL_REFERENCES = "References"; + private static final String USER_ONE = "UserOne_SiteServiceImplTest"; + + /** * Called during the transaction setup */ @@ -87,11 +97,15 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest // Get the required services this.formService = (FormService)this.applicationContext.getBean("FormService"); this.namespaceService = (NamespaceService)this.applicationContext.getBean("NamespaceService"); + this.scriptService = (ScriptService)this.applicationContext.getBean("ScriptService"); + this.personService = (PersonService)this.applicationContext.getBean("PersonService"); - // Authenticate as the system user AuthenticationComponent authenticationComponent = (AuthenticationComponent) this.applicationContext .getBean("authenticationComponent"); - authenticationComponent.setSystemUserAsCurrentUser(); + + // Do the tests as userOne + createUser(USER_ONE); + authenticationComponent.setCurrentUser(USER_ONE); String guid = GUID.generate(); @@ -153,7 +167,25 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest endTransaction(); } - public void testGetForm() throws Exception + private void createUser(String userName) + { + if (this.authenticationService.authenticationExists(userName) == false) + { + this.authenticationService.createAuthentication(userName, "PWD".toCharArray()); + + PropertyMap ppOne = new PropertyMap(4); + ppOne.put(ContentModel.PROP_USERNAME, userName); + ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); + ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); + ppOne.put(ContentModel.PROP_EMAIL, "email@email.com"); + ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); + + this.personService.createPerson(ppOne); + } + } + + @SuppressWarnings("unchecked") + public void testGetForm() throws Exception { Form form = this.formService.getForm(this.document.toString()); @@ -282,11 +314,36 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest assertEquals(this.associatedDoc.toString(), targets.get(0)); } - // == Test the JavaScript API == + 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 testJSAPI() throws Exception -// { -// ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/forms/script/test_formService.js"); -// this.scriptService.executeScript(location, new HashMap(0)); -// } + 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()); + + ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/forms/script/test_formService.js"); + this.scriptService.executeScript(location, model); + } } diff --git a/source/java/org/alfresco/repo/forms/script/ScriptForm.java b/source/java/org/alfresco/repo/forms/script/ScriptForm.java new file mode 100644 index 0000000000..c7ab44ee53 --- /dev/null +++ b/source/java/org/alfresco/repo/forms/script/ScriptForm.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2005-2008 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.forms.script; + +import java.io.Serializable; +import java.util.Collection; +import java.util.List; + +import org.alfresco.repo.forms.FieldDefinition; +import org.alfresco.repo.forms.FieldGroup; +import org.alfresco.repo.forms.Form; +import org.alfresco.repo.forms.FormData; + +/** + * Form JavaScript Object. + * + * @author Neil Mc Erlean + */ +public class ScriptForm implements Serializable +{ + private static final long serialVersionUID = 579853076546002023L; + + private Form form; + + /* default */ScriptForm(Form formObject) + { + this.form = formObject; + } + + public String getItem() + { + return form.getItem(); + } + + public String getType() + { + return form.getType(); + } + + public Collection getFieldGroups() + { + return form.getFieldGroups(); + } + + public Collection getFieldDefinitions() + { + return form.getFieldDefinitions(); + } + + public List getFieldDefinitionNames() + { + return form.getFieldDefinitionNames(); + } + + public FormData getFormData() + { + return form.getFormData(); + } +} diff --git a/source/java/org/alfresco/repo/forms/script/ScriptFormService.java b/source/java/org/alfresco/repo/forms/script/ScriptFormService.java new file mode 100644 index 0000000000..f08f43ae4e --- /dev/null +++ b/source/java/org/alfresco/repo/forms/script/ScriptFormService.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2005-2008 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.forms.script; + +import org.alfresco.repo.forms.Form; +import org.alfresco.repo.forms.FormService; +import org.alfresco.repo.jscript.BaseScopableProcessorExtension; +import org.alfresco.service.ServiceRegistry; + +/** + * Script object representing the form service. + * + * @author Neil McErlean + */ +public class ScriptFormService extends BaseScopableProcessorExtension +{ + /** Service Registry */ + private ServiceRegistry serviceRegistry; + + /** The site service */ + private FormService formService; + + /** + * Sets the Service Registry + * + * @param serviceRegistry + */ + public void setServiceRegistry(ServiceRegistry serviceRegistry) + { + this.serviceRegistry = serviceRegistry; + } + + /** + * Set the form service + * + * @param formService + * the form service + */ + public void setFormService(FormService formService) + { + this.formService = formService; + } + + public ScriptForm getForm(String item) + { + Form result = formService.getForm(item); + return new ScriptForm(result); + } +} diff --git a/source/java/org/alfresco/repo/forms/script/test_formService.js b/source/java/org/alfresco/repo/forms/script/test_formService.js new file mode 100644 index 0000000000..dc51737fcb --- /dev/null +++ b/source/java/org/alfresco/repo/forms/script/test_formService.js @@ -0,0 +1,107 @@ +function testGetFormForContentNode() +{ + // Get a known form and check its various attributes/properties. + var form = formService.getForm(testDoc); + test.assertNotNull(form, "Form should have been found: " + testDoc); + + test.assertEquals(testDoc, form.getItem()); + + test.assertEquals('cm:content', form.getType()); + + test.assertNull(form.getFieldGroups()); + + var fieldDefs = form.getFieldDefinitions(); + test.assertNotNull(fieldDefs); + test.assertEquals(19, fieldDefs.size()); + + var mappedFields = new Array(); + for (var i = 0; i < fieldDefs.size(); i++) + { + mappedFields[fieldDefs.get(i).getName()] = fieldDefs.get(i); + } + var nameField = mappedFields['cm:name']; + var titleField = mappedFields['cm:title']; + var descField = mappedFields['cm:description']; + var originatorField = mappedFields['cm:originator']; + var addresseeField = mappedFields['cm:addressee']; + var addresseesField = mappedFields['cm:addressees']; + var subjectField = mappedFields['cm:subjectline']; + var sentDateField = mappedFields['cm:sentdate']; + var referencesField = mappedFields['cm:references']; + + test.assertNotNull(nameField, "Expecting to find the cm:name field"); + test.assertNotNull(titleField, "Expecting to find the cm:title field"); + test.assertNotNull(descField, "Expecting to find the cm:description field"); + test.assertNotNull(originatorField, "Expecting to find the cm:originator field"); + test.assertNotNull(addresseeField, "Expecting to find the cm:addressee field"); + test.assertNotNull(addresseesField, "Expecting to find the cm:addressees field"); + test.assertNotNull(subjectField, "Expecting to find the cm:subjectline field"); + test.assertNotNull(sentDateField, "Expecting to find the cm:sentdate field"); + test.assertNotNull(referencesField, "Expecting to find the cm:references field"); + + // check the labels of all the fields + test.assertEquals("Name", nameField.getLabel()); + test.assertEquals("Title", titleField.getLabel()); + test.assertEquals("Description", descField.getLabel()); + test.assertEquals("Originator", originatorField.getLabel()); + test.assertEquals("Addressee", addresseeField.getLabel()); + test.assertEquals("Addressees", addresseesField.getLabel()); + test.assertEquals("Subject", subjectField.getLabel()); + test.assertEquals("Sent Date", sentDateField.getLabel()); + test.assertEquals("References", referencesField.getLabel()); + + // check details of name field + test.assertEquals("d:text", nameField.getDataType()); + test.assertTrue(nameField.isMandatory()); + // Expecting cm:name to be single-valued. + test.assertFalse(nameField.isRepeating()); + + // get the constraint for the name field and check + var constraints = nameField.getConstraints(); + test.assertEquals(1, constraints.size()); + var constraint = constraints.get(0); + test.assertEquals("REGEX", constraint.getType()); + var params = constraint.getParams(); + test.assertNotNull(params); + test.assertEquals(2, params.length); + test.assertNotNull(params["expression"]); + test.assertNotNull(params["requiresMatch"]); + + // check details of the addressees field + test.assertEquals("d:text", addresseesField.getDataType()); + test.assertFalse(addresseesField.isMandatory()); + // Expecting cm:addressees to be multi-valued. + test.assertTrue(addresseesField.isRepeating()); + test.assertNull(addresseesField.getConstraints()); + + // check the details of the association field + test.assertEquals("cm:content", referencesField.getEndpointType()); + //TODO Method name typo here "Enpoint" + test.assertEquals("TARGET", referencesField.getEnpointDirection().toString()); + test.assertFalse(referencesField.isEndpointMandatory()); + test.assertTrue(referencesField.isEndpointMany()); + + // check the form data + var formData = form.getFormData(); + test.assertNotNull(formData); + var fieldData = formData.getData(); + test.assertEquals("This is the title for the test document", fieldData["cm:title"].getValue()); + test.assertEquals("This is the description for the test document", fieldData["cm:description"].getValue()); + test.assertEquals("fred@customer.com", fieldData["cm:originator"].getValue()); + test.assertEquals("bill@example.com", fieldData["cm:addressee"].getValue()); + test.assertEquals("harry@example.com", fieldData["cm:addressees_0"].getValue()); + test.assertEquals("jane@example.com", fieldData["cm:addressees_1"].getValue()); + test.assertEquals("The subject is...", fieldData["cm:subjectline"].getValue()); + + //TODO Might add the equivalent of the VALUE_SENT_DATE testing here. + // In the meantime I'll use JavaScript's own Date object to assert that it is a valid date. + var sentDate = fieldData["cm:sentdate"].getValue(); + test.assertFalse(isNaN(Date.parse(sentDate))); + + var targets = fieldData["cm:references"].getValue(); + test.assertEquals(1, targets.size()); + test.assertEquals(testAssociatedDoc, targets.get(0)); +} + +// Execute tests +testGetFormForContentNode(); \ No newline at end of file