001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.webform.action;
022:
023: import com.liferay.portal.kernel.portlet.ConfigurationAction;
024: import com.liferay.portal.kernel.util.Constants;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portlet.PortletPreferencesFactoryUtil;
029: import com.liferay.util.servlet.SessionErrors;
030: import com.liferay.util.servlet.SessionMessages;
031:
032: import java.io.FileNotFoundException;
033: import java.io.FileOutputStream;
034:
035: import javax.portlet.ActionRequest;
036: import javax.portlet.ActionResponse;
037: import javax.portlet.PortletConfig;
038: import javax.portlet.PortletPreferences;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041:
042: /**
043: * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Jorge Ferrer
046: *
047: */
048: public class ConfigurationActionImpl implements ConfigurationAction {
049:
050: public void processAction(PortletConfig config, ActionRequest req,
051: ActionResponse res) throws Exception {
052:
053: String cmd = ParamUtil.getString(req, Constants.CMD);
054:
055: if (!cmd.equals(Constants.UPDATE)) {
056: return;
057: }
058:
059: String title = ParamUtil.getString(req, "title");
060: String description = ParamUtil.getString(req, "description");
061: boolean requireCaptcha = ParamUtil.getBoolean(req,
062: "requireCaptcha");
063: String successURL = ParamUtil.getString(req, "successURL");
064: boolean sendAsEmail = ParamUtil.getBoolean(req, "sendAsEmail");
065: String subject = ParamUtil.getString(req, "subject");
066: String emailAddress = ParamUtil.getString(req, "emailAddress");
067: boolean saveToFile = ParamUtil.getBoolean(req, "saveToFile");
068: String fileName = ParamUtil.getString(req, "fileName");
069:
070: String portletResource = ParamUtil.getString(req,
071: "portletResource");
072:
073: PortletPreferences prefs = PortletPreferencesFactoryUtil
074: .getPortletSetup(req, portletResource, true, true);
075:
076: if (Validator.isNull(title)) {
077: SessionErrors.add(req, "titleRequired");
078: }
079:
080: if (Validator.isNull(subject)) {
081: SessionErrors.add(req, "subjectRequired");
082: }
083:
084: if (!sendAsEmail && !saveToFile) {
085: SessionErrors.add(req, "handlingRequired");
086: }
087:
088: if (sendAsEmail) {
089: if (Validator.isNull(emailAddress)) {
090: SessionErrors.add(req, "emailAddressRequired");
091: } else if (!Validator.isEmailAddress(emailAddress)) {
092: SessionErrors.add(req, "emailAddressInvalid");
093: }
094: }
095:
096: if (saveToFile) {
097:
098: // Check if server can create a file as specified
099:
100: try {
101: FileOutputStream fos = new FileOutputStream(fileName,
102: true);
103:
104: fos.close();
105: } catch (SecurityException es) {
106: SessionErrors.add(req, "fileNameInvalid");
107: } catch (FileNotFoundException fnfe) {
108: SessionErrors.add(req, "fileNameInvalid");
109: }
110: }
111:
112: if (!SessionErrors.isEmpty(req)) {
113: return;
114: }
115:
116: prefs.setValue("title", title);
117: prefs.setValue("description", description);
118: prefs
119: .setValue("requireCaptcha", String
120: .valueOf(requireCaptcha));
121: prefs.setValue("successURL", successURL);
122: prefs.setValue("sendAsEmail", String.valueOf(sendAsEmail));
123: prefs.setValue("subject", subject);
124: prefs.setValue("emailAddress", emailAddress);
125: prefs.setValue("saveToFile", String.valueOf(saveToFile));
126: prefs.setValue("fileName", fileName);
127:
128: int i = 1;
129:
130: String fieldLabel = ParamUtil.getString(req, "fieldLabel" + i);
131: String fieldType = ParamUtil.getString(req, "fieldType" + i);
132: boolean fieldOptional = ParamUtil.getBoolean(req,
133: "fieldOptional" + i);
134: String fieldOptions = ParamUtil.getString(req, "fieldOptions"
135: + i);
136:
137: while ((i == 1) || (fieldLabel.trim().length() > 0)) {
138: prefs.setValue("fieldLabel" + i, fieldLabel);
139: prefs.setValue("fieldType" + i, fieldType);
140: prefs.setValue("fieldOptional" + i, String
141: .valueOf(fieldOptional));
142: prefs.setValue("fieldOptions" + i, fieldOptions);
143:
144: i++;
145:
146: fieldLabel = ParamUtil.getString(req, "fieldLabel" + i);
147: fieldType = ParamUtil.getString(req, "fieldType" + i);
148: fieldOptional = ParamUtil.getBoolean(req, "fieldOptional"
149: + i);
150: fieldOptions = ParamUtil.getString(req, "fieldOptions" + i);
151: }
152:
153: // Clear previous preferences that are now blank
154:
155: fieldLabel = prefs.getValue("fieldLabel" + i, StringPool.BLANK);
156:
157: while (fieldLabel.trim().length() > 0) {
158: prefs.setValue("fieldLabel" + i, StringPool.BLANK);
159: prefs.setValue("fieldType" + i, StringPool.BLANK);
160: prefs.setValue("fieldOptional" + i, StringPool.BLANK);
161: prefs.setValue("fieldOptions" + i, StringPool.BLANK);
162:
163: i++;
164:
165: fieldLabel = prefs.getValue("fieldLabel" + i,
166: StringPool.BLANK);
167: }
168:
169: if (SessionErrors.isEmpty(req)) {
170: prefs.store();
171:
172: SessionMessages.add(req, config.getPortletName()
173: + ".doConfigure");
174: }
175: }
176:
177: public String render(PortletConfig config, RenderRequest req,
178: RenderResponse res) throws Exception {
179:
180: return "/html/portlet/web_form/configuration.jsp";
181: }
182:
183: }
|