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