001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
025    import com.liferay.portal.util.ContentUtil;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.PropsUtil;
028    
029    import java.io.IOException;
030    
031    import java.util.Currency;
032    import java.util.Locale;
033    import java.util.Set;
034    import java.util.TreeSet;
035    
036    import javax.portlet.PortletPreferences;
037    import javax.portlet.ReadOnlyException;
038    import javax.portlet.ValidatorException;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class ShoppingPreferences {
044    
045            public static final String CC_NONE = "none";
046    
047            public static final String[] CC_TYPES =
048                    new String[] {"visa", "mastercard", "discover", "amex"};
049    
050            public static final String[] CURRENCY_IDS;
051    
052            static {
053                    String[] ids = null;
054    
055                    try {
056                            Set<String> set = new TreeSet<String>();
057    
058                            Locale[] locales = Locale.getAvailableLocales();
059    
060                            for (int i = 0; i < locales.length; i++) {
061                                    Locale locale = locales[i];
062    
063                                    if (locale.getCountry().length() == 2) {
064                                            Currency currency = Currency.getInstance(locale);
065    
066                                            String currencyId = currency.getCurrencyCode();
067    
068                                            set.add(currencyId);
069                                    }
070                            }
071    
072                            ids = set.toArray(new String[set.size()]);
073                    }
074                    catch (Exception e) {
075                            ids = new String[] {"USD", "CAD", "EUR", "GBP", "JPY"};
076                    }
077                    finally {
078                            CURRENCY_IDS = ids;
079                    }
080            }
081    
082            public static final double[] SHIPPING_RANGE = {
083                    0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
084                    Double.POSITIVE_INFINITY
085            };
086    
087            public static final double[] INSURANCE_RANGE = {
088                    0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
089                    Double.POSITIVE_INFINITY
090            };
091    
092            public static ShoppingPreferences getInstance(long companyId, long groupId)
093                    throws SystemException {
094    
095                    return new ShoppingPreferences(companyId, groupId);
096            }
097    
098            public String getPayPalEmailAddress() {
099                    return _preferences.getValue("paypal-email-address", StringPool.BLANK);
100            }
101    
102            public void setPayPalEmailAddress(String payPalEmailAddress)
103                    throws ReadOnlyException {
104    
105                    _preferences.setValue("paypal-email-address", payPalEmailAddress);
106            }
107    
108            public boolean usePayPal() {
109                    return Validator.isNotNull(getPayPalEmailAddress());
110            }
111    
112            public String getCurrencyId() {
113                    return _preferences.getValue("currency-id", "USD");
114            }
115    
116            public void setCurrencyId(String currencyId) throws ReadOnlyException {
117                    _preferences.setValue("currency-id", currencyId);
118            }
119    
120            public String[] getCcTypes() {
121                    String ccTypes = _preferences.getValue(
122                            "cc-types", StringUtil.merge(CC_TYPES));
123    
124                    if (ccTypes.equals(CC_NONE)) {
125                            return new String[0];
126                    }
127                    else {
128                            return StringUtil.split(ccTypes);
129                    }
130            }
131    
132            public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
133                    if (ccTypes.length == 0) {
134                            _preferences.setValue("cc-types", CC_NONE);
135                    }
136                    else {
137                            _preferences.setValue("cc-types", StringUtil.merge(ccTypes));
138                    }
139            }
140    
141            public String getTaxState() {
142                    return _preferences.getValue("tax-state", "CA");
143            }
144    
145            public void setTaxState(String taxState) throws ReadOnlyException {
146                    _preferences.setValue("tax-state", taxState);
147            }
148    
149            public double getTaxRate() {
150                    return GetterUtil.getDouble(_preferences.getValue(
151                            "tax-rate", StringPool.BLANK));
152            }
153    
154            public void setTaxRate(double taxRate) throws ReadOnlyException {
155                    _preferences.setValue("tax-rate", String.valueOf(taxRate));
156            }
157    
158            public String getShippingFormula() {
159                    return _preferences.getValue("shipping-formula", "flat");
160            }
161    
162            public void setShippingFormula(String shippingFormula)
163                    throws ReadOnlyException {
164    
165                    _preferences.setValue("shipping-formula", shippingFormula);
166            }
167    
168            public String[] getShipping() {
169                    String value = _preferences.getValue("shipping", null);
170    
171                    if (value == null) {
172                            return new String[5];
173                    }
174                    else {
175                            return StringUtil.split(value);
176                    }
177            }
178    
179            public void setShipping(String[] shipping) throws ReadOnlyException {
180                    _preferences.setValue("shipping", StringUtil.merge(shipping));
181            }
182    
183            public String[][] getAlternativeShipping() {
184                    String value = _preferences.getValue("alternative-shipping", null);
185    
186                    if (value == null) {
187                            return new String[0][0];
188                    }
189                    else {
190                            String[] array =
191                                    StringUtil.split("alternative-shipping", "[$_ARRAY_$]");
192    
193                            String[][] alternativeShipping = new String[array.length][0];
194    
195                            for (int i = 0; i < array.length; i++) {
196                                    alternativeShipping[i] = StringUtil.split(array[i]);
197                            }
198    
199                            return alternativeShipping;
200                    }
201            }
202    
203            public void setAlternativeShipping(String[][] alternativeShipping)
204                    throws ReadOnlyException {
205    
206                    if (alternativeShipping.length == 0) {
207                            _preferences.setValue("alternative-shipping", StringPool.BLANK);
208                    }
209    
210                    StringBundler sb = new StringBundler(
211                            alternativeShipping.length * 2 - 1);
212    
213                    for (int i = 0; i < alternativeShipping.length; i++) {
214                            sb.append(StringUtil.merge(alternativeShipping[i]));
215    
216                            if ((i + 1) < alternativeShipping.length) {
217                                    sb.append("[$_ARRAY_$]");
218                            }
219                    }
220    
221                    _preferences.setValue("alternative-shipping", sb.toString());
222            }
223    
224            public boolean useAlternativeShipping() {
225                    String[][] alternativeShipping = getAlternativeShipping();
226    
227                    try {
228                            for (int i = 0; i < 10; i++) {
229                                    if (Validator.isNotNull(alternativeShipping[0][i]) &&
230                                            Validator.isNotNull(alternativeShipping[1][i])) {
231    
232                                            return true;
233                                    }
234                            }
235                    }
236                    catch (Exception e) {
237                    }
238    
239                    return false;
240            }
241    
242            public String getAlternativeShippingName(int altShipping) {
243                    String altShippingName = StringPool.BLANK;
244    
245                    try {
246                            altShippingName = getAlternativeShipping()[0][altShipping];
247                    }
248                    catch (Exception e) {
249                    }
250    
251                    return altShippingName;
252            }
253    
254            public String getInsuranceFormula() {
255                    return _preferences.getValue("insurance-formula", "flat");
256            }
257    
258            public void setInsuranceFormula(String insuranceFormula)
259                    throws ReadOnlyException {
260    
261                    _preferences.setValue("insurance-formula", insuranceFormula);
262            }
263    
264            public String[] getInsurance() {
265                    String value = _preferences.getValue("insurance", null);
266    
267                    if (value == null) {
268                            return new String[5];
269                    }
270                    else {
271                            return StringUtil.split(value);
272                    }
273            }
274    
275            public void setInsurance(String[] insurance) throws ReadOnlyException {
276                    _preferences.setValue("insurance", StringUtil.merge(insurance));
277            }
278    
279            public double getMinOrder() {
280                    return GetterUtil.getDouble(_preferences.getValue(
281                            "min-order", StringPool.BLANK));
282            }
283    
284            public void setMinOrder(double minOrder) throws ReadOnlyException {
285                    _preferences.setValue("min-order", String.valueOf(minOrder));
286            }
287    
288            public String getEmailFromAddress() {
289                    String emailFromAddress = PropsUtil.get(
290                            PropsKeys.SHOPPING_EMAIL_FROM_ADDRESS);
291    
292                    return _preferences.getValue("email-from-address", emailFromAddress);
293            }
294    
295            public void setEmailFromAddress(String emailFromAddress)
296                    throws ReadOnlyException {
297    
298                    _preferences.setValue("email-from-address", emailFromAddress);
299            }
300    
301            public String getEmailFromName() {
302                    String emailFromName = PropsUtil.get(
303                            PropsKeys.SHOPPING_EMAIL_FROM_NAME);
304    
305                    return _preferences.getValue("email-from-name", emailFromName);
306            }
307    
308            public void setEmailFromName(String emailFromName)
309                    throws ReadOnlyException {
310    
311                    _preferences.setValue("email-from-name", emailFromName);
312            }
313    
314            public boolean getEmailOrderConfirmationEnabled() {
315                    String emailOrderConfirmationEnabled = _preferences.getValue(
316                            "email-order-confirmation-enabled", StringPool.BLANK);
317    
318                    if (Validator.isNotNull(emailOrderConfirmationEnabled)) {
319                            return GetterUtil.getBoolean(emailOrderConfirmationEnabled);
320                    }
321                    else {
322                            return GetterUtil.getBoolean(PropsUtil.get(
323                                    PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED));
324                    }
325            }
326    
327            public void setEmailOrderConfirmationEnabled(
328                            boolean emailOrderConfirmationEnabled)
329                    throws ReadOnlyException {
330    
331                    _preferences.setValue(
332                            "email-order-confirmation-enabled",
333                            String.valueOf(emailOrderConfirmationEnabled));
334            }
335    
336            public String getEmailOrderConfirmationBody() {
337                    String emailOrderConfirmationBody = _preferences.getValue(
338                            "email-order-confirmation-body", StringPool.BLANK);
339    
340                    if (Validator.isNotNull(emailOrderConfirmationBody)) {
341                            return emailOrderConfirmationBody;
342                    }
343                    else {
344                            return ContentUtil.get(PropsUtil.get(
345                                    PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY));
346                    }
347            }
348    
349            public void setEmailOrderConfirmationBody(String emailOrderConfirmationBody)
350                    throws ReadOnlyException {
351    
352                    _preferences.setValue(
353                            "email-order-confirmation-body", emailOrderConfirmationBody);
354            }
355    
356            public String getEmailOrderConfirmationSubject() {
357                    String emailOrderConfirmationSubject = _preferences.getValue(
358                            "email-order-confirmation-subject", StringPool.BLANK);
359    
360                    if (Validator.isNotNull(emailOrderConfirmationSubject)) {
361                            return emailOrderConfirmationSubject;
362                    }
363                    else {
364                            return ContentUtil.get(PropsUtil.get(
365                                    PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT));
366                    }
367            }
368    
369            public void setEmailOrderConfirmationSubject(
370                            String emailOrderConfirmationSubject)
371                    throws ReadOnlyException {
372    
373                    _preferences.setValue(
374                            "email-order-confirmation-subject", emailOrderConfirmationSubject);
375            }
376    
377            public boolean getEmailOrderShippingEnabled() {
378                    String emailOrderShippingEnabled = _preferences.getValue(
379                            "email-order-shipping-enabled", StringPool.BLANK);
380    
381                    if (Validator.isNotNull(emailOrderShippingEnabled)) {
382                            return GetterUtil.getBoolean(emailOrderShippingEnabled);
383                    }
384                    else {
385                            return GetterUtil.getBoolean(PropsUtil.get(
386                                    PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED));
387                    }
388            }
389    
390            public void setEmailOrderShippingEnabled(boolean emailOrderShippingEnabled)
391                    throws ReadOnlyException {
392    
393                    _preferences.setValue(
394                            "email-order-shipping-enabled",
395                            String.valueOf(emailOrderShippingEnabled));
396            }
397    
398            public String getEmailOrderShippingBody() {
399                    String emailOrderShippingBody = _preferences.getValue(
400                            "email-order-shipping-body", StringPool.BLANK);
401    
402                    if (Validator.isNotNull(emailOrderShippingBody)) {
403                            return emailOrderShippingBody;
404                    }
405                    else {
406                            return ContentUtil.get(PropsUtil.get(
407                                    PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_BODY));
408                    }
409            }
410    
411            public void setEmailOrderShippingBody(String emailOrderShippingBody)
412                    throws ReadOnlyException {
413    
414                    _preferences.setValue(
415                            "email-order-shipping-body", emailOrderShippingBody);
416            }
417    
418            public String getEmailOrderShippingSubject() {
419                    String emailOrderShippingSubject = _preferences.getValue(
420                            "email-order-shipping-subject", StringPool.BLANK);
421    
422                    if (Validator.isNotNull(emailOrderShippingSubject)) {
423                            return emailOrderShippingSubject;
424                    }
425                    else {
426                            return ContentUtil.get(PropsUtil.get(
427                                    PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
428                    }
429            }
430    
431            public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
432                    throws ReadOnlyException {
433    
434                    _preferences.setValue(
435                            "email-order-shipping-subject", emailOrderShippingSubject);
436            }
437    
438            public void store() throws IOException, ValidatorException {
439                    _preferences.store();
440            }
441    
442            protected ShoppingPreferences(long companyId, long groupId)
443                    throws SystemException {
444    
445                    long ownerId = groupId;
446                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
447                    long plid = PortletKeys.PREFS_PLID_SHARED;
448                    String portletId = PortletKeys.SHOPPING;
449    
450                    _preferences = PortletPreferencesLocalServiceUtil.getPreferences(
451                            companyId, ownerId, ownerType, plid, portletId);
452            }
453    
454            private PortletPreferences _preferences;
455    
456    }