001: /*
002: * (C) Copyright 2006 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.beanutil;
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.core.ActionConfig;
038: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
039: import com.nabhinc.portlet.mvcportlet.core.Form;
040: import com.nabhinc.portlet.mvcportlet.core.FormField;
041: import com.nabhinc.util.ArrayIterator;
042: import com.nabhinc.util.EnumerationIterator;
043: import com.nabhinc.util.ReflectionUtil;
044: import com.nabhinc.util.StringUtil;
045:
046: /**
047: *
048: *
049: * @author Padmanabh Dabke
050: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
051: * @since 1.1
052: */
053: public class ParameterSetter {
054: private Form[] psForms = null;
055:
056: /**
057: * Initialization.
058: *
059: */
060: public void init(Element config, ControllerPortletConfig cpConfig)
061: throws PortletException {
062: String formStr = config.getAttribute("forms");
063: if (formStr != null && formStr.length() > 0) {
064: String[] formNames = StringUtil.split(formStr, ",");
065: psForms = new Form[formNames.length];
066: for (int i = 0; i < formNames.length; i++) {
067: psForms[i] = cpConfig.getForm(formNames[i]);
068: if (psForms[i] == null) {
069: throw new PortletException("Bad form name "
070: + formNames[i] + ".");
071: }
072: }
073: }
074: }
075:
076: /**
077: */
078: public String process(ActionRequest request,
079: ActionResponse response, ActionConfig config, Object bean)
080: throws PortletException, IOException {
081:
082: if (bean != null) {
083: if (psForms != null) {
084: for (int i = 0; i < psForms.length; i++) {
085: setParameters(psForms[i], request, response, bean);
086: }
087:
088: } else {
089: Form form = config.getForm();
090: if (form == null)
091: throw new PortletException(
092: "Failed to locate a form associated with this action.");
093: setParameters(form, request, response, bean);
094: }
095: }
096: return "success";
097:
098: }
099:
100: @SuppressWarnings("unchecked")
101: private void setParameters(Form form, ActionRequest request,
102: ActionResponse response, Object bean)
103: throws PortletException {
104: List fieldList = form.getFieldList();
105: int i = 0;
106: try {
107: for (i = 0; i < fieldList.size(); i++) {
108: FormField field = (FormField) fieldList.get(i);
109: Object prop = ReflectionUtil.getProperty(bean, field
110: .getPropertyName());
111: if (prop == null)
112: continue;
113:
114: // Check for collection types. For these create an iterator
115: // and set multiple values.
116: Iterator iter = null;
117: if (prop.getClass().isArray()) {
118: iter = new ArrayIterator(prop);
119: } else if (prop instanceof Collection) {
120: iter = (((Collection) prop).iterator());
121: } else if (prop instanceof Iterator) {
122: iter = ((Iterator) prop);
123: } else if (prop instanceof Enumeration) {
124: iter = new EnumerationIterator((Enumeration) prop);
125: }
126:
127: if (iter == null) {
128: // Single value property
129: if (field.getIdPropertyName() != null) {
130: prop = ReflectionUtil.getProperty(prop, field
131: .getIdPropertyName());
132: }
133: String paramValue = field.getParameterSetter()
134: .formatParameterValue(request, prop);
135: response.setRenderParameter(field.getName(),
136: paramValue);
137: } else {
138: Vector valueVec = new Vector(5);
139: while (iter.hasNext()) {
140: Object paramValueObj = iter.next();
141: if (paramValueObj == null)
142: continue;
143: if (field.getIdPropertyName() != null) {
144: paramValueObj = ReflectionUtil.getProperty(
145: paramValueObj, field
146: .getIdPropertyName());
147: }
148: String paramValue = field.getParameterSetter()
149: .formatParameterValue(request,
150: paramValueObj);
151: valueVec.addElement(paramValue);
152: }
153: if (valueVec.size() > 0) {
154: String[] paramValues = new String[valueVec
155: .size()];
156: valueVec.copyInto(paramValues);
157: response.setRenderParameter(field.getName(),
158: paramValues);
159: }
160: }
161: }
162: } catch (IllegalAccessException e) {
163: throw new PortletException(
164: "Failed to access bean property "
165: + ((FormField) fieldList.get(i))
166: .getPropertyName(), e);
167: } catch (InvocationTargetException e) {
168: throw new PortletException(
169: "Failed to access bean property "
170: + ((FormField) fieldList.get(i))
171: .getPropertyName(), e);
172: } catch (IllegalArgumentException e) {
173: throw new PortletException(
174: "Failed to access bean property "
175: + ((FormField) fieldList.get(i))
176: .getPropertyName(), e);
177: }
178:
179: }
180:
181: }
|