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