1   /**
2    * Copyright (c) 2000-2009 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.BaseConfigurationAction;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.servlet.SessionMessages;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.portlet.webform.util.WebFormUtil;
35  
36  import java.io.FileNotFoundException;
37  import java.io.FileOutputStream;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletPreferences;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  /**
47   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Jorge Ferrer
50   * @author Alberto Montero
51   * @author Julio Camarero
52   *
53   */
54  public class ConfigurationActionImpl extends BaseConfigurationAction {
55  
56      public void processAction(
57              PortletConfig portletConfig, ActionRequest actionRequest,
58              ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          if (!cmd.equals(Constants.UPDATE)) {
64              return;
65          }
66  
67          String title = ParamUtil.getString(actionRequest, "title");
68          String description = ParamUtil.getString(actionRequest, "description");
69          boolean requireCaptcha = ParamUtil.getBoolean(
70              actionRequest, "requireCaptcha");
71          String successURL = ParamUtil.getString(actionRequest, "successURL");
72  
73          boolean sendAsEmail = ParamUtil.getBoolean(
74              actionRequest, "sendAsEmail");
75          String subject = ParamUtil.getString(actionRequest, "subject");
76          String emailAddress = ParamUtil.getString(
77              actionRequest, "emailAddress");
78  
79          boolean saveToDatabase = ParamUtil.getBoolean(
80              actionRequest, "saveToDatabase");
81  
82          boolean saveToFile = ParamUtil.getBoolean(actionRequest, "saveToFile");
83          String fileName = ParamUtil.getString(actionRequest, "fileName");
84  
85          boolean updateFields = ParamUtil.getBoolean(
86              actionRequest, "updateFields");
87  
88          String portletResource = ParamUtil.getString(
89              actionRequest, "portletResource");
90  
91          PortletPreferences preferences =
92              PortletPreferencesFactoryUtil.getPortletSetup(
93                  actionRequest, portletResource);
94  
95          if (Validator.isNull(title)) {
96              SessionErrors.add(actionRequest, "titleRequired");
97          }
98  
99          if (Validator.isNull(subject)) {
100             SessionErrors.add(actionRequest, "subjectRequired");
101         }
102 
103         if (!sendAsEmail && !saveToDatabase && !saveToFile) {
104             SessionErrors.add(actionRequest, "handlingRequired");
105         }
106 
107         if (sendAsEmail) {
108             if (Validator.isNull(emailAddress)) {
109                 SessionErrors.add(actionRequest, "emailAddressRequired");
110             }
111             else if (!Validator.isEmailAddress(emailAddress)) {
112                 SessionErrors.add(actionRequest, "emailAddressInvalid");
113             }
114         }
115 
116         if (saveToFile) {
117 
118             // Check if server can create a file as specified
119 
120             try {
121                 FileOutputStream fos = new FileOutputStream(fileName, true);
122 
123                 fos.close();
124             }
125             catch (SecurityException es) {
126                 SessionErrors.add(actionRequest, "fileNameInvalid");
127             }
128             catch (FileNotFoundException fnfe) {
129                 SessionErrors.add(actionRequest, "fileNameInvalid");
130             }
131         }
132 
133         if (!SessionErrors.isEmpty(actionRequest)) {
134             return;
135         }
136 
137         preferences.setValue("title", title);
138         preferences.setValue("description", description);
139         preferences.setValue("requireCaptcha", String.valueOf(requireCaptcha));
140         preferences.setValue("successURL", successURL);
141         preferences.setValue("sendAsEmail", String.valueOf(sendAsEmail));
142         preferences.setValue("subject", subject);
143         preferences.setValue("emailAddress", emailAddress);
144         preferences.setValue("saveToDatabase", String.valueOf(saveToDatabase));
145         preferences.setValue("saveToFile", String.valueOf(saveToFile));
146         preferences.setValue("fileName", fileName);
147 
148         if (updateFields) {
149             int i = 1;
150 
151             String databaseTableName = WebFormUtil.getNewDatabaseTableName(
152                 portletResource);
153 
154             preferences.setValue("databaseTableName", databaseTableName);
155 
156             int[] formFieldsIndexes = StringUtil.split(
157                 ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
158 
159             for (int formFieldsIndex : formFieldsIndexes) {
160                 String fieldLabel = ParamUtil.getString(
161                     actionRequest, "fieldLabel" + formFieldsIndex);
162 
163                 if (Validator.isNull(fieldLabel)){
164                     continue;
165                 }
166 
167                 String fieldType = ParamUtil.getString(
168                     actionRequest, "fieldType" + formFieldsIndex);
169                 boolean fieldOptional = ParamUtil.getBoolean(
170                     actionRequest, "fieldOptional" + formFieldsIndex);
171                 String fieldOptions = ParamUtil.getString(
172                     actionRequest, "fieldOptions" + formFieldsIndex);
173                 String fieldValidationScript = ParamUtil.getString(
174                     actionRequest, "fieldValidationScript" + formFieldsIndex);
175                 String fieldValidationErrorMessage = ParamUtil.getString(
176                     actionRequest,
177                     "fieldValidationErrorMessage" + formFieldsIndex);
178 
179                 if ((Validator.isNotNull(fieldValidationScript) ^
180                     (Validator.isNotNull(fieldValidationErrorMessage)))) {
181 
182                     SessionErrors.add(
183                         actionRequest, "invalidValidationDefinition" + i);
184                 }
185 
186                 preferences.setValue("fieldLabel" + i, fieldLabel);
187                 preferences.setValue("fieldType" + i, fieldType);
188                 preferences.setValue(
189                     "fieldOptional" + i, String.valueOf(fieldOptional));
190                 preferences.setValue("fieldOptions" + i, fieldOptions);
191                 preferences.setValue(
192                     "fieldValidationScript" + i, fieldValidationScript);
193                 preferences.setValue(
194                     "fieldValidationErrorMessage" + i,
195                     fieldValidationErrorMessage);
196 
197                 i++;
198             }
199 
200             if (!SessionErrors.isEmpty(actionRequest)) {
201                 return;
202             }
203 
204             // Clear previous preferences that are now blank
205 
206             String fieldLabel = preferences.getValue(
207                 "fieldLabel" + i, StringPool.BLANK);
208 
209             while (Validator.isNotNull(fieldLabel)) {
210                 preferences.setValue("fieldLabel" + i, StringPool.BLANK);
211                 preferences.setValue("fieldType" + i, StringPool.BLANK);
212                 preferences.setValue("fieldOptional" + i, StringPool.BLANK);
213                 preferences.setValue("fieldOptions" + i, StringPool.BLANK);
214                 preferences.setValue(
215                     "fieldValidationScript" + i, StringPool.BLANK);
216                 preferences.setValue(
217                     "fieldValidationErrorMessage" + i, StringPool.BLANK);
218 
219                 i++;
220 
221                 fieldLabel = preferences.getValue(
222                     "fieldLabel" + i, StringPool.BLANK);
223             }
224         }
225 
226         if (SessionErrors.isEmpty(actionRequest)) {
227             preferences.store();
228 
229             SessionMessages.add(
230                 actionRequest, portletConfig.getPortletName() + ".doConfigure");
231         }
232     }
233 
234     public String render(
235             PortletConfig portletConfig, RenderRequest renderRequest,
236             RenderResponse renderResponse)
237         throws Exception {
238 
239         return "/html/portlet/web_form/configuration.jsp";
240     }
241 
242 }