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