1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
31  import com.liferay.portal.util.ContentUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portal.util.PropsKeys;
34  import com.liferay.portal.util.PropsUtil;
35  
36  import java.io.IOException;
37  
38  import java.util.Currency;
39  import java.util.Locale;
40  import java.util.Set;
41  import java.util.TreeSet;
42  
43  import javax.portlet.PortletPreferences;
44  import javax.portlet.ReadOnlyException;
45  import javax.portlet.ValidatorException;
46  
47  /**
48   * <a href="ShoppingPreferences.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ShoppingPreferences {
54  
55      public static final String CC_NONE = "none";
56  
57      public static final String[] CC_TYPES =
58          new String[] {"visa", "mastercard", "discover", "amex"};
59  
60      public static final String[] CURRENCY_IDS;
61  
62      static {
63          String[] ids = null;
64  
65          try {
66              Set<String> set = new TreeSet<String>();
67  
68              Locale[] locales = Locale.getAvailableLocales();
69  
70              for (int i = 0; i < locales.length; i++) {
71                  Locale locale = locales[i];
72  
73                  if (locale.getCountry().length() == 2) {
74                      Currency currency = Currency.getInstance(locale);
75  
76                      String currencyId = currency.getCurrencyCode();
77  
78                      set.add(currencyId);
79                  }
80              }
81  
82              ids = set.toArray(new String[set.size()]);
83          }
84          catch (Exception e) {
85              ids = new String[] {"USD", "CAD", "EUR", "GBP", "JPY"};
86          }
87          finally {
88              CURRENCY_IDS = ids;
89          }
90      }
91  
92      public static final double[] SHIPPING_RANGE = {
93          0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
94          Double.POSITIVE_INFINITY
95      };
96  
97      public static final double[] INSURANCE_RANGE = {
98          0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
99          Double.POSITIVE_INFINITY
100     };
101 
102     public static ShoppingPreferences getInstance(long companyId, long groupId)
103         throws SystemException {
104 
105         return new ShoppingPreferences(companyId, groupId);
106     }
107 
108     public String getPayPalEmailAddress() {
109         return _preferences.getValue("paypal-email-address", StringPool.BLANK);
110     }
111 
112     public void setPayPalEmailAddress(String payPalEmailAddress)
113         throws ReadOnlyException {
114 
115         _preferences.setValue("paypal-email-address", payPalEmailAddress);
116     }
117 
118     public boolean usePayPal() {
119         return Validator.isNotNull(getPayPalEmailAddress());
120     }
121 
122     public String getCurrencyId() {
123         return _preferences.getValue("currency-id", "USD");
124     }
125 
126     public void setCurrencyId(String currencyId) throws ReadOnlyException {
127         _preferences.setValue("currency-id", currencyId);
128     }
129 
130     public String[] getCcTypes() {
131         String ccTypes = _preferences.getValue(
132             "cc-types", StringUtil.merge(CC_TYPES));
133 
134         if (ccTypes.equals(CC_NONE)) {
135             return new String[0];
136         }
137         else {
138             return StringUtil.split(ccTypes);
139         }
140     }
141 
142     public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
143         if (ccTypes.length == 0) {
144             _preferences.setValue("cc-types", CC_NONE);
145         }
146         else {
147             _preferences.setValue("cc-types", StringUtil.merge(ccTypes));
148         }
149     }
150 
151     public String getTaxState() {
152         return _preferences.getValue("tax-state", "CA");
153     }
154 
155     public void setTaxState(String taxState) throws ReadOnlyException {
156         _preferences.setValue("tax-state", taxState);
157     }
158 
159     public double getTaxRate() {
160         return GetterUtil.getDouble(_preferences.getValue(
161             "tax-rate", StringPool.BLANK));
162     }
163 
164     public void setTaxRate(double taxRate) throws ReadOnlyException {
165         _preferences.setValue("tax-rate", String.valueOf(taxRate));
166     }
167 
168     public String getShippingFormula() {
169         return _preferences.getValue("shipping-formula", "flat");
170     }
171 
172     public void setShippingFormula(String shippingFormula)
173         throws ReadOnlyException {
174 
175         _preferences.setValue("shipping-formula", shippingFormula);
176     }
177 
178     public String[] getShipping() {
179         String value = _preferences.getValue("shipping", null);
180 
181         if (value == null) {
182             return new String[5];
183         }
184         else {
185             return StringUtil.split(value);
186         }
187     }
188 
189     public void setShipping(String[] shipping) throws ReadOnlyException {
190         _preferences.setValue("shipping", StringUtil.merge(shipping));
191     }
192 
193     public String[][] getAlternativeShipping() {
194         String value = _preferences.getValue("alternative-shipping", null);
195 
196         if (value == null) {
197             return new String[0][0];
198         }
199         else {
200             String[] array =
201                 StringUtil.split("alternative-shipping", "[$_ARRAY_$]");
202 
203             String[][] alternativeShipping = new String[array.length][0];
204 
205             for (int i = 0; i < array.length; i++) {
206                 alternativeShipping[i] = StringUtil.split(array[i]);
207             }
208 
209             return alternativeShipping;
210         }
211     }
212 
213     public void setAlternativeShipping(String[][] alternativeShipping)
214         throws ReadOnlyException {
215 
216         StringBuilder sb = new StringBuilder();
217 
218         for (int i = 0; i < alternativeShipping.length; i++) {
219             sb.append(StringUtil.merge(alternativeShipping[i]));
220 
221             if ((i + 1) < alternativeShipping.length) {
222                 sb.append("[$_ARRAY_$]");
223             }
224         }
225 
226         _preferences.setValue("alternative-shipping", sb.toString());
227     }
228 
229     public boolean useAlternativeShipping() {
230         String[][] alternativeShipping = getAlternativeShipping();
231 
232         try {
233             for (int i = 0; i < 10; i++) {
234                 if (Validator.isNotNull(alternativeShipping[0][i]) &&
235                     Validator.isNotNull(alternativeShipping[1][i])) {
236 
237                     return true;
238                 }
239             }
240         }
241         catch (Exception e) {
242         }
243 
244         return false;
245     }
246 
247     public String getAlternativeShippingName(int altShipping) {
248         String altShippingName = StringPool.BLANK;
249 
250         try {
251             altShippingName = getAlternativeShipping()[0][altShipping];
252         }
253         catch (Exception e) {
254         }
255 
256         return altShippingName;
257     }
258 
259     public String getInsuranceFormula() {
260         return _preferences.getValue("insurance-formula", "flat");
261     }
262 
263     public void setInsuranceFormula(String insuranceFormula)
264         throws ReadOnlyException {
265 
266         _preferences.setValue("insurance-formula", insuranceFormula);
267     }
268 
269     public String[] getInsurance() {
270         String value = _preferences.getValue("insurance", null);
271 
272         if (value == null) {
273             return new String[5];
274         }
275         else {
276             return StringUtil.split(value);
277         }
278     }
279 
280     public void setInsurance(String[] insurance) throws ReadOnlyException {
281         _preferences.setValue("insurance", StringUtil.merge(insurance));
282     }
283 
284     public double getMinOrder() {
285         return GetterUtil.getDouble(_preferences.getValue(
286             "min-order", StringPool.BLANK));
287     }
288 
289     public void setMinOrder(double minOrder) throws ReadOnlyException {
290         _preferences.setValue("min-order", String.valueOf(minOrder));
291     }
292 
293     public String getEmailFromAddress() {
294         String emailFromAddress = PropsUtil.get(
295             PropsKeys.SHOPPING_EMAIL_FROM_ADDRESS);
296 
297         return _preferences.getValue("email-from-address", emailFromAddress);
298     }
299 
300     public void setEmailFromAddress(String emailFromAddress)
301         throws ReadOnlyException {
302 
303         _preferences.setValue("email-from-address", emailFromAddress);
304     }
305 
306     public String getEmailFromName() {
307         String emailFromName = PropsUtil.get(
308             PropsKeys.SHOPPING_EMAIL_FROM_NAME);
309 
310         return _preferences.getValue("email-from-name", emailFromName);
311     }
312 
313     public void setEmailFromName(String emailFromName)
314         throws ReadOnlyException {
315 
316         _preferences.setValue("email-from-name", emailFromName);
317     }
318 
319     public boolean getEmailOrderConfirmationEnabled() {
320         String emailOrderConfirmationEnabled = _preferences.getValue(
321             "email-order-confirmation-enabled", StringPool.BLANK);
322 
323         if (Validator.isNotNull(emailOrderConfirmationEnabled)) {
324             return GetterUtil.getBoolean(emailOrderConfirmationEnabled);
325         }
326         else {
327             return GetterUtil.getBoolean(PropsUtil.get(
328                 PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED));
329         }
330     }
331 
332     public void setEmailOrderConfirmationEnabled(
333             boolean emailOrderConfirmationEnabled)
334         throws ReadOnlyException {
335 
336         _preferences.setValue(
337             "email-order-confirmation-enabled",
338             String.valueOf(emailOrderConfirmationEnabled));
339     }
340 
341     public String getEmailOrderConfirmationBody() {
342         String emailOrderConfirmationBody = _preferences.getValue(
343             "email-order-confirmation-body", StringPool.BLANK);
344 
345         if (Validator.isNotNull(emailOrderConfirmationBody)) {
346             return emailOrderConfirmationBody;
347         }
348         else {
349             return ContentUtil.get(PropsUtil.get(
350                 PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY));
351         }
352     }
353 
354     public void setEmailOrderConfirmationBody(String emailOrderConfirmationBody)
355         throws ReadOnlyException {
356 
357         _preferences.setValue(
358             "email-order-confirmation-body", emailOrderConfirmationBody);
359     }
360 
361     public String getEmailOrderConfirmationSubject() {
362         String emailOrderConfirmationSubject = _preferences.getValue(
363             "email-order-confirmation-subject", StringPool.BLANK);
364 
365         if (Validator.isNotNull(emailOrderConfirmationSubject)) {
366             return emailOrderConfirmationSubject;
367         }
368         else {
369             return ContentUtil.get(PropsUtil.get(
370                 PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT));
371         }
372     }
373 
374     public void setEmailOrderConfirmationSubject(
375             String emailOrderConfirmationSubject)
376         throws ReadOnlyException {
377 
378         _preferences.setValue(
379             "email-order-confirmation-subject", emailOrderConfirmationSubject);
380     }
381 
382     public boolean getEmailOrderShippingEnabled() {
383         String emailOrderShippingEnabled = _preferences.getValue(
384             "email-order-shipping-enabled", StringPool.BLANK);
385 
386         if (Validator.isNotNull(emailOrderShippingEnabled)) {
387             return GetterUtil.getBoolean(emailOrderShippingEnabled);
388         }
389         else {
390             return GetterUtil.getBoolean(PropsUtil.get(
391                 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED));
392         }
393     }
394 
395     public void setEmailOrderShippingEnabled(boolean emailOrderShippingEnabled)
396         throws ReadOnlyException {
397 
398         _preferences.setValue(
399             "email-order-shipping-enabled",
400             String.valueOf(emailOrderShippingEnabled));
401     }
402 
403     public String getEmailOrderShippingBody() {
404         String emailOrderShippingBody = _preferences.getValue(
405             "email-order-shipping-body", StringPool.BLANK);
406 
407         if (Validator.isNotNull(emailOrderShippingBody)) {
408             return emailOrderShippingBody;
409         }
410         else {
411             return ContentUtil.get(PropsUtil.get(
412                 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_BODY));
413         }
414     }
415 
416     public void setEmailOrderShippingBody(String emailOrderShippingBody)
417         throws ReadOnlyException {
418 
419         _preferences.setValue(
420             "email-order-shipping-body", emailOrderShippingBody);
421     }
422 
423     public String getEmailOrderShippingSubject() {
424         String emailOrderShippingSubject = _preferences.getValue(
425             "email-order-shipping-subject", StringPool.BLANK);
426 
427         if (Validator.isNotNull(emailOrderShippingSubject)) {
428             return emailOrderShippingSubject;
429         }
430         else {
431             return ContentUtil.get(PropsUtil.get(
432                 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
433         }
434     }
435 
436     public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
437         throws ReadOnlyException {
438 
439         _preferences.setValue(
440             "email-order-shipping-subject", emailOrderShippingSubject);
441     }
442 
443     public void store() throws IOException, ValidatorException {
444         _preferences.store();
445     }
446 
447     protected ShoppingPreferences(long companyId, long groupId)
448         throws SystemException {
449 
450         long ownerId = groupId;
451         int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
452         long plid = PortletKeys.PREFS_PLID_SHARED;
453         String portletId = PortletKeys.SHOPPING;
454 
455         _preferences = PortletPreferencesLocalServiceUtil.getPreferences(
456             companyId, ownerId, ownerType, plid, portletId);
457     }
458 
459     private PortletPreferences _preferences;
460 
461 }