1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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 prefs = 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, prefs);
63          }
64          else if (tabs2.equals("shipping-calculation")) {
65              updateShippingCalculation(actionRequest, prefs);
66          }
67          else if (tabs2.equals("insurance-calculation")) {
68              updateInsuranceCalculation(actionRequest, prefs);
69          }
70          else if (tabs2.equals("emails")) {
71              if (tabs3.equals("email-from")) {
72                  updateEmailFrom(actionRequest, prefs);
73              }
74              else if (tabs3.equals("confirmation-email")) {
75                  updateEmailOrderConfirmation(actionRequest, prefs);
76              }
77              else if (tabs3.equals("shipping-email")) {
78                  updateEmailOrderShipping(actionRequest, prefs);
79              }
80          }
81  
82          if (SessionErrors.isEmpty(actionRequest)) {
83              prefs.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 prefs)
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             prefs.setEmailFromName(emailFromName);
115             prefs.setEmailFromAddress(emailFromAddress);
116         }
117     }
118 
119     protected void updateEmailOrderConfirmation(
120             ActionRequest actionRequest, ShoppingPreferences prefs)
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             prefs.setEmailOrderConfirmationEnabled(
138                 emailOrderConfirmationEnabled);
139             prefs.setEmailOrderConfirmationSubject(
140                 emailOrderConfirmationSubject);
141             prefs.setEmailOrderConfirmationBody(emailOrderConfirmationBody);
142         }
143     }
144 
145     protected void updateEmailOrderShipping(
146             ActionRequest actionRequest, ShoppingPreferences prefs)
147         throws Exception {
148 
149         boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
150             actionRequest, "emailOrderShippingEnabled");
151         String emailOrderShippingSubject = ParamUtil.getString(
152             actionRequest, "emailOrderShippingSubject");
153         String emailOrderShippingBody = ParamUtil.getString(
154             actionRequest, "emailOrderShippingBody");
155 
156         if (Validator.isNull(emailOrderShippingSubject)) {
157             SessionErrors.add(actionRequest, "emailOrderShippingSubject");
158         }
159         else if (Validator.isNull(emailOrderShippingBody)) {
160             SessionErrors.add(actionRequest, "emailOrderShippingBody");
161         }
162         else {
163             prefs.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
164             prefs.setEmailOrderShippingSubject(emailOrderShippingSubject);
165             prefs.setEmailOrderShippingBody(emailOrderShippingBody);
166         }
167     }
168 
169     protected void updateInsuranceCalculation(
170             ActionRequest actionRequest, ShoppingPreferences prefs)
171         throws Exception {
172 
173         String insuranceFormula = ParamUtil.getString(
174             actionRequest, "insuranceFormula");
175 
176         String[] insurance = new String[5];
177 
178         for (int i = 0; i < insurance.length; i++) {
179             insurance[i] = String.valueOf(
180                 ParamUtil.getDouble(actionRequest, "insurance" + i));
181         }
182 
183         prefs.setInsuranceFormula(insuranceFormula);
184         prefs.setInsurance(insurance);
185     }
186 
187     protected void updatePayment(
188             ActionRequest actionRequest, ShoppingPreferences prefs)
189         throws Exception {
190 
191         String payPalEmailAddress = ParamUtil.getString(
192             actionRequest, "payPalEmailAddress");
193         String[] ccTypes = StringUtil.split(
194             ParamUtil.getString(actionRequest, "ccTypes"));
195         String currencyId = ParamUtil.getString(actionRequest, "currencyId");
196         String taxState = ParamUtil.getString(actionRequest, "taxState");
197         double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
198         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
199 
200         prefs.setPayPalEmailAddress(payPalEmailAddress);
201         prefs.setCcTypes(ccTypes);
202         prefs.setCurrencyId(currencyId);
203         prefs.setTaxState(taxState);
204         prefs.setTaxRate(taxRate);
205         prefs.setMinOrder(minOrder);
206     }
207 
208     protected void updateShippingCalculation(
209             ActionRequest actionRequest, ShoppingPreferences prefs)
210         throws Exception {
211 
212         String shippingFormula = ParamUtil.getString(
213             actionRequest, "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(actionRequest, "shipping" + i));
220         }
221 
222         prefs.setShippingFormula(shippingFormula);
223         prefs.setShipping(shipping);
224     }
225 
226 }