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.portlet.LiferayPortletConfig;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.theme.ThemeDisplay;
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                            LiferayPortletConfig liferayPortletConfig =
085                                    (LiferayPortletConfig)portletConfig;
086    
087                            SessionMessages.add(
088                                    actionRequest,
089                                    liferayPortletConfig.getPortletId() +
090                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
091                                    PortletKeys.SHOPPING);
092    
093                            SessionMessages.add(
094                                    actionRequest,
095                                    liferayPortletConfig.getPortletId() +
096                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
097                    }
098            }
099    
100            protected void updateEmailFrom(
101                            ActionRequest actionRequest, ShoppingPreferences preferences)
102                    throws Exception {
103    
104                    String emailFromName = ParamUtil.getString(
105                            actionRequest, "emailFromName");
106                    String emailFromAddress = ParamUtil.getString(
107                            actionRequest, "emailFromAddress");
108    
109                    if (Validator.isNull(emailFromName)) {
110                            SessionErrors.add(actionRequest, "emailFromName");
111                    }
112                    else if (!Validator.isEmailAddress(emailFromAddress)) {
113                            SessionErrors.add(actionRequest, "emailFromAddress");
114                    }
115                    else {
116                            preferences.setEmailFromName(emailFromName);
117                            preferences.setEmailFromAddress(emailFromAddress);
118                    }
119            }
120    
121            protected void updateEmailOrderConfirmation(
122                            ActionRequest actionRequest, ShoppingPreferences preferences)
123                    throws Exception {
124    
125                    boolean emailOrderConfirmationEnabled = ParamUtil.getBoolean(
126                            actionRequest, "emailOrderConfirmationEnabled");
127                    String emailOrderConfirmationSubject = ParamUtil.getString(
128                            actionRequest, "emailOrderConfirmationSubject");
129                    String emailOrderConfirmationBody = ParamUtil.getString(
130                            actionRequest, "emailOrderConfirmationBody");
131    
132                    if (Validator.isNull(emailOrderConfirmationSubject)) {
133                            SessionErrors.add(actionRequest, "emailOrderConfirmationSubject");
134                    }
135                    else if (Validator.isNull(emailOrderConfirmationBody)) {
136                            SessionErrors.add(actionRequest, "emailOrderConfirmationBody");
137                    }
138                    else {
139                            preferences.setEmailOrderConfirmationEnabled(
140                                    emailOrderConfirmationEnabled);
141                            preferences.setEmailOrderConfirmationSubject(
142                                    emailOrderConfirmationSubject);
143                            preferences.setEmailOrderConfirmationBody(
144                                    emailOrderConfirmationBody);
145                    }
146            }
147    
148            protected void updateEmailOrderShipping(
149                            ActionRequest actionRequest, ShoppingPreferences preferences)
150                    throws Exception {
151    
152                    boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
153                            actionRequest, "emailOrderShippingEnabled");
154                    String emailOrderShippingSubject = ParamUtil.getString(
155                            actionRequest, "emailOrderShippingSubject");
156                    String emailOrderShippingBody = ParamUtil.getString(
157                            actionRequest, "emailOrderShippingBody");
158    
159                    if (Validator.isNull(emailOrderShippingSubject)) {
160                            SessionErrors.add(actionRequest, "emailOrderShippingSubject");
161                    }
162                    else if (Validator.isNull(emailOrderShippingBody)) {
163                            SessionErrors.add(actionRequest, "emailOrderShippingBody");
164                    }
165                    else {
166                            preferences.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
167                            preferences.setEmailOrderShippingSubject(emailOrderShippingSubject);
168                            preferences.setEmailOrderShippingBody(emailOrderShippingBody);
169                    }
170            }
171    
172            protected void updateInsuranceCalculation(
173                            ActionRequest actionRequest, ShoppingPreferences preferences)
174                    throws Exception {
175    
176                    String insuranceFormula = ParamUtil.getString(
177                            actionRequest, "insuranceFormula");
178    
179                    String[] insurance = new String[5];
180    
181                    for (int i = 0; i < insurance.length; i++) {
182                            insurance[i] = String.valueOf(
183                                    ParamUtil.getDouble(actionRequest, "insurance" + i));
184                    }
185    
186                    preferences.setInsuranceFormula(insuranceFormula);
187                    preferences.setInsurance(insurance);
188            }
189    
190            protected void updatePayment(
191                            ActionRequest actionRequest, ShoppingPreferences preferences)
192                    throws Exception {
193    
194                    String payPalEmailAddress = ParamUtil.getString(
195                            actionRequest, "payPalEmailAddress");
196                    String[] ccTypes = StringUtil.split(
197                            ParamUtil.getString(actionRequest, "ccTypes"));
198                    String currencyId = ParamUtil.getString(actionRequest, "currencyId");
199                    String taxState = ParamUtil.getString(actionRequest, "taxState");
200                    double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
201                    double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
202    
203                    preferences.setPayPalEmailAddress(payPalEmailAddress);
204                    preferences.setCcTypes(ccTypes);
205                    preferences.setCurrencyId(currencyId);
206                    preferences.setTaxState(taxState);
207                    preferences.setTaxRate(taxRate);
208                    preferences.setMinOrder(minOrder);
209            }
210    
211            protected void updateShippingCalculation(
212                            ActionRequest actionRequest, ShoppingPreferences preferences)
213                    throws Exception {
214    
215                    String shippingFormula = ParamUtil.getString(
216                            actionRequest, "shippingFormula");
217    
218                    String[] shipping = new String[5];
219    
220                    for (int i = 0; i < shipping.length; i++) {
221                            shipping[i] = String.valueOf(
222                                    ParamUtil.getDouble(actionRequest, "shipping" + i));
223                    }
224    
225                    preferences.setShippingFormula(shippingFormula);
226                    preferences.setShipping(shipping);
227            }
228    
229    }