1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ConfigurationActionImpl extends BaseConfigurationAction {
54  
55      public void processAction(
56              PortletConfig portletConfig, ActionRequest actionRequest,
57              ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          if (!cmd.equals(Constants.UPDATE)) {
63              return;
64          }
65  
66          String title = ParamUtil.getString(actionRequest, "title");
67          String description = ParamUtil.getString(actionRequest, "description");
68          boolean requireCaptcha = ParamUtil.getBoolean(
69              actionRequest, "requireCaptcha");
70          String successURL = ParamUtil.getString(actionRequest, "successURL");
71  
72          boolean sendAsEmail = ParamUtil.getBoolean(
73              actionRequest, "sendAsEmail");
74          String subject = ParamUtil.getString(actionRequest, "subject");
75          String emailAddress = ParamUtil.getString(
76              actionRequest, "emailAddress");
77  
78          boolean saveToDatabase = ParamUtil.getBoolean(
79              actionRequest, "saveToDatabase");
80  
81          boolean saveToFile = ParamUtil.getBoolean(actionRequest, "saveToFile");
82          String fileName = ParamUtil.getString(actionRequest, "fileName");
83  
84          boolean updateFields = ParamUtil.getBoolean(
85              actionRequest, "updateFields");
86  
87          String portletResource = ParamUtil.getString(
88              actionRequest, "portletResource");
89  
90          PortletPreferences preferences =
91              PortletPreferencesFactoryUtil.getPortletSetup(
92                  actionRequest, portletResource);
93  
94          if (Validator.isNull(title)) {
95              SessionErrors.add(actionRequest, "titleRequired");
96          }
97  
98          if (Validator.isNull(subject)) {
99              SessionErrors.add(actionRequest, "subjectRequired");
100         }
101 
102         if (!sendAsEmail && !saveToDatabase && !saveToFile) {
103             SessionErrors.add(actionRequest, "handlingRequired");
104         }
105 
106         if (sendAsEmail) {
107             if (Validator.isNull(emailAddress)) {
108                 SessionErrors.add(actionRequest, "emailAddressRequired");
109             }
110             else if (!Validator.isEmailAddress(emailAddress)) {
111                 SessionErrors.add(actionRequest, "emailAddressInvalid");
112             }
113         }
114 
115         if (saveToFile) {
116 
117             // Check if server can create a file as specified
118 
119             try {
120                 FileOutputStream fos = new FileOutputStream(fileName, true);
121 
122                 fos.close();
123             }
124             catch (SecurityException es) {
125                 SessionErrors.add(actionRequest, "fileNameInvalid");
126             }
127             catch (FileNotFoundException fnfe) {
128                 SessionErrors.add(actionRequest, "fileNameInvalid");
129             }
130         }
131 
132         if (!SessionErrors.isEmpty(actionRequest)) {
133             return;
134         }
135 
136         preferences.setValue("title", title);
137         preferences.setValue("description", description);
138         preferences.setValue("requireCaptcha", String.valueOf(requireCaptcha));
139         preferences.setValue("successURL", successURL);
140         preferences.setValue("sendAsEmail", String.valueOf(sendAsEmail));
141         preferences.setValue("subject", subject);
142         preferences.setValue("emailAddress", emailAddress);
143         preferences.setValue("saveToDatabase", String.valueOf(saveToDatabase));
144         preferences.setValue("saveToFile", String.valueOf(saveToFile));
145         preferences.setValue("fileName", fileName);
146 
147         if (updateFields) {
148             int i = 1;
149 
150             String databaseTableName = WebFormUtil.getNewDatabaseTableName(
151                 portletResource);
152 
153             preferences.setValue("databaseTableName", databaseTableName);
154 
155             int[] formFieldsIndexes = StringUtil.split(
156                 ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
157 
158             for (int formFieldsIndex : formFieldsIndexes) {
159                 String fieldLabel = ParamUtil.getString(
160                     actionRequest, "fieldLabel" + formFieldsIndex);
161 
162                 if (Validator.isNull(fieldLabel)) {
163                     continue;
164                 }
165 
166                 String fieldType = ParamUtil.getString(
167                     actionRequest, "fieldType" + formFieldsIndex);
168                 boolean fieldOptional = ParamUtil.getBoolean(
169                     actionRequest, "fieldOptional" + formFieldsIndex);
170                 String fieldOptions = ParamUtil.getString(
171                     actionRequest, "fieldOptions" + formFieldsIndex);
172                 String fieldValidationScript = ParamUtil.getString(
173                     actionRequest, "fieldValidationScript" + formFieldsIndex);
174                 String fieldValidationErrorMessage = ParamUtil.getString(
175                     actionRequest,
176                     "fieldValidationErrorMessage" + formFieldsIndex);
177 
178                 if ((Validator.isNotNull(fieldValidationScript) ^
179                     (Validator.isNotNull(fieldValidationErrorMessage)))) {
180 
181                     SessionErrors.add(
182                         actionRequest, "invalidValidationDefinition" + i);
183                 }
184 
185                 preferences.setValue("fieldLabel" + i, fieldLabel);
186                 preferences.setValue("fieldType" + i, fieldType);
187                 preferences.setValue(
188                     "fieldOptional" + i, String.valueOf(fieldOptional));
189                 preferences.setValue("fieldOptions" + i, fieldOptions);
190                 preferences.setValue(
191                     "fieldValidationScript" + i, fieldValidationScript);
192                 preferences.setValue(
193                     "fieldValidationErrorMessage" + i,
194                     fieldValidationErrorMessage);
195 
196                 i++;
197             }
198 
199             if (!SessionErrors.isEmpty(actionRequest)) {
200                 return;
201             }
202 
203             // Clear previous preferences that are now blank
204 
205             String fieldLabel = preferences.getValue(
206                 "fieldLabel" + i, StringPool.BLANK);
207 
208             while (Validator.isNotNull(fieldLabel)) {
209                 preferences.setValue("fieldLabel" + i, StringPool.BLANK);
210                 preferences.setValue("fieldType" + i, StringPool.BLANK);
211                 preferences.setValue("fieldOptional" + i, StringPool.BLANK);
212                 preferences.setValue("fieldOptions" + i, StringPool.BLANK);
213                 preferences.setValue(
214                     "fieldValidationScript" + i, StringPool.BLANK);
215                 preferences.setValue(
216                     "fieldValidationErrorMessage" + i, StringPool.BLANK);
217 
218                 i++;
219 
220                 fieldLabel = preferences.getValue(
221                     "fieldLabel" + i, StringPool.BLANK);
222             }
223         }
224 
225         if (SessionErrors.isEmpty(actionRequest)) {
226             preferences.store();
227 
228             SessionMessages.add(
229                 actionRequest, portletConfig.getPortletName() + ".doConfigure");
230         }
231     }
232 
233     public String render(
234             PortletConfig portletConfig, RenderRequest renderRequest,
235             RenderResponse renderResponse)
236         throws Exception {
237 
238         return "/html/portlet/web_form/configuration.jsp";
239     }
240 
241 }