1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.action;
16  
17  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.servlet.SessionMessages;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.shopping.util.ShoppingPreferences;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.ActionResponse;
30  import javax.portlet.PortletConfig;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  /**
35   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class ConfigurationActionImpl extends BaseConfigurationAction {
40  
41      public void processAction(
42              PortletConfig portletConfig, ActionRequest actionRequest,
43              ActionResponse actionResponse)
44          throws Exception {
45  
46          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
47  
48          if (!cmd.equals(Constants.UPDATE)) {
49              return;
50          }
51  
52          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
53              WebKeys.THEME_DISPLAY);
54  
55          ShoppingPreferences preferences = ShoppingPreferences.getInstance(
56              themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
57  
58          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
59          String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
60  
61          if (tabs2.equals("payment-settings")) {
62              updatePayment(actionRequest, preferences);
63          }
64          else if (tabs2.equals("shipping-calculation")) {
65              updateShippingCalculation(actionRequest, preferences);
66          }
67          else if (tabs2.equals("insurance-calculation")) {
68              updateInsuranceCalculation(actionRequest, preferences);
69          }
70          else if (tabs2.equals("emails")) {
71              if (tabs3.equals("email-from")) {
72                  updateEmailFrom(actionRequest, preferences);
73              }
74              else if (tabs3.equals("confirmation-email")) {
75                  updateEmailOrderConfirmation(actionRequest, preferences);
76              }
77              else if (tabs3.equals("shipping-email")) {
78                  updateEmailOrderShipping(actionRequest, preferences);
79              }
80          }
81  
82          if (SessionErrors.isEmpty(actionRequest)) {
83              preferences.store();
84  
85              SessionMessages.add(
86                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
87          }
88      }
89  
90      public String render(
91              PortletConfig portletConfig, RenderRequest renderRequest,
92              RenderResponse renderResponse)
93          throws Exception {
94  
95          return "/html/portlet/shopping/configuration.jsp";
96      }
97  
98      protected void updateEmailFrom(
99              ActionRequest actionRequest, ShoppingPreferences preferences)
100         throws Exception {
101 
102         String emailFromName = ParamUtil.getString(
103             actionRequest, "emailFromName");
104         String emailFromAddress = ParamUtil.getString(
105             actionRequest, "emailFromAddress");
106 
107         if (Validator.isNull(emailFromName)) {
108             SessionErrors.add(actionRequest, "emailFromName");
109         }
110         else if (!Validator.isEmailAddress(emailFromAddress)) {
111             SessionErrors.add(actionRequest, "emailFromAddress");
112         }
113         else {
114             preferences.setEmailFromName(emailFromName);
115             preferences.setEmailFromAddress(emailFromAddress);
116         }
117     }
118 
119     protected void updateEmailOrderConfirmation(
120             ActionRequest actionRequest, ShoppingPreferences preferences)
121         throws Exception {
122 
123         boolean emailOrderConfirmationEnabled = ParamUtil.getBoolean(
124             actionRequest, "emailOrderConfirmationEnabled");
125         String emailOrderConfirmationSubject = ParamUtil.getString(
126             actionRequest, "emailOrderConfirmationSubject");
127         String emailOrderConfirmationBody = ParamUtil.getString(
128             actionRequest, "emailOrderConfirmationBody");
129 
130         if (Validator.isNull(emailOrderConfirmationSubject)) {
131             SessionErrors.add(actionRequest, "emailOrderConfirmationSubject");
132         }
133         else if (Validator.isNull(emailOrderConfirmationBody)) {
134             SessionErrors.add(actionRequest, "emailOrderConfirmationBody");
135         }
136         else {
137             preferences.setEmailOrderConfirmationEnabled(
138                 emailOrderConfirmationEnabled);
139             preferences.setEmailOrderConfirmationSubject(
140                 emailOrderConfirmationSubject);
141             preferences.setEmailOrderConfirmationBody(
142                 emailOrderConfirmationBody);
143         }
144     }
145 
146     protected void updateEmailOrderShipping(
147             ActionRequest actionRequest, ShoppingPreferences preferences)
148         throws Exception {
149 
150         boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
151             actionRequest, "emailOrderShippingEnabled");
152         String emailOrderShippingSubject = ParamUtil.getString(
153             actionRequest, "emailOrderShippingSubject");
154         String emailOrderShippingBody = ParamUtil.getString(
155             actionRequest, "emailOrderShippingBody");
156 
157         if (Validator.isNull(emailOrderShippingSubject)) {
158             SessionErrors.add(actionRequest, "emailOrderShippingSubject");
159         }
160         else if (Validator.isNull(emailOrderShippingBody)) {
161             SessionErrors.add(actionRequest, "emailOrderShippingBody");
162         }
163         else {
164             preferences.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
165             preferences.setEmailOrderShippingSubject(emailOrderShippingSubject);
166             preferences.setEmailOrderShippingBody(emailOrderShippingBody);
167         }
168     }
169 
170     protected void updateInsuranceCalculation(
171             ActionRequest actionRequest, ShoppingPreferences preferences)
172         throws Exception {
173 
174         String insuranceFormula = ParamUtil.getString(
175             actionRequest, "insuranceFormula");
176 
177         String[] insurance = new String[5];
178 
179         for (int i = 0; i < insurance.length; i++) {
180             insurance[i] = String.valueOf(
181                 ParamUtil.getDouble(actionRequest, "insurance" + i));
182         }
183 
184         preferences.setInsuranceFormula(insuranceFormula);
185         preferences.setInsurance(insurance);
186     }
187 
188     protected void updatePayment(
189             ActionRequest actionRequest, ShoppingPreferences preferences)
190         throws Exception {
191 
192         String payPalEmailAddress = ParamUtil.getString(
193             actionRequest, "payPalEmailAddress");
194         String[] ccTypes = StringUtil.split(
195             ParamUtil.getString(actionRequest, "ccTypes"));
196         String currencyId = ParamUtil.getString(actionRequest, "currencyId");
197         String taxState = ParamUtil.getString(actionRequest, "taxState");
198         double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
199         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
200 
201         preferences.setPayPalEmailAddress(payPalEmailAddress);
202         preferences.setCcTypes(ccTypes);
203         preferences.setCurrencyId(currencyId);
204         preferences.setTaxState(taxState);
205         preferences.setTaxRate(taxRate);
206         preferences.setMinOrder(minOrder);
207     }
208 
209     protected void updateShippingCalculation(
210             ActionRequest actionRequest, ShoppingPreferences preferences)
211         throws Exception {
212 
213         String shippingFormula = ParamUtil.getString(
214             actionRequest, "shippingFormula");
215 
216         String[] shipping = new String[5];
217 
218         for (int i = 0; i < shipping.length; i++) {
219             shipping[i] = String.valueOf(
220                 ParamUtil.getDouble(actionRequest, "shipping" + i));
221         }
222 
223         preferences.setShippingFormula(shippingFormula);
224         preferences.setShipping(shipping);
225     }
226 
227 }