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