1   /**
2    * Copyright (c) 2000-2009 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 preferences = ShoppingPreferences.getInstance(
65              themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
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, preferences);
72          }
73          else if (tabs2.equals("shipping-calculation")) {
74              updateShippingCalculation(actionRequest, preferences);
75          }
76          else if (tabs2.equals("insurance-calculation")) {
77              updateInsuranceCalculation(actionRequest, preferences);
78          }
79          else if (tabs2.equals("emails")) {
80              if (tabs3.equals("email-from")) {
81                  updateEmailFrom(actionRequest, preferences);
82              }
83              else if (tabs3.equals("confirmation-email")) {
84                  updateEmailOrderConfirmation(actionRequest, preferences);
85              }
86              else if (tabs3.equals("shipping-email")) {
87                  updateEmailOrderShipping(actionRequest, preferences);
88              }
89          }
90  
91          if (SessionErrors.isEmpty(actionRequest)) {
92              preferences.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 preferences)
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             preferences.setEmailFromName(emailFromName);
124             preferences.setEmailFromAddress(emailFromAddress);
125         }
126     }
127 
128     protected void updateEmailOrderConfirmation(
129             ActionRequest actionRequest, ShoppingPreferences preferences)
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             preferences.setEmailOrderConfirmationEnabled(
147                 emailOrderConfirmationEnabled);
148             preferences.setEmailOrderConfirmationSubject(
149                 emailOrderConfirmationSubject);
150             preferences.setEmailOrderConfirmationBody(
151                 emailOrderConfirmationBody);
152         }
153     }
154 
155     protected void updateEmailOrderShipping(
156             ActionRequest actionRequest, ShoppingPreferences preferences)
157         throws Exception {
158 
159         boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
160             actionRequest, "emailOrderShippingEnabled");
161         String emailOrderShippingSubject = ParamUtil.getString(
162             actionRequest, "emailOrderShippingSubject");
163         String emailOrderShippingBody = ParamUtil.getString(
164             actionRequest, "emailOrderShippingBody");
165 
166         if (Validator.isNull(emailOrderShippingSubject)) {
167             SessionErrors.add(actionRequest, "emailOrderShippingSubject");
168         }
169         else if (Validator.isNull(emailOrderShippingBody)) {
170             SessionErrors.add(actionRequest, "emailOrderShippingBody");
171         }
172         else {
173             preferences.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
174             preferences.setEmailOrderShippingSubject(emailOrderShippingSubject);
175             preferences.setEmailOrderShippingBody(emailOrderShippingBody);
176         }
177     }
178 
179     protected void updateInsuranceCalculation(
180             ActionRequest actionRequest, ShoppingPreferences preferences)
181         throws Exception {
182 
183         String insuranceFormula = ParamUtil.getString(
184             actionRequest, "insuranceFormula");
185 
186         String[] insurance = new String[5];
187 
188         for (int i = 0; i < insurance.length; i++) {
189             insurance[i] = String.valueOf(
190                 ParamUtil.getDouble(actionRequest, "insurance" + i));
191         }
192 
193         preferences.setInsuranceFormula(insuranceFormula);
194         preferences.setInsurance(insurance);
195     }
196 
197     protected void updatePayment(
198             ActionRequest actionRequest, ShoppingPreferences preferences)
199         throws Exception {
200 
201         String payPalEmailAddress = ParamUtil.getString(
202             actionRequest, "payPalEmailAddress");
203         String[] ccTypes = StringUtil.split(
204             ParamUtil.getString(actionRequest, "ccTypes"));
205         String currencyId = ParamUtil.getString(actionRequest, "currencyId");
206         String taxState = ParamUtil.getString(actionRequest, "taxState");
207         double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
208         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
209 
210         preferences.setPayPalEmailAddress(payPalEmailAddress);
211         preferences.setCcTypes(ccTypes);
212         preferences.setCurrencyId(currencyId);
213         preferences.setTaxState(taxState);
214         preferences.setTaxRate(taxRate);
215         preferences.setMinOrder(minOrder);
216     }
217 
218     protected void updateShippingCalculation(
219             ActionRequest actionRequest, ShoppingPreferences preferences)
220         throws Exception {
221 
222         String shippingFormula = ParamUtil.getString(
223             actionRequest, "shippingFormula");
224 
225         String[] shipping = new String[5];
226 
227         for (int i = 0; i < shipping.length; i++) {
228             shipping[i] = String.valueOf(
229                 ParamUtil.getDouble(actionRequest, "shipping" + i));
230         }
231 
232         preferences.setShippingFormula(shippingFormula);
233         preferences.setShipping(shipping);
234     }
235 
236 }