001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.action;
016    
017    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.shopping.util.ShoppingPreferences;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ConfigurationActionImpl extends BaseConfigurationAction {
038    
039            public void processAction(
040                            PortletConfig portletConfig, ActionRequest actionRequest,
041                            ActionResponse actionResponse)
042                    throws Exception {
043    
044                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
045    
046                    if (!cmd.equals(Constants.UPDATE)) {
047                            return;
048                    }
049    
050                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052    
053                    ShoppingPreferences preferences = ShoppingPreferences.getInstance(
054                            themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
055    
056                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
057                    String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
058    
059                    if (tabs2.equals("payment-settings")) {
060                            updatePayment(actionRequest, preferences);
061                    }
062                    else if (tabs2.equals("shipping-calculation")) {
063                            updateShippingCalculation(actionRequest, preferences);
064                    }
065                    else if (tabs2.equals("insurance-calculation")) {
066                            updateInsuranceCalculation(actionRequest, preferences);
067                    }
068                    else if (tabs2.equals("emails")) {
069                            if (tabs3.equals("email-from")) {
070                                    updateEmailFrom(actionRequest, preferences);
071                            }
072                            else if (tabs3.equals("confirmation-email")) {
073                                    updateEmailOrderConfirmation(actionRequest, preferences);
074                            }
075                            else if (tabs3.equals("shipping-email")) {
076                                    updateEmailOrderShipping(actionRequest, preferences);
077                            }
078                    }
079    
080                    if (SessionErrors.isEmpty(actionRequest)) {
081                            preferences.store();
082    
083                            SessionMessages.add(
084                                    actionRequest, portletConfig.getPortletName() + ".doConfigure");
085                    }
086            }
087    
088            public String render(
089                            PortletConfig portletConfig, RenderRequest renderRequest,
090                            RenderResponse renderResponse)
091                    throws Exception {
092    
093                    return "/html/portlet/shopping/configuration.jsp";
094            }
095    
096            protected void updateEmailFrom(
097                            ActionRequest actionRequest, ShoppingPreferences preferences)
098                    throws Exception {
099    
100                    String emailFromName = ParamUtil.getString(
101                            actionRequest, "emailFromName");
102                    String emailFromAddress = ParamUtil.getString(
103                            actionRequest, "emailFromAddress");
104    
105                    if (Validator.isNull(emailFromName)) {
106                            SessionErrors.add(actionRequest, "emailFromName");
107                    }
108                    else if (!Validator.isEmailAddress(emailFromAddress)) {
109                            SessionErrors.add(actionRequest, "emailFromAddress");
110                    }
111                    else {
112                            preferences.setEmailFromName(emailFromName);
113                            preferences.setEmailFromAddress(emailFromAddress);
114                    }
115            }
116    
117            protected void updateEmailOrderConfirmation(
118                            ActionRequest actionRequest, ShoppingPreferences preferences)
119                    throws Exception {
120    
121                    boolean emailOrderConfirmationEnabled = ParamUtil.getBoolean(
122                            actionRequest, "emailOrderConfirmationEnabled");
123                    String emailOrderConfirmationSubject = ParamUtil.getString(
124                            actionRequest, "emailOrderConfirmationSubject");
125                    String emailOrderConfirmationBody = ParamUtil.getString(
126                            actionRequest, "emailOrderConfirmationBody");
127    
128                    if (Validator.isNull(emailOrderConfirmationSubject)) {
129                            SessionErrors.add(actionRequest, "emailOrderConfirmationSubject");
130                    }
131                    else if (Validator.isNull(emailOrderConfirmationBody)) {
132                            SessionErrors.add(actionRequest, "emailOrderConfirmationBody");
133                    }
134                    else {
135                            preferences.setEmailOrderConfirmationEnabled(
136                                    emailOrderConfirmationEnabled);
137                            preferences.setEmailOrderConfirmationSubject(
138                                    emailOrderConfirmationSubject);
139                            preferences.setEmailOrderConfirmationBody(
140                                    emailOrderConfirmationBody);
141                    }
142            }
143    
144            protected void updateEmailOrderShipping(
145                            ActionRequest actionRequest, ShoppingPreferences preferences)
146                    throws Exception {
147    
148                    boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
149                            actionRequest, "emailOrderShippingEnabled");
150                    String emailOrderShippingSubject = ParamUtil.getString(
151                            actionRequest, "emailOrderShippingSubject");
152                    String emailOrderShippingBody = ParamUtil.getString(
153                            actionRequest, "emailOrderShippingBody");
154    
155                    if (Validator.isNull(emailOrderShippingSubject)) {
156                            SessionErrors.add(actionRequest, "emailOrderShippingSubject");
157                    }
158                    else if (Validator.isNull(emailOrderShippingBody)) {
159                            SessionErrors.add(actionRequest, "emailOrderShippingBody");
160                    }
161                    else {
162                            preferences.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
163                            preferences.setEmailOrderShippingSubject(emailOrderShippingSubject);
164                            preferences.setEmailOrderShippingBody(emailOrderShippingBody);
165                    }
166            }
167    
168            protected void updateInsuranceCalculation(
169                            ActionRequest actionRequest, ShoppingPreferences preferences)
170                    throws Exception {
171    
172                    String insuranceFormula = ParamUtil.getString(
173                            actionRequest, "insuranceFormula");
174    
175                    String[] insurance = new String[5];
176    
177                    for (int i = 0; i < insurance.length; i++) {
178                            insurance[i] = String.valueOf(
179                                    ParamUtil.getDouble(actionRequest, "insurance" + i));
180                    }
181    
182                    preferences.setInsuranceFormula(insuranceFormula);
183                    preferences.setInsurance(insurance);
184            }
185    
186            protected void updatePayment(
187                            ActionRequest actionRequest, ShoppingPreferences preferences)
188                    throws Exception {
189    
190                    String payPalEmailAddress = ParamUtil.getString(
191                            actionRequest, "payPalEmailAddress");
192                    String[] ccTypes = StringUtil.split(
193                            ParamUtil.getString(actionRequest, "ccTypes"));
194                    String currencyId = ParamUtil.getString(actionRequest, "currencyId");
195                    String taxState = ParamUtil.getString(actionRequest, "taxState");
196                    double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
197                    double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
198    
199                    preferences.setPayPalEmailAddress(payPalEmailAddress);
200                    preferences.setCcTypes(ccTypes);
201                    preferences.setCurrencyId(currencyId);
202                    preferences.setTaxState(taxState);
203                    preferences.setTaxRate(taxRate);
204                    preferences.setMinOrder(minOrder);
205            }
206    
207            protected void updateShippingCalculation(
208                            ActionRequest actionRequest, ShoppingPreferences preferences)
209                    throws Exception {
210    
211                    String shippingFormula = ParamUtil.getString(
212                            actionRequest, "shippingFormula");
213    
214                    String[] shipping = new String[5];
215    
216                    for (int i = 0; i < shipping.length; i++) {
217                            shipping[i] = String.valueOf(
218                                    ParamUtil.getDouble(actionRequest, "shipping" + i));
219                    }
220    
221                    preferences.setShippingFormula(shippingFormula);
222                    preferences.setShipping(shipping);
223            }
224    
225    }