From: Chad B. <cwb...@us...> - 2008-07-03 18:27:09
|
User: cwbrandon Date: 08/07/03 11:27:18 Modified: andromda-jsf2/src/main/resources/templates/jsf2/flow ViewPopulator.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/flow/portlet PortletPhaseListener.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/controllers Controller.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/utils FormPopulator.java.vsl Log: improve population from request attributes (don't override values that are already set). Also don't populate view variables with values that haven't been already set on the form. Revision Changes Path 1.11 +11 -1 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/ViewPopulator.java.vsl Index: ViewPopulator.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/ViewPopulator.java.vsl,v retrieving revision 1.10 retrieving revision 1.11 diff -u -w -r1.10 -r1.11 --- ViewPopulator.java.vsl 3 Jul 2008 16:33:04 -0000 1.10 +++ ViewPopulator.java.vsl 3 Jul 2008 18:27:17 -0000 1.11 @@ -187,7 +187,17 @@ final boolean $readableVariable = org.apache.commons.beanutils.PropertyUtils.isReadable(form, "$variable.name"); if ($readableVariable) { - request.setAttribute("$variable.name", org.apache.commons.beanutils.PropertyUtils.getProperty(form, "$variable.name")); + java.lang.Boolean propertySet = null; + final String isSetPropertyName = "${variable.name}Set"; + if (org.apache.commons.beanutils.PropertyUtils.isReadable(form, isSetPropertyName)) + { + propertySet = (java.lang.Boolean)org.apache.commons.beanutils.PropertyUtils.getProperty(form, isSetPropertyName); + } + // - only set the property if its been set, or we can't tell if it has + if (propertySet == null || propertySet) + { + request.setAttribute("${variable.name}", org.apache.commons.beanutils.PropertyUtils.getProperty(form, "${variable.name}")); + } #if ($variable.reset) // reset $variable.name (since the model indicates that it should be reset) org.apache.commons.beanutils.PropertyUtils.setProperty(form, "$variable.name", $variable.type.javaNullString); 1.15 +2 -3 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/portlet/PortletPhaseListener.java.vsl Index: PortletPhaseListener.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/portlet/PortletPhaseListener.java.vsl,v retrieving revision 1.14 retrieving revision 1.15 diff -u -w -r1.14 -r1.15 --- PortletPhaseListener.java.vsl 1 Jul 2008 15:42:41 -0000 1.14 +++ PortletPhaseListener.java.vsl 3 Jul 2008 18:27:18 -0000 1.15 @@ -70,9 +70,9 @@ event.getFacesContext(), null, controller.${useCase.controllerAction}()); // - get the new view id before population since we are navigating to a new view viewId = event.getFacesContext().getViewRoot().getViewId(); - this.populateView(event, viewId); } #end + this.populateView(event, viewId); } else if (javax.faces.event.PhaseId.APPLY_REQUEST_VALUES.equals(event.getPhaseId())) { @@ -112,8 +112,7 @@ protected void handleAfterPhase(javax.faces.event.PhaseEvent event) { final String viewId = this.getViewId(event); - if (javax.faces.event.PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId()) || - javax.faces.event.PhaseId.APPLY_REQUEST_VALUES.equals(event.getPhaseId())) + if (javax.faces.event.PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId())) { this.populateView(event, viewId); } 1.20 +1 -1 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/controllers/Controller.java.vsl Index: Controller.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/controllers/Controller.java.vsl,v retrieving revision 1.19 retrieving revision 1.20 diff -u -w -r1.19 -r1.20 --- Controller.java.vsl 1 Jul 2008 15:42:42 -0000 1.19 +++ Controller.java.vsl 3 Jul 2008 18:27:18 -0000 1.20 @@ -64,7 +64,7 @@ ${managedBeansPackage}.${formPopulatorName}.populateFormFromPropertyMap( form, form.getDateTimeFormatters(), (java.util.Map)this.getRequestAttribute(ACTION_EVENT_ATTRIBUTES), new String[] {"id"}); // - populate the form with any request attributes that may match - ${managedBeansPackage}.${formPopulatorName}.populateFormFromRequestAttributes(form, form.getDateTimeFormatters()); + ${managedBeansPackage}.${formPopulatorName}.populateFormFromRequestAttributes(form, form.getDateTimeFormatters(), false); // - populate the form with any request parameters that may match ${managedBeansPackage}.${formPopulatorName}.populateFormFromPropertyMap( form, form.getDateTimeFormatters(), this.getContext().getExternalContext().getRequestParameterMap()); 1.12 +92 -44 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/FormPopulator.java.vsl Index: FormPopulator.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/FormPopulator.java.vsl,v retrieving revision 1.11 retrieving revision 1.12 diff -u -w -r1.11 -r1.12 --- FormPopulator.java.vsl 2 Jul 2008 23:44:38 -0000 1.11 +++ FormPopulator.java.vsl 3 Jul 2008 18:27:18 -0000 1.12 @@ -102,8 +102,9 @@ * @param form the form to populate. * @param formatters any date or time formatters. * @param request the request object from which to populate attributes. + * @param override whether or not to override properties already set on the given form. */ - public static final void populateFormFromRequestAttributes(final Object form, final java.util.Map formatters) + public static final void populateFormFromRequestAttributes(final Object form, final java.util.Map formatters, boolean override) { final javax.faces.context.FacesContext context = javax.faces.context.FacesContext.getCurrentInstance(); final String[] names = ${managedBeansPackage}.JsfUtils.getAttributeNames(context.getExternalContext().getRequest()); @@ -115,7 +116,21 @@ attributes.put(name, ${managedBeansPackage}.JsfUtils.getAttribute(context.getExternalContext().getRequest(), name)); } } - populateFormFromPropertyMap(form, formatters, attributes); + populateFormFromPropertyMap(form, formatters, attributes, override); + } + + /** + * Populates the form from the given map of properties. If a matching property is null or an empty + * string, then null is placed on the form. + * + * @param form the form to populate. + * @param formatters any date or time formatters. + * @param properties the properties to populate from. + * @param override whether or not to override properties already set on the given form. + */ + public static final void populateFormFromPropertyMap(final Object form, final java.util.Map formatters, final java.util.Map properties, boolean override) + { + populateFormFromPropertyMap(form, formatters, properties, null, override); } /** @@ -128,7 +143,7 @@ */ public static final void populateFormFromPropertyMap(final Object form, final java.util.Map formatters, final java.util.Map properties) { - populateFormFromPropertyMap(form, formatters, properties, null); + populateFormFromPropertyMap(form, formatters, properties, null, true); } /** @@ -140,7 +155,24 @@ * @param properties the properties to populate from. * @param ignoreProperties names of any properties to ignore when it comes to populating on the form. */ - public static final void populateFormFromPropertyMap(final Object form, final java.util.Map formatters, final java.util.Map properties, final String[] ignoreProperties) + public static final void populateFormFromPropertyMap(final Object form, final java.util.Map formatters, + final java.util.Map properties, final String[] ignoreProperties) + { + populateFormFromPropertyMap(form, formatters, properties, ignoreProperties, true); + } + + /** + * Populates the form from the given map of properties. If a matching property is null or an empty + * string, then null is placed on the form. + * + * @param form the form to populate. + * @param formatters any date or time formatters. + * @param properties the properties to populate from. + * @param ignoreProperties names of any properties to ignore when it comes to populating on the form. + * @param override whether or not to override properties already set on the given form. + */ + public static final void populateFormFromPropertyMap(final Object form, final java.util.Map formatters, + final java.util.Map properties, final String[] ignoreProperties, boolean override) { if (properties != null) { @@ -157,6 +189,21 @@ org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptor(form, name); if (descriptor != null) { + boolean populateProperty = true; + if (!override) + { + final String isSetPropertyName = name + "Set"; + if (org.apache.commons.beanutils.PropertyUtils.isReadable(form, isSetPropertyName)) + { + final boolean isPropertySet = (java.lang.Boolean)org.apache.commons.beanutils.PropertyUtils.getProperty(form, isSetPropertyName); + if (isPropertySet) + { + populateProperty = false; + } + } + } + if (populateProperty) + { final Object property = properties.get(name); // - only convert if the string is not empty @@ -219,6 +266,7 @@ } } } + } catch (final Throwable throwable) { throw new RuntimeException(throwable); |