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: import java.util.List;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletContext;
029: import javax.portlet.PortletException;
030: import javax.portlet.PortletSession;
031:
032: import org.w3c.dom.Element;
033:
034: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
035: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
036: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
037: import com.nabhinc.portlet.mvcportlet.core.Constants;
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.portlet.mvcportlet.util.ConfigUtil;
042: import com.nabhinc.util.StringUtil;
043: import com.nabhinc.util.XMLUtil;
044:
045: /**
046: * Resets attribute values
047: *
048: * @author Padmanabh Dabke
049: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
050: */
051: public class ResetValues extends BaseRequestProcessor implements
052: ActionProcessor {
053:
054: private int sdvScope = Constants.SCOPE_UNSPECIFIED;
055: private Form[] sdvForms = null;
056:
057: /**
058: * Initialization.
059: * @param config XML configuration element
060: * @throws PortletException
061: */
062: public void init(Element arg0, ControllerPortletConfig config)
063: throws PortletException {
064: super .init(arg0, config);
065: String scopeStr = arg0.getAttribute("scope");
066: if (scopeStr != null && (!scopeStr.equals(""))) {
067: sdvScope = ConfigUtil.getScope(scopeStr);
068: if (sdvScope == Constants.SCOPE_REQUEST) {
069: throw new PortletException("Invalid scope string: "
070: + scopeStr + ".");
071: }
072: }
073: String formStr = XMLUtil.getSubElementText(arg0, "forms");
074: if (formStr != null) {
075: String[] formNames = StringUtil.split(formStr, ",");
076: sdvForms = new Form[formNames.length];
077: for (int i = 0; i < formNames.length; i++) {
078: sdvForms[i] = brpConfig.getForm(formNames[i]);
079: if (sdvForms[i] == null) {
080: throw new PortletException("Bad form name "
081: + formNames[i] + ".");
082: }
083: }
084: }
085: }
086:
087: /**
088: * Resets render parameter values from the default values of fields in
089: * the form associated with this action. Only portlet session, application
090: * scope and portlet context attributes can be reset (request scope parameters do not
091: * need to explicitly reset).
092: */
093: public String process(ActionRequest request,
094: ActionResponse response, ActionConfig config)
095: throws PortletException, IOException {
096:
097: if (sdvForms != null) {
098: for (int i = 0; i < sdvForms.length; i++) {
099: resetFormAttributes(sdvForms[i], request, response);
100: }
101:
102: } else {
103: Form form = config.getForm();
104: resetFormAttributes(form, request, response);
105: }
106: return "success";
107: }
108:
109: private void resetFormAttributes(Form form, ActionRequest request,
110: ActionResponse response) throws PortletException {
111: if (form == null) {
112: throw new PortletException(
113: "ResetValues processor invoked without an associated form.");
114: }
115: int scope = Constants.SCOPE_PORTLET_SESSION;
116: if (sdvScope != Constants.SCOPE_UNSPECIFIED) {
117: scope = sdvScope;
118: } else if (form.getScopeAsInt() != Constants.SCOPE_UNSPECIFIED) {
119: scope = form.getScopeAsInt();
120: }
121:
122: List fields = form.getFieldList();
123: if (scope == Constants.SCOPE_PORTLET_CONTEXT) {
124: PortletContext pContext = request.getPortletSession()
125: .getPortletContext();
126: for (int i = 0; i < fields.size(); i++) {
127: FormField field = (FormField) fields.get(i);
128: String fName = field.getName();
129: pContext.removeAttribute(fName);
130: }
131:
132: } else {
133: int sessionScope = (scope == Constants.SCOPE_APPLICATION_SESSION) ? PortletSession.APPLICATION_SCOPE
134: : PortletSession.PORTLET_SCOPE;
135: PortletSession ses = request.getPortletSession();
136: for (int i = 0; i < fields.size(); i++) {
137: FormField field = (FormField) fields.get(i);
138: String fName = field.getName();
139: ses.removeAttribute(fName, sessionScope);
140: }
141:
142: }
143: return;
144:
145: }
146:
147: }
|