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