001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.actionprocessor;
022:
023: import java.io.IOException;
024: import java.lang.reflect.InvocationTargetException;
025: import java.util.Collection;
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Vector;
030:
031: import javax.portlet.ActionRequest;
032: import javax.portlet.ActionResponse;
033: import javax.portlet.PortletException;
034:
035: import org.w3c.dom.Element;
036:
037: import com.nabhinc.portlet.mvcportlet.common.AttributeBeanCreator;
038: import com.nabhinc.portlet.mvcportlet.common.BeanCreator;
039: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
040: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
041: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
042: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
043: import com.nabhinc.portlet.mvcportlet.core.Form;
044: import com.nabhinc.portlet.mvcportlet.core.FormField;
045: import com.nabhinc.util.ArrayIterator;
046: import com.nabhinc.util.EnumerationIterator;
047: import com.nabhinc.util.ReflectionUtil;
048: import com.nabhinc.util.StringUtil;
049: import com.nabhinc.util.XMLUtil;
050:
051: /**
052: * Sets render parameters derived from bean properties.
053: *
054: * @author Padmanabh Dabke
055: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
056: */
057: public class ParameterSetter extends BaseRequestProcessor implements
058: ActionProcessor {
059:
060: private BeanCreator psBeanCreator = new AttributeBeanCreator();
061:
062: private Form[] psForms = null;
063:
064: private class ParamInfo {
065: // public PropertyFormatter formatter = FormatterFactory.getBaseFormatter();
066:
067: public String propertyName = null;
068:
069: public String parameterName = null;
070:
071: public String idPropertyName = null;
072: }
073:
074: /**
075: * Initialization.
076: *
077: * @param config
078: * XML configuration element
079: * @throws PortletException
080: * Thrown if sql parameter is not specified, "params" parameter
081: * is specified but corresponding "param-types" are not
082: * specified.
083: */
084: public void init(Element config, ControllerPortletConfig cpConfig)
085: throws PortletException {
086: super .init(config, cpConfig);
087:
088: Element beanElem = XMLUtil
089: .getSubElement(config, "bean-creator");
090: if (beanElem != null) {
091: try {
092: BeanCreator bCreator = (BeanCreator) Class.forName(
093: beanElem.getAttribute("class")).newInstance();
094: bCreator.init(beanElem);
095: psBeanCreator = bCreator;
096: } catch (Exception e) {
097: throw new PortletException(
098: "Failed to create bean creator instance.", e);
099: }
100: }
101: String formStr = config.getAttribute("forms");
102: if (formStr != null && formStr.length() > 0) {
103: String[] formNames = StringUtil.split(formStr, ",");
104: psForms = new Form[formNames.length];
105: for (int i = 0; i < formNames.length; i++) {
106: psForms[i] = brpConfig.getForm(formNames[i]);
107: if (psForms[i] == null) {
108: throw new PortletException("Bad form name "
109: + formNames[i] + ".");
110: }
111: }
112: }
113: }
114:
115: /**
116: */
117: public String process(ActionRequest request,
118: ActionResponse response, ActionConfig config)
119: throws PortletException, IOException {
120:
121: Object bean = psBeanCreator.getBean(request);
122: if (bean != null) {
123: if (psForms != null) {
124: for (int i = 0; i < psForms.length; i++) {
125: setParameters(psForms[i], request, response, bean);
126: }
127:
128: } else {
129: Form form = config.getForm();
130: if (form == null)
131: throw new PortletException(
132: "Failed to locate a form associated with this action.");
133: setParameters(form, request, response, bean);
134: }
135: }
136: return "success";
137:
138: }
139:
140: private void setParameters(Form form, ActionRequest request,
141: ActionResponse response, Object bean)
142: throws PortletException {
143: List fieldList = form.getFieldList();
144: int i = 0;
145: try {
146: for (i = 0; i < fieldList.size(); i++) {
147: FormField field = (FormField) fieldList.get(i);
148: Object prop = ReflectionUtil.getProperty(bean, field
149: .getPropertyName());
150: if (prop == null)
151: continue;
152:
153: // Check for collection types. For these create an iterator
154: // and set multiple values.
155: Iterator iter = null;
156: if (prop.getClass().isArray()) {
157: iter = new ArrayIterator(prop);
158: } else if (prop instanceof Collection) {
159: iter = (((Collection) prop).iterator());
160: } else if (prop instanceof Iterator) {
161: iter = ((Iterator) prop);
162: } else if (prop instanceof Enumeration) {
163: iter = new EnumerationIterator((Enumeration) prop);
164: }
165:
166: if (iter == null) {
167: // Single value property
168: if (field.getIdPropertyName() != null) {
169: prop = ReflectionUtil.getProperty(prop, field
170: .getIdPropertyName());
171: }
172: String paramValue = field.getParameterSetter()
173: .formatParameterValue(request, prop);
174: response.setRenderParameter(field.getName(),
175: paramValue);
176: } else {
177: Vector valueVec = new Vector(5);
178: while (iter.hasNext()) {
179: Object paramValueObj = iter.next();
180: if (paramValueObj == null)
181: continue;
182: if (field.getIdPropertyName() != null) {
183: paramValueObj = ReflectionUtil.getProperty(
184: paramValueObj, field
185: .getIdPropertyName());
186: }
187: String paramValue = field.getParameterSetter()
188: .formatParameterValue(request,
189: paramValueObj);
190: valueVec.addElement(paramValue);
191: }
192: if (valueVec.size() > 0) {
193: String[] paramValues = new String[valueVec
194: .size()];
195: valueVec.copyInto(paramValues);
196: response.setRenderParameter(field.getName(),
197: paramValues);
198: }
199: }
200: }
201: } catch (IllegalAccessException e) {
202: throw new PortletException(
203: "Failed to access bean property "
204: + ((FormField) fieldList.get(i))
205: .getPropertyName(), e);
206: } catch (InvocationTargetException e) {
207: throw new PortletException(
208: "Failed to access bean property "
209: + ((FormField) fieldList.get(i))
210: .getPropertyName(), e);
211: } catch (IllegalArgumentException e) {
212: throw new PortletException(
213: "Failed to access bean property "
214: + ((FormField) fieldList.get(i))
215: .getPropertyName(), e);
216: }
217:
218: }
219:
220: }
|