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.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: * Sets render parameter values from the default values of fields in
047: * the form associated with this action.
048: *
049: * @author Padmanabh Dabke
050: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
051: */
052: public class SetDefaultValues extends BaseRequestProcessor implements
053: ActionProcessor {
054:
055: private int sdvScope = Constants.SCOPE_UNSPECIFIED;
056: private Form[] sdvForms = null;
057:
058: /**
059: * Initialization.
060: * @param config XML configuration element
061: * @throws PortletException
062: */
063: public void init(Element arg0, ControllerPortletConfig config)
064: throws PortletException {
065: super .init(arg0, config);
066: String scopeStr = arg0.getAttribute("scope");
067: if (scopeStr != null && (!scopeStr.equals(""))) {
068: sdvScope = ConfigUtil.getScope(scopeStr);
069: }
070: String formStr = XMLUtil.getSubElementText(arg0, "forms");
071: if (formStr != null) {
072: String[] formNames = StringUtil.split(formStr, ",");
073: sdvForms = new Form[formNames.length];
074: for (int i = 0; i < formNames.length; i++) {
075: sdvForms[i] = brpConfig.getForm(formNames[i]);
076: if (sdvForms[i] == null) {
077: throw new PortletException("Bad form name "
078: + formNames[i] + ".");
079: }
080: }
081: }
082: }
083:
084: /**
085: * Sets render parameter values from the default values of fields in
086: * the form associated with this action.
087: */
088: public String process(ActionRequest request,
089: ActionResponse response, ActionConfig config)
090: throws PortletException, IOException {
091:
092: if (sdvForms != null) {
093: for (int i = 0; i < sdvForms.length; i++) {
094: setFormDefaults(sdvForms[i], request, response);
095: }
096:
097: } else {
098: Form form = config.getForm();
099: setFormDefaults(form, request, response);
100: }
101: return "success";
102: }
103:
104: private void setFormDefaults(Form form, ActionRequest request,
105: ActionResponse response) throws PortletException {
106: if (form == null) {
107: throw new PortletException(
108: "SetDefaultValues processor invoked without an associated form.");
109: }
110:
111: int scope = Constants.SCOPE_REQUEST;
112: if (sdvScope != Constants.SCOPE_UNSPECIFIED) {
113: scope = sdvScope;
114: } else if (form.getScopeAsInt() != Constants.SCOPE_UNSPECIFIED) {
115: scope = form.getScopeAsInt();
116: }
117:
118: List fields = form.getFieldList();
119: FormField field = null;
120: switch (scope) {
121: case Constants.SCOPE_REQUEST:
122: for (int i = 0; i < fields.size(); i++) {
123: field = (FormField) fields.get(i);
124: //if (field.isIndexed()) {
125: String[] fValues = field.getDefaultValues(request);
126: if (fValues != null)
127: response.setRenderParameter(field.getName(),
128: fValues);
129: /*} else {
130: String fValue = field.getDefaultValue(request);
131: // if (fValue == null) fValue = "";
132: if (fValue != null) response.setRenderParameter(field.getName(), fValue);
133: }
134: */
135: }
136: break;
137: case Constants.SCOPE_PORTLET_CONTEXT:
138: PortletContext pContext = request.getPortletSession()
139: .getPortletContext();
140: for (int i = 0; i < fields.size(); i++) {
141: field = (FormField) fields.get(i);
142: String fName = field.getName();
143: pContext.removeAttribute(fName);
144: String[] fValues = field.getDefaultValues(request);
145: if (fValues != null)
146: pContext.setAttribute(fName, fValues);
147: }
148: break;
149: case Constants.SCOPE_PORTLET_SESSION:
150: case Constants.SCOPE_APPLICATION_SESSION:
151: int sessionScope = (scope == Constants.SCOPE_APPLICATION_SESSION ? PortletSession.APPLICATION_SCOPE
152: : PortletSession.PORTLET_SCOPE);
153: PortletSession ses = request.getPortletSession();
154: for (int i = 0; i < fields.size(); i++) {
155: field = (FormField) fields.get(i);
156: String fName = field.getName();
157: ses.removeAttribute(fName, sessionScope);
158: //if (field.isIndexed()) {
159: String[] fValues = field.getDefaultValues(request);
160: if (fValues != null)
161: ses.setAttribute(fName, fValues, sessionScope);
162: /*
163: } else {
164: String fValue = field.getDefaultValue(request);
165: // if (fValue == null) fValue = "";
166: if (fValue != null) ses.setAttribute(fName, fValue, scope);
167: }
168: */
169: }
170: }
171:
172: return;
173:
174: }
175:
176: }
|