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.mail.service.MailServiceUtil;
024: import com.liferay.portal.captcha.CaptchaTextException;
025: import com.liferay.portal.captcha.CaptchaUtil;
026: import com.liferay.portal.kernel.mail.MailMessage;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.StringMaker;
029: import com.liferay.portal.kernel.util.StringPool;
030: import com.liferay.portal.kernel.util.StringUtil;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.struts.PortletAction;
033: import com.liferay.portal.util.PropsUtil;
034: import com.liferay.portlet.PortletConfigImpl;
035: import com.liferay.portlet.PortletPreferencesFactoryUtil;
036: import com.liferay.util.FileUtil;
037: import com.liferay.util.servlet.SessionErrors;
038: import com.liferay.util.servlet.SessionMessages;
039:
040: import java.util.ArrayList;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import javax.mail.internet.InternetAddress;
045:
046: import javax.portlet.ActionRequest;
047: import javax.portlet.ActionResponse;
048: import javax.portlet.PortletConfig;
049: import javax.portlet.PortletPreferences;
050: import javax.portlet.RenderRequest;
051: import javax.portlet.RenderResponse;
052:
053: import org.apache.commons.logging.Log;
054: import org.apache.commons.logging.LogFactory;
055: import org.apache.struts.action.ActionForm;
056: import org.apache.struts.action.ActionForward;
057: import org.apache.struts.action.ActionMapping;
058:
059: /**
060: * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
061: *
062: * @author Daniel Weisser
063: * @author Jorge Ferrer
064: *
065: */
066: public class ViewAction extends PortletAction {
067:
068: public void processAction(ActionMapping mapping, ActionForm form,
069: PortletConfig config, ActionRequest req, ActionResponse res)
070: throws Exception {
071:
072: PortletConfigImpl configImpl = (PortletConfigImpl) config;
073:
074: String portletId = configImpl.getPortletId();
075:
076: PortletPreferences prefs = PortletPreferencesFactoryUtil
077: .getPortletSetup(req, portletId, true, true);
078:
079: boolean requireCaptcha = GetterUtil.getBoolean(prefs.getValue(
080: "requireCaptcha", StringPool.BLANK));
081: String successURL = GetterUtil.getString(prefs.getValue(
082: "successURL", StringPool.BLANK));
083: boolean sendAsEmail = GetterUtil.getBoolean(prefs.getValue(
084: "sendAsEmail", StringPool.BLANK), true);
085: boolean saveToFile = GetterUtil.getBoolean(prefs.getValue(
086: "saveToFile", StringPool.BLANK));
087: String fileName = GetterUtil.getString(prefs.getValue(
088: "fileName", StringPool.BLANK));
089:
090: if (requireCaptcha) {
091: try {
092: CaptchaUtil.check(req);
093: } catch (CaptchaTextException cte) {
094: SessionErrors.add(req, CaptchaTextException.class
095: .getName());
096: }
097: }
098:
099: List fieldValues = new ArrayList();
100:
101: for (int i = 1; i <= _MAX_FIELDS; i++) {
102: fieldValues.add(req.getParameter("field" + i));
103: }
104:
105: if (validate(fieldValues, prefs)) {
106: boolean emailSent = false;
107: boolean fileSaved = false;
108:
109: if (sendAsEmail) {
110: emailSent = sendEmail(fieldValues, prefs);
111: }
112:
113: if (saveToFile) {
114: fileSaved = saveFile(fieldValues, prefs, fileName);
115: }
116:
117: if ((sendAsEmail == emailSent) && (saveToFile == fileSaved)) {
118: SessionMessages.add(req, "emailSent");
119: } else {
120: SessionErrors.add(req, "emailNotSent");
121: }
122: } else {
123: SessionErrors.add(req, "allFieldsRequired");
124: }
125:
126: if (SessionErrors.isEmpty(req)
127: && Validator.isNotNull(successURL)) {
128: res.sendRedirect(successURL);
129: }
130: }
131:
132: public ActionForward render(ActionMapping mapping, ActionForm form,
133: PortletConfig config, RenderRequest req, RenderResponse res)
134: throws Exception {
135:
136: return mapping.findForward("portlet.web_form.view");
137: }
138:
139: protected String getMailBody(List fieldValues,
140: PortletPreferences prefs) {
141: StringMaker sm = new StringMaker();
142:
143: Iterator itr = fieldValues.iterator();
144:
145: for (int i = 1; itr.hasNext(); i++) {
146: String fieldValue = (String) itr.next();
147: String fieldLabel = prefs.getValue("fieldLabel" + i,
148: StringPool.BLANK);
149:
150: if (Validator.isNotNull(fieldLabel)) {
151: sm.append(fieldLabel);
152: sm.append(" : ");
153: sm.append(fieldValue);
154: sm.append("\n");
155: }
156: }
157:
158: return sm.toString();
159: }
160:
161: protected boolean saveFile(List fieldValues,
162: PortletPreferences prefs, String fileName) {
163:
164: // Save the file as a standard Excel CSV format. Use ; as a delimiter,
165: // quote each entry with double quotes, and escape double quotes in
166: // values a two double quotes.
167:
168: StringMaker sm = new StringMaker();
169:
170: Iterator itr = fieldValues.iterator();
171:
172: for (int i = 1; itr.hasNext(); i++) {
173: String fieldValue = (String) itr.next();
174: String fieldLabel = prefs.getValue("fieldLabel" + i,
175: StringPool.BLANK);
176:
177: if (Validator.isNotNull(fieldLabel)) {
178: sm.append("\"");
179: sm.append(StringUtil.replace(fieldValue, "\"", "\"\""));
180: sm.append("\";");
181: }
182: }
183:
184: String s = sm.substring(0, sm.length() - 1) + "\n";
185:
186: try {
187: FileUtil.append(fileName, s);
188:
189: return true;
190: } catch (Exception e) {
191: _log.error("The web form email could not be saved", e);
192:
193: return false;
194: }
195: }
196:
197: protected boolean sendEmail(List fieldValues,
198: PortletPreferences prefs) {
199: try {
200: String subject = prefs
201: .getValue("subject", StringPool.BLANK);
202: String emailAddress = prefs.getValue("emailAddress",
203: StringPool.BLANK);
204:
205: if (Validator.isNull(emailAddress)) {
206: _log
207: .error("The web form email cannot be sent because no email "
208: + "address is configured");
209:
210: return false;
211: }
212:
213: String body = getMailBody(fieldValues, prefs);
214:
215: InternetAddress fromAddress = new InternetAddress(
216: emailAddress);
217: InternetAddress toAddress = new InternetAddress(
218: emailAddress);
219:
220: MailMessage mailMessage = new MailMessage(fromAddress,
221: toAddress, subject, body);
222:
223: MailServiceUtil.sendEmail(mailMessage);
224:
225: return true;
226: } catch (Exception e) {
227: _log.error("The web form email could not be sent", e);
228:
229: return false;
230: }
231: }
232:
233: protected boolean validate(List fieldValues,
234: PortletPreferences prefs) {
235: for (int i = 1; i < _MAX_FIELDS; i++) {
236: String fieldLabel = prefs.getValue("fieldLabel" + i,
237: StringPool.BLANK);
238: boolean fieldOptional = GetterUtil.getBoolean(prefs
239: .getValue("fieldOptional" + i, StringPool.BLANK));
240: String fieldValue = (String) fieldValues.get(i - 1);
241:
242: if (!fieldOptional && Validator.isNotNull(fieldLabel)
243: && Validator.isNull(fieldValue)) {
244:
245: return false;
246: }
247: }
248:
249: return true;
250: }
251:
252: private static final int _MAX_FIELDS = GetterUtil
253: .getInteger(PropsUtil
254: .get(PropsUtil.WEB_FORM_PORTLET_MAX_FIELDS));
255:
256: private static Log _log = LogFactory.getLog(ViewAction.class);
257:
258: }
|