001    /**
002     * Copyright (c) 2000-present 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.SettingsConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.WebKeys;
023    
024    import java.text.NumberFormat;
025    import java.text.ParseException;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ConfigurationActionImpl extends SettingsConfigurationAction {
035    
036            @Override
037            public void processAction(
038                            PortletConfig portletConfig, ActionRequest actionRequest,
039                            ActionResponse actionResponse)
040                    throws Exception {
041    
042                    validateEmail(actionRequest, "emailOrderConfirmation");
043                    validateEmail(actionRequest, "emailOrderShipping");
044                    validateEmailFrom(actionRequest);
045    
046                    updatePayment(actionRequest);
047    
048                    super.processAction(portletConfig, actionRequest, actionResponse);
049            }
050    
051            protected void updateInsuranceCalculation(ActionRequest actionRequest) {
052                    String[] insurance = new String[5];
053    
054                    for (int i = 0; i < insurance.length; i++) {
055                            insurance[i] = String.valueOf(
056                                    ParamUtil.getDouble(actionRequest, "insurance" + i));
057                    }
058    
059                    setPreference(actionRequest, "insurance", insurance);
060            }
061    
062            @Override
063            protected void updateMultiValuedKeys(ActionRequest actionRequest) {
064                    super.updateMultiValuedKeys(actionRequest);
065    
066                    updateInsuranceCalculation(actionRequest);
067                    updateShippingCalculation(actionRequest);
068            }
069    
070            protected void updatePayment(ActionRequest actionRequest) {
071                    String taxRatePercent = ParamUtil.getString(actionRequest, "taxRate");
072    
073                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
074                            WebKeys.THEME_DISPLAY);
075    
076                    NumberFormat percentFormat = NumberFormat.getPercentInstance(
077                            themeDisplay.getLocale());
078    
079                    try {
080                            double taxRate = GetterUtil.getDouble(
081                                    percentFormat.parse(taxRatePercent));
082    
083                            setPreference(actionRequest, "taxRate", String.valueOf(taxRate));
084                    }
085                    catch (ParseException pe) {
086                            SessionErrors.add(actionRequest, "taxRate");
087                    }
088            }
089    
090            protected void updateShippingCalculation(ActionRequest actionRequest) {
091                    String[] shipping = new String[5];
092    
093                    for (int i = 0; i < shipping.length; i++) {
094                            shipping[i] = String.valueOf(
095                                    ParamUtil.getDouble(actionRequest, "shipping" + i));
096                    }
097    
098                    setPreference(actionRequest, "shipping", shipping);
099            }
100    
101    }