001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.shopping.util;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
031: import com.liferay.portal.util.ContentUtil;
032: import com.liferay.portal.util.PortletKeys;
033: import com.liferay.portal.util.PropsUtil;
034:
035: import java.io.IOException;
036:
037: import java.util.Currency;
038: import java.util.Locale;
039: import java.util.Set;
040: import java.util.TreeSet;
041:
042: import javax.portlet.PortletPreferences;
043: import javax.portlet.ReadOnlyException;
044: import javax.portlet.ValidatorException;
045:
046: /**
047: * <a href="ShoppingPreferences.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class ShoppingPreferences {
053:
054: public static final String[] CC_TYPES = new String[] { "visa",
055: "mastercard", "discover", "amex" };
056:
057: public static final String[] CURRENCY_IDS;
058:
059: static {
060: String[] ids = null;
061:
062: try {
063: Set set = new TreeSet();
064:
065: Locale[] locales = Locale.getAvailableLocales();
066:
067: for (int i = 0; i < locales.length; i++) {
068: Locale locale = locales[i];
069:
070: if (locale.getCountry().length() == 2) {
071: Currency currency = Currency.getInstance(locale);
072:
073: String currencyId = currency.getCurrencyCode();
074:
075: set.add(currencyId);
076: }
077: }
078:
079: ids = (String[]) set.toArray(new String[0]);
080: } catch (Exception e) {
081: ids = new String[] { "USD", "CAD", "EUR", "GBP", "JPY" };
082: } finally {
083: CURRENCY_IDS = ids;
084: }
085: }
086:
087: public static final double[] SHIPPING_RANGE = { 0.01, 9.99, 10.00,
088: 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
089: Double.POSITIVE_INFINITY };
090:
091: public static final double[] INSURANCE_RANGE = { 0.01, 9.99, 10.00,
092: 49.99, 50.00, 99.99, 100.00, 199.99, 200.00,
093: Double.POSITIVE_INFINITY };
094:
095: public static ShoppingPreferences getInstance(long companyId,
096: long groupId) throws PortalException, SystemException {
097:
098: return new ShoppingPreferences(companyId, groupId);
099: }
100:
101: public String getPayPalEmailAddress() {
102: return _prefs
103: .getValue("paypal-email-address", StringPool.BLANK);
104: }
105:
106: public void setPayPalEmailAddress(String payPalEmailAddress)
107: throws ReadOnlyException {
108:
109: _prefs.setValue("paypal-email-address", payPalEmailAddress);
110: }
111:
112: public boolean usePayPal() {
113: return Validator.isNotNull(getPayPalEmailAddress());
114: }
115:
116: public String getCurrencyId() {
117: return _prefs.getValue("currency-id", "USD");
118: }
119:
120: public void setCurrencyId(String currencyId)
121: throws ReadOnlyException {
122: _prefs.setValue("currency-id", currencyId);
123: }
124:
125: public String[] getCcTypes() {
126: return StringUtil.split(_prefs.getValue("cc-types", StringUtil
127: .merge(CC_TYPES)));
128: }
129:
130: public void setCcTypes(String[] ccTypes) throws ReadOnlyException {
131: _prefs.setValue("cc-types", StringUtil.merge(ccTypes));
132: }
133:
134: public String getTaxState() {
135: return _prefs.getValue("tax-state", "CA");
136: }
137:
138: public void setTaxState(String taxState) throws ReadOnlyException {
139: _prefs.setValue("tax-state", taxState);
140: }
141:
142: public double getTaxRate() {
143: return GetterUtil.getDouble(_prefs.getValue("tax-rate",
144: StringPool.BLANK));
145: }
146:
147: public void setTaxRate(double taxRate) throws ReadOnlyException {
148: _prefs.setValue("tax-rate", String.valueOf(taxRate));
149: }
150:
151: public String getShippingFormula() {
152: return _prefs.getValue("shipping-formula", "flat");
153: }
154:
155: public void setShippingFormula(String shippingFormula)
156: throws ReadOnlyException {
157:
158: _prefs.setValue("shipping-formula", shippingFormula);
159: }
160:
161: public String[] getShipping() {
162: String value = _prefs.getValue("shipping", null);
163:
164: if (value == null) {
165: return new String[5];
166: } else {
167: return StringUtil.split(value);
168: }
169: }
170:
171: public void setShipping(String[] shipping) throws ReadOnlyException {
172: _prefs.setValue("shipping", StringUtil.merge(shipping));
173: }
174:
175: public String[][] getAlternativeShipping() {
176: String value = _prefs.getValue("alternative-shipping", null);
177:
178: if (value == null) {
179: return new String[0][0];
180: } else {
181: String[] array = StringUtil.split("alternative-shipping",
182: "[$_ARRAY_$]");
183:
184: String[][] alternativeShipping = new String[array.length][0];
185:
186: for (int i = 0; i < array.length; i++) {
187: alternativeShipping[i] = StringUtil.split(array[i]);
188: }
189:
190: return alternativeShipping;
191: }
192: }
193:
194: public void setAlternativeShipping(String[][] alternativeShipping)
195: throws ReadOnlyException {
196:
197: StringMaker sm = new StringMaker();
198:
199: for (int i = 0; i < alternativeShipping.length; i++) {
200: sm.append(StringUtil.merge(alternativeShipping[i]));
201:
202: if ((i + 1) < alternativeShipping.length) {
203: sm.append("[$_ARRAY_$]");
204: }
205: }
206:
207: _prefs.setValue("alternative-shipping", sm.toString());
208: }
209:
210: public boolean useAlternativeShipping() {
211: String[][] alternativeShipping = getAlternativeShipping();
212:
213: try {
214: for (int i = 0; i < 10; i++) {
215: if (Validator.isNotNull(alternativeShipping[0][i])
216: && Validator
217: .isNotNull(alternativeShipping[1][i])) {
218:
219: return true;
220: }
221: }
222: } catch (Exception e) {
223: }
224:
225: return false;
226: }
227:
228: public String getAlternativeShippingName(int altShipping) {
229: String altShippingName = StringPool.BLANK;
230:
231: try {
232: altShippingName = getAlternativeShipping()[0][altShipping];
233: } catch (Exception e) {
234: }
235:
236: return altShippingName;
237: }
238:
239: public String getInsuranceFormula() {
240: return _prefs.getValue("insurance-formula", "flat");
241: }
242:
243: public void setInsuranceFormula(String insuranceFormula)
244: throws ReadOnlyException {
245:
246: _prefs.setValue("insurance-formula", insuranceFormula);
247: }
248:
249: public String[] getInsurance() {
250: String value = _prefs.getValue("insurance", null);
251:
252: if (value == null) {
253: return new String[5];
254: } else {
255: return StringUtil.split(value);
256: }
257: }
258:
259: public void setInsurance(String[] insurance)
260: throws ReadOnlyException {
261: _prefs.setValue("insurance", StringUtil.merge(insurance));
262: }
263:
264: public double getMinOrder() {
265: return GetterUtil.getDouble(_prefs.getValue("min-order",
266: StringPool.BLANK));
267: }
268:
269: public void setMinOrder(double minOrder) throws ReadOnlyException {
270: _prefs.setValue("min-order", String.valueOf(minOrder));
271: }
272:
273: public String getEmailFromAddress() {
274: String emailFromAddress = PropsUtil
275: .get(PropsUtil.SHOPPING_EMAIL_FROM_ADDRESS);
276:
277: return _prefs.getValue("email-from-address", emailFromAddress);
278: }
279:
280: public void setEmailFromAddress(String emailFromAddress)
281: throws ReadOnlyException {
282:
283: _prefs.setValue("email-from-address", emailFromAddress);
284: }
285:
286: public String getEmailFromName() {
287: String emailFromName = PropsUtil
288: .get(PropsUtil.SHOPPING_EMAIL_FROM_NAME);
289:
290: return _prefs.getValue("email-from-name", emailFromName);
291: }
292:
293: public void setEmailFromName(String emailFromName)
294: throws ReadOnlyException {
295:
296: _prefs.setValue("email-from-name", emailFromName);
297: }
298:
299: public boolean getEmailOrderConfirmationEnabled() {
300: String emailOrderConfirmationEnabled = _prefs.getValue(
301: "email-order-confirmation-enabled", StringPool.BLANK);
302:
303: if (Validator.isNotNull(emailOrderConfirmationEnabled)) {
304: return GetterUtil.getBoolean(emailOrderConfirmationEnabled);
305: } else {
306: return GetterUtil
307: .getBoolean(PropsUtil
308: .get(PropsUtil.SHOPPING_EMAIL_ORDER_CONFIRMATION_ENABLED));
309: }
310: }
311:
312: public void setEmailOrderConfirmationEnabled(
313: boolean emailOrderConfirmationEnabled)
314: throws ReadOnlyException {
315:
316: _prefs.setValue("email-order-confirmation-enabled", String
317: .valueOf(emailOrderConfirmationEnabled));
318: }
319:
320: public String getEmailOrderConfirmationBody() throws IOException {
321: String emailOrderConfirmationBody = _prefs.getValue(
322: "email-order-confirmation-body", StringPool.BLANK);
323:
324: if (Validator.isNotNull(emailOrderConfirmationBody)) {
325: return emailOrderConfirmationBody;
326: } else {
327: return ContentUtil
328: .get(PropsUtil
329: .get(PropsUtil.SHOPPING_EMAIL_ORDER_CONFIRMATION_BODY));
330: }
331: }
332:
333: public void setEmailOrderConfirmationBody(
334: String emailOrderConfirmationBody) throws ReadOnlyException {
335:
336: _prefs.setValue("email-order-confirmation-body",
337: emailOrderConfirmationBody);
338: }
339:
340: public String getEmailOrderConfirmationSubject() throws IOException {
341: String emailOrderConfirmationSubject = _prefs.getValue(
342: "email-order-confirmation-subject", StringPool.BLANK);
343:
344: if (Validator.isNotNull(emailOrderConfirmationSubject)) {
345: return emailOrderConfirmationSubject;
346: } else {
347: return ContentUtil
348: .get(PropsUtil
349: .get(PropsUtil.SHOPPING_EMAIL_ORDER_CONFIRMATION_SUBJECT));
350: }
351: }
352:
353: public void setEmailOrderConfirmationSubject(
354: String emailOrderConfirmationSubject)
355: throws ReadOnlyException {
356:
357: _prefs.setValue("email-order-confirmation-subject",
358: emailOrderConfirmationSubject);
359: }
360:
361: public boolean getEmailOrderShippingEnabled() {
362: String emailOrderShippingEnabled = _prefs.getValue(
363: "email-order-shipping-enabled", StringPool.BLANK);
364:
365: if (Validator.isNotNull(emailOrderShippingEnabled)) {
366: return GetterUtil.getBoolean(emailOrderShippingEnabled);
367: } else {
368: return GetterUtil
369: .getBoolean(PropsUtil
370: .get(PropsUtil.SHOPPING_EMAIL_ORDER_SHIPPING_ENABLED));
371: }
372: }
373:
374: public void setEmailOrderShippingEnabled(
375: boolean emailOrderShippingEnabled) throws ReadOnlyException {
376:
377: _prefs.setValue("email-order-shipping-enabled", String
378: .valueOf(emailOrderShippingEnabled));
379: }
380:
381: public String getEmailOrderShippingBody() throws IOException {
382: String emailOrderShippingBody = _prefs.getValue(
383: "email-order-shipping-body", StringPool.BLANK);
384:
385: if (Validator.isNotNull(emailOrderShippingBody)) {
386: return emailOrderShippingBody;
387: } else {
388: return ContentUtil.get(PropsUtil
389: .get(PropsUtil.SHOPPING_EMAIL_ORDER_SHIPPING_BODY));
390: }
391: }
392:
393: public void setEmailOrderShippingBody(String emailOrderShippingBody)
394: throws ReadOnlyException {
395:
396: _prefs.setValue("email-order-shipping-body",
397: emailOrderShippingBody);
398: }
399:
400: public String getEmailOrderShippingSubject() throws IOException {
401: String emailOrderShippingSubject = _prefs.getValue(
402: "email-order-shipping-subject", StringPool.BLANK);
403:
404: if (Validator.isNotNull(emailOrderShippingSubject)) {
405: return emailOrderShippingSubject;
406: } else {
407: return ContentUtil
408: .get(PropsUtil
409: .get(PropsUtil.SHOPPING_EMAIL_ORDER_SHIPPING_SUBJECT));
410: }
411: }
412:
413: public void setEmailOrderShippingSubject(
414: String emailOrderShippingSubject) throws ReadOnlyException {
415:
416: _prefs.setValue("email-order-shipping-subject",
417: emailOrderShippingSubject);
418: }
419:
420: public void store() throws IOException, ValidatorException {
421: _prefs.store();
422: }
423:
424: protected ShoppingPreferences(long companyId, long groupId)
425: throws PortalException, SystemException {
426:
427: long ownerId = groupId;
428: int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
429: long plid = PortletKeys.PREFS_PLID_SHARED;
430: String portletId = PortletKeys.SHOPPING;
431:
432: _prefs = PortletPreferencesLocalServiceUtil.getPreferences(
433: companyId, ownerId, ownerType, plid, portletId);
434: }
435:
436: private PortletPreferences _prefs;
437:
438: }
|