001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portal.util.SubscriptionSender;
027    import com.liferay.portlet.shopping.BillingCityException;
028    import com.liferay.portlet.shopping.BillingCountryException;
029    import com.liferay.portlet.shopping.BillingEmailAddressException;
030    import com.liferay.portlet.shopping.BillingFirstNameException;
031    import com.liferay.portlet.shopping.BillingLastNameException;
032    import com.liferay.portlet.shopping.BillingPhoneException;
033    import com.liferay.portlet.shopping.BillingStateException;
034    import com.liferay.portlet.shopping.BillingStreetException;
035    import com.liferay.portlet.shopping.BillingZipException;
036    import com.liferay.portlet.shopping.CCExpirationException;
037    import com.liferay.portlet.shopping.CCNameException;
038    import com.liferay.portlet.shopping.CCNumberException;
039    import com.liferay.portlet.shopping.CCTypeException;
040    import com.liferay.portlet.shopping.CartMinOrderException;
041    import com.liferay.portlet.shopping.NoSuchOrderException;
042    import com.liferay.portlet.shopping.ShippingCityException;
043    import com.liferay.portlet.shopping.ShippingCountryException;
044    import com.liferay.portlet.shopping.ShippingEmailAddressException;
045    import com.liferay.portlet.shopping.ShippingFirstNameException;
046    import com.liferay.portlet.shopping.ShippingLastNameException;
047    import com.liferay.portlet.shopping.ShippingPhoneException;
048    import com.liferay.portlet.shopping.ShippingStateException;
049    import com.liferay.portlet.shopping.ShippingStreetException;
050    import com.liferay.portlet.shopping.ShippingZipException;
051    import com.liferay.portlet.shopping.model.ShoppingCart;
052    import com.liferay.portlet.shopping.model.ShoppingCartItem;
053    import com.liferay.portlet.shopping.model.ShoppingItem;
054    import com.liferay.portlet.shopping.model.ShoppingItemField;
055    import com.liferay.portlet.shopping.model.ShoppingOrder;
056    import com.liferay.portlet.shopping.model.ShoppingOrderConstants;
057    import com.liferay.portlet.shopping.model.ShoppingOrderItem;
058    import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
059    import com.liferay.portlet.shopping.service.base.ShoppingOrderLocalServiceBaseImpl;
060    import com.liferay.portlet.shopping.util.ShoppingPreferences;
061    import com.liferay.portlet.shopping.util.ShoppingUtil;
062    import com.liferay.portlet.shopping.util.comparator.OrderDateComparator;
063    import com.liferay.util.CreditCard;
064    import com.liferay.util.PwdGenerator;
065    
066    import java.util.Currency;
067    import java.util.Date;
068    import java.util.Iterator;
069    import java.util.List;
070    import java.util.Map;
071    
072    /**
073     * @author Brian Wing Shun Chan
074     */
075    public class ShoppingOrderLocalServiceImpl
076            extends ShoppingOrderLocalServiceBaseImpl {
077    
078            public ShoppingOrder addLatestOrder(long userId, long groupId)
079                    throws PortalException, SystemException {
080    
081                    // Order
082    
083                    User user = userPersistence.findByPrimaryKey(userId);
084                    Date now = new Date();
085    
086                    String number = getNumber();
087    
088                    ShoppingOrder order = null;
089    
090                    long orderId = counterLocalService.increment();
091    
092                    List<ShoppingOrder> pastOrders =
093                            shoppingOrderPersistence.findByG_U_PPPS(
094                                    groupId, userId, ShoppingOrderConstants.STATUS_CHECKOUT, 0, 1);
095    
096                    if (pastOrders.size() > 0) {
097                            ShoppingOrder pastOrder = pastOrders.get(0);
098    
099                            order = shoppingOrderPersistence.create(orderId);
100    
101                            order.setBillingCompany(pastOrder.getBillingCompany());
102                            order.setBillingStreet(pastOrder.getBillingStreet());
103                            order.setBillingCity(pastOrder.getBillingCity());
104                            order.setBillingState(pastOrder.getBillingState());
105                            order.setBillingZip(pastOrder.getBillingZip());
106                            order.setBillingCountry(pastOrder.getBillingCountry());
107                            order.setBillingPhone(pastOrder.getBillingPhone());
108                            order.setShipToBilling(pastOrder.isShipToBilling());
109                            order.setShippingCompany(pastOrder.getShippingCompany());
110                            order.setShippingStreet(pastOrder.getShippingStreet());
111                            order.setShippingCity(pastOrder.getShippingCity());
112                            order.setShippingState(pastOrder.getShippingState());
113                            order.setShippingZip(pastOrder.getShippingZip());
114                            order.setShippingCountry(pastOrder.getShippingCountry());
115                            order.setShippingPhone(pastOrder.getShippingPhone());
116                    }
117                    else {
118                            order = shoppingOrderPersistence.create(orderId);
119                    }
120    
121                    order.setGroupId(groupId);
122                    order.setCompanyId(user.getCompanyId());
123                    order.setUserId(user.getUserId());
124                    order.setUserName(user.getFullName());
125                    order.setCreateDate(now);
126                    order.setModifiedDate(now);
127                    order.setNumber(number);
128                    order.setBillingFirstName(user.getFirstName());
129                    order.setBillingLastName(user.getLastName());
130                    order.setBillingEmailAddress(user.getEmailAddress());
131                    order.setShippingFirstName(user.getFirstName());
132                    order.setShippingLastName(user.getLastName());
133                    order.setShippingEmailAddress(user.getEmailAddress());
134                    order.setCcName(user.getFullName());
135                    order.setPpPaymentStatus(ShoppingOrderConstants.STATUS_LATEST);
136                    order.setSendOrderEmail(true);
137                    order.setSendShippingEmail(true);
138    
139                    shoppingOrderPersistence.update(order, false);
140    
141                    // Message boards
142    
143                    if (PropsValues.SHOPPING_ORDER_COMMENTS_ENABLED) {
144                            mbMessageLocalService.addDiscussionMessage(
145                                    userId, order.getUserName(), groupId,
146                                    ShoppingOrder.class.getName(), orderId,
147                                    WorkflowConstants.ACTION_PUBLISH);
148                    }
149    
150                    return order;
151            }
152    
153            public void completeOrder(
154                            String number, String ppTxnId, String ppPaymentStatus,
155                            double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail,
156                            boolean updateInventory, ServiceContext serviceContext)
157                    throws PortalException, SystemException {
158    
159                    // Order
160    
161                    ShoppingOrder order = shoppingOrderPersistence.findByNumber(number);
162    
163                    order.setModifiedDate(new Date());
164                    order.setPpTxnId(ppTxnId);
165                    order.setPpPaymentStatus(ppPaymentStatus);
166                    order.setPpPaymentGross(ppPaymentGross);
167                    order.setPpReceiverEmail(ppReceiverEmail);
168                    order.setPpPayerEmail(ppPayerEmail);
169    
170                    shoppingOrderPersistence.update(order, false);
171    
172                    // Inventory
173    
174                    if (updateInventory &&
175                            ppPaymentStatus.equals(ShoppingOrderConstants.STATUS_COMPLETED)) {
176    
177                            List<ShoppingOrderItem> orderItems =
178                                    shoppingOrderItemLocalService.getOrderItems(order.getOrderId());
179    
180                            for (ShoppingOrderItem orderItem : orderItems) {
181                                    ShoppingItem item = shoppingItemLocalService.getItem(
182                                            ShoppingUtil.getItemId(orderItem.getItemId()));
183    
184                                    if (!item.isFields()) {
185                                            int quantity =
186                                                    item.getStockQuantity() - orderItem.getQuantity();
187    
188                                            item.setStockQuantity(quantity);
189                                    }
190                                    else {
191                                            List<ShoppingItemField> itemFields =
192                                                    shoppingItemFieldLocalService.getItemFields(
193                                                            item.getItemId());
194    
195                                            ShoppingItemField[] itemFieldsArray = itemFields.toArray(
196                                                    new ShoppingItemField[itemFields.size()]);
197    
198                                            String[] fieldsArray = ShoppingCartItemImpl.getFieldsArray(
199                                                    ShoppingUtil.getItemFields(orderItem.getItemId()));
200    
201                                            int rowPos = ShoppingUtil.getFieldsQuantitiesPos(
202                                                    item, itemFieldsArray, fieldsArray);
203    
204                                            String[] fieldsQuantities = item.getFieldsQuantitiesArray();
205    
206                                            try {
207                                                    int quantity =
208                                                            GetterUtil.getInteger(fieldsQuantities[rowPos]) -
209                                                            orderItem.getQuantity();
210    
211                                                    fieldsQuantities[rowPos] = String.valueOf(quantity);
212    
213                                                    item.setFieldsQuantitiesArray(fieldsQuantities);
214                                            }
215                                            catch (Exception e) {
216                                            }
217                                    }
218    
219                                    shoppingItemPersistence.update(item, false);
220                            }
221                    }
222    
223                    // Email
224    
225                    sendEmail(order, "confirmation", serviceContext);
226            }
227    
228            public void deleteOrder(long orderId)
229                    throws PortalException, SystemException {
230    
231                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
232                            orderId);
233    
234                    deleteOrder(order);
235            }
236    
237            public void deleteOrder(ShoppingOrder order)
238                    throws PortalException, SystemException {
239    
240                    // Order
241    
242                    shoppingOrderPersistence.remove(order);
243    
244                    // Items
245    
246                    shoppingOrderItemPersistence.removeByOrderId(order.getOrderId());
247    
248                    // Message boards
249    
250                    mbMessageLocalService.deleteDiscussionMessages(
251                            ShoppingOrder.class.getName(), order.getOrderId());
252            }
253    
254            public void deleteOrders(long groupId)
255                    throws PortalException, SystemException {
256    
257                    List<ShoppingOrder> orders = shoppingOrderPersistence.findByGroupId(
258                            groupId);
259    
260                    for (ShoppingOrder order : orders) {
261                            deleteOrder(order);
262                    }
263            }
264    
265            public ShoppingOrder getLatestOrder(long userId, long groupId)
266                    throws PortalException, SystemException {
267    
268                    List<ShoppingOrder> orders = shoppingOrderPersistence.findByG_U_PPPS(
269                            groupId, userId, ShoppingOrderConstants.STATUS_LATEST, 0, 1);
270    
271                    ShoppingOrder order = null;
272    
273                    if (orders.size() == 1) {
274                            order = orders.get(0);
275                    }
276                    else {
277                            order = shoppingOrderLocalService.addLatestOrder(userId, groupId);
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(ShoppingOrderConstants.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(ShoppingUtil.calculateTax(items, order.getBillingState()));
361                    order.setShipping(
362                            ShoppingUtil.calculateAlternativeShipping(
363                                    items, cart.getAltShipping()));
364                    order.setAltShipping(
365                            shoppingPrefs.getAlternativeShippingName(cart.getAltShipping()));
366                    order.setRequiresShipping(requiresShipping);
367                    order.setInsure(cart.isInsure());
368                    order.setInsurance(ShoppingUtil.calculateInsurance(items));
369                    order.setCouponCodes(cart.getCouponCodes());
370                    order.setCouponDiscount(
371                            ShoppingUtil.calculateCouponDiscount(
372                                    items, order.getBillingState(), cart.getCoupon()));
373                    order.setSendOrderEmail(true);
374                    order.setSendShippingEmail(true);
375    
376                    shoppingOrderPersistence.update(order, false);
377    
378                    return order;
379            }
380    
381            public List<ShoppingOrder> search(
382                            long groupId, long companyId, long userId, String number,
383                            String billingFirstName, String billingLastName,
384                            String billingEmailAddress, String shippingFirstName,
385                            String shippingLastName, String shippingEmailAddress,
386                            String ppPaymentStatus, boolean andOperator, int start, int end)
387                    throws SystemException {
388    
389                    OrderDateComparator obc = new OrderDateComparator(false);
390    
391                    return shoppingOrderFinder.findByG_C_U_N_PPPS(
392                            groupId, companyId, userId, number, billingFirstName,
393                            billingLastName, billingEmailAddress, shippingFirstName,
394                            shippingLastName, shippingEmailAddress, ppPaymentStatus,
395                            andOperator, start, end, obc);
396            }
397    
398            public int searchCount(
399                            long groupId, long companyId, long userId, String number,
400                            String billingFirstName, String billingLastName,
401                            String billingEmailAddress, String shippingFirstName,
402                            String shippingLastName, String shippingEmailAddress,
403                            String ppPaymentStatus, boolean andOperator)
404                    throws SystemException {
405    
406                    return shoppingOrderFinder.countByG_C_U_N_PPPS(
407                            groupId, companyId, userId, number, billingFirstName,
408                            billingLastName, billingEmailAddress, shippingFirstName,
409                            shippingLastName, shippingEmailAddress, ppPaymentStatus,
410                            andOperator);
411            }
412    
413            public void sendEmail(
414                            long orderId, String emailType, ServiceContext serviceContext)
415                    throws PortalException, SystemException {
416    
417                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
418                            orderId);
419    
420                    sendEmail(order, emailType, serviceContext);
421            }
422    
423            public void sendEmail(
424                            ShoppingOrder order, String emailType,
425                            ServiceContext serviceContext)
426                    throws PortalException, SystemException {
427    
428                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
429                            order.getCompanyId(), order.getGroupId());
430    
431                    if (emailType.equals("confirmation") &&
432                            shoppingPrefs.getEmailOrderConfirmationEnabled()) {
433                    }
434                    else if (emailType.equals("shipping") &&
435                                     shoppingPrefs.getEmailOrderShippingEnabled()) {
436                    }
437                    else {
438                            return;
439                    }
440    
441                    User user = userPersistence.findByPrimaryKey(order.getUserId());
442    
443                    Currency currency = Currency.getInstance(shoppingPrefs.getCurrencyId());
444    
445                    String billingAddress =
446                            order.getBillingFirstName() + " " + order.getBillingLastName() +
447                                    "<br>" +
448                            order.getBillingEmailAddress() + "<br>" +
449                            order.getBillingStreet() + "<br>" +
450                            order.getBillingCity() + "<br>" +
451                            order.getBillingState() + "<br>" +
452                            order.getBillingZip() + "<br>" +
453                            order.getBillingCountry() + "<br>" +
454                            order.getBillingPhone() + "<br>";
455    
456                    String shippingAddress =
457                            order.getShippingFirstName() + " " + order.getShippingLastName() +
458                                    "<br>" +
459                            order.getShippingEmailAddress() + "<br>" +
460                            order.getShippingStreet() + "<br>" +
461                            order.getShippingCity() + "<br>" +
462                            order.getShippingState() + "<br>" +
463                            order.getShippingZip() + "<br>" +
464                            order.getShippingCountry() + "<br>" +
465                            order.getShippingPhone() + "<br>";
466    
467                    double total = ShoppingUtil.calculateTotal(order);
468    
469                    String fromName = shoppingPrefs.getEmailFromName(order.getCompanyId());
470                    String fromAddress = shoppingPrefs.getEmailFromAddress(
471                            order.getCompanyId());
472    
473                    String toName = user.getFullName();
474                    String toAddress = user.getEmailAddress();
475    
476                    String subject = null;
477                    String body = null;
478    
479                    if (emailType.equals("confirmation")) {
480                            subject = shoppingPrefs.getEmailOrderConfirmationSubject();
481                            body = shoppingPrefs.getEmailOrderConfirmationBody();
482                    }
483                    else if (emailType.equals("shipping")) {
484                            subject = shoppingPrefs.getEmailOrderShippingSubject();
485                            body = shoppingPrefs.getEmailOrderShippingBody();
486                    }
487    
488                    SubscriptionSender subscriptionSender = new SubscriptionSender();
489    
490                    subscriptionSender.setBody(body);
491                    subscriptionSender.setCompanyId(order.getCompanyId());
492                    subscriptionSender.setContextAttributes(
493                            "[$ORDER_BILLING_ADDRESS$]", billingAddress, "[$ORDER_CURRENCY$]",
494                            currency.getSymbol(), "[$ORDER_NUMBER$]", order.getNumber(),
495                            "[$ORDER_SHIPPING_ADDRESS$]", shippingAddress, "[$ORDER_TOTAL$]",
496                            total);
497                    subscriptionSender.setFrom(fromAddress, fromName);
498                    subscriptionSender.setHtmlFormat(true);
499                    subscriptionSender.setMailId("shopping_order", order.getOrderId());
500                    subscriptionSender.setPortletId(PortletKeys.SHOPPING);
501                    subscriptionSender.setScopeGroupId(order.getGroupId());
502                    subscriptionSender.setServiceContext(serviceContext);
503                    subscriptionSender.setSubject(subject);
504                    subscriptionSender.setUserId(order.getUserId());
505    
506                    subscriptionSender.addRuntimeSubscribers(toAddress, toName);
507    
508                    subscriptionSender.flushNotificationsAsync();
509    
510                    if (emailType.equals("confirmation") && order.isSendOrderEmail()) {
511                            order.setSendOrderEmail(false);
512    
513                            shoppingOrderPersistence.update(order, false);
514                    }
515                    else if (emailType.equals("shipping") &&
516                                     order.isSendShippingEmail()) {
517    
518                            order.setSendShippingEmail(false);
519    
520                            shoppingOrderPersistence.update(order, false);
521                    }
522            }
523    
524            public ShoppingOrder updateLatestOrder(
525                            long userId, long groupId, String billingFirstName,
526                            String billingLastName, String billingEmailAddress,
527                            String billingCompany, String billingStreet, String billingCity,
528                            String billingState, String billingZip, String billingCountry,
529                            String billingPhone, boolean shipToBilling,
530                            String shippingFirstName, String shippingLastName,
531                            String shippingEmailAddress, String shippingCompany,
532                            String shippingStreet, String shippingCity, String shippingState,
533                            String shippingZip, String shippingCountry, String shippingPhone,
534                            String ccName, String ccType, String ccNumber, int ccExpMonth,
535                            int ccExpYear, String ccVerNumber, String comments)
536                    throws PortalException, SystemException {
537    
538                    ShoppingOrder order = getLatestOrder(userId, groupId);
539    
540                    return updateOrder(
541                            order.getOrderId(), billingFirstName, billingLastName,
542                            billingEmailAddress, billingCompany, billingStreet, billingCity,
543                            billingState, billingZip, billingCountry, billingPhone,
544                            shipToBilling, shippingFirstName, shippingLastName,
545                            shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
546                            shippingState, shippingZip, shippingCountry, shippingPhone,
547                            ccName, ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber,
548                            comments);
549            }
550    
551            public ShoppingOrder updateOrder(
552                            long orderId, String ppTxnId, String ppPaymentStatus,
553                            double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
554                    throws PortalException, SystemException {
555    
556                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
557                            orderId);
558    
559                    order.setModifiedDate(new Date());
560                    order.setPpTxnId(ppTxnId);
561                    order.setPpPaymentStatus(ppPaymentStatus);
562                    order.setPpPaymentGross(ppPaymentGross);
563                    order.setPpReceiverEmail(ppReceiverEmail);
564                    order.setPpPayerEmail(ppPayerEmail);
565    
566                    shoppingOrderPersistence.update(order, false);
567    
568                    return order;
569            }
570    
571            public ShoppingOrder updateOrder(
572                            long orderId, String billingFirstName, String billingLastName,
573                            String billingEmailAddress, String billingCompany,
574                            String billingStreet, String billingCity, String billingState,
575                            String billingZip, String billingCountry, String billingPhone,
576                            boolean shipToBilling, String shippingFirstName,
577                            String shippingLastName, String shippingEmailAddress,
578                            String shippingCompany, String shippingStreet, String shippingCity,
579                            String shippingState, String shippingZip, String shippingCountry,
580                            String shippingPhone, String ccName, String ccType, String ccNumber,
581                            int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
582                    throws PortalException, SystemException {
583    
584                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
585                            orderId);
586    
587                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
588                            order.getCompanyId(), order.getGroupId());
589    
590                    validate(
591                            shoppingPrefs, billingFirstName, billingLastName,
592                            billingEmailAddress, billingStreet, billingCity, billingState,
593                            billingZip, billingCountry, billingPhone, shipToBilling,
594                            shippingFirstName, shippingLastName, shippingEmailAddress,
595                            shippingStreet, shippingCity, shippingState, shippingZip,
596                            shippingCountry, shippingPhone, ccName, ccType, ccNumber,
597                            ccExpMonth, ccExpYear, ccVerNumber);
598    
599                    order.setModifiedDate(new Date());
600                    order.setBillingFirstName(billingFirstName);
601                    order.setBillingLastName(billingLastName);
602                    order.setBillingEmailAddress(billingEmailAddress);
603                    order.setBillingCompany(billingCompany);
604                    order.setBillingStreet(billingStreet);
605                    order.setBillingCity(billingCity);
606                    order.setBillingState(billingState);
607                    order.setBillingZip(billingZip);
608                    order.setBillingCountry(billingCountry);
609                    order.setBillingPhone(billingPhone);
610                    order.setShipToBilling(shipToBilling);
611    
612                    if (shipToBilling) {
613                            order.setShippingFirstName(billingFirstName);
614                            order.setShippingLastName(billingLastName);
615                            order.setShippingEmailAddress(billingEmailAddress);
616                            order.setShippingCompany(billingCompany);
617                            order.setShippingStreet(billingStreet);
618                            order.setShippingCity(billingCity);
619                            order.setShippingState(billingState);
620                            order.setShippingZip(billingZip);
621                            order.setShippingCountry(billingCountry);
622                            order.setShippingPhone(billingPhone);
623                    }
624                    else {
625                            order.setShippingFirstName(shippingFirstName);
626                            order.setShippingLastName(shippingLastName);
627                            order.setShippingEmailAddress(shippingEmailAddress);
628                            order.setShippingCompany(shippingCompany);
629                            order.setShippingStreet(shippingStreet);
630                            order.setShippingCity(shippingCity);
631                            order.setShippingState(shippingState);
632                            order.setShippingZip(shippingZip);
633                            order.setShippingCountry(shippingCountry);
634                            order.setShippingPhone(shippingPhone);
635                    }
636    
637                    order.setCcName(ccName);
638                    order.setCcType(ccType);
639                    order.setCcNumber(ccNumber);
640                    order.setCcExpMonth(ccExpMonth);
641                    order.setCcExpYear(ccExpYear);
642                    order.setCcVerNumber(ccVerNumber);
643                    order.setComments(comments);
644    
645                    shoppingOrderPersistence.update(order, false);
646    
647                    return order;
648            }
649    
650            protected String getNumber() throws SystemException {
651                    String number = PwdGenerator.getPassword(
652                            PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
653    
654                    try {
655                            shoppingOrderPersistence.findByNumber(number);
656    
657                            return getNumber();
658                    }
659                    catch (NoSuchOrderException nsoe) {
660                            return number;
661                    }
662            }
663    
664            protected void validate(
665                            ShoppingPreferences shoppingPrefs, String billingFirstName,
666                            String billingLastName, String billingEmailAddress,
667                            String billingStreet, String billingCity, String billingState,
668                            String billingZip, String billingCountry, String billingPhone,
669                            boolean shipToBilling, String shippingFirstName,
670                            String shippingLastName, String shippingEmailAddress,
671                            String shippingStreet, String shippingCity, String shippingState,
672                            String shippingZip, String shippingCountry, String shippingPhone,
673                            String ccName, String ccType, String ccNumber, int ccExpMonth,
674                            int ccExpYear, String ccVerNumber)
675                    throws PortalException {
676    
677                    if (Validator.isNull(billingFirstName)) {
678                            throw new BillingFirstNameException();
679                    }
680                    else if (Validator.isNull(billingLastName)) {
681                            throw new BillingLastNameException();
682                    }
683                    else if (!Validator.isEmailAddress(billingEmailAddress)) {
684                            throw new BillingEmailAddressException();
685                    }
686                    else if (Validator.isNull(billingStreet)) {
687                            throw new BillingStreetException();
688                    }
689                    else if (Validator.isNull(billingCity)) {
690                            throw new BillingCityException();
691                    }
692                    else if (Validator.isNull(billingState)) {
693                            throw new BillingStateException();
694                    }
695                    else if (Validator.isNull(billingZip)) {
696                            throw new BillingZipException();
697                    }
698                    else if (Validator.isNull(billingCountry)) {
699                            throw new BillingCountryException();
700                    }
701                    else if (Validator.isNull(billingPhone)) {
702                            throw new BillingPhoneException();
703                    }
704    
705                    if (!shipToBilling) {
706                            if (Validator.isNull(shippingFirstName)) {
707                                    throw new ShippingFirstNameException();
708                            }
709                            else if (Validator.isNull(shippingLastName)) {
710                                    throw new ShippingLastNameException();
711                            }
712                            else if (!Validator.isEmailAddress(shippingEmailAddress)) {
713                                    throw new ShippingEmailAddressException();
714                            }
715                            else if (Validator.isNull(shippingStreet)) {
716                                    throw new ShippingStreetException();
717                            }
718                            else if (Validator.isNull(shippingCity)) {
719                                    throw new ShippingCityException();
720                            }
721                            else if (Validator.isNull(shippingState)) {
722                                    throw new ShippingStateException();
723                            }
724                            else if (Validator.isNull(shippingZip)) {
725                                    throw new ShippingZipException();
726                            }
727                            else if (Validator.isNull(shippingCountry)) {
728                                    throw new ShippingCountryException();
729                            }
730                            else if (Validator.isNull(shippingPhone)) {
731                                    throw new ShippingPhoneException();
732                            }
733                    }
734    
735                    if ((!shoppingPrefs.usePayPal()) &&
736                            (shoppingPrefs.getCcTypes().length > 0)) {
737    
738                            if (Validator.isNull(ccName)) {
739                                    throw new CCNameException();
740                            }
741                            else if (Validator.isNull(ccType)) {
742                                    throw new CCTypeException();
743                            }
744                            else if (!CreditCard.isValidNumber(ccNumber, ccType)) {
745                                    throw new CCNumberException();
746                            }
747                            else if (!CreditCard.isValidExpirationDate(ccExpMonth, ccExpYear)) {
748                                    throw new CCExpirationException();
749                            }
750                    }
751            }
752    
753    }