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    
108                    String[] array = StringUtil.split("alternativeShipping", "[$_ARRAY_$]");
109    
110                    String[][] alternativeShipping = new String[array.length][0];
111    
112                    for (int i = 0; i < array.length; i++) {
113                            alternativeShipping[i] = StringUtil.split(array[i]);
114                    }
115    
116                    return alternativeShipping;
117            }
118    
119            public String getAlternativeShippingName(int altShipping) {
120                    String altShippingName = StringPool.BLANK;
121    
122                    try {
123                            altShippingName = getAlternativeShipping()[0][altShipping];
124                    }
125                    catch (Exception e) {
126                    }
127    
128                    return altShippingName;
129            }
130    
131            public String[] getCcTypes() {
132                    String ccTypes = _portletPreferences.getValue(
133                            "ccTypes", StringUtil.merge(CC_TYPES));
134    
135                    if (ccTypes.equals(CC_NONE)) {
136                            return new String[0];
137                    }
138                    else {
139                            return StringUtil.split(ccTypes);
140                    }
141            }
142    
143            public String getCurrencyId() {
144                    return _portletPreferences.getValue("currencyId", "USD");
145            }
146    
147            public String getEmailFromAddress(long companyId) throws SystemException {
148                    return PortalUtil.getEmailFromAddress(
149                            _portletPreferences, companyId,
150                            PropsValues.SHOPPING_EMAIL_FROM_ADDRESS);
151            }
152    
153            public String getEmailFromName(long companyId) throws SystemException {
154                    return PortalUtil.getEmailFromName(
155                            _portletPreferences, companyId,
156                            PropsValues.SHOPPING_EMAIL_FROM_NAME);
157            }
158    
159            public String getEmailOrderConfirmationBody() {
160                    String emailOrderConfirmationBody = _portletPreferences.getValue(
161                            "emailOrderConfirmationBody", StringPool.BLANK);
162    
163                    if (Validator.isNotNull(emailOrderConfirmationBody)) {
164                            return emailOrderConfirmationBody;
165                    }
166                    else {
167                            return ContentUtil.get(
168                                    PropsUtil.get(
169                                            PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY));
170                    }
171            }
172    
173            public boolean getEmailOrderConfirmationEnabled() {
174                    String emailOrderConfirmationEnabled = _portletPreferences.getValue(
175                            "emailOrderConfirmationEnabled", StringPool.BLANK);
176    
177                    if (Validator.isNotNull(emailOrderConfirmationEnabled)) {
178                            return GetterUtil.getBoolean(emailOrderConfirmationEnabled);
179                    }
180                    else {
181                            return GetterUtil.getBoolean(
182                                    PropsUtil.get(
183                                            PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED));
184                    }
185            }
186    
187            public String getEmailOrderConfirmationSubject() {
188                    String emailOrderConfirmationSubject = _portletPreferences.getValue(
189                            "emailOrderConfirmationSubject", StringPool.BLANK);
190    
191                    if (Validator.isNotNull(emailOrderConfirmationSubject)) {
192                            return emailOrderConfirmationSubject;
193                    }
194                    else {
195                            return ContentUtil.get(
196                                    PropsUtil.get(
197                                            PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT));
198                    }
199            }
200    
201            public String getEmailOrderShippingBody() {
202                    String emailOrderShippingBody = _portletPreferences.getValue(
203                            "emailOrderShippingBody", StringPool.BLANK);
204    
205                    if (Validator.isNotNull(emailOrderShippingBody)) {
206                            return emailOrderShippingBody;
207                    }
208                    else {
209                            return ContentUtil.get(
210                                    PropsUtil.get(PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_BODY));
211                    }
212            }
213    
214            public boolean getEmailOrderShippingEnabled() {
215                    String emailOrderShippingEnabled = _portletPreferences.getValue(
216                            "emailOrderShippingEnabled", StringPool.BLANK);
217    
218                    if (Validator.isNotNull(emailOrderShippingEnabled)) {
219                            return GetterUtil.getBoolean(emailOrderShippingEnabled);
220                    }
221                    else {
222                            return GetterUtil.getBoolean(
223                                    PropsUtil.get(PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED));
224                    }
225            }
226    
227            public String getEmailOrderShippingSubject() {
228                    String emailOrderShippingSubject = _portletPreferences.getValue(
229                            "emailOrderShippingSubject", StringPool.BLANK);
230    
231                    if (Validator.isNotNull(emailOrderShippingSubject)) {
232                            return emailOrderShippingSubject;
233                    }
234                    else {
235                            return ContentUtil.get(
236                                    PropsUtil.get(PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
237                    }
238            }
239    
240            public String[] getInsurance() {
241                    String value = _portletPreferences.getValue("insurance", null);
242    
243                    if (value == null) {
244                            return new String[5];
245                    }
246                    else {
247                            return StringUtil.split(value);
248                    }
249            }
250    
251            public String getInsuranceFormula() {
252                    return _portletPreferences.getValue("insuranceFormula", "flat");
253            }
254    
255            public double getMinOrder() {
256                    return GetterUtil.getDouble(
257                            _portletPreferences.getValue("minOrder", StringPool.BLANK));
258            }
259    
260            public String getPayPalEmailAddress() {
261                    return _portletPreferences.getValue(
262                            "paypalEmailAddress", StringPool.BLANK);
263            }
264    
265            public String[] getShipping() {
266                    String value = _portletPreferences.getValue("shipping", null);
267    
268                    if (value == null) {
269                            return new String[5];
270                    }
271                    else {
272                            return StringUtil.split(value);
273                    }
274            }
275    
276            public String getShippingFormula() {
277                    return _portletPreferences.getValue("shippingFormula", "flat");
278            }
279    
280            public double getTaxRate() {
281                    return GetterUtil.getDouble(
282                            _portletPreferences.getValue("taxRate", StringPool.BLANK));
283            }
284    
285            public String getTaxState() {
286                    return _portletPreferences.getValue("taxState", "CA");
287            }
288    
289            public void setAlternativeShipping(String[][] alternativeShipping)
290                    throws ReadOnlyException {
291    
292                    if (alternativeShipping.length == 0) {
293                            _portletPreferences.setValue(
294                                    "alternativeShipping", StringPool.BLANK);
295                    }
296    
297                    StringBundler sb = new StringBundler(
298                            alternativeShipping.length * 2 - 1);
299    
300                    for (int i = 0; i < alternativeShipping.length; i++) {
301                            sb.append(StringUtil.merge(alternativeShipping[i]));
302    
303                            if ((i + 1) < alternativeShipping.length) {
304                                    sb.append("[$_ARRAY_$]");
305                            }
306                    }
307    
308                    _portletPreferences.setValue("alternativeShipping", sb.toString());
309            }
310    
311            public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
312                    if (ccTypes.length == 0) {
313                            _portletPreferences.setValue("ccTypes", CC_NONE);
314                    }
315                    else {
316                            _portletPreferences.setValue("ccTypes", StringUtil.merge(ccTypes));
317                    }
318            }
319    
320            public void setCurrencyId(String currencyId) throws ReadOnlyException {
321                    _portletPreferences.setValue("currencyId", currencyId);
322            }
323    
324            public void setEmailFromAddress(String emailFromAddress)
325                    throws ReadOnlyException {
326    
327                    _portletPreferences.setValue("emailFromAddress", emailFromAddress);
328            }
329    
330            public void setEmailFromName(String emailFromName)
331                    throws ReadOnlyException {
332    
333                    _portletPreferences.setValue("emailFromName", emailFromName);
334            }
335    
336            public void setEmailOrderConfirmationBody(String emailOrderConfirmationBody)
337                    throws ReadOnlyException {
338    
339                    _portletPreferences.setValue(
340                            "emailOrderConfirmationBody", emailOrderConfirmationBody);
341            }
342    
343            public void setEmailOrderConfirmationEnabled(
344                            boolean emailOrderConfirmationEnabled)
345                    throws ReadOnlyException {
346    
347                    _portletPreferences.setValue(
348                            "emailOrderConfirmationEnabled",
349                            String.valueOf(emailOrderConfirmationEnabled));
350            }
351    
352            public void setEmailOrderConfirmationSubject(
353                            String emailOrderConfirmationSubject)
354                    throws ReadOnlyException {
355    
356                    _portletPreferences.setValue(
357                            "emailOrderConfirmationSubject", emailOrderConfirmationSubject);
358            }
359    
360            public void setEmailOrderShippingBody(String emailOrderShippingBody)
361                    throws ReadOnlyException {
362    
363                    _portletPreferences.setValue(
364                            "emailOrderShippingBody", emailOrderShippingBody);
365            }
366    
367            public void setEmailOrderShippingEnabled(boolean emailOrderShippingEnabled)
368                    throws ReadOnlyException {
369    
370                    _portletPreferences.setValue(
371                            "emailOrderShippingEnabled",
372                            String.valueOf(emailOrderShippingEnabled));
373            }
374    
375            public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
376                    throws ReadOnlyException {
377    
378                    _portletPreferences.setValue(
379                            "emailOrderShippingSubject", emailOrderShippingSubject);
380            }
381    
382            public void setInsurance(String[] insurance) throws ReadOnlyException {
383                    _portletPreferences.setValue("insurance", StringUtil.merge(insurance));
384            }
385    
386            public void setInsuranceFormula(String insuranceFormula)
387                    throws ReadOnlyException {
388    
389                    _portletPreferences.setValue("insuranceFormula", insuranceFormula);
390            }
391    
392            public void setMinOrder(double minOrder) throws ReadOnlyException {
393                    _portletPreferences.setValue("minOrder", String.valueOf(minOrder));
394            }
395    
396            public void setPayPalEmailAddress(String payPalEmailAddress)
397                    throws ReadOnlyException {
398    
399                    _portletPreferences.setValue("paypalEmailAddress", payPalEmailAddress);
400            }
401    
402            public void setShipping(String[] shipping) throws ReadOnlyException {
403                    _portletPreferences.setValue("shipping", StringUtil.merge(shipping));
404            }
405    
406            public void setShippingFormula(String shippingFormula)
407                    throws ReadOnlyException {
408    
409                    _portletPreferences.setValue("shippingFormula", shippingFormula);
410            }
411    
412            public void setTaxRate(double taxRate) throws ReadOnlyException {
413                    _portletPreferences.setValue("taxRate", String.valueOf(taxRate));
414            }
415    
416            public void setTaxState(String taxState) throws ReadOnlyException {
417                    _portletPreferences.setValue("taxState", taxState);
418            }
419    
420            public void store() throws IOException, ValidatorException {
421                    _portletPreferences.store();
422            }
423    
424            public boolean useAlternativeShipping() {
425                    String[][] alternativeShipping = getAlternativeShipping();
426    
427                    try {
428                            for (int i = 0; i < 10; i++) {
429                                    if (Validator.isNotNull(alternativeShipping[0][i]) &&
430                                            Validator.isNotNull(alternativeShipping[1][i])) {
431    
432                                            return true;
433                                    }
434                            }
435                    }
436                    catch (Exception e) {
437                    }
438    
439                    return false;
440            }
441    
442            public boolean usePayPal() {
443                    return Validator.isNotNull(getPayPalEmailAddress());
444            }
445    
446            protected ShoppingPreferences(long companyId, long groupId)
447                    throws SystemException {
448    
449                    long ownerId = groupId;
450                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
451                    long plid = PortletKeys.PREFS_PLID_SHARED;
452                    String portletId = PortletKeys.SHOPPING;
453    
454                    _portletPreferences = PortletPreferencesLocalServiceUtil.getPreferences(
455                            companyId, ownerId, ownerType, plid, portletId);
456            }
457    
458            private PortletPreferences _portletPreferences;
459    
460    }