1
22
23 package com.liferay.portlet.shopping.util;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringMaker;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
33 import com.liferay.portal.util.ContentUtil;
34 import com.liferay.portal.util.PortletKeys;
35 import com.liferay.portal.util.PropsUtil;
36
37 import java.io.IOException;
38
39 import java.util.Currency;
40 import java.util.Locale;
41 import java.util.Set;
42 import java.util.TreeSet;
43
44 import javax.portlet.PortletPreferences;
45 import javax.portlet.ReadOnlyException;
46 import javax.portlet.ValidatorException;
47
48
54 public class ShoppingPreferences {
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 set = new TreeSet();
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 = (String[])set.toArray(new String[0]);
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 PortalException, 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 return StringUtil.split(_prefs.getValue(
131 "cc-types", StringUtil.merge(CC_TYPES)));
132 }
133
134 public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
135 _prefs.setValue("cc-types", StringUtil.merge(ccTypes));
136 }
137
138 public String getTaxState() {
139 return _prefs.getValue("tax-state", "CA");
140 }
141
142 public void setTaxState(String taxState) throws ReadOnlyException {
143 _prefs.setValue("tax-state", taxState);
144 }
145
146 public double getTaxRate() {
147 return GetterUtil.getDouble(_prefs.getValue(
148 "tax-rate", StringPool.BLANK));
149 }
150
151 public void setTaxRate(double taxRate) throws ReadOnlyException {
152 _prefs.setValue("tax-rate", String.valueOf(taxRate));
153 }
154
155 public String getShippingFormula() {
156 return _prefs.getValue("shipping-formula", "flat");
157 }
158
159 public void setShippingFormula(String shippingFormula)
160 throws ReadOnlyException {
161
162 _prefs.setValue("shipping-formula", shippingFormula);
163 }
164
165 public String[] getShipping() {
166 String value = _prefs.getValue("shipping", null);
167
168 if (value == null) {
169 return new String[5];
170 }
171 else {
172 return StringUtil.split(value);
173 }
174 }
175
176 public void setShipping(String[] shipping) throws ReadOnlyException {
177 _prefs.setValue("shipping", StringUtil.merge(shipping));
178 }
179
180 public String[][] getAlternativeShipping() {
181 String value = _prefs.getValue("alternative-shipping", null);
182
183 if (value == null) {
184 return new String[0][0];
185 }
186 else {
187 String[] array =
188 StringUtil.split("alternative-shipping", "[$_ARRAY_$]");
189
190 String[][] alternativeShipping = new String[array.length][0];
191
192 for (int i = 0; i < array.length; i++) {
193 alternativeShipping[i] = StringUtil.split(array[i]);
194 }
195
196 return alternativeShipping;
197 }
198 }
199
200 public void setAlternativeShipping(String[][] alternativeShipping)
201 throws ReadOnlyException {
202
203 StringMaker sm = new StringMaker();
204
205 for (int i = 0; i < alternativeShipping.length; i++) {
206 sm.append(StringUtil.merge(alternativeShipping[i]));
207
208 if ((i + 1) < alternativeShipping.length) {
209 sm.append("[$_ARRAY_$]");
210 }
211 }
212
213 _prefs.setValue("alternative-shipping", sm.toString());
214 }
215
216 public boolean useAlternativeShipping() {
217 String[][] alternativeShipping = getAlternativeShipping();
218
219 try {
220 for (int i = 0; i < 10; i++) {
221 if (Validator.isNotNull(alternativeShipping[0][i]) &&
222 Validator.isNotNull(alternativeShipping[1][i])) {
223
224 return true;
225 }
226 }
227 }
228 catch (Exception e) {
229 }
230
231 return false;
232 }
233
234 public String getAlternativeShippingName(int altShipping) {
235 String altShippingName = StringPool.BLANK;
236
237 try {
238 altShippingName = getAlternativeShipping()[0][altShipping];
239 }
240 catch (Exception e) {
241 }
242
243 return altShippingName;
244 }
245
246 public String getInsuranceFormula() {
247 return _prefs.getValue("insurance-formula", "flat");
248 }
249
250 public void setInsuranceFormula(String insuranceFormula)
251 throws ReadOnlyException {
252
253 _prefs.setValue("insurance-formula", insuranceFormula);
254 }
255
256 public String[] getInsurance() {
257 String value = _prefs.getValue("insurance", null);
258
259 if (value == null) {
260 return new String[5];
261 }
262 else {
263 return StringUtil.split(value);
264 }
265 }
266
267 public void setInsurance(String[] insurance) throws ReadOnlyException {
268 _prefs.setValue("insurance", StringUtil.merge(insurance));
269 }
270
271 public double getMinOrder() {
272 return GetterUtil.getDouble(_prefs.getValue(
273 "min-order", StringPool.BLANK));
274 }
275
276 public void setMinOrder(double minOrder) throws ReadOnlyException {
277 _prefs.setValue("min-order", String.valueOf(minOrder));
278 }
279
280 public String getEmailFromAddress() {
281 String emailFromAddress = PropsUtil.get(
282 PropsUtil.SHOPPING_EMAIL_FROM_ADDRESS);
283
284 return _prefs.getValue("email-from-address", emailFromAddress);
285 }
286
287 public void setEmailFromAddress(String emailFromAddress)
288 throws ReadOnlyException {
289
290 _prefs.setValue("email-from-address", emailFromAddress);
291 }
292
293 public String getEmailFromName() {
294 String emailFromName = PropsUtil.get(
295 PropsUtil.SHOPPING_EMAIL_FROM_NAME);
296
297 return _prefs.getValue("email-from-name", emailFromName);
298 }
299
300 public void setEmailFromName(String emailFromName)
301 throws ReadOnlyException {
302
303 _prefs.setValue("email-from-name", emailFromName);
304 }
305
306 public boolean getEmailOrderConfirmationEnabled() {
307 String emailOrderConfirmationEnabled = _prefs.getValue(
308 "email-order-confirmation-enabled", StringPool.BLANK);
309
310 if (Validator.isNotNull(emailOrderConfirmationEnabled)) {
311 return GetterUtil.getBoolean(emailOrderConfirmationEnabled);
312 }
313 else {
314 return GetterUtil.getBoolean(PropsUtil.get(
315 PropsUtil.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED));
316 }
317 }
318
319 public void setEmailOrderConfirmationEnabled(
320 boolean emailOrderConfirmationEnabled)
321 throws ReadOnlyException {
322
323 _prefs.setValue(
324 "email-order-confirmation-enabled",
325 String.valueOf(emailOrderConfirmationEnabled));
326 }
327
328 public String getEmailOrderConfirmationBody() throws IOException {
329 String emailOrderConfirmationBody = _prefs.getValue(
330 "email-order-confirmation-body", StringPool.BLANK);
331
332 if (Validator.isNotNull(emailOrderConfirmationBody)) {
333 return emailOrderConfirmationBody;
334 }
335 else {
336 return ContentUtil.get(PropsUtil.get(
337 PropsUtil.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY));
338 }
339 }
340
341 public void setEmailOrderConfirmationBody(String emailOrderConfirmationBody)
342 throws ReadOnlyException {
343
344 _prefs.setValue(
345 "email-order-confirmation-body", emailOrderConfirmationBody);
346 }
347
348 public String getEmailOrderConfirmationSubject() throws IOException {
349 String emailOrderConfirmationSubject = _prefs.getValue(
350 "email-order-confirmation-subject", StringPool.BLANK);
351
352 if (Validator.isNotNull(emailOrderConfirmationSubject)) {
353 return emailOrderConfirmationSubject;
354 }
355 else {
356 return ContentUtil.get(PropsUtil.get(
357 PropsUtil.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT));
358 }
359 }
360
361 public void setEmailOrderConfirmationSubject(
362 String emailOrderConfirmationSubject)
363 throws ReadOnlyException {
364
365 _prefs.setValue(
366 "email-order-confirmation-subject", emailOrderConfirmationSubject);
367 }
368
369 public boolean getEmailOrderShippingEnabled() {
370 String emailOrderShippingEnabled = _prefs.getValue(
371 "email-order-shipping-enabled", StringPool.BLANK);
372
373 if (Validator.isNotNull(emailOrderShippingEnabled)) {
374 return GetterUtil.getBoolean(emailOrderShippingEnabled);
375 }
376 else {
377 return GetterUtil.getBoolean(PropsUtil.get(
378 PropsUtil.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED));
379 }
380 }
381
382 public void setEmailOrderShippingEnabled(boolean emailOrderShippingEnabled)
383 throws ReadOnlyException {
384
385 _prefs.setValue(
386 "email-order-shipping-enabled",
387 String.valueOf(emailOrderShippingEnabled));
388 }
389
390 public String getEmailOrderShippingBody() throws IOException {
391 String emailOrderShippingBody = _prefs.getValue(
392 "email-order-shipping-body", StringPool.BLANK);
393
394 if (Validator.isNotNull(emailOrderShippingBody)) {
395 return emailOrderShippingBody;
396 }
397 else {
398 return ContentUtil.get(PropsUtil.get(
399 PropsUtil.SHOPPING_EMAIL_ORDER_SHIPPING_BODY));
400 }
401 }
402
403 public void setEmailOrderShippingBody(String emailOrderShippingBody)
404 throws ReadOnlyException {
405
406 _prefs.setValue("email-order-shipping-body", emailOrderShippingBody);
407 }
408
409 public String getEmailOrderShippingSubject() throws IOException {
410 String emailOrderShippingSubject = _prefs.getValue(
411 "email-order-shipping-subject", StringPool.BLANK);
412
413 if (Validator.isNotNull(emailOrderShippingSubject)) {
414 return emailOrderShippingSubject;
415 }
416 else {
417 return ContentUtil.get(PropsUtil.get(
418 PropsUtil.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
419 }
420 }
421
422 public void setEmailOrderShippingSubject(String emailOrderShippingSubject)
423 throws ReadOnlyException {
424
425 _prefs.setValue(
426 "email-order-shipping-subject", emailOrderShippingSubject);
427 }
428
429 public void store() throws IOException, ValidatorException {
430 _prefs.store();
431 }
432
433 protected ShoppingPreferences(long companyId, long groupId)
434 throws PortalException, SystemException {
435
436 long ownerId = groupId;
437 int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
438 long plid = PortletKeys.PREFS_PLID_SHARED;
439 String portletId = PortletKeys.SHOPPING;
440
441 _prefs = PortletPreferencesLocalServiceUtil.getPreferences(
442 companyId, ownerId, ownerType, plid, portletId);
443 }
444
445 private PortletPreferences _prefs;
446
447 }