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 prefs = 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, prefs);
71          }
72          else if (tabs2.equals("shipping-calculation")) {
73              updateShippingCalculation(actionRequest, prefs);
74          }
75          else if (tabs2.equals("insurance-calculation")) {
76              updateInsuranceCalculation(actionRequest, prefs);
77          }
78          else if (tabs2.equals("emails")) {
79              if (tabs3.equals("email-from")) {
80                  updateEmailFrom(actionRequest, prefs);
81              }
82              else if (tabs3.equals("confirmation-email")) {
83                  updateEmailOrderConfirmation(actionRequest, prefs);
84              }
85              else if (tabs3.equals("shipping-email")) {
86                  updateEmailOrderShipping(actionRequest, prefs);
87              }
88          }
89  
90          if (SessionErrors.isEmpty(actionRequest)) {
91              prefs.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 prefs)
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             prefs.setEmailFromName(emailFromName);
123             prefs.setEmailFromAddress(emailFromAddress);
124         }
125     }
126 
127     protected void updateEmailOrderConfirmation(
128             ActionRequest actionRequest, ShoppingPreferences prefs)
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             prefs.setEmailOrderConfirmationEnabled(
146                 emailOrderConfirmationEnabled);
147             prefs.setEmailOrderConfirmationSubject(
148                 emailOrderConfirmationSubject);
149             prefs.setEmailOrderConfirmationBody(emailOrderConfirmationBody);
150         }
151     }
152 
153     protected void updateEmailOrderShipping(
154             ActionRequest actionRequest, ShoppingPreferences prefs)
155         throws Exception {
156 
157         boolean emailOrderShippingEnabled = ParamUtil.getBoolean(
158             actionRequest, "emailOrderShippingEnabled");
159         String emailOrderShippingSubject = ParamUtil.getString(
160             actionRequest, "emailOrderShippingSubject");
161         String emailOrderShippingBody = ParamUtil.getString(
162             actionRequest, "emailOrderShippingBody");
163 
164         if (Validator.isNull(emailOrderShippingSubject)) {
165             SessionErrors.add(actionRequest, "emailOrderShippingSubject");
166         }
167         else if (Validator.isNull(emailOrderShippingBody)) {
168             SessionErrors.add(actionRequest, "emailOrderShippingBody");
169         }
170         else {
171             prefs.setEmailOrderShippingEnabled(emailOrderShippingEnabled);
172             prefs.setEmailOrderShippingSubject(emailOrderShippingSubject);
173             prefs.setEmailOrderShippingBody(emailOrderShippingBody);
174         }
175     }
176 
177     protected void updateInsuranceCalculation(
178             ActionRequest actionRequest, ShoppingPreferences prefs)
179         throws Exception {
180 
181         String insuranceFormula = ParamUtil.getString(
182             actionRequest, "insuranceFormula");
183 
184         String[] insurance = new String[5];
185 
186         for (int i = 0; i < insurance.length; i++) {
187             insurance[i] = String.valueOf(
188                 ParamUtil.getDouble(actionRequest, "insurance" + i));
189         }
190 
191         prefs.setInsuranceFormula(insuranceFormula);
192         prefs.setInsurance(insurance);
193     }
194 
195     protected void updatePayment(
196             ActionRequest actionRequest, ShoppingPreferences prefs)
197         throws Exception {
198 
199         String payPalEmailAddress = ParamUtil.getString(
200             actionRequest, "payPalEmailAddress");
201         String[] ccTypes = StringUtil.split(
202             ParamUtil.getString(actionRequest, "ccTypes"));
203         String currencyId = ParamUtil.getString(actionRequest, "currencyId");
204         String taxState = ParamUtil.getString(actionRequest, "taxState");
205         double taxRate = ParamUtil.getDouble(actionRequest, "taxRate") / 100;
206         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder");
207 
208         prefs.setPayPalEmailAddress(payPalEmailAddress);
209         prefs.setCcTypes(ccTypes);
210         prefs.setCurrencyId(currencyId);
211         prefs.setTaxState(taxState);
212         prefs.setTaxRate(taxRate);
213         prefs.setMinOrder(minOrder);
214     }
215 
216     protected void updateShippingCalculation(
217             ActionRequest actionRequest, ShoppingPreferences prefs)
218         throws Exception {
219 
220         String shippingFormula = ParamUtil.getString(
221             actionRequest, "shippingFormula");
222 
223         String[] shipping = new String[5];
224 
225         for (int i = 0; i < shipping.length; i++) {
226             shipping[i] = String.valueOf(
227                 ParamUtil.getDouble(actionRequest, "shipping" + i));
228         }
229 
230         prefs.setShippingFormula(shippingFormula);
231         prefs.setShipping(shipping);
232     }
233 
234 }