001: /*
002: * (C) Copyright 2005 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:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletException;
028: import javax.portlet.PortletSession;
029: import javax.portlet.ValidatorException;
030:
031: import org.w3c.dom.Element;
032:
033: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
034: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
035: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
036: import com.nabhinc.portlet.mvcportlet.core.Constants;
037: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
038: import com.nabhinc.portlet.mvcportlet.core.Form;
039: import com.nabhinc.portlet.mvcportlet.util.ConfigUtil;
040: import com.nabhinc.util.StringUtil;
041: import com.nabhinc.util.XMLUtil;
042:
043: /**
044: * Sets portlet preference values from request parameters/session attributes.
045: * The action processor is configured as follows:
046: * <pre>
047: * <action-processor name="PrefSetter"
048: * class="com.nabhinc.portlet.mvcportlet.actionprocessor.PreferenceSetter">
049: * <params>param1,param2,param3</params>
050: * <scope>scopeString</scope>
051: * <prefs>pref1,pref2,pref3</prefs>
052: * </action-processor>
053: * </pre>
054: * "params" is a comma separated list of request parameters.<br/>
055: * "scope" must be <code>request</code>, <code>portlet_session</code>, <code>application_session</code>
056: * or <code>portlet_context</code>. The scope applies to the parameter names. In case of session scopes
057: * the parameter names are interpreted as session attribute names.
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>request</code> scope is
060: * assumed.<br/>
061: * "prefs" is a comma separated list of preference names. If this is ommitted
062: * attribute names are assumed to be the same names as the parameter names.
063: * <p/>
064: * This processor returns "success" if preference are valid and saved successfully. It returns
065: * "preference-validation-error" if ValidatorException is thrown during the store call on
066: * preferences. The request parameter <code>Constants.ERROR_MESSAGE_PARAM + request.getPortletMode()</code>
067: * is set to the localized value of the error message.
068: *
069: * @author Padmanabh Dabke
070: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
071: * @since 0.9.1
072: */
073: public class PreferenceSetter extends BaseRequestProcessor implements
074: ActionProcessor {
075:
076: private String[] psParamNames = null;
077: private String[] psPrefNames = null;
078: private int asScope = Constants.SCOPE_UNSPECIFIED;
079:
080: /**
081: * Requires <code>params</code> element to be specified.
082: */
083: public void init(Element config,
084: ControllerPortletConfig controllerConfig)
085: throws PortletException {
086: super .init(config, controllerConfig);
087: String paramStr = XMLUtil.getSubElementText(config, "params");
088: if (paramStr == null) {
089: throw new PortletException(
090: "Missing required config element: param");
091: }
092:
093: psParamNames = StringUtil.split(paramStr, ",");
094:
095: String attrStr = XMLUtil.getSubElementText(config, "prefs");
096: if (attrStr == null) {
097: psPrefNames = psParamNames;
098: } else {
099: psPrefNames = StringUtil.split(attrStr, ",");
100: }
101:
102: if (psParamNames.length != psPrefNames.length) {
103: throw new PortletException(
104: "Number of parameters != number of attributes.");
105: }
106: String scopeStr = XMLUtil.getSubElementText(config, "scope");
107: if (scopeStr != null) {
108: asScope = ConfigUtil.getScope(scopeStr);
109: }
110: }
111:
112: /**
113: * Processes the request's parameters and sets them as attributes to the
114: * specified scope. <code>success</code> is always returned.
115: * @param request
116: * @param response
117: * @param actionConfig
118: * @return <code>success</code>
119: * @throws PortletException
120: * @throws IOException
121: */
122: public String process(ActionRequest request,
123: ActionResponse response, ActionConfig actionConfig)
124: throws PortletException, IOException {
125:
126: Form f = actionConfig.getForm();
127: int scope = Constants.SCOPE_REQUEST;
128: if (asScope != Constants.SCOPE_UNSPECIFIED) {
129: scope = asScope;
130: } else if (f != null
131: && f.getScopeAsInt() != Constants.SCOPE_UNSPECIFIED) {
132: scope = f.getScopeAsInt();
133: }
134: for (int i = 0; i < psParamNames.length; i++) {
135: //String paramValue = request.getParameter(asParamNames[i]);
136: String[] paramValues = null;
137:
138: switch (scope) {
139: case Constants.SCOPE_REQUEST:
140: paramValues = request
141: .getParameterValues(psParamNames[i]);
142: break;
143: case Constants.SCOPE_PORTLET_CONTEXT:
144: paramValues = (String[]) request.getPortletSession()
145: .getPortletContext().getAttribute(
146: psParamNames[i]);
147: break;
148: case Constants.SCOPE_APPLICATION_SESSION:
149: paramValues = (String[]) request.getPortletSession()
150: .getAttribute(psParamNames[i],
151: PortletSession.APPLICATION_SCOPE);
152: break;
153: case Constants.SCOPE_PORTLET_SESSION:
154: paramValues = (String[]) request.getPortletSession()
155: .getAttribute(psParamNames[i],
156: PortletSession.PORTLET_SCOPE);
157: break;
158: }
159:
160: request.getPreferences().setValues(psPrefNames[i],
161: paramValues);
162: }
163: try {
164: request.getPreferences().store();
165: } catch (ValidatorException e) {
166: response.setRenderParameter(
167: Constants.ERROR_MESSAGE_PARAM
168: + request.getPortletMode().toString()
169: .toLowerCase(), brpConfig
170: .getLocalizedMessage(e.getMessage(),
171: request));
172: return "preference-validation-error";
173: }
174: return "success";
175: }
176:
177: }
|