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.BaseJSPSettingsConfigurationAction;
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
035            extends BaseJSPSettingsConfigurationAction {
036    
037            @Override
038            public void processAction(
039                            PortletConfig portletConfig, ActionRequest actionRequest,
040                            ActionResponse actionResponse)
041                    throws Exception {
042    
043                    validateEmail(actionRequest, "emailOrderConfirmation");
044                    validateEmail(actionRequest, "emailOrderShipping");
045                    validateEmailFrom(actionRequest);
046    
047                    updatePayment(actionRequest);
048    
049                    super.processAction(portletConfig, actionRequest, actionResponse);
050            }
051    
052            protected void updateInsuranceCalculation(ActionRequest actionRequest) {
053                    String[] insurance = new String[5];
054    
055                    for (int i = 0; i < insurance.length; i++) {
056                            insurance[i] = String.valueOf(
057                                    ParamUtil.getDouble(actionRequest, "insurance" + i));
058                    }
059    
060                    setPreference(actionRequest, "insurance", insurance);
061            }
062    
063            @Override
064            protected void updateMultiValuedKeys(ActionRequest actionRequest) {
065                    super.updateMultiValuedKeys(actionRequest);
066    
067                    updateInsuranceCalculation(actionRequest);
068                    updateShippingCalculation(actionRequest);
069            }
070    
071            protected void updatePayment(ActionRequest actionRequest) {
072                    String taxRatePercent = ParamUtil.getString(actionRequest, "taxRate");
073    
074                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
075                            WebKeys.THEME_DISPLAY);
076    
077                    NumberFormat percentFormat = NumberFormat.getPercentInstance(
078                            themeDisplay.getLocale());
079    
080                    try {
081                            double taxRate = GetterUtil.getDouble(
082                                    percentFormat.parse(taxRatePercent));
083    
084                            setPreference(actionRequest, "taxRate", String.valueOf(taxRate));
085                    }
086                    catch (ParseException pe) {
087                            SessionErrors.add(actionRequest, "taxRate");
088                    }
089            }
090    
091            protected void updateShippingCalculation(ActionRequest actionRequest) {
092                    String[] shipping = new String[5];
093    
094                    for (int i = 0; i < shipping.length; i++) {
095                            shipping[i] = String.valueOf(
096                                    ParamUtil.getDouble(actionRequest, "shipping" + i));
097                    }
098    
099                    setPreference(actionRequest, "shipping", shipping);
100            }
101    
102    }