001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    // Subscriptions
245    
246                    subscriptionLocalService.deleteSubscriptions(
247                            order.getCompanyId(), ShoppingOrder.class.getName(),
248                            order.getOrderId());
249    
250                    // Items
251    
252                    shoppingOrderItemPersistence.removeByOrderId(order.getOrderId());
253    
254                    // Message boards
255    
256                    mbMessageLocalService.deleteDiscussionMessages(
257                            ShoppingOrder.class.getName(), order.getOrderId());
258            }
259    
260            public void deleteOrders(long groupId)
261                    throws PortalException, SystemException {
262    
263                    List<ShoppingOrder> orders = shoppingOrderPersistence.findByGroupId(
264                            groupId);
265    
266                    for (ShoppingOrder order : orders) {
267                            deleteOrder(order);
268                    }
269            }
270    
271            public ShoppingOrder getLatestOrder(long userId, long groupId)
272                    throws PortalException, SystemException {
273    
274                    List<ShoppingOrder> orders = shoppingOrderPersistence.findByG_U_PPPS(
275                            groupId, userId, ShoppingOrderConstants.STATUS_LATEST, 0, 1);
276    
277                    ShoppingOrder order = null;
278    
279                    if (orders.size() == 1) {
280                            order = orders.get(0);
281                    }
282                    else {
283                            order = shoppingOrderLocalService.addLatestOrder(userId, groupId);
284                    }
285    
286                    return order;
287            }
288    
289            public ShoppingOrder getOrder(long orderId)
290                    throws PortalException, SystemException {
291    
292                    return shoppingOrderPersistence.findByPrimaryKey(orderId);
293            }
294    
295            public ShoppingOrder getOrder(String number)
296                    throws PortalException, SystemException {
297    
298                    return shoppingOrderPersistence.findByNumber(number);
299            }
300    
301            public ShoppingOrder getPayPalTxnIdOrder(String ppTxnId)
302                    throws PortalException, SystemException {
303    
304                    return shoppingOrderPersistence.findByPPTxnId(ppTxnId);
305            }
306    
307            public ShoppingOrder saveLatestOrder(ShoppingCart cart)
308                    throws PortalException, SystemException {
309    
310                    Map<ShoppingCartItem, Integer> items = cart.getItems();
311                    Date now = new Date();
312    
313                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
314                            cart.getCompanyId(), cart.getGroupId());
315    
316                    if (!ShoppingUtil.meetsMinOrder(shoppingPrefs, items)) {
317                            throw new CartMinOrderException();
318                    }
319    
320                    ShoppingOrder order = getLatestOrder(
321                            cart.getUserId(), cart.getGroupId());
322    
323                    order.setCreateDate(now);
324                    order.setModifiedDate(now);
325                    order.setPpPaymentStatus(ShoppingOrderConstants.STATUS_CHECKOUT);
326    
327                    shoppingOrderPersistence.update(order, false);
328    
329                    boolean requiresShipping = false;
330    
331                    Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
332                            items.entrySet().iterator();
333    
334                    while (itr.hasNext()) {
335                            Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
336    
337                            ShoppingCartItem cartItem = entry.getKey();
338                            Integer count = entry.getValue();
339    
340                            ShoppingItem item = cartItem.getItem();
341    
342                            if (item.isRequiresShipping()) {
343                                    requiresShipping = true;
344                            }
345    
346                            long orderItemId = counterLocalService.increment();
347    
348                            ShoppingOrderItem orderItem = shoppingOrderItemPersistence.create(
349                                    orderItemId);
350    
351                            orderItem.setOrderId(order.getOrderId());
352                            orderItem.setItemId(cartItem.getCartItemId());
353                            orderItem.setSku(item.getSku());
354                            orderItem.setName(item.getName());
355                            orderItem.setDescription(item.getDescription());
356                            orderItem.setProperties(item.getProperties());
357                            orderItem.setPrice(
358                                    ShoppingUtil.calculateActualPrice(item, count.intValue()) /
359                                            count.intValue());
360                            orderItem.setQuantity(count.intValue());
361    
362                            shoppingOrderItemPersistence.update(orderItem, false);
363                    }
364    
365                    order.setModifiedDate(new Date());
366                    order.setTax(ShoppingUtil.calculateTax(items, order.getBillingState()));
367                    order.setShipping(
368                            ShoppingUtil.calculateAlternativeShipping(
369                                    items, cart.getAltShipping()));
370                    order.setAltShipping(
371                            shoppingPrefs.getAlternativeShippingName(cart.getAltShipping()));
372                    order.setRequiresShipping(requiresShipping);
373                    order.setInsure(cart.isInsure());
374                    order.setInsurance(ShoppingUtil.calculateInsurance(items));
375                    order.setCouponCodes(cart.getCouponCodes());
376                    order.setCouponDiscount(
377                            ShoppingUtil.calculateCouponDiscount(
378                                    items, order.getBillingState(), cart.getCoupon()));
379                    order.setSendOrderEmail(true);
380                    order.setSendShippingEmail(true);
381    
382                    shoppingOrderPersistence.update(order, false);
383    
384                    return order;
385            }
386    
387            public List<ShoppingOrder> search(
388                            long groupId, long companyId, long userId, String number,
389                            String billingFirstName, String billingLastName,
390                            String billingEmailAddress, String shippingFirstName,
391                            String shippingLastName, String shippingEmailAddress,
392                            String ppPaymentStatus, boolean andOperator, int start, int end)
393                    throws SystemException {
394    
395                    OrderDateComparator obc = new OrderDateComparator(false);
396    
397                    return shoppingOrderFinder.findByG_C_U_N_PPPS(
398                            groupId, companyId, userId, number, billingFirstName,
399                            billingLastName, billingEmailAddress, shippingFirstName,
400                            shippingLastName, shippingEmailAddress, ppPaymentStatus,
401                            andOperator, start, end, obc);
402            }
403    
404            public int searchCount(
405                            long groupId, long companyId, long userId, String number,
406                            String billingFirstName, String billingLastName,
407                            String billingEmailAddress, String shippingFirstName,
408                            String shippingLastName, String shippingEmailAddress,
409                            String ppPaymentStatus, boolean andOperator)
410                    throws SystemException {
411    
412                    return shoppingOrderFinder.countByG_C_U_N_PPPS(
413                            groupId, companyId, userId, number, billingFirstName,
414                            billingLastName, billingEmailAddress, shippingFirstName,
415                            shippingLastName, shippingEmailAddress, ppPaymentStatus,
416                            andOperator);
417            }
418    
419            public void sendEmail(
420                            long orderId, String emailType, ServiceContext serviceContext)
421                    throws PortalException, SystemException {
422    
423                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
424                            orderId);
425    
426                    sendEmail(order, emailType, serviceContext);
427            }
428    
429            public void sendEmail(
430                            ShoppingOrder order, String emailType,
431                            ServiceContext serviceContext)
432                    throws PortalException, SystemException {
433    
434                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
435                            order.getCompanyId(), order.getGroupId());
436    
437                    if (emailType.equals("confirmation") &&
438                            shoppingPrefs.getEmailOrderConfirmationEnabled()) {
439                    }
440                    else if (emailType.equals("shipping") &&
441                                     shoppingPrefs.getEmailOrderShippingEnabled()) {
442                    }
443                    else {
444                            return;
445                    }
446    
447                    User user = userPersistence.findByPrimaryKey(order.getUserId());
448    
449                    Currency currency = Currency.getInstance(shoppingPrefs.getCurrencyId());
450    
451                    String billingAddress =
452                            order.getBillingFirstName() + " " + order.getBillingLastName() +
453                                    "<br>" +
454                            order.getBillingEmailAddress() + "<br>" +
455                            order.getBillingStreet() + "<br>" +
456                            order.getBillingCity() + "<br>" +
457                            order.getBillingState() + "<br>" +
458                            order.getBillingZip() + "<br>" +
459                            order.getBillingCountry() + "<br>" +
460                            order.getBillingPhone() + "<br>";
461    
462                    String shippingAddress =
463                            order.getShippingFirstName() + " " + order.getShippingLastName() +
464                                    "<br>" +
465                            order.getShippingEmailAddress() + "<br>" +
466                            order.getShippingStreet() + "<br>" +
467                            order.getShippingCity() + "<br>" +
468                            order.getShippingState() + "<br>" +
469                            order.getShippingZip() + "<br>" +
470                            order.getShippingCountry() + "<br>" +
471                            order.getShippingPhone() + "<br>";
472    
473                    double total = ShoppingUtil.calculateTotal(order);
474    
475                    String fromName = shoppingPrefs.getEmailFromName(order.getCompanyId());
476                    String fromAddress = shoppingPrefs.getEmailFromAddress(
477                            order.getCompanyId());
478    
479                    String toName = user.getFullName();
480                    String toAddress = user.getEmailAddress();
481    
482                    String subject = null;
483                    String body = null;
484    
485                    if (emailType.equals("confirmation")) {
486                            subject = shoppingPrefs.getEmailOrderConfirmationSubject();
487                            body = shoppingPrefs.getEmailOrderConfirmationBody();
488                    }
489                    else if (emailType.equals("shipping")) {
490                            subject = shoppingPrefs.getEmailOrderShippingSubject();
491                            body = shoppingPrefs.getEmailOrderShippingBody();
492                    }
493    
494                    SubscriptionSender subscriptionSender = new SubscriptionSender();
495    
496                    subscriptionSender.setBody(body);
497                    subscriptionSender.setCompanyId(order.getCompanyId());
498                    subscriptionSender.setContextAttributes(
499                            "[$ORDER_BILLING_ADDRESS$]", billingAddress, "[$ORDER_CURRENCY$]",
500                            currency.getSymbol(), "[$ORDER_NUMBER$]", order.getNumber(),
501                            "[$ORDER_SHIPPING_ADDRESS$]", shippingAddress, "[$ORDER_TOTAL$]",
502                            total);
503                    subscriptionSender.setFrom(fromAddress, fromName);
504                    subscriptionSender.setHtmlFormat(true);
505                    subscriptionSender.setMailId("shopping_order", order.getOrderId());
506                    subscriptionSender.setPortletId(PortletKeys.SHOPPING);
507                    subscriptionSender.setScopeGroupId(order.getGroupId());
508                    subscriptionSender.setServiceContext(serviceContext);
509                    subscriptionSender.setSubject(subject);
510                    subscriptionSender.setUserId(order.getUserId());
511    
512                    subscriptionSender.addRuntimeSubscribers(toAddress, toName);
513    
514                    subscriptionSender.flushNotificationsAsync();
515    
516                    if (emailType.equals("confirmation") && order.isSendOrderEmail()) {
517                            order.setSendOrderEmail(false);
518    
519                            shoppingOrderPersistence.update(order, false);
520                    }
521                    else if (emailType.equals("shipping") && order.isSendShippingEmail()) {
522                            order.setSendShippingEmail(false);
523    
524                            shoppingOrderPersistence.update(order, false);
525                    }
526            }
527    
528            public ShoppingOrder updateLatestOrder(
529                            long userId, long groupId, String billingFirstName,
530                            String billingLastName, String billingEmailAddress,
531                            String billingCompany, String billingStreet, String billingCity,
532                            String billingState, String billingZip, String billingCountry,
533                            String billingPhone, boolean shipToBilling,
534                            String shippingFirstName, String shippingLastName,
535                            String shippingEmailAddress, String shippingCompany,
536                            String shippingStreet, String shippingCity, String shippingState,
537                            String shippingZip, String shippingCountry, String shippingPhone,
538                            String ccName, String ccType, String ccNumber, int ccExpMonth,
539                            int ccExpYear, String ccVerNumber, String comments)
540                    throws PortalException, SystemException {
541    
542                    ShoppingOrder order = getLatestOrder(userId, groupId);
543    
544                    return updateOrder(
545                            order.getOrderId(), billingFirstName, billingLastName,
546                            billingEmailAddress, billingCompany, billingStreet, billingCity,
547                            billingState, billingZip, billingCountry, billingPhone,
548                            shipToBilling, shippingFirstName, shippingLastName,
549                            shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
550                            shippingState, shippingZip, shippingCountry, shippingPhone, ccName,
551                            ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
552            }
553    
554            public ShoppingOrder updateOrder(
555                            long orderId, String ppTxnId, String ppPaymentStatus,
556                            double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
557                    throws PortalException, SystemException {
558    
559                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
560                            orderId);
561    
562                    order.setModifiedDate(new Date());
563                    order.setPpTxnId(ppTxnId);
564                    order.setPpPaymentStatus(ppPaymentStatus);
565                    order.setPpPaymentGross(ppPaymentGross);
566                    order.setPpReceiverEmail(ppReceiverEmail);
567                    order.setPpPayerEmail(ppPayerEmail);
568    
569                    shoppingOrderPersistence.update(order, false);
570    
571                    return order;
572            }
573    
574            public ShoppingOrder updateOrder(
575                            long orderId, String billingFirstName, String billingLastName,
576                            String billingEmailAddress, String billingCompany,
577                            String billingStreet, String billingCity, String billingState,
578                            String billingZip, String billingCountry, String billingPhone,
579                            boolean shipToBilling, String shippingFirstName,
580                            String shippingLastName, String shippingEmailAddress,
581                            String shippingCompany, String shippingStreet, String shippingCity,
582                            String shippingState, String shippingZip, String shippingCountry,
583                            String shippingPhone, String ccName, String ccType, String ccNumber,
584                            int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
585                    throws PortalException, SystemException {
586    
587                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
588                            orderId);
589    
590                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
591                            order.getCompanyId(), order.getGroupId());
592    
593                    validate(
594                            shoppingPrefs, billingFirstName, billingLastName,
595                            billingEmailAddress, billingStreet, billingCity, billingState,
596                            billingZip, billingCountry, billingPhone, shipToBilling,
597                            shippingFirstName, shippingLastName, shippingEmailAddress,
598                            shippingStreet, shippingCity, shippingState, shippingZip,
599                            shippingCountry, shippingPhone, ccName, ccType, ccNumber,
600                            ccExpMonth, ccExpYear, ccVerNumber);
601    
602                    order.setModifiedDate(new Date());
603                    order.setBillingFirstName(billingFirstName);
604                    order.setBillingLastName(billingLastName);
605                    order.setBillingEmailAddress(billingEmailAddress);
606                    order.setBillingCompany(billingCompany);
607                    order.setBillingStreet(billingStreet);
608                    order.setBillingCity(billingCity);
609                    order.setBillingState(billingState);
610                    order.setBillingZip(billingZip);
611                    order.setBillingCountry(billingCountry);
612                    order.setBillingPhone(billingPhone);
613                    order.setShipToBilling(shipToBilling);
614    
615                    if (shipToBilling) {
616                            order.setShippingFirstName(billingFirstName);
617                            order.setShippingLastName(billingLastName);
618                            order.setShippingEmailAddress(billingEmailAddress);
619                            order.setShippingCompany(billingCompany);
620                            order.setShippingStreet(billingStreet);
621                            order.setShippingCity(billingCity);
622                            order.setShippingState(billingState);
623                            order.setShippingZip(billingZip);
624                            order.setShippingCountry(billingCountry);
625                            order.setShippingPhone(billingPhone);
626                    }
627                    else {
628                            order.setShippingFirstName(shippingFirstName);
629                            order.setShippingLastName(shippingLastName);
630                            order.setShippingEmailAddress(shippingEmailAddress);
631                            order.setShippingCompany(shippingCompany);
632                            order.setShippingStreet(shippingStreet);
633                            order.setShippingCity(shippingCity);
634                            order.setShippingState(shippingState);
635                            order.setShippingZip(shippingZip);
636                            order.setShippingCountry(shippingCountry);
637                            order.setShippingPhone(shippingPhone);
638                    }
639    
640                    order.setCcName(ccName);
641                    order.setCcType(ccType);
642                    order.setCcNumber(ccNumber);
643                    order.setCcExpMonth(ccExpMonth);
644                    order.setCcExpYear(ccExpYear);
645                    order.setCcVerNumber(ccVerNumber);
646                    order.setComments(comments);
647    
648                    shoppingOrderPersistence.update(order, false);
649    
650                    return order;
651            }
652    
653            protected String getNumber() throws SystemException {
654                    String number = PwdGenerator.getPassword(
655                            PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
656    
657                    try {
658                            shoppingOrderPersistence.findByNumber(number);
659    
660                            return getNumber();
661                    }
662                    catch (NoSuchOrderException nsoe) {
663                            return number;
664                    }
665            }
666    
667            protected void validate(
668                            ShoppingPreferences shoppingPrefs, String billingFirstName,
669                            String billingLastName, String billingEmailAddress,
670                            String billingStreet, String billingCity, String billingState,
671                            String billingZip, String billingCountry, String billingPhone,
672                            boolean shipToBilling, String shippingFirstName,
673                            String shippingLastName, String shippingEmailAddress,
674                            String shippingStreet, String shippingCity, String shippingState,
675                            String shippingZip, String shippingCountry, String shippingPhone,
676                            String ccName, String ccType, String ccNumber, int ccExpMonth,
677                            int ccExpYear, String ccVerNumber)
678                    throws PortalException {
679    
680                    if (Validator.isNull(billingFirstName)) {
681                            throw new BillingFirstNameException();
682                    }
683                    else if (Validator.isNull(billingLastName)) {
684                            throw new BillingLastNameException();
685                    }
686                    else if (!Validator.isEmailAddress(billingEmailAddress)) {
687                            throw new BillingEmailAddressException();
688                    }
689                    else if (Validator.isNull(billingStreet)) {
690                            throw new BillingStreetException();
691                    }
692                    else if (Validator.isNull(billingCity)) {
693                            throw new BillingCityException();
694                    }
695                    else if (Validator.isNull(billingState)) {
696                            throw new BillingStateException();
697                    }
698                    else if (Validator.isNull(billingZip)) {
699                            throw new BillingZipException();
700                    }
701                    else if (Validator.isNull(billingCountry)) {
702                            throw new BillingCountryException();
703                    }
704                    else if (Validator.isNull(billingPhone)) {
705                            throw new BillingPhoneException();
706                    }
707    
708                    if (!shipToBilling) {
709                            if (Validator.isNull(shippingFirstName)) {
710                                    throw new ShippingFirstNameException();
711                            }
712                            else if (Validator.isNull(shippingLastName)) {
713                                    throw new ShippingLastNameException();
714                            }
715                            else if (!Validator.isEmailAddress(shippingEmailAddress)) {
716                                    throw new ShippingEmailAddressException();
717                            }
718                            else if (Validator.isNull(shippingStreet)) {
719                                    throw new ShippingStreetException();
720                            }
721                            else if (Validator.isNull(shippingCity)) {
722                                    throw new ShippingCityException();
723                            }
724                            else if (Validator.isNull(shippingState)) {
725                                    throw new ShippingStateException();
726                            }
727                            else if (Validator.isNull(shippingZip)) {
728                                    throw new ShippingZipException();
729                            }
730                            else if (Validator.isNull(shippingCountry)) {
731                                    throw new ShippingCountryException();
732                            }
733                            else if (Validator.isNull(shippingPhone)) {
734                                    throw new ShippingPhoneException();
735                            }
736                    }
737    
738                    if (!shoppingPrefs.usePayPal() &&
739                            (shoppingPrefs.getCcTypes().length > 0)) {
740    
741                            if (Validator.isNull(ccName)) {
742                                    throw new CCNameException();
743                            }
744                            else if (Validator.isNull(ccType)) {
745                                    throw new CCTypeException();
746                            }
747                            else if (!CreditCard.isValidNumber(ccNumber, ccType)) {
748                                    throw new CCNumberException();
749                            }
750                            else if (!CreditCard.isValidExpirationDate(ccExpMonth, ccExpYear)) {
751                                    throw new CCExpirationException();
752                            }
753                    }
754            }
755    
756    }