1
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.PropsKeys;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
32 import com.liferay.portal.util.ContentUtil;
33 import com.liferay.portal.util.PortletKeys;
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
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 _preferences.getValue("paypal-email-address", StringPool.BLANK);
109 }
110
111 public void setPayPalEmailAddress(String payPalEmailAddress)
112 throws ReadOnlyException {
113
114 _preferences.setValue("paypal-email-address", payPalEmailAddress);
115 }
116
117 public boolean usePayPal() {
118 return Validator.isNotNull(getPayPalEmailAddress());
119 }
120
121 public String getCurrencyId() {
122 return _preferences.getValue("currency-id", "USD");
123 }
124
125 public void setCurrencyId(String currencyId) throws ReadOnlyException {
126 _preferences.setValue("currency-id", currencyId);
127 }
128
129 public String[] getCcTypes() {
130 String ccTypes = _preferences.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 _preferences.setValue("cc-types", CC_NONE);
144 }
145 else {
146 _preferences.setValue("cc-types", StringUtil.merge(ccTypes));
147 }
148 }
149
150 public String getTaxState() {
151 return _preferences.getValue("tax-state", "CA");
152 }
153
154 public void setTaxState(String taxState) throws ReadOnlyException {
155 _preferences.setValue("tax-state", taxState);
156 }
157
158 public double getTaxRate() {
159 return GetterUtil.getDouble(_preferences.getValue(
160 "tax-rate", StringPool.BLANK));
161 }
162
163 public void setTaxRate(double taxRate) throws ReadOnlyException {
164 _preferences.setValue("tax-rate", String.valueOf(taxRate));
165 }
166
167 public String getShippingFormula() {
168 return _preferences.getValue("shipping-formula", "flat");
169 }
170
171 public void setShippingFormula(String shippingFormula)
172 throws ReadOnlyException {
173
174 _preferences.setValue("shipping-formula", shippingFormula);
175 }
176
177 public String[] getShipping() {
178 String value = _preferences.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 _preferences.setValue("shipping", StringUtil.merge(shipping));
190 }
191
192 public String[][] getAlternativeShipping() {
193 String value = _preferences.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 _preferences.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 _preferences.getValue("insurance-formula", "flat");
260 }
261
262 public void setInsuranceFormula(String insuranceFormula)
263 throws ReadOnlyException {
264
265 _preferences.setValue("insurance-formula", insuranceFormula);
266 }
267
268 public String[] getInsurance() {
269 String value = _preferences.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 _preferences.setValue("insurance", StringUtil.merge(insurance));
281 }
282
283 public double getMinOrder() {
284 return GetterUtil.getDouble(_preferences.getValue(
285 "min-order", StringPool.BLANK));
286 }
287
288 public void setMinOrder(double minOrder) throws ReadOnlyException {
289 _preferences.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 _preferences.getValue("email-from-address", emailFromAddress);
297 }
298
299 public void setEmailFromAddress(String emailFromAddress)
300 throws ReadOnlyException {
301
302 _preferences.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 _preferences.getValue("email-from-name", emailFromName);
310 }
311
312 public void setEmailFromName(String emailFromName)
313 throws ReadOnlyException {
314
315 _preferences.setValue("email-from-name", emailFromName);
316 }
317
318 public boolean getEmailOrderConfirmationEnabled() {
319 String emailOrderConfirmationEnabled = _preferences.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 _preferences.setValue(
336 "email-order-confirmation-enabled",
337 String.valueOf(emailOrderConfirmationEnabled));
338 }
339
340 public String getEmailOrderConfirmationBody() {
341 String emailOrderConfirmationBody = _preferences.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 _preferences.setValue(
357 "email-order-confirmation-body", emailOrderConfirmationBody);
358 }
359
360 public String getEmailOrderConfirmationSubject() {
361 String emailOrderConfirmationSubject = _preferences.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 _preferences.setValue(
378 "email-order-confirmation-subject", emailOrderConfirmationSubject);
379 }
380
381 public boolean getEmailOrderShippingEnabled() {
382 String emailOrderShippingEnabled = _preferences.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 _preferences.setValue(
398 "email-order-shipping-enabled",
399 String.valueOf(emailOrderShippingEnabled));
400 }
401
402 public String getEmailOrderShippingBody() {
403 String emailOrderShippingBody = _preferences.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 _preferences.setValue(
419 "email-order-shipping-body", emailOrderShippingBody);
420 }
421
422 public String getEmailOrderShippingSubject() {
423 String emailOrderShippingSubject = _preferences.getValue(
424 "email-order-shipping-subject", StringPool.BLANK);
425
426 if (Validator.isNotNull(emailOrderShippingSubject)) {
427 return emailOrderShippingSubject;
428 }
429 else {
430 return ContentUtil.get(PropsUtil.get(
431 PropsKeys.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
432 }
433 }
434
435 public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
436 throws ReadOnlyException {
437
438 _preferences.setValue(
439 "email-order-shipping-subject", emailOrderShippingSubject);
440 }
441
442 public void store() throws IOException, ValidatorException {
443 _preferences.store();
444 }
445
446 protected ShoppingPreferences(long companyId, long groupId)
447 throws SystemException {
448
449 long ownerId = groupId;
450 int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
451 long plid = PortletKeys.PREFS_PLID_SHARED;
452 String portletId = PortletKeys.SHOPPING;
453
454 _preferences = PortletPreferencesLocalServiceUtil.getPreferences(
455 companyId, ownerId, ownerType, plid, portletId);
456 }
457
458 private PortletPreferences _preferences;
459
460 }