From: Chad B. <cwb...@us...> - 2008-07-01 15:42:35
|
User: cwbrandon Date: 08/07/01 08:42:43 Modified: andromda-jsf2/src/main/resources/templates/jsf2/flow/portlet PortletPhaseListener.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/utils JsfUtils.java.vsl FormPopulator.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/controllers Controller.java.vsl Log: Add ability to pass parameters from URL to portlets. Also add population of form from request attributes with matching names. Revision Changes Path 1.14 +18 -0 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.13 retrieving revision 1.14 diff -u -w -r1.13 -r1.14 --- PortletPhaseListener.java.vsl 30 Jun 2008 15:15:22 -0000 1.13 +++ PortletPhaseListener.java.vsl 1 Jul 2008 15:42:41 -0000 1.14 @@ -16,12 +16,30 @@ private static final String FACES_CONTEXT = "facesContext"; +#if ($liferayPortlet) + /** + * Liferay specific attribute available in the request that gives the current URL in the browser. + */ + private static final String CURRENT_URL = "CURRENT_URL"; + +#end @Override protected void handleBeforePhase(javax.faces.event.PhaseEvent event) { String viewId = this.getViewId(event); if (viewId != null && event.getFacesContext() != null) { +#if ($liferayPortlet) + // - get the parameters from the current URL and set them as request attributes to make them available to the current portlets + // (otherwise we'd need to namespace prefix the parameter names in order to pass them to the portlet - at least in Liferay) + final javax.portlet.PortletRequest request = (javax.portlet.PortletRequest)event.getFacesContext().getExternalContext().getRequest(); + final String currentUrl = (String)request.getAttribute(CURRENT_URL); + final java.util.Map<String, Object> parameters = ${managedBeansPackage}.JsfUtils.extractParameters(currentUrl); + for (final String parameter : parameters.keySet()) + { + request.setAttribute(parameter, parameters.get(parameter)); + } +#end // - make the faces context available to all views ((javax.portlet.PortletRequest)event.getFacesContext().getExternalContext().getRequest()).setAttribute(FACES_CONTEXT, event.getFacesContext()); 1.7 +99 -6 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/JsfUtils.java.vsl Index: JsfUtils.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/JsfUtils.java.vsl,v retrieving revision 1.6 retrieving revision 1.7 diff -u -w -r1.6 -r1.7 --- JsfUtils.java.vsl 20 May 2008 16:21:46 -0000 1.6 +++ JsfUtils.java.vsl 1 Jul 2008 15:42:41 -0000 1.7 @@ -255,6 +255,93 @@ } /** + * Gets the names of all attributes on the given object. + * + * @param object the object on which to get all attribute names. + * @return an array of all attribute names. + */ + public static String[] getAttributeNames(final Object object) + { + final java.util.Collection<java.lang.String> names = new java.util.ArrayList<String>(); + try + { + if (object != null) + { + try + { + final java.lang.reflect.Method method = object.getClass().getMethod("getAttributeNames", new Class[]{}); + final java.util.Enumeration enumeration = (java.util.Enumeration)method.invoke(object, (Object[])null); + if (enumeration != null) + { + while (enumeration.hasMoreElements()) + { + names.add(java.lang.String.valueOf(enumeration.nextElement())); + } + } + } + catch (NoSuchMethodException exception) + { + } + } + } + catch (Exception exception) + { + throw new RuntimeException(exception); + } + return names.toArray(new java.lang.String[0]); + } + + /** + * Extracts and returns the parameters from the given URL string. + * + * @param url the URL from which to extract parameters. + * @return the Map of parameters. + */ + @SuppressWarnings("unchecked") + public static java.util.Map<String, Object> extractParameters(final String url) + { + final java.util.Map<String, Object> parameterMap = new java.util.LinkedHashMap<String, Object>(); + if (url.contains("?")) + { + final String parameterString = url.replaceAll(".*\\?", ""); + if (parameterString.trim().length() > 0) + { + final String[] parametersAndValues = parameterString.split("\\s*&\\s*"); + for (final String parameterAndValue : parametersAndValues) + { + final String[] parameters = parameterAndValue.split("\\s*=\\s*"); + final String parameter = parameters[0]; + Object existingValue = parameterMap.get(parameter); + String value = null; + if (parameters.length > 1) + { + value = parameters[1]; + } + if (existingValue != null) + { + if (existingValue instanceof java.util.Collection) + { + ((java.util.Collection)existingValue).add(value); + } + else + { + final java.util.Collection<Object> values = new java.util.ArrayList<Object>(); + values.add(existingValue); + values.add(value); + parameterMap.put(parameter, values); + } + } + else + { + parameterMap.put(parameter, value); + } + } + } + } + return parameterMap; + } + + /** * Uses the converter identified by converterId to convert the value to a String. * @value the value to be converted * @converterId the id of the converter to be used @@ -264,7 +351,8 @@ public static String valueFromConverter( final Object value, final String converterId, - final String componentId){ + final String componentId) + { final javax.faces.context.FacesContext facesContext=javax.faces.context.FacesContext.getCurrentInstance(); final javax.faces.convert.Converter converter = facesContext.getApplication().createConverter(converterId); return converter.getAsString(facesContext, @@ -280,7 +368,8 @@ */ public static String valueFromConverter( final Object value, - final String converterId){ + final String converterId) + { final javax.faces.context.FacesContext facesContext=javax.faces.context.FacesContext.getCurrentInstance(); final javax.faces.convert.Converter converter = facesContext.getApplication().createConverter(converterId); return converter.getAsString(facesContext,null,value); @@ -292,11 +381,15 @@ * @event ActionEvent containing the parameter * @return the parameter value. */ - public static Object getParameterValue(String parameterName, javax.faces.event.ActionEvent event){ - for(Object uiObject : event.getComponent().getChildren()){ - if(uiObject instanceof javax.faces.component.UIParameter){ + public static Object getParameterValue(String parameterName, javax.faces.event.ActionEvent event) + { + for(Object uiObject : event.getComponent().getChildren()) + { + if(uiObject instanceof javax.faces.component.UIParameter) + { final javax.faces.component.UIParameter param = (javax.faces.component.UIParameter)uiObject; - if(param.getName().equals(parameterName)) { + if(param.getName().equals(parameterName)) + { return param.getValue(); } } 1.9 +22 -0 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.8 retrieving revision 1.9 diff -u -w -r1.8 -r1.9 --- FormPopulator.java.vsl 3 Jun 2008 21:22:02 -0000 1.8 +++ FormPopulator.java.vsl 1 Jul 2008 15:42:42 -0000 1.9 @@ -97,6 +97,28 @@ } /** + * Populates the form from the attributes contained in the request object. + * + * @param form the form to populate. + * @param formatters any date or time formatters. + * @param request the request object from which to populate attributes. + */ + public static final void populateFormFromRequestAttributes(final Object form, final java.util.Map formatters) + { + final javax.faces.context.FacesContext context = javax.faces.context.FacesContext.getCurrentInstance(); + final String[] names = org.andromda.presentation.jsf2.JsfUtils.getAttributeNames(context.getExternalContext().getRequest()); + final java.util.Map<String, Object> attributes = new java.util.LinkedHashMap<String, Object>(); + if (names != null) + { + for (final String name : names) + { + attributes.put(name, org.andromda.presentation.jsf2.JsfUtils.getAttribute(context.getExternalContext().getRequest(), name)); + } + } + populateFormFromPropertyMap(form, formatters, attributes); + } + + /** * 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. * 1.19 +2 -0 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.18 retrieving revision 1.19 diff -u -w -r1.18 -r1.19 --- Controller.java.vsl 3 Jun 2008 21:26:23 -0000 1.18 +++ Controller.java.vsl 1 Jul 2008 15:42:42 -0000 1.19 @@ -63,6 +63,8 @@ // is a reserved name in JSF (the id of a component), therefore we have to unfortunately ignore any availble "id" attribute ${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()); // - populate the form with any request parameters that may match ${managedBeansPackage}.${formPopulatorName}.populateFormFromPropertyMap( form, form.getDateTimeFormatters(), this.getContext().getExternalContext().getRequestParameterMap()); |