1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.webform.action;
24  
25  import com.liferay.portal.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portlet.PortletPreferencesFactoryUtil;
31  import com.liferay.util.servlet.SessionErrors;
32  import com.liferay.util.servlet.SessionMessages;
33  
34  import java.io.FileNotFoundException;
35  import java.io.FileOutputStream;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  /**
45   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
50  public class ConfigurationActionImpl implements ConfigurationAction {
51  
52      public void processAction(
53              PortletConfig config, ActionRequest req, ActionResponse res)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(req, Constants.CMD);
57  
58          if (!cmd.equals(Constants.UPDATE)) {
59              return;
60          }
61  
62          String title = ParamUtil.getString(req, "title");
63          String description = ParamUtil.getString(req, "description");
64          boolean requireCaptcha = ParamUtil.getBoolean(req, "requireCaptcha");
65          String successURL = ParamUtil.getString(req, "successURL");
66          boolean sendAsEmail = ParamUtil.getBoolean(req, "sendAsEmail");
67          String subject = ParamUtil.getString(req, "subject");
68          String emailAddress = ParamUtil.getString(req, "emailAddress");
69          boolean saveToFile = ParamUtil.getBoolean(req, "saveToFile");
70          String fileName = ParamUtil.getString(req, "fileName");
71  
72          String portletResource = ParamUtil.getString(req, "portletResource");
73  
74          PortletPreferences prefs =
75              PortletPreferencesFactoryUtil.getPortletSetup(req, portletResource);
76  
77          if (Validator.isNull(title)) {
78              SessionErrors.add(req, "titleRequired");
79          }
80  
81          if (Validator.isNull(subject)) {
82              SessionErrors.add(req, "subjectRequired");
83          }
84  
85          if (!sendAsEmail && !saveToFile){
86              SessionErrors.add(req, "handlingRequired");
87          }
88  
89          if (sendAsEmail) {
90              if (Validator.isNull(emailAddress)) {
91                  SessionErrors.add(req, "emailAddressRequired");
92              }
93              else if (!Validator.isEmailAddress(emailAddress)) {
94                  SessionErrors.add(req, "emailAddressInvalid");
95              }
96          }
97  
98          if (saveToFile) {
99  
100             // Check if server can create a file as specified
101 
102             try {
103                 FileOutputStream fos = new FileOutputStream(fileName, true);
104 
105                 fos.close();
106             }
107             catch (SecurityException es) {
108                 SessionErrors.add(req, "fileNameInvalid");
109             }
110             catch (FileNotFoundException fnfe) {
111                 SessionErrors.add(req, "fileNameInvalid");
112             }
113         }
114 
115         if (!SessionErrors.isEmpty(req)) {
116             return;
117         }
118 
119         prefs.setValue("title", title);
120         prefs.setValue("description", description);
121         prefs.setValue("requireCaptcha", String.valueOf(requireCaptcha));
122         prefs.setValue("successURL", successURL);
123         prefs.setValue("sendAsEmail", String.valueOf(sendAsEmail));
124         prefs.setValue("subject", subject);
125         prefs.setValue("emailAddress", emailAddress);
126         prefs.setValue("saveToFile", String.valueOf(saveToFile));
127         prefs.setValue("fileName", fileName);
128 
129         int i = 1;
130 
131         String fieldLabel = ParamUtil.getString(req, "fieldLabel" + i);
132         String fieldType = ParamUtil.getString(req, "fieldType" + i);
133         boolean fieldOptional = ParamUtil.getBoolean(req, "fieldOptional" + i);
134         String fieldOptions = ParamUtil.getString(req, "fieldOptions" + i);
135 
136         while ((i == 1) || (fieldLabel.trim().length() > 0)) {
137             prefs.setValue("fieldLabel" + i, fieldLabel);
138             prefs.setValue("fieldType" + i, fieldType);
139             prefs.setValue("fieldOptional" + i, String.valueOf(fieldOptional));
140             prefs.setValue("fieldOptions" + i, fieldOptions);
141 
142             i++;
143 
144             fieldLabel = ParamUtil.getString(req, "fieldLabel" + i);
145             fieldType = ParamUtil.getString(req, "fieldType" + i);
146             fieldOptional = ParamUtil.getBoolean(req, "fieldOptional" + i);
147             fieldOptions = ParamUtil.getString(req, "fieldOptions" + i);
148         }
149 
150         // Clear previous preferences that are now blank
151 
152         fieldLabel = prefs.getValue("fieldLabel" + i, StringPool.BLANK);
153 
154         while (fieldLabel.trim().length() > 0) {
155             prefs.setValue("fieldLabel" + i, StringPool.BLANK);
156             prefs.setValue("fieldType" + i, StringPool.BLANK);
157             prefs.setValue("fieldOptional" + i, StringPool.BLANK);
158             prefs.setValue("fieldOptions" + i, StringPool.BLANK);
159 
160             i++;
161 
162             fieldLabel = prefs.getValue("fieldLabel" + i, StringPool.BLANK);
163         }
164 
165         if (SessionErrors.isEmpty(req)) {
166             prefs.store();
167 
168             SessionMessages.add(req, config.getPortletName() + ".doConfigure");
169         }
170     }
171 
172     public String render(
173             PortletConfig config, RenderRequest req, RenderResponse res)
174         throws Exception {
175 
176         return "/html/portlet/web_form/configuration.jsp";
177     }
178 
179 }