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