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