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