1
14
15 package com.liferay.portlet.shopping.util;
16
17 import com.liferay.portal.kernel.exception.SystemException;
18 import com.liferay.portal.kernel.util.GetterUtil;
19 import com.liferay.portal.kernel.util.PropsKeys;
20 import com.liferay.portal.kernel.util.StringBundler;
21 import com.liferay.portal.kernel.util.StringPool;
22 import com.liferay.portal.kernel.util.StringUtil;
23 import com.liferay.portal.kernel.util.Validator;
24 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
25 import com.liferay.portal.util.ContentUtil;
26 import com.liferay.portal.util.PortletKeys;
27 import com.liferay.portal.util.PropsUtil;
28
29 import java.io.IOException;
30
31 import java.util.Currency;
32 import java.util.Locale;
33 import java.util.Set;
34 import java.util.TreeSet;
35
36 import javax.portlet.PortletPreferences;
37 import javax.portlet.ReadOnlyException;
38 import javax.portlet.ValidatorException;
39
40
45 public class ShoppingPreferences {
46
47 public static final String CC_NONE = "none";
48
49 public static final String[] CC_TYPES =
50 new String[] {"visa", "mastercard", "discover", "amex"};
51
52 public static final String[] CURRENCY_IDS;
53
54 static {
55 String[] ids = null;
56
57 try {
58 Set<String> set = new TreeSet<String>();
59
60 Locale[] locales = Locale.getAvailableLocales();
61
62 for (int i = 0; i < locales.length; i++) {
63 Locale locale = locales[i];
64
65 if (locale.getCountry().length() == 2) {
66 Currency currency = Currency.getInstance(locale);
67
68 String currencyId = currency.getCurrencyCode();
69
70 set.add(currencyId);
71 }
72 }
73
74 ids = set.toArray(new String[set.size()]);
75 }
76 catch (Exception e) {
77 ids = new String[] {"USD", "CAD", "EUR", "GBP", "JPY"};
78 }
79 finally {
80 CURRENCY_IDS = ids;
81 }
82 }
83
84 public static final double[] SHIPPING_RANGE = {
85 0.01, 9.99, 10.00, 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
86 Double.POSITIVE_INFINITY
87 };
88
89 public static final double[] INSURANCE_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 ShoppingPreferences getInstance(long companyId, long groupId)
95 throws SystemException {
96
97 return new ShoppingPreferences(companyId, groupId);
98 }
99
100 public String getPayPalEmailAddress() {
101 return _preferences.getValue("paypal-email-address", StringPool.BLANK);
102 }
103
104 public void setPayPalEmailAddress(String payPalEmailAddress)
105 throws ReadOnlyException {
106
107 _preferences.setValue("paypal-email-address", payPalEmailAddress);
108 }
109
110 public boolean usePayPal() {
111 return Validator.isNotNull(getPayPalEmailAddress());
112 }
113
114 public String getCurrencyId() {
115 return _preferences.getValue("currency-id", "USD");
116 }
117
118 public void setCurrencyId(String currencyId) throws ReadOnlyException {
119 _preferences.setValue("currency-id", currencyId);
120 }
121
122 public String[] getCcTypes() {
123 String ccTypes = _preferences.getValue(
124 "cc-types", StringUtil.merge(CC_TYPES));
125
126 if (ccTypes.equals(CC_NONE)) {
127 return new String[0];
128 }
129 else {
130 return StringUtil.split(ccTypes);
131 }
132 }
133
134 public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
135 if (ccTypes.length == 0) {
136 _preferences.setValue("cc-types", CC_NONE);
137 }
138 else {
139 _preferences.setValue("cc-types", StringUtil.merge(ccTypes));
140 }
141 }
142
143 public String getTaxState() {
144 return _preferences.getValue("tax-state", "CA");
145 }
146
147 public void setTaxState(String taxState) throws ReadOnlyException {
148 _preferences.setValue("tax-state", taxState);
149 }
150
151 public double getTaxRate() {
152 return GetterUtil.getDouble(_preferences.getValue(
153 "tax-rate", StringPool.BLANK));
154 }
155
156 public void setTaxRate(double taxRate) throws ReadOnlyException {
157 _preferences.setValue("tax-rate", String.valueOf(taxRate));
158 }
159
160 public String getShippingFormula() {
161 return _preferences.getValue("shipping-formula", "flat");
162 }
163
164 public void setShippingFormula(String shippingFormula)
165 throws ReadOnlyException {
166
167 _preferences.setValue("shipping-formula", shippingFormula);
168 }
169
170 public String[] getShipping() {
171 String value = _preferences.getValue("shipping", null);
172
173 if (value == null) {
174 return new String[5];
175 }
176 else {
177 return StringUtil.split(value);
178 }
179 }
180
181 public void setShipping(String[] shipping) throws ReadOnlyException {
182 _preferences.setValue("shipping", StringUtil.merge(shipping));
183 }
184
185 public String[][] getAlternativeShipping() {
186 String value = _preferences.getValue("alternative-shipping", null);
187
188 if (value == null) {
189 return new String[0][0];
190 }
191 else {
192 String[] array =
193 StringUtil.split("alternative-shipping", "[$_ARRAY_$]");
194
195 String[][] alternativeShipping = new String[array.length][0];
196
197 for (int i = 0; i < array.length; i++) {
198 alternativeShipping[i] = StringUtil.split(array[i]);
199 }
200
201 return alternativeShipping;
202 }
203 }
204
205 public void setAlternativeShipping(String[][] alternativeShipping)
206 throws ReadOnlyException {
207
208 if (alternativeShipping.length == 0) {
209 _preferences.setValue("alternative-shipping", StringPool.BLANK);
210 }
211
212 StringBundler sb = new StringBundler(
213 alternativeShipping.length * 2 - 1);
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 _preferences.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 _preferences.getValue("insurance-formula", "flat");
258 }
259
260 public void setInsuranceFormula(String insuranceFormula)
261 throws ReadOnlyException {
262
263 _preferences.setValue("insurance-formula", insuranceFormula);
264 }
265
266 public String[] getInsurance() {
267 String value = _preferences.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 _preferences.setValue("insurance", StringUtil.merge(insurance));
279 }
280
281 public double getMinOrder() {
282 return GetterUtil.getDouble(_preferences.getValue(
283 "min-order", StringPool.BLANK));
284 }
285
286 public void setMinOrder(double minOrder) throws ReadOnlyException {
287 _preferences.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 _preferences.getValue("email-from-address", emailFromAddress);
295 }
296
297 public void setEmailFromAddress(String emailFromAddress)
298 throws ReadOnlyException {
299
300 _preferences.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 _preferences.getValue("email-from-name", emailFromName);
308 }
309
310 public void setEmailFromName(String emailFromName)
311 throws ReadOnlyException {
312
313 _preferences.setValue("email-from-name", emailFromName);
314 }
315
316 public boolean getEmailOrderConfirmationEnabled() {
317 String emailOrderConfirmationEnabled = _preferences.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 _preferences.setValue(
334 "email-order-confirmation-enabled",
335 String.valueOf(emailOrderConfirmationEnabled));
336 }
337
338 public String getEmailOrderConfirmationBody() {
339 String emailOrderConfirmationBody = _preferences.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 _preferences.setValue(
355 "email-order-confirmation-body", emailOrderConfirmationBody);
356 }
357
358 public String getEmailOrderConfirmationSubject() {
359 String emailOrderConfirmationSubject = _preferences.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 _preferences.setValue(
376 "email-order-confirmation-subject", emailOrderConfirmationSubject);
377 }
378
379 public boolean getEmailOrderShippingEnabled() {
380 String emailOrderShippingEnabled = _preferences.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 _preferences.setValue(
396 "email-order-shipping-enabled",
397 String.valueOf(emailOrderShippingEnabled));
398 }
399
400 public String getEmailOrderShippingBody() {
401 String emailOrderShippingBody = _preferences.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 _preferences.setValue(
417 "email-order-shipping-body", emailOrderShippingBody);
418 }
419
420 public String getEmailOrderShippingSubject() {
421 String emailOrderShippingSubject = _preferences.getValue(
422 "email-order-shipping-subject", StringPool.BLANK);
423
424 if (Validator.isNotNull(emailOrderShippingSubject)) {
425 return emailOrderShippingSubject;
426 }
427 else {
428 return ContentUtil.get(PropsUtil.get(
429 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
430 }
431 }
432
433 public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
434 throws ReadOnlyException {
435
436 _preferences.setValue(
437 "email-order-shipping-subject", emailOrderShippingSubject);
438 }
439
440 public void store() throws IOException, ValidatorException {
441 _preferences.store();
442 }
443
444 protected ShoppingPreferences(long companyId, long groupId)
445 throws SystemException {
446
447 long ownerId = groupId;
448 int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
449 long plid = PortletKeys.PREFS_PLID_SHARED;
450 String portletId = PortletKeys.SHOPPING;
451
452 _preferences = PortletPreferencesLocalServiceUtil.getPreferences(
453 companyId, ownerId, ownerType, plid, portletId);
454 }
455
456 private PortletPreferences _preferences;
457
458 }