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