001    /**
002     * Copyright (c) 2000-present 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;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.resource.manager.ClassLoaderResourceManager;
019    import com.liferay.portal.kernel.resource.manager.ResourceManager;
020    import com.liferay.portal.kernel.settings.FallbackKeys;
021    import com.liferay.portal.kernel.settings.LocalizedValuesMap;
022    import com.liferay.portal.kernel.settings.ParameterMapSettings;
023    import com.liferay.portal.kernel.settings.Settings;
024    import com.liferay.portal.kernel.settings.SettingsFactory;
025    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
026    import com.liferay.portal.kernel.settings.TypedSettings;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portlet.shopping.util.ShoppingConstants;
032    
033    import java.util.Currency;
034    import java.util.Locale;
035    import java.util.Map;
036    import java.util.Set;
037    import java.util.TreeSet;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Eduardo Garcia
042     */
043    public class ShoppingSettings {
044    
045            public static final String[] ALL_KEYS = {
046                    "alternativeShipping", "ccTypes", "currencyId", "emailFromAddress",
047                    "emailFromName", "emailOrderConfirmationBody",
048                    "emailOrderConfirmationSubject", "emailOrderShippingBody",
049                    "emailOrderShippingSubject", "insurance", "insuranceFormula",
050                    "minOrder", "paypalEmailAddress", "shipping", "shippingFormula",
051                    "taxRate", "taxState", "emailOrderConfirmationEnabled",
052                    "emailOrderShippingEnabled"
053            };
054    
055            public static final String CC_NONE = "none";
056    
057            public static final String[] CC_TYPES =
058                    {"visa", "mastercard", "discover", "amex"};
059    
060            public static final String[] CURRENCY_IDS;
061    
062            public static final double[] INSURANCE_RANGE = {
063                    0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
064                    Double.POSITIVE_INFINITY
065            };
066    
067            public static final double[] SHIPPING_RANGE = {
068                    0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
069                    Double.POSITIVE_INFINITY
070            };
071    
072            static {
073                    String[] ids = null;
074    
075                    try {
076                            Set<String> set = new TreeSet<String>();
077    
078                            Locale[] locales = Locale.getAvailableLocales();
079    
080                            for (int i = 0; i < locales.length; i++) {
081                                    Locale locale = locales[i];
082    
083                                    if (locale.getCountry().length() == 2) {
084                                            Currency currency = Currency.getInstance(locale);
085    
086                                            String currencyId = currency.getCurrencyCode();
087    
088                                            set.add(currencyId);
089                                    }
090                            }
091    
092                            ids = set.toArray(new String[set.size()]);
093                    }
094                    catch (Exception e) {
095                            ids = new String[] {"USD", "CAD", "EUR", "GBP", "JPY"};
096                    }
097                    finally {
098                            CURRENCY_IDS = ids;
099                    }
100            }
101    
102            public static ShoppingSettings getInstance(long groupId)
103                    throws PortalException {
104    
105                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
106                            groupId, ShoppingConstants.SERVICE_NAME);
107    
108                    return new ShoppingSettings(settings);
109            }
110    
111            public static ShoppingSettings getInstance(
112                            long groupId, Map<String, String[]> parameterMap)
113                    throws PortalException {
114    
115                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
116                            groupId, ShoppingConstants.SERVICE_NAME);
117    
118                    ParameterMapSettings parameterMapSettings = new ParameterMapSettings(
119                            parameterMap, settings);
120    
121                    return new ShoppingSettings(parameterMapSettings);
122            }
123    
124            public ShoppingSettings(Settings settings) {
125                    _typedSettings = new TypedSettings(settings);
126            }
127    
128            public String[][] getAlternativeShipping() {
129                    String value = _typedSettings.getValue("alternativeShipping", null);
130    
131                    if (value == null) {
132                            return new String[0][0];
133                    }
134    
135                    String[] array = StringUtil.split("alternativeShipping", "[$_ARRAY_$]");
136    
137                    String[][] alternativeShipping = new String[array.length][0];
138    
139                    for (int i = 0; i < array.length; i++) {
140                            alternativeShipping[i] = StringUtil.split(array[i]);
141                    }
142    
143                    return alternativeShipping;
144            }
145    
146            public String getAlternativeShippingName(int altShipping) {
147                    String altShippingName = StringPool.BLANK;
148    
149                    try {
150                            altShippingName = getAlternativeShipping()[0][altShipping];
151                    }
152                    catch (Exception e) {
153                    }
154    
155                    return altShippingName;
156            }
157    
158            public String[] getCcTypes() {
159                    String[] ccTypes = _typedSettings.getValues("ccTypes");
160    
161                    if ((ccTypes.length == 1) && ccTypes[0].equals(CC_NONE)) {
162                            return StringPool.EMPTY_ARRAY;
163                    }
164    
165                    return ccTypes;
166            }
167    
168            public String getCurrencyId() {
169                    return _typedSettings.getValue("currencyId", "USD");
170            }
171    
172            public String getEmailFromAddress() {
173                    return _typedSettings.getValue("emailFromAddress");
174            }
175    
176            public String getEmailFromName() {
177                    return _typedSettings.getValue("emailFromName");
178            }
179    
180            public LocalizedValuesMap getEmailOrderConfirmationBody() {
181                    LocalizedValuesMap emailOrderConfirmationBody =
182                            _typedSettings.getLocalizedValuesMap("emailOrderConfirmationBody");
183    
184                    return emailOrderConfirmationBody;
185            }
186    
187            public String getEmailOrderConfirmationBodyXml() {
188                    LocalizedValuesMap emailOrderConfirmationBodyMap =
189                            getEmailOrderConfirmationBody();
190    
191                    return emailOrderConfirmationBodyMap.getLocalizationXml();
192            }
193    
194            public LocalizedValuesMap getEmailOrderConfirmationSubject() {
195                    LocalizedValuesMap emailOrderConfirmationSubject =
196                            _typedSettings.getLocalizedValuesMap(
197                                    "emailOrderConfirmationSubject");
198    
199                    return emailOrderConfirmationSubject;
200            }
201    
202            public String getEmailOrderConfirmationSubjectXml() {
203                    LocalizedValuesMap emailOrderConfirmationSubjectMap =
204                            getEmailOrderConfirmationSubject();
205    
206                    return emailOrderConfirmationSubjectMap.getLocalizationXml();
207            }
208    
209            public LocalizedValuesMap getEmailOrderShippingBody() {
210                    return _typedSettings.getLocalizedValuesMap("emailOrderShippingBody");
211            }
212    
213            public String getEmailOrderShippingBodyXml() {
214                    LocalizedValuesMap emailOrderShippingBodyMap =
215                            getEmailOrderShippingBody();
216    
217                    return emailOrderShippingBodyMap.getLocalizationXml();
218            }
219    
220            public LocalizedValuesMap getEmailOrderShippingSubject() {
221                    return _typedSettings.getLocalizedValuesMap(
222                            "emailOrderShippingSubject");
223            }
224    
225            public String getEmailOrderShippingSubjectXml() {
226                    LocalizedValuesMap emailOrderShippingSubjectMap =
227                            getEmailOrderShippingSubject();
228    
229                    return emailOrderShippingSubjectMap.getLocalizationXml();
230            }
231    
232            public String[] getInsurance() {
233                    return _typedSettings.getValues("insurance");
234            }
235    
236            public String getInsuranceFormula() {
237                    return _typedSettings.getValue("insuranceFormula");
238            }
239    
240            public double getMinOrder() {
241                    return _typedSettings.getDoubleValue("minOrder");
242            }
243    
244            public String getPayPalEmailAddress() {
245                    return _typedSettings.getValue("paypalEmailAddress");
246            }
247    
248            public String[] getShipping() {
249                    return _typedSettings.getValues("shipping");
250            }
251    
252            public String getShippingFormula() {
253                    return _typedSettings.getValue("shippingFormula");
254            }
255    
256            public double getTaxRate() {
257                    return _typedSettings.getDoubleValue("taxRate");
258            }
259    
260            public String getTaxState() {
261                    return _typedSettings.getValue("taxState");
262            }
263    
264            public boolean isEmailOrderConfirmationEnabled() {
265                    return _typedSettings.getBooleanValue("emailOrderConfirmationEnabled");
266            }
267    
268            public boolean isEmailOrderShippingEnabled() {
269                    return _typedSettings.getBooleanValue("emailOrderShippingEnabled");
270            }
271    
272            public boolean useAlternativeShipping() {
273                    String[][] alternativeShipping = getAlternativeShipping();
274    
275                    try {
276                            for (int i = 0; i < 10; i++) {
277                                    if (Validator.isNotNull(alternativeShipping[0][i]) &&
278                                            Validator.isNotNull(alternativeShipping[1][i])) {
279    
280                                            return true;
281                                    }
282                            }
283                    }
284                    catch (Exception e) {
285                    }
286    
287                    return false;
288            }
289    
290            public boolean usePayPal() {
291                    return Validator.isNotNull(getPayPalEmailAddress());
292            }
293    
294            private static FallbackKeys _getFallbackKeys() {
295                    FallbackKeys fallbackKeys = new FallbackKeys();
296    
297                    fallbackKeys.add("ccTypes", PropsKeys.SHOPPING_CREDIT_CARD_TYPES);
298                    fallbackKeys.add("currencyId", PropsKeys.SHOPPING_CURRENCY_ID);
299                    fallbackKeys.add(
300                            "emailFromAddress", PropsKeys.SHOPPING_EMAIL_FROM_ADDRESS,
301                            PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
302                    fallbackKeys.add(
303                            "emailFromName", PropsKeys.SHOPPING_EMAIL_FROM_NAME,
304                            PropsKeys.ADMIN_EMAIL_FROM_NAME);
305                    fallbackKeys.add(
306                            "emailOrderConfirmationBody",
307                            PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY);
308                    fallbackKeys.add(
309                            "emailOrderConfirmationEnabled",
310                            PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED);
311                    fallbackKeys.add(
312                            "emailOrderConfirmationSubject",
313                            PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT);
314                    fallbackKeys.add(
315                            "emailOrderShippingBody",
316                            PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_BODY);
317                    fallbackKeys.add(
318                            "emailOrderShippingEnabled",
319                            PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED);
320                    fallbackKeys.add(
321                            "emailOrderShippingSubject",
322                            PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT);
323                    fallbackKeys.add("insurance", PropsKeys.SHOPPING_INSURANCE);
324                    fallbackKeys.add(
325                            "insuranceFormula", PropsKeys.SHOPPING_INSURANCE_FORMULA);
326                    fallbackKeys.add("minOrder", PropsKeys.SHOPPING_MIN_ORDER);
327                    fallbackKeys.add(
328                            "paypalEmailAddress", PropsKeys.SHOPPING_PAYPAL_EMAIL_ADDRESS);
329                    fallbackKeys.add("shipping", PropsKeys.SHOPPING_SHIPPING);
330                    fallbackKeys.add(
331                            "shippingFormula", PropsKeys.SHOPPING_SHIPPING_FORMULA);
332                    fallbackKeys.add("taxRate", PropsKeys.SHOPPING_TAX_RATE);
333                    fallbackKeys.add("taxState", PropsKeys.SHOPPING_TAX_STATE);
334    
335                    return fallbackKeys;
336            }
337    
338            private static final String[] _MULTI_VALUED_KEYS = {
339                    "ccTypes", "insurance", "shipping"
340            };
341    
342            private static final ResourceManager _resourceManager =
343                    new ClassLoaderResourceManager(ShoppingSettings.class.getClassLoader());
344    
345            static {
346                    SettingsFactory settingsFactory =
347                            SettingsFactoryUtil.getSettingsFactory();
348    
349                    settingsFactory.registerSettingsMetadata(
350                            ShoppingConstants.SERVICE_NAME, _getFallbackKeys(),
351                            _MULTI_VALUED_KEYS, _resourceManager);
352            }
353    
354            private TypedSettings _typedSettings;
355    
356    }