1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.action;
24  
25  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.servlet.SessionMessages;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.shopping.util.ShoppingPreferences;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  /**
43   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class ConfigurationActionImpl extends BaseConfigurationAction {
48  
49      public void processAction(
50              PortletConfig portletConfig, ActionRequest actionRequest,
51              ActionResponse actionResponse)
52          throws Exception {
53  
54          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
55  
56          if (!cmd.equals(Constants.UPDATE)) {
57              return;
58          }
59  
60          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
61              WebKeys.THEME_DISPLAY);
62  
63          ShoppingPreferences preferences = ShoppingPreferences.getInstance(
64              themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
65  
66          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
67          String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
68  
69          if (tabs2.equals("payment-settings")) {
70              updatePayment(actionRequest, preferences);
71          }
72          else if (tabs2.equals("shipping-calculation")) {
73              updateShippingCalculation(actionRequest, preferences);
74          }
75          else if (tabs2.equals("insurance-calculation")) {
76              updateInsuranceCalculation(actionRequest, preferences);
77          }
78          else if (tabs2.equals("emails")) {
79              if (tabs3.equals("email-from")) {
80                  updateEmailFrom(actionRequest, preferences);
81              }
82              else if (tabs3.equals("confirmation-email")) {
83                  updateEmailOrderConfirmation(actionRequest, preferences);
84              }
85              else if (tabs3.equals("shipping-email")) {
86                  updateEmailOrderShipping(actionRequest, preferences);
87              }
88          }
89  
90          if (SessionErrors.isEmpty(actionRequest)) {
91              preferences.store();
92  
93              SessionMessages.add(
94                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
95          }
96      }
97  
98      public String render(
99              PortletConfig portletConfig, RenderRequest renderRequest,
100             RenderResponse renderResponse)
101         throws Exception {
102 
103         return "/html/portlet/shopping/configuration.jsp";
104     }
105 
106     protected void updateEmailFrom(
107             ActionRequest actionRequest, ShoppingPreferences preferences)
108         throws Exception {
109 
110         String emailFromName = ParamUtil.getString(
111             actionRequest, "emailFromName");
112         String emailFromAddress = ParamUtil.getString(
113             actionRequest, "emailFromAddress");
114 
115         if (Validator.isNull(emailFromName)) {
116             SessionErrors.add(actionRequest, "emailFromName");
117         }
118         else if (!Validator.isEmailAddress(emailFromAddress)) {
119             SessionErrors.add(actionRequest, "emailFromAddress");
120         }
121         else {
122             preferences.setEmailFromName(emailFromName);
123             preferences.setEmailFromAddress(emailFromAddress);
124         }
125     }
126 
127     protected void updateEmailOrderConfirmation(
128             ActionRequest actionRequest, ShoppingPreferences preferences)
129         throws Exception {
130 
131         boolean emailOrderConfirmationEnabled = ParamUtil.getBoolean(
132             actionRequest, "emailOrderConfirmationEnabled");
133         String emailOrderConfirmationSubject = ParamUtil.getString(
134             actionRequest, "emailOrderConfirmationSubject");
135         String emailOrderConfirmationBody = ParamUtil.getString(
136             actionRequest, "emailOrderConfirmationBody");
137 
138         if (Validator.isNull(emailOrderConfirmationSubject)) {
139             SessionErrors.add(actionRequest, "emailOrderConfirmationSubject");
140         }
141         else if (Validator.isNull(emailOrderConfirmationBody)) {
142             SessionErrors.add(actionRequest, "emailOrderConfirmationBody");
143         }
144         else {
145             preferences.setEmailOrderConfirmationEnabled(
146                 emailOrderConfirmationEnabled);
147             preferences.setEmailOrderConfirmationSubject(
148                 emailOrderConfirmationSubject);
149             preferences.setEmailOrderConfirmationBody(
150                 emailOrderConfirmationBody);
151         }
152     }
153 
154     protected void updateEmailOrderShipping(
155             ActionRequest actionRequest, ShoppingPreferences preferences)
156         throws Exception {
157 
158         boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
159             actionRequest, "emailOrderShippingEnabled");
160         String emailOrderShippingSubject = ParamUtil.getString(
161             actionRequest, "emailOrderShippingSubject");
162         String emailOrderShippingBody = ParamUtil.getString(
163             actionRequest, "emailOrderShippingBody");
164 
165         if (Validator.isNull(emailOrderShippingSubject)) {
166             SessionErrors.add(actionRequest, "emailOrderShippingSubject");
167         }
168         else if (Validator.isNull(emailOrderShippingBody)) {
169             SessionErrors.add(actionRequest, "emailOrderShippingBody");
170         }
171         else {
172             preferences.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
173             preferences.setEmailOrderShippingSubject(emailOrderShippingSubject);
174             preferences.setEmailOrderShippingBody(emailOrderShippingBody);
175         }
176     }
177 
178     protected void updateInsuranceCalculation(
179             ActionRequest actionRequest, ShoppingPreferences preferences)
180         throws Exception {
181 
182         String insuranceFormula = ParamUtil.getString(
183             actionRequest, "insuranceFormula");
184 
185         String[] insurance = new String[5];
186 
187         for (int i = 0; i < insurance.length; i++) {
188             insurance[i] = String.valueOf(
189                 ParamUtil.getDouble(actionRequest, "insurance" + i));
190         }
191 
192         preferences.setInsuranceFormula(insuranceFormula);
193         preferences.setInsurance(insurance);
194     }
195 
196     protected void updatePayment(
197             ActionRequest actionRequest, ShoppingPreferences preferences)
198         throws Exception {
199 
200         String payPalEmailAddress = ParamUtil.getString(
201             actionRequest, "payPalEmailAddress");
202         String[] ccTypes = StringUtil.split(
203             ParamUtil.getString(actionRequest, "ccTypes"));
204         String currencyId = ParamUtil.getString(actionRequest, "currencyId");
205         String taxState = ParamUtil.getString(actionRequest, "taxState");
206         double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
207         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
208 
209         preferences.setPayPalEmailAddress(payPalEmailAddress);
210         preferences.setCcTypes(ccTypes);
211         preferences.setCurrencyId(currencyId);
212         preferences.setTaxState(taxState);
213         preferences.setTaxRate(taxRate);
214         preferences.setMinOrder(minOrder);
215     }
216 
217     protected void updateShippingCalculation(
218             ActionRequest actionRequest, ShoppingPreferences preferences)
219         throws Exception {
220 
221         String shippingFormula = ParamUtil.getString(
222             actionRequest, "shippingFormula");
223 
224         String[] shipping = new String[5];
225 
226         for (int i = 0; i < shipping.length; i++) {
227             shipping[i] = String.valueOf(
228                 ParamUtil.getDouble(actionRequest, "shipping" + i));
229         }
230 
231         preferences.setShippingFormula(shippingFormula);
232         preferences.setShipping(shipping);
233     }
234 
235 }