1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.shopping.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.mail.MailMessage;
20  import com.liferay.portal.kernel.util.CalendarUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Company;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.PortletKeys;
28  import com.liferay.portal.util.PropsValues;
29  import com.liferay.portlet.shopping.BillingCityException;
30  import com.liferay.portlet.shopping.BillingCountryException;
31  import com.liferay.portlet.shopping.BillingEmailAddressException;
32  import com.liferay.portlet.shopping.BillingFirstNameException;
33  import com.liferay.portlet.shopping.BillingLastNameException;
34  import com.liferay.portlet.shopping.BillingPhoneException;
35  import com.liferay.portlet.shopping.BillingStateException;
36  import com.liferay.portlet.shopping.BillingStreetException;
37  import com.liferay.portlet.shopping.BillingZipException;
38  import com.liferay.portlet.shopping.CCExpirationException;
39  import com.liferay.portlet.shopping.CCNameException;
40  import com.liferay.portlet.shopping.CCNumberException;
41  import com.liferay.portlet.shopping.CCTypeException;
42  import com.liferay.portlet.shopping.CartMinOrderException;
43  import com.liferay.portlet.shopping.NoSuchOrderException;
44  import com.liferay.portlet.shopping.ShippingCityException;
45  import com.liferay.portlet.shopping.ShippingCountryException;
46  import com.liferay.portlet.shopping.ShippingEmailAddressException;
47  import com.liferay.portlet.shopping.ShippingFirstNameException;
48  import com.liferay.portlet.shopping.ShippingLastNameException;
49  import com.liferay.portlet.shopping.ShippingPhoneException;
50  import com.liferay.portlet.shopping.ShippingStateException;
51  import com.liferay.portlet.shopping.ShippingStreetException;
52  import com.liferay.portlet.shopping.ShippingZipException;
53  import com.liferay.portlet.shopping.model.ShoppingCart;
54  import com.liferay.portlet.shopping.model.ShoppingCartItem;
55  import com.liferay.portlet.shopping.model.ShoppingItem;
56  import com.liferay.portlet.shopping.model.ShoppingItemField;
57  import com.liferay.portlet.shopping.model.ShoppingOrder;
58  import com.liferay.portlet.shopping.model.ShoppingOrderItem;
59  import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
60  import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
61  import com.liferay.portlet.shopping.service.base.ShoppingOrderLocalServiceBaseImpl;
62  import com.liferay.portlet.shopping.util.ShoppingPreferences;
63  import com.liferay.portlet.shopping.util.ShoppingUtil;
64  import com.liferay.portlet.shopping.util.comparator.OrderDateComparator;
65  import com.liferay.util.CreditCard;
66  import com.liferay.util.PwdGenerator;
67  
68  import java.io.IOException;
69  
70  import java.util.Currency;
71  import java.util.Date;
72  import java.util.Iterator;
73  import java.util.List;
74  import java.util.Map;
75  
76  import javax.mail.internet.InternetAddress;
77  
78  /**
79   * <a href="ShoppingOrderLocalServiceImpl.java.html"><b><i>View Source</i></b>
80   * </a>
81   *
82   * @author Brian Wing Shun Chan
83   */
84  public class ShoppingOrderLocalServiceImpl
85      extends ShoppingOrderLocalServiceBaseImpl {
86  
87      public ShoppingOrder addLatestOrder(long userId, long groupId)
88          throws PortalException, SystemException {
89  
90          // Order
91  
92          User user = userPersistence.findByPrimaryKey(userId);
93          Date now = new Date();
94  
95          String number = getNumber();
96  
97          ShoppingOrder order = null;
98  
99          long orderId = counterLocalService.increment();
100 
101         List<ShoppingOrder> pastOrders =
102             shoppingOrderPersistence.findByG_U_PPPS(
103                 groupId, userId, ShoppingOrderImpl.STATUS_CHECKOUT, 0, 1);
104 
105         if (pastOrders.size() > 0) {
106             ShoppingOrder pastOrder = pastOrders.get(0);
107 
108             order = shoppingOrderPersistence.create(orderId);
109 
110             order.setBillingCompany(pastOrder.getBillingCompany());
111             order.setBillingStreet(pastOrder.getBillingStreet());
112             order.setBillingCity(pastOrder.getBillingCity());
113             order.setBillingState(pastOrder.getBillingState());
114             order.setBillingZip(pastOrder.getBillingZip());
115             order.setBillingCountry(pastOrder.getBillingCountry());
116             order.setBillingPhone(pastOrder.getBillingPhone());
117             order.setShipToBilling(pastOrder.isShipToBilling());
118             order.setShippingCompany(pastOrder.getShippingCompany());
119             order.setShippingStreet(pastOrder.getShippingStreet());
120             order.setShippingCity(pastOrder.getShippingCity());
121             order.setShippingState(pastOrder.getShippingState());
122             order.setShippingZip(pastOrder.getShippingZip());
123             order.setShippingCountry(pastOrder.getShippingCountry());
124             order.setShippingPhone(pastOrder.getShippingPhone());
125         }
126         else {
127             order = shoppingOrderPersistence.create(orderId);
128         }
129 
130         order.setGroupId(groupId);
131         order.setCompanyId(user.getCompanyId());
132         order.setUserId(user.getUserId());
133         order.setUserName(user.getFullName());
134         order.setCreateDate(now);
135         order.setModifiedDate(now);
136         order.setNumber(number);
137         order.setBillingFirstName(user.getFirstName());
138         order.setBillingLastName(user.getLastName());
139         order.setBillingEmailAddress(user.getEmailAddress());
140         order.setShippingFirstName(user.getFirstName());
141         order.setShippingLastName(user.getLastName());
142         order.setShippingEmailAddress(user.getEmailAddress());
143         order.setCcName(user.getFullName());
144         order.setPpPaymentStatus(ShoppingOrderImpl.STATUS_LATEST);
145         order.setSendOrderEmail(true);
146         order.setSendShippingEmail(true);
147 
148         shoppingOrderPersistence.update(order, false);
149 
150         // Message boards
151 
152         if (PropsValues.SHOPPING_ORDER_COMMENTS_ENABLED) {
153             mbMessageLocalService.addDiscussionMessage(
154                 userId, order.getUserName(), ShoppingOrder.class.getName(),
155                 orderId);
156         }
157 
158         return order;
159     }
160 
161     public void completeOrder(
162             String number, String ppTxnId, String ppPaymentStatus,
163             double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail,
164             boolean updateInventory)
165         throws PortalException, SystemException {
166 
167         // Order
168 
169         ShoppingOrder order = shoppingOrderPersistence.findByNumber(number);
170 
171         order.setModifiedDate(new Date());
172         order.setPpTxnId(ppTxnId);
173         order.setPpPaymentStatus(ppPaymentStatus);
174         order.setPpPaymentGross(ppPaymentGross);
175         order.setPpReceiverEmail(ppReceiverEmail);
176         order.setPpPayerEmail(ppPayerEmail);
177 
178         shoppingOrderPersistence.update(order, false);
179 
180         // Inventory
181 
182         if (updateInventory &&
183             ppPaymentStatus.equals(ShoppingOrderImpl.STATUS_COMPLETED)) {
184 
185             List<ShoppingOrderItem> orderItems =
186                 shoppingOrderItemLocalService.getOrderItems(order.getOrderId());
187 
188             for (ShoppingOrderItem orderItem : orderItems) {
189                 ShoppingItem item = shoppingItemLocalService.getItem(
190                     ShoppingUtil.getItemId(orderItem.getItemId()));
191 
192                 if (!item.isFields()) {
193                     int quantity =
194                         item.getStockQuantity() - orderItem.getQuantity();
195 
196                     item.setStockQuantity(quantity);
197                 }
198                 else {
199                     List<ShoppingItemField> itemFields =
200                         shoppingItemFieldLocalService.getItemFields(
201                             item.getItemId());
202 
203                     ShoppingItemField[] itemFieldsArray = itemFields.toArray(
204                         new ShoppingItemField[itemFields.size()]);
205 
206                     String[] fieldsArray = ShoppingCartItemImpl.getFieldsArray(
207                         ShoppingUtil.getItemFields(orderItem.getItemId()));
208 
209                     int rowPos = ShoppingUtil.getFieldsQuantitiesPos(
210                         item, itemFieldsArray, fieldsArray);
211 
212                     String[] fieldsQuantities = item.getFieldsQuantitiesArray();
213 
214                     try {
215                         int quantity =
216                             GetterUtil.getInteger(fieldsQuantities[rowPos]) -
217                             orderItem.getQuantity();
218 
219                         fieldsQuantities[rowPos] = String.valueOf(quantity);
220 
221                         item.setFieldsQuantitiesArray(fieldsQuantities);
222                     }
223                     catch (Exception e) {
224                     }
225                 }
226 
227                 shoppingItemPersistence.update(item, false);
228             }
229         }
230 
231         // Email
232 
233         try {
234             doSendEmail(order, "confirmation");
235         }
236         catch (IOException ioe) {
237             throw new SystemException(ioe);
238         }
239     }
240 
241     public void deleteOrder(long orderId)
242         throws PortalException, SystemException {
243 
244         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
245             orderId);
246 
247         deleteOrder(order);
248     }
249 
250     public void deleteOrder(ShoppingOrder order)
251         throws PortalException, SystemException {
252 
253         // Order
254 
255         shoppingOrderPersistence.remove(order);
256 
257         // Items
258 
259         shoppingOrderItemPersistence.removeByOrderId(order.getOrderId());
260 
261         // Message boards
262 
263         mbMessageLocalService.deleteDiscussionMessages(
264             ShoppingOrder.class.getName(), order.getOrderId());
265     }
266 
267     public void deleteOrders(long groupId)
268         throws PortalException, SystemException {
269 
270         List<ShoppingOrder> orders = shoppingOrderPersistence.findByGroupId(
271             groupId);
272 
273         for (ShoppingOrder order : orders) {
274             deleteOrder(order);
275         }
276     }
277 
278     public ShoppingOrder getLatestOrder(long userId, long groupId)
279         throws PortalException, SystemException {
280 
281         List<ShoppingOrder> orders = shoppingOrderPersistence.findByG_U_PPPS(
282             groupId, userId, ShoppingOrderImpl.STATUS_LATEST, 0, 1);
283 
284         ShoppingOrder order = null;
285 
286         if (orders.size() == 1) {
287             order = orders.get(0);
288         }
289         else {
290             order = shoppingOrderLocalService.addLatestOrder(userId, groupId);
291         }
292 
293         return order;
294     }
295 
296     public ShoppingOrder getOrder(long orderId)
297         throws PortalException, SystemException {
298 
299         return shoppingOrderPersistence.findByPrimaryKey(orderId);
300     }
301 
302     public ShoppingOrder getOrder(String number)
303         throws PortalException, SystemException {
304 
305         return shoppingOrderPersistence.findByNumber(number);
306     }
307 
308     public ShoppingOrder getPayPalTxnIdOrder(String ppTxnId)
309         throws PortalException, SystemException {
310 
311         return shoppingOrderPersistence.findByPPTxnId(ppTxnId);
312     }
313 
314     public ShoppingOrder saveLatestOrder(ShoppingCart cart)
315         throws PortalException, SystemException {
316 
317         Map<ShoppingCartItem, Integer> items = cart.getItems();
318         Date now = new Date();
319 
320         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
321             cart.getCompanyId(), cart.getGroupId());
322 
323         if (!ShoppingUtil.meetsMinOrder(shoppingPrefs, items)) {
324             throw new CartMinOrderException();
325         }
326 
327         ShoppingOrder order = getLatestOrder(
328             cart.getUserId(), cart.getGroupId());
329 
330         order.setCreateDate(now);
331         order.setModifiedDate(now);
332         order.setPpPaymentStatus(ShoppingOrderImpl.STATUS_CHECKOUT);
333 
334         shoppingOrderPersistence.update(order, false);
335 
336         boolean requiresShipping = false;
337 
338         Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
339             items.entrySet().iterator();
340 
341         while (itr.hasNext()) {
342             Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
343 
344             ShoppingCartItem cartItem = entry.getKey();
345             Integer count = entry.getValue();
346 
347             ShoppingItem item = cartItem.getItem();
348 
349             if (item.isRequiresShipping()) {
350                 requiresShipping = true;
351             }
352 
353             long orderItemId = counterLocalService.increment();
354 
355             ShoppingOrderItem orderItem = shoppingOrderItemPersistence.create(
356                 orderItemId);
357 
358             orderItem.setOrderId(order.getOrderId());
359             orderItem.setItemId(cartItem.getCartItemId());
360             orderItem.setSku(item.getSku());
361             orderItem.setName(item.getName());
362             orderItem.setDescription(item.getDescription());
363             orderItem.setProperties(item.getProperties());
364             orderItem.setPrice(
365                 ShoppingUtil.calculateActualPrice(item, count.intValue()) /
366                     count.intValue());
367             orderItem.setQuantity(count.intValue());
368 
369             shoppingOrderItemPersistence.update(orderItem, false);
370         }
371 
372         order.setModifiedDate(new Date());
373         order.setTax(
374             ShoppingUtil.calculateTax(items, order.getBillingState()));
375         order.setShipping(
376             ShoppingUtil.calculateAlternativeShipping(
377                 items, cart.getAltShipping()));
378         order.setAltShipping(
379             shoppingPrefs.getAlternativeShippingName(cart.getAltShipping()));
380         order.setRequiresShipping(requiresShipping);
381         order.setInsure(cart.isInsure());
382         order.setInsurance(ShoppingUtil.calculateInsurance(items));
383         order.setCouponCodes(cart.getCouponCodes());
384         order.setCouponDiscount(
385             ShoppingUtil.calculateCouponDiscount(
386                 items, order.getBillingState(), cart.getCoupon()));
387         order.setSendOrderEmail(true);
388         order.setSendShippingEmail(true);
389 
390         shoppingOrderPersistence.update(order, false);
391 
392         return order;
393     }
394 
395     public List<ShoppingOrder> search(
396             long groupId, long companyId, long userId, String number,
397             String billingFirstName, String billingLastName,
398             String billingEmailAddress, String shippingFirstName,
399             String shippingLastName, String shippingEmailAddress,
400             String ppPaymentStatus, boolean andOperator, int start, int end)
401         throws SystemException {
402 
403         OrderDateComparator obc = new OrderDateComparator(false);
404 
405         return shoppingOrderFinder.findByG_C_U_N_PPPS(
406             groupId, companyId, userId, number, billingFirstName,
407             billingLastName, billingEmailAddress, shippingFirstName,
408             shippingLastName, shippingEmailAddress, ppPaymentStatus,
409             andOperator, start, end, obc);
410     }
411 
412     public int searchCount(
413             long groupId, long companyId, long userId, String number,
414             String billingFirstName, String billingLastName,
415             String billingEmailAddress, String shippingFirstName,
416             String shippingLastName, String shippingEmailAddress,
417             String ppPaymentStatus, boolean andOperator)
418         throws SystemException {
419 
420         return shoppingOrderFinder.countByG_C_U_N_PPPS(
421             groupId, companyId, userId, number, billingFirstName,
422             billingLastName, billingEmailAddress, shippingFirstName,
423             shippingLastName, shippingEmailAddress, ppPaymentStatus,
424             andOperator);
425     }
426 
427     public void sendEmail(long orderId, String emailType)
428         throws PortalException, SystemException {
429 
430         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
431             orderId);
432 
433         try {
434             doSendEmail(order, emailType);
435         }
436         catch (IOException ioe) {
437             throw new SystemException(ioe);
438         }
439     }
440 
441     public void sendEmail(ShoppingOrder order, String emailType)
442         throws PortalException, SystemException {
443 
444         try {
445             doSendEmail(order, emailType);
446         }
447         catch (IOException ioe) {
448             throw new SystemException(ioe);
449         }
450     }
451 
452     public ShoppingOrder updateLatestOrder(
453             long userId, long groupId, String billingFirstName,
454             String billingLastName, String billingEmailAddress,
455             String billingCompany, String billingStreet, String billingCity,
456             String billingState, String billingZip, String billingCountry,
457             String billingPhone, boolean shipToBilling,
458             String shippingFirstName, String shippingLastName,
459             String shippingEmailAddress, String shippingCompany,
460             String shippingStreet, String shippingCity, String shippingState,
461             String shippingZip, String shippingCountry, String shippingPhone,
462             String ccName, String ccType, String ccNumber, int ccExpMonth,
463             int ccExpYear, String ccVerNumber, String comments)
464         throws PortalException, SystemException {
465 
466         ShoppingOrder order = getLatestOrder(userId, groupId);
467 
468         return updateOrder(
469             order.getOrderId(), billingFirstName, billingLastName,
470             billingEmailAddress, billingCompany, billingStreet, billingCity,
471             billingState, billingZip, billingCountry, billingPhone,
472             shipToBilling, shippingFirstName, shippingLastName,
473             shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
474             shippingState, shippingZip, shippingCountry, shippingPhone,
475             ccName, ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber,
476             comments);
477     }
478 
479     public ShoppingOrder updateOrder(
480             long orderId, String ppTxnId, String ppPaymentStatus,
481             double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
482         throws PortalException, SystemException {
483 
484         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
485             orderId);
486 
487         order.setModifiedDate(new Date());
488         order.setPpTxnId(ppTxnId);
489         order.setPpPaymentStatus(ppPaymentStatus);
490         order.setPpPaymentGross(ppPaymentGross);
491         order.setPpReceiverEmail(ppReceiverEmail);
492         order.setPpPayerEmail(ppPayerEmail);
493 
494         shoppingOrderPersistence.update(order, false);
495 
496         return order;
497     }
498 
499     public ShoppingOrder updateOrder(
500             long orderId, String billingFirstName, String billingLastName,
501             String billingEmailAddress, String billingCompany,
502             String billingStreet, String billingCity, String billingState,
503             String billingZip, String billingCountry, String billingPhone,
504             boolean shipToBilling, String shippingFirstName,
505             String shippingLastName, String shippingEmailAddress,
506             String shippingCompany, String shippingStreet, String shippingCity,
507             String shippingState, String shippingZip, String shippingCountry,
508             String shippingPhone, String ccName, String ccType, String ccNumber,
509             int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
510         throws PortalException, SystemException {
511 
512         ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
513             orderId);
514 
515         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
516             order.getCompanyId(), order.getGroupId());
517 
518         validate(
519             shoppingPrefs, billingFirstName, billingLastName,
520             billingEmailAddress, billingStreet, billingCity, billingState,
521             billingZip, billingCountry, billingPhone, shipToBilling,
522             shippingFirstName, shippingLastName, shippingEmailAddress,
523             shippingStreet, shippingCity, shippingState, shippingZip,
524             shippingCountry, shippingPhone, ccName, ccType, ccNumber,
525             ccExpMonth, ccExpYear, ccVerNumber);
526 
527         order.setModifiedDate(new Date());
528         order.setBillingFirstName(billingFirstName);
529         order.setBillingLastName(billingLastName);
530         order.setBillingEmailAddress(billingEmailAddress);
531         order.setBillingCompany(billingCompany);
532         order.setBillingStreet(billingStreet);
533         order.setBillingCity(billingCity);
534         order.setBillingState(billingState);
535         order.setBillingZip(billingZip);
536         order.setBillingCountry(billingCountry);
537         order.setBillingPhone(billingPhone);
538         order.setShipToBilling(shipToBilling);
539 
540         if (shipToBilling) {
541             order.setShippingFirstName(billingFirstName);
542             order.setShippingLastName(billingLastName);
543             order.setShippingEmailAddress(billingEmailAddress);
544             order.setShippingCompany(billingCompany);
545             order.setShippingStreet(billingStreet);
546             order.setShippingCity(billingCity);
547             order.setShippingState(billingState);
548             order.setShippingZip(billingZip);
549             order.setShippingCountry(billingCountry);
550             order.setShippingPhone(billingPhone);
551         }
552         else {
553             order.setShippingFirstName(shippingFirstName);
554             order.setShippingLastName(shippingLastName);
555             order.setShippingEmailAddress(shippingEmailAddress);
556             order.setShippingCompany(shippingCompany);
557             order.setShippingStreet(shippingStreet);
558             order.setShippingCity(shippingCity);
559             order.setShippingState(shippingState);
560             order.setShippingZip(shippingZip);
561             order.setShippingCountry(shippingCountry);
562             order.setShippingPhone(shippingPhone);
563         }
564 
565         order.setCcName(ccName);
566         order.setCcType(ccType);
567         order.setCcNumber(ccNumber);
568         order.setCcExpMonth(ccExpMonth);
569         order.setCcExpYear(ccExpYear);
570         order.setCcVerNumber(ccVerNumber);
571         order.setComments(comments);
572 
573         shoppingOrderPersistence.update(order, false);
574 
575         return order;
576     }
577 
578     protected void doSendEmail(ShoppingOrder order, String emailType)
579         throws IOException, PortalException, SystemException {
580 
581         ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
582             order.getCompanyId(), order.getGroupId());
583 
584         if (emailType.equals("confirmation") &&
585             shoppingPrefs.getEmailOrderConfirmationEnabled()) {
586         }
587         else if (emailType.equals("shipping") &&
588                  shoppingPrefs.getEmailOrderShippingEnabled()) {
589         }
590         else {
591             return;
592         }
593 
594         Company company = companyPersistence.findByPrimaryKey(
595             order.getCompanyId());
596 
597         User user = userPersistence.findByPrimaryKey(order.getUserId());
598 
599         Currency currency = Currency.getInstance(shoppingPrefs.getCurrencyId());
600 
601         String billingAddress =
602             order.getBillingFirstName() + " " + order.getBillingLastName() +
603                 "<br>" +
604             order.getBillingEmailAddress() + "<br>" +
605             order.getBillingStreet() + "<br>" +
606             order.getBillingCity() + "<br>" +
607             order.getBillingState() + "<br>" +
608             order.getBillingZip() + "<br>" +
609             order.getBillingCountry() + "<br>" +
610             order.getBillingPhone() + "<br>";
611 
612         String shippingAddress =
613             order.getShippingFirstName() + " " + order.getShippingLastName() +
614                 "<br>" +
615             order.getShippingEmailAddress() + "<br>" +
616             order.getShippingStreet() + "<br>" +
617             order.getShippingCity() + "<br>" +
618             order.getShippingState() + "<br>" +
619             order.getShippingZip() + "<br>" +
620             order.getShippingCountry() + "<br>" +
621             order.getShippingPhone() + "<br>";
622 
623         double total = ShoppingUtil.calculateTotal(order);
624 
625         String portletName = PortalUtil.getPortletTitle(
626             PortletKeys.SHOPPING, user);
627 
628         String fromName = shoppingPrefs.getEmailFromName();
629         String fromAddress = shoppingPrefs.getEmailFromAddress();
630 
631         String toName = user.getFullName();
632         String toAddress = user.getEmailAddress();
633 
634         String subject = null;
635         String body = null;
636 
637         if (emailType.equals("confirmation")) {
638             subject = shoppingPrefs.getEmailOrderConfirmationSubject();
639             body = shoppingPrefs.getEmailOrderConfirmationBody();
640         }
641         else if (emailType.equals("shipping")) {
642             subject = shoppingPrefs.getEmailOrderShippingSubject();
643             body = shoppingPrefs.getEmailOrderShippingBody();
644         }
645 
646         subject = StringUtil.replace(
647             subject,
648             new String[] {
649                 "[$FROM_ADDRESS$]",
650                 "[$FROM_NAME$]",
651                 "[$ORDER_BILLING_ADDRESS$]",
652                 "[$ORDER_CURRENCY$]",
653                 "[$ORDER_NUMBER$]",
654                 "[$ORDER_SHIPPING_ADDRESS$]",
655                 "[$ORDER_TOTAL$]",
656                 "[$PORTAL_URL$]",
657                 "[$PORTLET_NAME$]",
658                 "[$TO_ADDRESS$]",
659                 "[$TO_NAME$]"
660             },
661             new String[] {
662                 fromAddress,
663                 fromName,
664                 billingAddress,
665                 currency.getSymbol(),
666                 order.getNumber(),
667                 shippingAddress,
668                 String.valueOf(total),
669                 company.getVirtualHost(),
670                 portletName,
671                 toAddress,
672                 toName
673             });
674 
675         body = StringUtil.replace(
676             body,
677             new String[] {
678                 "[$FROM_ADDRESS$]",
679                 "[$FROM_NAME$]",
680                 "[$ORDER_BILLING_ADDRESS$]",
681                 "[$ORDER_CURRENCY$]",
682                 "[$ORDER_NUMBER$]",
683                 "[$ORDER_SHIPPING_ADDRESS$]",
684                 "[$ORDER_TOTAL$]",
685                 "[$PORTAL_URL$]",
686                 "[$PORTLET_NAME$]",
687                 "[$TO_ADDRESS$]",
688                 "[$TO_NAME$]"
689             },
690             new String[] {
691                 fromAddress,
692                 fromName,
693                 billingAddress,
694                 currency.getSymbol(),
695                 order.getNumber(),
696                 shippingAddress,
697                 String.valueOf(total),
698                 company.getVirtualHost(),
699                 portletName,
700                 toAddress,
701                 toName
702             });
703 
704         InternetAddress from = new InternetAddress(fromAddress, fromName);
705 
706         InternetAddress to = new InternetAddress(toAddress, toName);
707 
708         MailMessage message = new MailMessage(from, to, subject, body, true);
709 
710         mailService.sendEmail(message);
711 
712         if (emailType.equals("confirmation") && order.isSendOrderEmail()) {
713             order.setSendOrderEmail(false);
714 
715             shoppingOrderPersistence.update(order, false);
716         }
717         else if (emailType.equals("shipping") &&
718                  order.isSendShippingEmail()) {
719 
720             order.setSendShippingEmail(false);
721 
722             shoppingOrderPersistence.update(order, false);
723         }
724     }
725 
726     protected String getNumber() throws SystemException {
727         String number = PwdGenerator.getPassword(
728             PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
729 
730         try {
731             shoppingOrderPersistence.findByNumber(number);
732 
733             return getNumber();
734         }
735         catch (NoSuchOrderException nsoe) {
736             return number;
737         }
738     }
739 
740     protected void validate(
741             ShoppingPreferences shoppingPrefs, String billingFirstName,
742             String billingLastName, String billingEmailAddress,
743             String billingStreet, String billingCity, String billingState,
744             String billingZip, String billingCountry, String billingPhone,
745             boolean shipToBilling, String shippingFirstName,
746             String shippingLastName, String shippingEmailAddress,
747             String shippingStreet, String shippingCity, String shippingState,
748             String shippingZip, String shippingCountry, String shippingPhone,
749             String ccName, String ccType, String ccNumber, int ccExpMonth,
750             int ccExpYear, String ccVerNumber)
751         throws PortalException {
752 
753         if (Validator.isNull(billingFirstName)) {
754             throw new BillingFirstNameException();
755         }
756         else if (Validator.isNull(billingLastName)) {
757             throw new BillingLastNameException();
758         }
759         else if (!Validator.isEmailAddress(billingEmailAddress)) {
760             throw new BillingEmailAddressException();
761         }
762         else if (Validator.isNull(billingStreet)) {
763             throw new BillingStreetException();
764         }
765         else if (Validator.isNull(billingCity)) {
766             throw new BillingCityException();
767         }
768         else if (Validator.isNull(billingState)) {
769             throw new BillingStateException();
770         }
771         else if (Validator.isNull(billingZip)) {
772             throw new BillingZipException();
773         }
774         else if (Validator.isNull(billingCountry)) {
775             throw new BillingCountryException();
776         }
777         else if (Validator.isNull(billingPhone)) {
778             throw new BillingPhoneException();
779         }
780 
781         if (!shipToBilling) {
782             if (Validator.isNull(shippingFirstName)) {
783                 throw new ShippingFirstNameException();
784             }
785             else if (Validator.isNull(shippingLastName)) {
786                 throw new ShippingLastNameException();
787             }
788             else if (!Validator.isEmailAddress(shippingEmailAddress)) {
789                 throw new ShippingEmailAddressException();
790             }
791             else if (Validator.isNull(shippingStreet)) {
792                 throw new ShippingStreetException();
793             }
794             else if (Validator.isNull(shippingCity)) {
795                 throw new ShippingCityException();
796             }
797             else if (Validator.isNull(shippingState)) {
798                 throw new ShippingStateException();
799             }
800             else if (Validator.isNull(shippingZip)) {
801                 throw new ShippingZipException();
802             }
803             else if (Validator.isNull(shippingCountry)) {
804                 throw new ShippingCountryException();
805             }
806             else if (Validator.isNull(shippingPhone)) {
807                 throw new ShippingPhoneException();
808             }
809         }
810 
811         if ((!shoppingPrefs.usePayPal()) &&
812             (shoppingPrefs.getCcTypes().length > 0)) {
813 
814             if (Validator.isNull(ccName)) {
815                 throw new CCNameException();
816             }
817             else if (Validator.isNull(ccType)) {
818                 throw new CCTypeException();
819             }
820             else if (!CreditCard.isValid(ccNumber, ccType)) {
821                 throw new CCNumberException();
822             }
823             else if (!CalendarUtil.isFuture(ccExpMonth, ccExpYear)) {
824                 throw new CCExpirationException();
825             }
826         }
827     }
828 
829 }