1   /**
2    * Copyright (c) 2000-2007 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(
76                  req, portletResource, true, true);
77  
78          if (Validator.isNull(title)) {
79              SessionErrors.add(req, "titleRequired");
80          }
81  
82          if (Validator.isNull(subject)) {
83              SessionErrors.add(req, "subjectRequired");
84          }
85  
86          if (!sendAsEmail && !saveToFile){
87              SessionErrors.add(req, "handlingRequired");
88          }
89  
90          if (sendAsEmail) {
91              if (Validator.isNull(emailAddress)) {
92                  SessionErrors.add(req, "emailAddressRequired");
93              }
94              else if (!Validator.isEmailAddress(emailAddress)) {
95                  SessionErrors.add(req, "emailAddressInvalid");
96              }
97          }
98  
99          if (saveToFile) {
100 
101             // Check if server can create a file as specified
102 
103             try {
104                 FileOutputStream fos = new FileOutputStream(fileName, true);
105 
106                 fos.close();
107             }
108             catch (SecurityException es) {
109                 SessionErrors.add(req, "fileNameInvalid");
110             }
111             catch (FileNotFoundException fnfe) {
112                 SessionErrors.add(req, "fileNameInvalid");
113             }
114         }
115 
116         if (!SessionErrors.isEmpty(req)) {
117             return;
118         }
119 
120         prefs.setValue("title", title);
121         prefs.setValue("description", description);
122         prefs.setValue("requireCaptcha", String.valueOf(requireCaptcha));
123         prefs.setValue("successURL", successURL);
124         prefs.setValue("sendAsEmail", String.valueOf(sendAsEmail));
125         prefs.setValue("subject", subject);
126         prefs.setValue("emailAddress", emailAddress);
127         prefs.setValue("saveToFile", String.valueOf(saveToFile));
128         prefs.setValue("fileName", fileName);
129 
130         int i = 1;
131 
132         String fieldLabel = ParamUtil.getString(req, "fieldLabel" + i);
133         String fieldType = ParamUtil.getString(req, "fieldType" + i);
134         boolean fieldOptional = ParamUtil.getBoolean(req, "fieldOptional" + i);
135         String fieldOptions = ParamUtil.getString(req, "fieldOptions" + i);
136 
137         while ((i == 1) || (fieldLabel.trim().length() > 0)) {
138             prefs.setValue("fieldLabel" + i, fieldLabel);
139             prefs.setValue("fieldType" + i, fieldType);
140             prefs.setValue("fieldOptional" + i, String.valueOf(fieldOptional));
141             prefs.setValue("fieldOptions" + i, fieldOptions);
142 
143             i++;
144 
145             fieldLabel = ParamUtil.getString(req, "fieldLabel" + i);
146             fieldType = ParamUtil.getString(req, "fieldType" + i);
147             fieldOptional = ParamUtil.getBoolean(req, "fieldOptional" + i);
148             fieldOptions = ParamUtil.getString(req, "fieldOptions" + i);
149         }
150 
151         // Clear previous preferences that are now blank
152 
153         fieldLabel = prefs.getValue("fieldLabel" + i, StringPool.BLANK);
154 
155         while (fieldLabel.trim().length() > 0) {
156             prefs.setValue("fieldLabel" + i, StringPool.BLANK);
157             prefs.setValue("fieldType" + i, StringPool.BLANK);
158             prefs.setValue("fieldOptional" + i, StringPool.BLANK);
159             prefs.setValue("fieldOptions" + i, StringPool.BLANK);
160 
161             i++;
162 
163             fieldLabel = prefs.getValue("fieldLabel" + i, StringPool.BLANK);
164         }
165 
166         if (SessionErrors.isEmpty(req)) {
167             prefs.store();
168 
169             SessionMessages.add(req, config.getPortletName() + ".doConfigure");
170         }
171     }
172 
173     public String render(
174             PortletConfig config, RenderRequest req, RenderResponse res)
175         throws Exception {
176 
177         return "/html/portlet/web_form/configuration.jsp";
178     }
179 
180 }