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