From ac4d99423b5b588a96ffdd5bfe70d04ed2d5beee Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Tue, 25 Nov 2008 09:29:08 +0000 Subject: [PATCH] Implementing FormConfigElement.combine and associated tests. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@12111 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../web/config/FormConfigElement.java | 223 +++++++++++++++++- .../config/WebClientFormsOverridingTest.java | 71 ++++-- .../test-config-forms-override.xml | 44 +++- 3 files changed, 305 insertions(+), 33 deletions(-) diff --git a/source/java/org/alfresco/web/config/FormConfigElement.java b/source/java/org/alfresco/web/config/FormConfigElement.java index b000b3d52c..4d391409e0 100644 --- a/source/java/org/alfresco/web/config/FormConfigElement.java +++ b/source/java/org/alfresco/web/config/FormConfigElement.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.StringTokenizer; @@ -115,10 +116,123 @@ public class FormConfigElement extends ConfigElementAdapter /** * @see org.alfresco.config.ConfigElement#combine(org.alfresco.config.ConfigElement) */ - public ConfigElement combine(ConfigElement configElement) + public ConfigElement combine(ConfigElement otherConfigElement) { - // TODO Impl this. - throw new UnsupportedOperationException("This method not yet impl'd."); + FormConfigElement otherFormElem = (FormConfigElement)otherConfigElement; + FormConfigElement result = new FormConfigElement(); + + combineSubmissionURL(otherFormElem, result); + + combineTemplates(otherFormElem, result); + + combineFieldVisibilities(otherFormElem, result); + + combineSets(otherFormElem, result); + + //TODO Fields + + //TODO field-controls + + //TODO constraint-for-field + + combineModelOverrides(otherFormElem, result); + + return result; + } + + private void combineModelOverrides(FormConfigElement otherFormElem, + FormConfigElement result) + { + for (StringPair override : modelOverrides) + { + result.addModelOverrides(override.getName(), override.getValue()); + } + for (StringPair override : otherFormElem.modelOverrides) + { + result.addModelOverrides(override.getName(), override.getValue()); + } + } + + private void combineSets(FormConfigElement otherFormElem, + FormConfigElement result) + { + for (String nextOldSet : setIdentifiers) + { + FormSet nextOldSetData = sets.get(nextOldSet); + String setId = nextOldSetData.getSetId(); + String parentId = nextOldSetData.getParentId(); + String appearance = nextOldSetData.getAppearance(); + result.addSet(setId, parentId, appearance); + } + for (String nextNewSet : otherFormElem.setIdentifiers) + { + FormSet nextNewSetData = otherFormElem.sets.get(nextNewSet); + String setId = nextNewSetData.getSetId(); + String parentId = nextNewSetData.getParentId(); + String appearance = nextNewSetData.getAppearance(); + result.addSet(setId, parentId, appearance); + } + } + + private void combineFieldVisibilities(FormConfigElement otherFormElem, + FormConfigElement result) + { + List combinedInstructions = new ArrayList(); + combinedInstructions.addAll(this.visibilityInstructions); + + //If there are already instructions pertaining to a particular + // field id, we can just leave them in place as the new should override the old. + //TODO Test this is true. + combinedInstructions.addAll(otherFormElem.visibilityInstructions); + for (FieldVisibilityInstruction fvi : combinedInstructions) + { + result.addFieldVisibility(fvi.isShow() ? "show" : "hide" + , fvi.getFieldId(), fvi.getModesAsString()); + } + } + + private void combineTemplates(FormConfigElement otherFormElem, + FormConfigElement result) + { + for (String s : this.createTemplates) + { + String reqsRole = this.rolesForCreateTemplates.get(s); + result.addFormTemplate("create-form", s, reqsRole); + } + for (String s : otherFormElem.createTemplates) + { + String reqsRole = otherFormElem.rolesForCreateTemplates.get(s); + result.addFormTemplate("create-form", s, reqsRole); + } + + for (String s : this.editTemplates) + { + String reqsRole = this.rolesForEditTemplates.get(s); + result.addFormTemplate("edit-form", s, reqsRole); + } + for (String s : otherFormElem.editTemplates) + { + String reqsRole = otherFormElem.rolesForEditTemplates.get(s); + result.addFormTemplate("edit-form", s, reqsRole); + } + + for (String s : this.viewTemplates) + { + String reqsRole = this.rolesForViewTemplates.get(s); + result.addFormTemplate("view-form", s, reqsRole); + } + for (String s : otherFormElem.viewTemplates) + { + String reqsRole = otherFormElem.rolesForViewTemplates.get(s); + result.addFormTemplate("view-form", s, reqsRole); + } + } + + private void combineSubmissionURL(FormConfigElement otherFormElem, + FormConfigElement result) + { + String otherSubmissionURL = otherFormElem.getSubmissionURL(); + result.setSubmissionURL(otherSubmissionURL == null ? this.submissionURL : otherSubmissionURL); } public String getSubmissionURL() @@ -257,6 +371,21 @@ public class FormConfigElement extends ConfigElementAdapter return null; } + public List getCreateTemplates() + { + return Collections.unmodifiableList(createTemplates); + } + + public List getEditTemplates() + { + return Collections.unmodifiableList(editTemplates); + } + + public List getViewTemplates() + { + return Collections.unmodifiableList(viewTemplates); + } + public List getModelOverrideProperties() { return Collections.unmodifiableList(modelOverrides); @@ -277,17 +406,26 @@ public class FormConfigElement extends ConfigElementAdapter if (nodeName.equals("create-form")) { - createTemplates.add(template); + if (!createTemplates.contains(template)) + { + createTemplates.add(template); + } rolesForCreateTemplates.put(template, requiredRole); } else if (nodeName.equals("edit-form")) { - editTemplates.add(template); + if (!editTemplates.contains(template)) + { + editTemplates.add(template); + } rolesForEditTemplates.put(template, requiredRole); } else if (nodeName.equals("view-form")) { - viewTemplates.add(template); + if (!viewTemplates.contains(template)) + { + viewTemplates.add(template); + } rolesForViewTemplates.put(template, requiredRole); } else @@ -306,7 +444,10 @@ public class FormConfigElement extends ConfigElementAdapter /* package */void addSet(String setId, String parentSetId, String appearance) { - setIdentifiers.add(setId); + if (!setIdentifiers.contains(setId)) + { + setIdentifiers.add(setId); + } sets.put(setId, new FormSet(setId, parentSetId, appearance)); } @@ -344,7 +485,16 @@ public class FormConfigElement extends ConfigElementAdapter /* package */void addModelOverrides(String name, String value) { - modelOverrides.add(new StringPair(name, value)); + StringPair modelOverride = new StringPair(name, value); + //TODO Consider using a different collection type here. + for (Iterator iter = modelOverrides.iterator(); iter.hasNext(); ) + { + if (iter.next().getName().equals(name)) + { + iter.remove(); + } + } + modelOverrides.add(modelOverride); } public static class FormSet @@ -370,6 +520,27 @@ public class FormConfigElement extends ConfigElementAdapter { return appearance; } + + @Override + public boolean equals(Object otherObj) + { + if (otherObj == null + || !otherObj.getClass().equals(this.getClass())) + { + return false; + } + FormSet otherSet = (FormSet) otherObj; + return this.setId.equals(otherSet.setId) + && this.parentId.equals(otherSet.parentId) + && this.appearance.equals(otherSet.appearance); + } + + @Override + public int hashCode() + { + return setId.hashCode() + + 7 * parentId.hashCode() + 13 * appearance.hashCode(); + } } public static class FormField @@ -478,6 +649,20 @@ public class FormConfigElement extends ConfigElementAdapter return Collections.unmodifiableList(forModes); } + public String getModesAsString() + { + StringBuilder result = new StringBuilder(); + for (Iterator iter = forModes.iterator(); iter.hasNext(); ) + { + result.append(iter.next()); + if (iter.hasNext()) + { + result.append(", "); + } + } + return result.toString(); + } + public String toString() { StringBuilder result = new StringBuilder(); @@ -485,5 +670,27 @@ public class FormConfigElement extends ConfigElementAdapter result.append(" ").append(fieldId).append(" ").append(forModes); return result.toString(); } + + @Override + public boolean equals(Object otherObj) + { + if (otherObj == null + || !otherObj.getClass().equals(this.getClass())) + { + return false; + } + FieldVisibilityInstruction otherFVI = (FieldVisibilityInstruction) otherObj; + return this.show == otherFVI.show + && this.fieldId.equals(otherFVI.fieldId) + && this.forModes.equals(otherFVI.forModes); + } + + @Override + public int hashCode() + { + return show ? 1 : 0 + + 7 * fieldId.hashCode() + 13 * forModes.hashCode(); + } } } + diff --git a/source/java/org/alfresco/web/config/WebClientFormsOverridingTest.java b/source/java/org/alfresco/web/config/WebClientFormsOverridingTest.java index df08049775..438875b259 100644 --- a/source/java/org/alfresco/web/config/WebClientFormsOverridingTest.java +++ b/source/java/org/alfresco/web/config/WebClientFormsOverridingTest.java @@ -24,6 +24,9 @@ */ package org.alfresco.web.config; +import java.util.ArrayList; +import java.util.List; + import org.alfresco.config.Config; import org.alfresco.config.ConfigElement; import org.alfresco.config.xml.XMLConfigService; @@ -31,29 +34,44 @@ import org.alfresco.util.BaseTest; import org.alfresco.web.config.DefaultControlsConfigElement.ControlParam; /** - * JUnit tests to exercise the forms-related capabilities in to the web client config - * service. These only include those override-related tests that require multiple - * config xml files. + * JUnit tests to exercise the forms-related capabilities in to the web client + * config service. These only include those override-related tests that require + * multiple config xml files. * * @author Neil McErlean */ public class WebClientFormsOverridingTest extends BaseTest { - /** - * @see junit.framework.TestCase#setUp() - */ - protected void setUp() throws Exception - { - super.setUp(); - } + private XMLConfigService configService; + private Config globalConfig; + private FormConfigElement formConfigElement; + private static final String FORMS_CONFIG = "test-config-forms.xml"; + private static final String FORMS_OVERRIDE_CONFIG = "test-config-forms-override.xml"; - public void testDefaultControlsOverride() + /** + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { - XMLConfigService svc = initXMLConfigService("test-config-forms.xml", - "test-config-forms-override.xml"); + super.setUp(); + configService = initXMLConfigService(FORMS_CONFIG, + FORMS_OVERRIDE_CONFIG); + assertNotNull("configService was null.", configService); + globalConfig = configService.getGlobalConfig(); + assertNotNull("Global config was null.", globalConfig); - // get hold of the default-controls config from the global section - Config globalConfig = svc.getGlobalConfig(); + Config contentConfig = configService.getConfig("content"); + assertNotNull("contentConfig was null.", contentConfig); + + ConfigElement confElement = contentConfig.getConfigElement("form"); + assertNotNull("confElement was null.", confElement); + assertTrue("confElement should be instanceof FormConfigElement.", + confElement instanceof FormConfigElement); + formConfigElement = (FormConfigElement) confElement; + } + + public void testDefaultControlsOverride() + { ConfigElement globalDefaultControls = globalConfig .getConfigElement("default-controls"); assertNotNull("global default-controls element should not be null", @@ -71,14 +89,9 @@ public class WebClientFormsOverridingTest extends BaseTest assertTrue("New control-param missing.", dcCE .getControlParamsFor("abc").contains(expectedNewControlParam)); } - - public void testConstraintHandlersOverride() - { - XMLConfigService svc = initXMLConfigService("test-config-forms.xml", - "test-config-forms-override.xml"); - // get hold of the constraint-handlers config from the global section - Config globalConfig = svc.getGlobalConfig(); + public void testConstraintHandlersOverride() + { ConfigElement globalConstraintHandlers = globalConfig .getConfigElement("constraint-handlers"); assertNotNull("global constraint-handlers element should not be null", @@ -97,4 +110,18 @@ public class WebClientFormsOverridingTest extends BaseTest assertEquals("Modified message is wrong.", "Overridden Message", chCE .getMessageFor("NUMERIC")); } + + public void testFormSubmissionUrlAndModelOverridePropsOverride() + { + assertEquals("Submission URL was incorrect.", "overridden/submission/url", + formConfigElement.getSubmissionURL()); + + List expectedModelOverrideProperties = new ArrayList(); + expectedModelOverrideProperties.add(new StringPair( + "fields.title.mandatory", "false")); + assertEquals("Expected property missing.", + expectedModelOverrideProperties, formConfigElement + .getModelOverrideProperties()); + } + } diff --git a/source/test-resources/test-config-forms-override.xml b/source/test-resources/test-config-forms-override.xml index 19e6e398e6..f7b8f44123 100644 --- a/source/test-resources/test-config-forms-override.xml +++ b/source/test-resources/test-config-forms-override.xml @@ -1,4 +1,4 @@ - + @@ -12,13 +12,51 @@ Never. - + + + message="Overridden Message" message-id="regex_error" /> + + + + +
+ + + + + + + + + + + + + + + + + bar + + + + + + + + + false + + +
+
\ No newline at end of file