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