001    /**
002     * Copyright (c) 2000-2013 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.DefaultConfigurationAction;
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.PortalUtil;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.shopping.util.ShoppingPreferences;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ConfigurationActionImpl extends DefaultConfigurationAction {
038    
039            @Override
040            public void processAction(
041                            PortletConfig portletConfig, ActionRequest actionRequest,
042                            ActionResponse actionResponse)
043                    throws Exception {
044    
045                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
046    
047                    if (!cmd.equals(Constants.UPDATE)) {
048                            return;
049                    }
050    
051                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
052                            WebKeys.THEME_DISPLAY);
053    
054                    ShoppingPreferences preferences = ShoppingPreferences.getInstance(
055                            themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
056    
057                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
058                    String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
059    
060                    if (tabs2.equals("payment-settings")) {
061                            updatePayment(actionRequest, preferences);
062                    }
063                    else if (tabs2.equals("shipping-calculation")) {
064                            updateShippingCalculation(actionRequest, preferences);
065                    }
066                    else if (tabs2.equals("insurance-calculation")) {
067                            updateInsuranceCalculation(actionRequest, preferences);
068                    }
069                    else if (tabs2.equals("emails")) {
070                            if (tabs3.equals("email-from")) {
071                                    updateEmailFrom(actionRequest, preferences);
072                            }
073                            else if (tabs3.equals("confirmation-email")) {
074                                    updateEmailOrderConfirmation(actionRequest, preferences);
075                            }
076                            else if (tabs3.equals("shipping-email")) {
077                                    updateEmailOrderShipping(actionRequest, preferences);
078                            }
079                    }
080    
081                    if (SessionErrors.isEmpty(actionRequest)) {
082                            preferences.store();
083    
084                            SessionMessages.add(
085                                    actionRequest,
086                                    PortalUtil.getPortletId(actionRequest) +
087                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
088                                    PortletKeys.SHOPPING);
089    
090                            SessionMessages.add(
091                                    actionRequest,
092                                    PortalUtil.getPortletId(actionRequest) +
093                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
094                    }
095            }
096    
097            protected void updateEmailFrom(
098                            ActionRequest actionRequest, ShoppingPreferences preferences)
099                    throws Exception {
100    
101                    String emailFromName = ParamUtil.getString(
102                            actionRequest, "emailFromName");
103                    String emailFromAddress = ParamUtil.getString(
104                            actionRequest, "emailFromAddress");
105    
106                    if (Validator.isNull(emailFromName)) {
107                            SessionErrors.add(actionRequest, "emailFromName");
108                    }
109                    else if (!Validator.isEmailAddress(emailFromAddress)) {
110                            SessionErrors.add(actionRequest, "emailFromAddress");
111                    }
112                    else {
113                            preferences.setEmailFromName(emailFromName);
114                            preferences.setEmailFromAddress(emailFromAddress);
115                    }
116            }
117    
118            protected void updateEmailOrderConfirmation(
119                            ActionRequest actionRequest, ShoppingPreferences preferences)
120                    throws Exception {
121    
122                    boolean emailOrderConfirmationEnabled = ParamUtil.getBoolean(
123                            actionRequest, "emailOrderConfirmationEnabled");
124                    String emailOrderConfirmationSubject = ParamUtil.getString(
125                            actionRequest, "emailOrderConfirmationSubject");
126                    String emailOrderConfirmationBody = ParamUtil.getString(
127                            actionRequest, "emailOrderConfirmationBody");
128    
129                    if (Validator.isNull(emailOrderConfirmationSubject)) {
130                            SessionErrors.add(actionRequest, "emailOrderConfirmationSubject");
131                    }
132                    else if (Validator.isNull(emailOrderConfirmationBody)) {
133                            SessionErrors.add(actionRequest, "emailOrderConfirmationBody");
134                    }
135                    else {
136                            preferences.setEmailOrderConfirmationEnabled(
137                                    emailOrderConfirmationEnabled);
138                            preferences.setEmailOrderConfirmationSubject(
139                                    emailOrderConfirmationSubject);
140                            preferences.setEmailOrderConfirmationBody(
141                                    emailOrderConfirmationBody);
142                    }
143            }
144    
145            protected void updateEmailOrderShipping(
146                            ActionRequest actionRequest, ShoppingPreferences preferences)
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                            preferences.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
164                            preferences.setEmailOrderShippingSubject(emailOrderShippingSubject);
165                            preferences.setEmailOrderShippingBody(emailOrderShippingBody);
166                    }
167            }
168    
169            protected void updateInsuranceCalculation(
170                            ActionRequest actionRequest, ShoppingPreferences preferences)
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                    preferences.setInsuranceFormula(insuranceFormula);
184                    preferences.setInsurance(insurance);
185            }
186    
187            protected void updatePayment(
188                            ActionRequest actionRequest, ShoppingPreferences preferences)
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                    preferences.setPayPalEmailAddress(payPalEmailAddress);
201                    preferences.setCcTypes(ccTypes);
202                    preferences.setCurrencyId(currencyId);
203                    preferences.setTaxState(taxState);
204                    preferences.setTaxRate(taxRate);
205                    preferences.setMinOrder(minOrder);
206            }
207    
208            protected void updateShippingCalculation(
209                            ActionRequest actionRequest, ShoppingPreferences preferences)
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                    preferences.setShippingFormula(shippingFormula);
223                    preferences.setShipping(shipping);
224            }
225    
226    }