1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.webform.action;
21  
22  import com.liferay.portal.kernel.portlet.ConfigurationAction;
23  import com.liferay.portal.kernel.servlet.SessionErrors;
24  import com.liferay.portal.kernel.servlet.SessionMessages;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portlet.PortletPreferencesFactoryUtil;
31  import com.liferay.portlet.webform.util.WebFormUtil;
32  
33  import java.io.FileNotFoundException;
34  import java.io.FileOutputStream;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletPreferences;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  /**
44   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   * @author Alberto Montero
48   * @author Julio Camarero
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 preferences =
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         preferences.setValue("title", title);
135         preferences.setValue("description", description);
136         preferences.setValue("requireCaptcha", String.valueOf(requireCaptcha));
137         preferences.setValue("successURL", successURL);
138         preferences.setValue("sendAsEmail", String.valueOf(sendAsEmail));
139         preferences.setValue("subject", subject);
140         preferences.setValue("emailAddress", emailAddress);
141         preferences.setValue("saveToDatabase", String.valueOf(saveToDatabase));
142         preferences.setValue("saveToFile", String.valueOf(saveToFile));
143         preferences.setValue("fileName", fileName);
144 
145         if (updateFields) {
146             int i = 1;
147 
148             String databaseTableName = WebFormUtil.getNewDatabaseTableName(
149                 portletResource);
150 
151             preferences.setValue("databaseTableName", databaseTableName);
152 
153             int[] formFieldsIndexes = StringUtil.split(
154                 ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0);
155 
156             for (int formFieldsIndex : formFieldsIndexes) {
157                 String fieldLabel = ParamUtil.getString(
158                     actionRequest, "fieldLabel" + formFieldsIndex);
159 
160                 if (Validator.isNull(fieldLabel)){
161                     continue;
162                 }
163 
164                 String fieldType = ParamUtil.getString(
165                     actionRequest, "fieldType" + formFieldsIndex);
166                 boolean fieldOptional = ParamUtil.getBoolean(
167                     actionRequest, "fieldOptional" + formFieldsIndex);
168                 String fieldOptions = ParamUtil.getString(
169                     actionRequest, "fieldOptions" + formFieldsIndex);
170                 String fieldValidationScript = ParamUtil.getString(
171                     actionRequest, "fieldValidationScript" + formFieldsIndex);
172                 String fieldValidationErrorMessage = ParamUtil.getString(
173                     actionRequest,
174                     "fieldValidationErrorMessage" + formFieldsIndex);
175 
176                 if ((Validator.isNotNull(fieldValidationScript) ^
177                     (Validator.isNotNull(fieldValidationErrorMessage)))) {
178 
179                     SessionErrors.add(
180                         actionRequest, "invalidValidationDefinition" + i);
181                 }
182 
183                 preferences.setValue("fieldLabel" + i, fieldLabel);
184                 preferences.setValue("fieldType" + i, fieldType);
185                 preferences.setValue(
186                     "fieldOptional" + i, String.valueOf(fieldOptional));
187                 preferences.setValue("fieldOptions" + i, fieldOptions);
188                 preferences.setValue(
189                     "fieldValidationScript" + i, fieldValidationScript);
190                 preferences.setValue(
191                     "fieldValidationErrorMessage" + i,
192                     fieldValidationErrorMessage);
193 
194                 i++;
195             }
196 
197             if (!SessionErrors.isEmpty(actionRequest)) {
198                 return;
199             }
200 
201             // Clear previous preferences that are now blank
202 
203             String fieldLabel = preferences.getValue(
204                 "fieldLabel" + i, StringPool.BLANK);
205 
206             while (Validator.isNotNull(fieldLabel)) {
207                 preferences.setValue("fieldLabel" + i, StringPool.BLANK);
208                 preferences.setValue("fieldType" + i, StringPool.BLANK);
209                 preferences.setValue("fieldOptional" + i, StringPool.BLANK);
210                 preferences.setValue("fieldOptions" + i, StringPool.BLANK);
211                 preferences.setValue(
212                     "fieldValidationScript" + i, StringPool.BLANK);
213                 preferences.setValue(
214                     "fieldValidationErrorMessage" + i, StringPool.BLANK);
215 
216                 i++;
217 
218                 fieldLabel = preferences.getValue(
219                     "fieldLabel" + i, StringPool.BLANK);
220             }
221         }
222 
223         if (SessionErrors.isEmpty(actionRequest)) {
224             preferences.store();
225 
226             SessionMessages.add(
227                 actionRequest, portletConfig.getPortletName() + ".doConfigure");
228         }
229     }
230 
231     public String render(
232             PortletConfig portletConfig, RenderRequest renderRequest,
233             RenderResponse renderResponse)
234         throws Exception {
235 
236         return "/html/portlet/web_form/configuration.jsp";
237     }
238 
239 }