1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.util;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
28  import com.liferay.portal.util.ContentUtil;
29  import com.liferay.portal.util.PortletKeys;
30  import com.liferay.portal.util.PropsKeys;
31  import com.liferay.portal.util.PropsUtil;
32  
33  import java.io.IOException;
34  
35  import java.util.Currency;
36  import java.util.Locale;
37  import java.util.Set;
38  import java.util.TreeSet;
39  
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.ReadOnlyException;
42  import javax.portlet.ValidatorException;
43  
44  /**
45   * <a href="ShoppingPreferences.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ShoppingPreferences {
51  
52      public static final String CC_NONE = "none";
53  
54      public static final String[] CC_TYPES =
55          new String[] {"visa", "mastercard", "discover", "amex"};
56  
57      public static final String[] CURRENCY_IDS;
58  
59      static {
60          String[] ids = null;
61  
62          try {
63              Set<String> set = new TreeSet<String>();
64  
65              Locale[] locales = Locale.getAvailableLocales();
66  
67              for (int i = 0; i < locales.length; i++) {
68                  Locale locale = locales[i];
69  
70                  if (locale.getCountry().length() == 2) {
71                      Currency currency = Currency.getInstance(locale);
72  
73                      String currencyId = currency.getCurrencyCode();
74  
75                      set.add(currencyId);
76                  }
77              }
78  
79              ids = set.toArray(new String[set.size()]);
80          }
81          catch (Exception e) {
82              ids = new String[] {"USD", "CAD", "EUR", "GBP", "JPY"};
83          }
84          finally {
85              CURRENCY_IDS = ids;
86          }
87      }
88  
89      public static final double[] SHIPPING_RANGE = {
90          0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
91          Double.POSITIVE_INFINITY
92      };
93  
94      public static final double[] INSURANCE_RANGE = {
95          0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
96          Double.POSITIVE_INFINITY
97      };
98  
99      public static ShoppingPreferences getInstance(long companyId, long groupId)
100         throws SystemException {
101 
102         return new ShoppingPreferences(companyId, groupId);
103     }
104 
105     public String getPayPalEmailAddress() {
106         return _prefs.getValue("paypal-email-address", StringPool.BLANK);
107     }
108 
109     public void setPayPalEmailAddress(String payPalEmailAddress)
110         throws ReadOnlyException {
111 
112         _prefs.setValue("paypal-email-address", payPalEmailAddress);
113     }
114 
115     public boolean usePayPal() {
116         return Validator.isNotNull(getPayPalEmailAddress());
117     }
118 
119     public String getCurrencyId() {
120         return _prefs.getValue("currency-id", "USD");
121     }
122 
123     public void setCurrencyId(String currencyId) throws ReadOnlyException {
124         _prefs.setValue("currency-id", currencyId);
125     }
126 
127     public String[] getCcTypes() {
128         String ccTypes = _prefs.getValue(
129             "cc-types", StringUtil.merge(CC_TYPES));
130 
131         if (ccTypes.equals(CC_NONE)) {
132             return new String[0];
133         }
134         else {
135             return StringUtil.split(ccTypes);
136         }
137     }
138 
139     public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
140         if (ccTypes.length == 0) {
141             _prefs.setValue("cc-types", CC_NONE);
142         }
143         else {
144             _prefs.setValue("cc-types", StringUtil.merge(ccTypes));
145         }
146     }
147 
148     public String getTaxState() {
149         return _prefs.getValue("tax-state", "CA");
150     }
151 
152     public void setTaxState(String taxState) throws ReadOnlyException {
153         _prefs.setValue("tax-state", taxState);
154     }
155 
156     public double getTaxRate() {
157         return GetterUtil.getDouble(_prefs.getValue(
158             "tax-rate", StringPool.BLANK));
159     }
160 
161     public void setTaxRate(double taxRate) throws ReadOnlyException {
162         _prefs.setValue("tax-rate", String.valueOf(taxRate));
163     }
164 
165     public String getShippingFormula() {
166         return _prefs.getValue("shipping-formula", "flat");
167     }
168 
169     public void setShippingFormula(String shippingFormula)
170         throws ReadOnlyException {
171 
172         _prefs.setValue("shipping-formula", shippingFormula);
173     }
174 
175     public String[] getShipping() {
176         String value = _prefs.getValue("shipping", null);
177 
178         if (value == null) {
179             return new String[5];
180         }
181         else {
182             return StringUtil.split(value);
183         }
184     }
185 
186     public void setShipping(String[] shipping) throws ReadOnlyException {
187         _prefs.setValue("shipping", StringUtil.merge(shipping));
188     }
189 
190     public String[][] getAlternativeShipping() {
191         String value = _prefs.getValue("alternative-shipping", null);
192 
193         if (value == null) {
194             return new String[0][0];
195         }
196         else {
197             String[] array =
198                 StringUtil.split("alternative-shipping", "[$_ARRAY_$]");
199 
200             String[][] alternativeShipping = new String[array.length][0];
201 
202             for (int i = 0; i < array.length; i++) {
203                 alternativeShipping[i] = StringUtil.split(array[i]);
204             }
205 
206             return alternativeShipping;
207         }
208     }
209 
210     public void setAlternativeShipping(String[][] alternativeShipping)
211         throws ReadOnlyException {
212 
213         StringBuilder sb = new StringBuilder();
214 
215         for (int i = 0; i < alternativeShipping.length; i++) {
216             sb.append(StringUtil.merge(alternativeShipping[i]));
217 
218             if ((i + 1) < alternativeShipping.length) {
219                 sb.append("[$_ARRAY_$]");
220             }
221         }
222 
223         _prefs.setValue("alternative-shipping", sb.toString());
224     }
225 
226     public boolean useAlternativeShipping() {
227         String[][] alternativeShipping = getAlternativeShipping();
228 
229         try {
230             for (int i = 0; i < 10; i++) {
231                 if (Validator.isNotNull(alternativeShipping[0][i]) &&
232                     Validator.isNotNull(alternativeShipping[1][i])) {
233 
234                     return true;
235                 }
236             }
237         }
238         catch (Exception e) {
239         }
240 
241         return false;
242     }
243 
244     public String getAlternativeShippingName(int altShipping) {
245         String altShippingName = StringPool.BLANK;
246 
247         try {
248             altShippingName = getAlternativeShipping()[0][altShipping];
249         }
250         catch (Exception e) {
251         }
252 
253         return altShippingName;
254     }
255 
256     public String getInsuranceFormula() {
257         return _prefs.getValue("insurance-formula", "flat");
258     }
259 
260     public void setInsuranceFormula(String insuranceFormula)
261         throws ReadOnlyException {
262 
263         _prefs.setValue("insurance-formula", insuranceFormula);
264     }
265 
266     public String[] getInsurance() {
267         String value = _prefs.getValue("insurance", null);
268 
269         if (value == null) {
270             return new String[5];
271         }
272         else {
273             return StringUtil.split(value);
274         }
275     }
276 
277     public void setInsurance(String[] insurance) throws ReadOnlyException {
278         _prefs.setValue("insurance", StringUtil.merge(insurance));
279     }
280 
281     public double getMinOrder() {
282         return GetterUtil.getDouble(_prefs.getValue(
283             "min-order", StringPool.BLANK));
284     }
285 
286     public void setMinOrder(double minOrder) throws ReadOnlyException {
287         _prefs.setValue("min-order", String.valueOf(minOrder));
288     }
289 
290     public String getEmailFromAddress() {
291         String emailFromAddress = PropsUtil.get(
292             PropsKeys.SHOPPING_EMAIL_FROM_ADDRESS);
293 
294         return _prefs.getValue("email-from-address", emailFromAddress);
295     }
296 
297     public void setEmailFromAddress(String emailFromAddress)
298         throws ReadOnlyException {
299 
300         _prefs.setValue("email-from-address", emailFromAddress);
301     }
302 
303     public String getEmailFromName() {
304         String emailFromName = PropsUtil.get(
305             PropsKeys.SHOPPING_EMAIL_FROM_NAME);
306 
307         return _prefs.getValue("email-from-name", emailFromName);
308     }
309 
310     public void setEmailFromName(String emailFromName)
311         throws ReadOnlyException {
312 
313         _prefs.setValue("email-from-name", emailFromName);
314     }
315 
316     public boolean getEmailOrderConfirmationEnabled() {
317         String emailOrderConfirmationEnabled = _prefs.getValue(
318             "email-order-confirmation-enabled", StringPool.BLANK);
319 
320         if (Validator.isNotNull(emailOrderConfirmationEnabled)) {
321             return GetterUtil.getBoolean(emailOrderConfirmationEnabled);
322         }
323         else {
324             return GetterUtil.getBoolean(PropsUtil.get(
325                 PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED));
326         }
327     }
328 
329     public void setEmailOrderConfirmationEnabled(
330             boolean emailOrderConfirmationEnabled)
331         throws ReadOnlyException {
332 
333         _prefs.setValue(
334             "email-order-confirmation-enabled",
335             String.valueOf(emailOrderConfirmationEnabled));
336     }
337 
338     public String getEmailOrderConfirmationBody() {
339         String emailOrderConfirmationBody = _prefs.getValue(
340             "email-order-confirmation-body", StringPool.BLANK);
341 
342         if (Validator.isNotNull(emailOrderConfirmationBody)) {
343             return emailOrderConfirmationBody;
344         }
345         else {
346             return ContentUtil.get(PropsUtil.get(
347                 PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY));
348         }
349     }
350 
351     public void setEmailOrderConfirmationBody(String emailOrderConfirmationBody)
352         throws ReadOnlyException {
353 
354         _prefs.setValue(
355             "email-order-confirmation-body", emailOrderConfirmationBody);
356     }
357 
358     public String getEmailOrderConfirmationSubject() {
359         String emailOrderConfirmationSubject = _prefs.getValue(
360             "email-order-confirmation-subject", StringPool.BLANK);
361 
362         if (Validator.isNotNull(emailOrderConfirmationSubject)) {
363             return emailOrderConfirmationSubject;
364         }
365         else {
366             return ContentUtil.get(PropsUtil.get(
367                 PropsKeys.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT));
368         }
369     }
370 
371     public void setEmailOrderConfirmationSubject(
372             String emailOrderConfirmationSubject)
373         throws ReadOnlyException {
374 
375         _prefs.setValue(
376             "email-order-confirmation-subject", emailOrderConfirmationSubject);
377     }
378 
379     public boolean getEmailOrderShippingEnabled() {
380         String emailOrderShippingEnabled = _prefs.getValue(
381             "email-order-shipping-enabled", StringPool.BLANK);
382 
383         if (Validator.isNotNull(emailOrderShippingEnabled)) {
384             return GetterUtil.getBoolean(emailOrderShippingEnabled);
385         }
386         else {
387             return GetterUtil.getBoolean(PropsUtil.get(
388                 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED));
389         }
390     }
391 
392     public void setEmailOrderShippingEnabled(boolean emailOrderShippingEnabled)
393         throws ReadOnlyException {
394 
395         _prefs.setValue(
396             "email-order-shipping-enabled",
397             String.valueOf(emailOrderShippingEnabled));
398     }
399 
400     public String getEmailOrderShippingBody() {
401         String emailOrderShippingBody = _prefs.getValue(
402             "email-order-shipping-body", StringPool.BLANK);
403 
404         if (Validator.isNotNull(emailOrderShippingBody)) {
405             return emailOrderShippingBody;
406         }
407         else {
408             return ContentUtil.get(PropsUtil.get(
409                 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_BODY));
410         }
411     }
412 
413     public void setEmailOrderShippingBody(String emailOrderShippingBody)
414         throws ReadOnlyException {
415 
416         _prefs.setValue("email-order-shipping-body", emailOrderShippingBody);
417     }
418 
419     public String getEmailOrderShippingSubject() {
420         String emailOrderShippingSubject = _prefs.getValue(
421             "email-order-shipping-subject", StringPool.BLANK);
422 
423         if (Validator.isNotNull(emailOrderShippingSubject)) {
424             return emailOrderShippingSubject;
425         }
426         else {
427             return ContentUtil.get(PropsUtil.get(
428                 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
429         }
430     }
431 
432     public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
433         throws ReadOnlyException {
434 
435         _prefs.setValue(
436             "email-order-shipping-subject", emailOrderShippingSubject);
437     }
438 
439     public void store() throws IOException, ValidatorException {
440         _prefs.store();
441     }
442 
443     protected ShoppingPreferences(long companyId, long groupId)
444         throws SystemException {
445 
446         long ownerId = groupId;
447         int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
448         long plid = PortletKeys.PREFS_PLID_SHARED;
449         String portletId = PortletKeys.SHOPPING;
450 
451         _prefs = PortletPreferencesLocalServiceUtil.getPreferences(
452             companyId, ownerId, ownerType, plid, portletId);
453     }
454 
455     private PortletPreferences _prefs;
456 
457 }