1
22
23 package com.liferay.portlet.webform.action;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.mail.service.MailServiceUtil;
27 import com.liferay.portal.captcha.CaptchaTextException;
28 import com.liferay.portal.captcha.CaptchaUtil;
29 import com.liferay.portal.kernel.mail.MailMessage;
30 import com.liferay.portal.kernel.servlet.SessionErrors;
31 import com.liferay.portal.kernel.servlet.SessionMessages;
32 import com.liferay.portal.kernel.util.FileUtil;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.StringPool;
35 import com.liferay.portal.kernel.util.StringUtil;
36 import com.liferay.portal.kernel.util.Validator;
37 import com.liferay.portal.struts.PortletAction;
38 import com.liferay.portlet.PortletConfigImpl;
39 import com.liferay.portlet.PortletPreferencesFactoryUtil;
40 import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
41 import com.liferay.portlet.webform.util.WebFormUtil;
42
43 import java.util.ArrayList;
44 import java.util.Iterator;
45 import java.util.List;
46
47 import javax.mail.internet.InternetAddress;
48
49 import javax.portlet.ActionRequest;
50 import javax.portlet.ActionResponse;
51 import javax.portlet.PortletConfig;
52 import javax.portlet.PortletPreferences;
53 import javax.portlet.RenderRequest;
54 import javax.portlet.RenderResponse;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.apache.struts.action.ActionForm;
59 import org.apache.struts.action.ActionForward;
60 import org.apache.struts.action.ActionMapping;
61
62
70 public class ViewAction extends PortletAction {
71
72 public void processAction(
73 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
74 ActionRequest actionRequest, ActionResponse actionResponse)
75 throws Exception {
76
77 PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
78
79 String portletId = portletConfigImpl.getPortletId();
80
81 PortletPreferences prefs =
82 PortletPreferencesFactoryUtil.getPortletSetup(
83 actionRequest, portletId);
84
85 boolean requireCaptcha = GetterUtil.getBoolean(
86 prefs.getValue("requireCaptcha", StringPool.BLANK));
87 String successURL = GetterUtil.getString(
88 prefs.getValue("successURL", StringPool.BLANK));
89 boolean sendAsEmail = GetterUtil.getBoolean(
90 prefs.getValue("sendAsEmail", StringPool.BLANK));
91 boolean saveToDatabase = GetterUtil.getBoolean(
92 prefs.getValue("saveToDatabase", StringPool.BLANK));
93 String databaseTableName = GetterUtil.getString(
94 prefs.getValue("databaseTableName", StringPool.BLANK));
95 boolean saveToFile = GetterUtil.getBoolean(
96 prefs.getValue("saveToFile", StringPool.BLANK));
97 String fileName = GetterUtil.getString(
98 prefs.getValue("fileName", StringPool.BLANK));
99
100 if (requireCaptcha) {
101 try {
102 CaptchaUtil.check(actionRequest);
103 }
104 catch (CaptchaTextException cte) {
105 SessionErrors.add(
106 actionRequest, CaptchaTextException.class.getName());
107
108 return;
109 }
110 }
111
112 List<String> fieldValues = new ArrayList<String>();
113
114 for (int i = 1; i <= WebFormUtil.MAX_FIELDS; i++) {
115 fieldValues.add(actionRequest.getParameter("field" + i));
116 }
117
118 if (validate(fieldValues, prefs)) {
119 boolean emailSuccess = true;
120 boolean databaseSuccess = true;
121 boolean fileSuccess = true;
122
123 if (sendAsEmail) {
124 emailSuccess = sendEmail(fieldValues, prefs);
125 }
126
127 if (saveToDatabase) {
128 if (Validator.isNull(databaseTableName)) {
129 databaseTableName = WebFormUtil.getNewDatabaseTableName(
130 portletId);
131
132 prefs.setValue("databaseTableName", databaseTableName);
133
134 prefs.store();
135 }
136
137 databaseSuccess = saveDatabase(
138 fieldValues, prefs, databaseTableName);
139 }
140
141 if (saveToFile) {
142 fileSuccess = saveFile(fieldValues, prefs, fileName);
143 }
144
145 if (emailSuccess && databaseSuccess && fileSuccess) {
146 SessionMessages.add(actionRequest, "success");
147 }
148 else {
149 SessionErrors.add(actionRequest, "error");
150 }
151 }
152 else {
153 SessionErrors.add(actionRequest, "requiredFieldMissing");
154 }
155
156 if (SessionErrors.isEmpty(actionRequest)) {
157 if (Validator.isNotNull(successURL)) {
158 actionResponse.sendRedirect(successURL);
159 }
160 else {
161 sendRedirect(actionRequest, actionResponse);
162 }
163 }
164 }
165
166 public ActionForward render(
167 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
168 RenderRequest renderRequest, RenderResponse renderResponse)
169 throws Exception {
170
171 return mapping.findForward("portlet.web_form.view");
172 }
173
174 protected String getMailBody(
175 List<String> fieldValues, PortletPreferences prefs) {
176
177 StringBuilder sb = new StringBuilder();
178
179 Iterator<String> itr = fieldValues.iterator();
180
181 for (int i = 1; itr.hasNext(); i++) {
182 String fieldValue = itr.next();
183
184 String fieldLabel = prefs.getValue(
185 "fieldLabel" + i, StringPool.BLANK);
186
187 if (Validator.isNotNull(fieldLabel)) {
188 sb.append(fieldLabel);
189 sb.append(" : ");
190 sb.append(fieldValue);
191 sb.append("\n");
192 }
193 }
194
195 return sb.toString();
196 }
197
198 private boolean saveDatabase(
199 List<String> fieldValues, PortletPreferences prefs,
200 String databaseTableName)
201 throws Exception {
202
203 WebFormUtil.checkTable(databaseTableName, prefs);
204
205 long classPK = CounterLocalServiceUtil.increment(
206 WebFormUtil.class.getName());
207
208 Iterator<String> itr = fieldValues.iterator();
209
210 try {
211 for (int i = 1; itr.hasNext(); i++) {
212 String fieldValue = itr.next();
213
214 String fieldLabel = prefs.getValue(
215 "fieldLabel" + i, StringPool.BLANK);
216
217 if (Validator.isNotNull(fieldLabel)) {
218 ExpandoValueLocalServiceUtil.addValue(
219 WebFormUtil.class.getName(), databaseTableName,
220 fieldLabel, classPK, fieldValue);
221 }
222 }
223
224 return true;
225 }
226 catch (Exception e) {
227 _log.error(
228 "The web form data could not be saved to the database", e);
229
230 return false;
231 }
232 }
233
234 protected boolean saveFile(
235 List<String> fieldValues, PortletPreferences prefs, String fileName) {
236
237
241 StringBuilder sb = new StringBuilder();
242
243 Iterator<String> itr = fieldValues.iterator();
244
245 for (int i = 1; itr.hasNext(); i++) {
246 String fieldValue = itr.next();
247
248 String fieldLabel = prefs.getValue(
249 "fieldLabel" + i, StringPool.BLANK);
250
251 if (Validator.isNotNull(fieldLabel)) {
252 sb.append("\"");
253 sb.append(StringUtil.replace(fieldValue, "\"", "\"\""));
254 sb.append("\";");
255 }
256 }
257
258 String s = sb.substring(0, sb.length() - 1) + "\n";
259
260 try {
261 FileUtil.write(fileName, s, false, true);
262
263 return true;
264 }
265 catch (Exception e) {
266 _log.error("The web form data could not be saved to a file", e);
267
268 return false;
269 }
270 }
271
272 protected boolean sendEmail(
273 List<String> fieldValues, PortletPreferences prefs) {
274
275 try {
276 String subject = prefs.getValue("subject", StringPool.BLANK);
277 String emailAddress = prefs.getValue(
278 "emailAddress", StringPool.BLANK);
279
280 if (Validator.isNull(emailAddress)) {
281 _log.error(
282 "The web form email cannot be sent because no email " +
283 "address is configured");
284
285 return false;
286 }
287
288 String body = getMailBody(fieldValues, prefs);
289
290 InternetAddress fromAddress = new InternetAddress(emailAddress);
291 InternetAddress toAddress = new InternetAddress(emailAddress);
292
293 MailMessage mailMessage = new MailMessage(
294 fromAddress, toAddress, subject, body);
295
296 MailServiceUtil.sendEmail(mailMessage);
297
298 return true;
299 }
300 catch (Exception e) {
301 _log.error("The web form email could not be sent", e);
302
303 return false;
304 }
305 }
306
307 protected boolean validate(
308 List<String> fieldValues, PortletPreferences prefs) {
309
310 for (int i = 1; i < WebFormUtil.MAX_FIELDS; i++) {
311 String fieldValue = fieldValues.get(i - 1);
312
313 String fieldLabel = prefs.getValue(
314 "fieldLabel" + i, StringPool.BLANK);
315 boolean fieldOptional = GetterUtil.getBoolean(
316 prefs.getValue("fieldOptional" + i, StringPool.BLANK));
317
318 if (!fieldOptional && Validator.isNotNull(fieldLabel) &&
319 Validator.isNull(fieldValue)) {
320
321 return false;
322 }
323 }
324
325 return true;
326 }
327
328 protected boolean isCheckMethodOnProcessAction() {
329 return _CHECK_METHOD_ON_PROCESS_ACTION;
330 }
331
332 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
333
334 private static Log _log = LogFactory.getLog(ViewAction.class);
335
336 }