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.shopping.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.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.shopping.util.ShoppingPreferences;
33  import com.liferay.util.servlet.SessionErrors;
34  import com.liferay.util.servlet.SessionMessages;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  /**
43   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class ConfigurationActionImpl implements ConfigurationAction {
49  
50      public void processAction(
51              PortletConfig config, ActionRequest req, ActionResponse res)
52          throws Exception {
53  
54          String cmd = ParamUtil.getString(req, Constants.CMD);
55  
56          if (!cmd.equals(Constants.UPDATE)) {
57              return;
58          }
59  
60          ThemeDisplay themeDisplay =
61              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
62  
63          ShoppingPreferences prefs = ShoppingPreferences.getInstance(
64              themeDisplay.getCompanyId(), themeDisplay.getPortletGroupId());
65  
66          String tabs2 = ParamUtil.getString(req, "tabs2");
67          String tabs3 = ParamUtil.getString(req, "tabs3");
68  
69          if (tabs2.equals("payment-settings")) {
70              updatePayment(req, prefs);
71          }
72          else if (tabs2.equals("shipping-calculation")) {
73              updateShippingCalculation(req, prefs);
74          }
75          else if (tabs2.equals("insurance-calculation")) {
76              updateInsuranceCalculation(req, prefs);
77          }
78          else if (tabs2.equals("emails")) {
79              if (tabs3.equals("email-from")) {
80                  updateEmailFrom(req, prefs);
81              }
82              else if (tabs3.equals("confirmation-email")) {
83                  updateEmailOrderConfirmation(req, prefs);
84              }
85              else if (tabs3.equals("shipping-email")) {
86                  updateEmailOrderShipping(req, prefs);
87              }
88          }
89  
90          if (SessionErrors.isEmpty(req)) {
91              prefs.store();
92  
93              SessionMessages.add(req, config.getPortletName() + ".doConfigure");
94          }
95      }
96  
97      public String render(
98              PortletConfig config, RenderRequest req, RenderResponse res)
99          throws Exception {
100 
101         return "/html/portlet/shopping/configuration.jsp";
102     }
103 
104     protected void updateEmailFrom(ActionRequest req, ShoppingPreferences prefs)
105         throws Exception {
106 
107         String emailFromName = ParamUtil.getString(req, "emailFromName");
108         String emailFromAddress = ParamUtil.getString(req, "emailFromAddress");
109 
110         if (Validator.isNull(emailFromName)) {
111             SessionErrors.add(req, "emailFromName");
112         }
113         else if (!Validator.isEmailAddress(emailFromAddress)) {
114             SessionErrors.add(req, "emailFromAddress");
115         }
116         else {
117             prefs.setEmailFromName(emailFromName);
118             prefs.setEmailFromAddress(emailFromAddress);
119         }
120     }
121 
122     protected void updateEmailOrderConfirmation(
123             ActionRequest req, ShoppingPreferences prefs)
124         throws Exception {
125 
126         boolean emailOrderConfirmationEnabled = ParamUtil.getBoolean(
127             req, "emailOrderConfirmationEnabled");
128         String emailOrderConfirmationSubject = ParamUtil.getString(
129             req, "emailOrderConfirmationSubject");
130         String emailOrderConfirmationBody = ParamUtil.getString(
131             req, "emailOrderConfirmationBody");
132 
133         if (Validator.isNull(emailOrderConfirmationSubject)) {
134             SessionErrors.add(req, "emailOrderConfirmationSubject");
135         }
136         else if (Validator.isNull(emailOrderConfirmationBody)) {
137             SessionErrors.add(req, "emailOrderConfirmationBody");
138         }
139         else {
140             prefs.setEmailOrderConfirmationEnabled(
141                 emailOrderConfirmationEnabled);
142             prefs.setEmailOrderConfirmationSubject(
143                 emailOrderConfirmationSubject);
144             prefs.setEmailOrderConfirmationBody(emailOrderConfirmationBody);
145         }
146     }
147 
148     protected void updateEmailOrderShipping(
149             ActionRequest req, ShoppingPreferences prefs)
150         throws Exception {
151 
152         boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
153             req, "emailOrderShippingEnabled");
154         String emailOrderShippingSubject = ParamUtil.getString(
155             req, "emailOrderShippingSubject");
156         String emailOrderShippingBody = ParamUtil.getString(
157             req, "emailOrderShippingBody");
158 
159         if (Validator.isNull(emailOrderShippingSubject)) {
160             SessionErrors.add(req, "emailOrderShippingSubject");
161         }
162         else if (Validator.isNull(emailOrderShippingBody)) {
163             SessionErrors.add(req, "emailOrderShippingBody");
164         }
165         else {
166             prefs.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
167             prefs.setEmailOrderShippingSubject(emailOrderShippingSubject);
168             prefs.setEmailOrderShippingBody(emailOrderShippingBody);
169         }
170     }
171 
172     protected void updateInsuranceCalculation(
173             ActionRequest req, ShoppingPreferences prefs)
174         throws Exception {
175 
176         String insuranceFormula = ParamUtil.getString(req, "insuranceFormula");
177 
178         String[] insurance = new String[5];
179 
180         for (int i = 0; i < insurance.length; i++) {
181             insurance[i] = String.valueOf(
182                 ParamUtil.getDouble(req, "insurance" + i));
183         }
184 
185         prefs.setInsuranceFormula(insuranceFormula);
186         prefs.setInsurance(insurance);
187     }
188 
189     protected void updatePayment(ActionRequest req, ShoppingPreferences prefs)
190         throws Exception {
191 
192         String payPalEmailAddress = ParamUtil.getString(
193             req, "payPalEmailAddress");
194         String[] ccTypes = StringUtil.split(
195             ParamUtil.getString(req, "ccTypes"));
196         String currencyId = ParamUtil.getString(req, "currencyId");
197         String taxState = ParamUtil.getString(req, "taxState");
198         double taxRate = ParamUtil.getDouble(req, "taxRate") / 100;
199         double minOrder = ParamUtil.getDouble(req, "minOrder");
200 
201         prefs.setPayPalEmailAddress(payPalEmailAddress);
202         prefs.setCcTypes(ccTypes);
203         prefs.setCurrencyId(currencyId);
204         prefs.setTaxState(taxState);
205         prefs.setTaxRate(taxRate);
206         prefs.setMinOrder(minOrder);
207     }
208 
209     protected void updateShippingCalculation(
210             ActionRequest req, ShoppingPreferences prefs)
211         throws Exception {
212 
213         String shippingFormula = ParamUtil.getString(req, "shippingFormula");
214 
215         String[] shipping = new String[5];
216 
217         for (int i = 0; i < shipping.length; i++) {
218             shipping[i] = String.valueOf(
219                 ParamUtil.getDouble(req, "shipping" + i));
220         }
221 
222         prefs.setShippingFormula(shippingFormula);
223         prefs.setShipping(shipping);
224     }
225 
226 }