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.common;
022:
023: import java.io.IOException;
024: import java.util.List;
025:
026: import javax.portlet.PortletException;
027: import javax.portlet.PortletRequest;
028: import javax.portlet.PortletResponse;
029: import javax.portlet.PortletSession;
030:
031: import org.w3c.dom.Element;
032:
033: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
034: import com.nabhinc.portlet.mvcportlet.core.Constants;
035: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
036: import com.nabhinc.portlet.mvcportlet.core.Form;
037: import com.nabhinc.portlet.mvcportlet.core.FormField;
038: import com.nabhinc.portlet.mvcportlet.core.RequestConfig;
039: import com.nabhinc.portlet.mvcportlet.util.ConfigUtil;
040: import com.nabhinc.util.StringUtil;
041: import com.nabhinc.util.XMLUtil;
042:
043: /**
044: * Sets attribute values from request parameters or preferences. For setting attributes
045: * from request parameters, the action processor is
046: * configured as follows:
047: * <pre>
048: * <action-processor name="AttrSetter"
049: * class="com.nabhinc.portlet.mvcportlet.actionprocessor.AttributeSetter">
050: * <params>param1,param2,param3</params>
051: * <scope>scopeString</scope>
052: * <attributes>attr1,attr2,attr3</attributes>
053: * </action-processor>
054: * </pre>
055: * "params" is a comma separated list of request parameters. If you want to set attributes
056: * from portlet preferences, use "prefs" tag. Only one of these two tags can be specified at a time.<br/>
057: * "scope" must be <code>request</code>, <code>portlet_session</code>, <code>application_session</code> or <code>portlet_context</code>.
058: * If the "scope" is not set, and this processor is mapped to a form, then the
059: * form element's scope, if specified, will be used. Otherwise, <code>portlet_session</code> scope is
060: * assumed.<br/>
061: * "attributes" is a comma separated list of attribute names. If this is ommitted
062: * attribute names are assumed to be the same names as the parameter names.
063: *
064: * @author Padmanabh Dabke
065: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
066: * @since 1.1
067: */
068: public class BaseAttributeSetter extends BaseRequestProcessor {
069:
070: private String[] asParamNames = null;
071: private String[] asPrefNames = null;
072: private String[] asAttrNames = null;
073: private int asScope = Constants.SCOPE_UNSPECIFIED;
074:
075: /**
076: * Requires <code>params</code> element to be specified.
077: */
078: public void init(Element config,
079: ControllerPortletConfig controllerConfig)
080: throws PortletException {
081: super .init(config, controllerConfig);
082: String paramStr = XMLUtil.getSubElementText(config, "params");
083: String prefStr = XMLUtil.getSubElementText(config, "prefs");
084:
085: if (paramStr != null && prefStr != null) {
086: throw new PortletException(
087: "params or prefs cannot both be specified at the same time.");
088: }
089:
090: if (paramStr != null) {
091: asParamNames = StringUtil.split(paramStr, ",");
092:
093: String attrStr = XMLUtil.getSubElementText(config,
094: "attributes");
095: if (attrStr == null) {
096: asAttrNames = asParamNames;
097: } else {
098: asAttrNames = StringUtil.split(attrStr, ",");
099: }
100:
101: if (asParamNames.length != asAttrNames.length) {
102: throw new PortletException(
103: "Number of parameters != number of attributes.");
104: }
105: } else if (prefStr != null) {
106: asPrefNames = StringUtil.split(prefStr, ",");
107:
108: String attrStr = XMLUtil.getSubElementText(config,
109: "attributes");
110: if (attrStr == null) {
111: asAttrNames = asPrefNames;
112: } else {
113: asAttrNames = StringUtil.split(attrStr, ",");
114: }
115:
116: if (asPrefNames.length != asAttrNames.length) {
117: throw new PortletException(
118: "Number of preferences != number of attributes.");
119: }
120:
121: }
122: String scopeStr = XMLUtil.getSubElementText(config, "scope");
123: if (scopeStr != null) {
124: asScope = ConfigUtil.getScope(scopeStr);
125: }
126: }
127:
128: /**
129: * Processes the request's parameters and sets them as attributes to the
130: * specified scope. <code>success</code> is always returned.
131: * @param request
132: * @param response
133: * @param actionConfig
134: * @return <code>success</code>
135: * @throws PortletException
136: * @throws IOException
137: */
138: public String process(PortletRequest request,
139: PortletResponse response, RequestConfig actionConfig)
140: throws PortletException, IOException {
141:
142: Form f = actionConfig.getForm();
143: if (asParamNames == null && asPrefNames == null) {
144: setParamNamesFromForm(f);
145: }
146: int scope = Constants.SCOPE_PORTLET_SESSION;
147: if (asScope != Constants.SCOPE_UNSPECIFIED) {
148: scope = asScope;
149: } else if (f != null
150: && f.getScopeAsInt() != Constants.SCOPE_UNSPECIFIED) {
151: scope = f.getScopeAsInt();
152: }
153: String[] sourceArray = asParamNames == null ? asPrefNames
154: : asParamNames;
155: for (int i = 0; i < sourceArray.length; i++) {
156: //String paramValue = request.getParameter(asParamNames[i]);
157: String[] paramValues = asParamNames == null ? request
158: .getPreferences().getValues(asPrefNames[i], null)
159: : request.getParameterValues(asParamNames[i]);
160: if (paramValues == null || paramValues.length == 0)
161: continue;
162:
163: switch (scope) {
164: case Constants.SCOPE_REQUEST:
165: request.setAttribute(asAttrNames[i], paramValues);
166: break;
167: case Constants.SCOPE_PORTLET_CONTEXT:
168: request.getPortletSession().getPortletContext()
169: .setAttribute(asAttrNames[i], paramValues);
170: break;
171: case Constants.SCOPE_APPLICATION_SESSION:
172: request.getPortletSession().setAttribute(
173: asAttrNames[i], paramValues,
174: PortletSession.APPLICATION_SCOPE);
175: break;
176: case Constants.SCOPE_PORTLET_SESSION:
177: request.getPortletSession().setAttribute(
178: asAttrNames[i], paramValues);
179: break;
180: }
181: }
182: return "success";
183: }
184:
185: private synchronized void setParamNamesFromForm(Form f) {
186: List fieldList = f.getFieldList();
187: this .asParamNames = new String[fieldList.size()];
188: for (int i = 0; i < fieldList.size(); i++) {
189: this .asParamNames[i] = ((FormField) fieldList.get(i))
190: .getName();
191: }
192: }
193:
194: }
|