001    /**
002     * Copyright (c) 2000-2012 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") && order.isSendShippingEmail()) {
516                            order.setSendShippingEmail(false);
517    
518                            shoppingOrderPersistence.update(order, false);
519                    }
520            }
521    
522            public ShoppingOrder updateLatestOrder(
523                            long userId, long groupId, String billingFirstName,
524                            String billingLastName, String billingEmailAddress,
525                            String billingCompany, String billingStreet, String billingCity,
526                            String billingState, String billingZip, String billingCountry,
527                            String billingPhone, boolean shipToBilling,
528                            String shippingFirstName, String shippingLastName,
529                            String shippingEmailAddress, String shippingCompany,
530                            String shippingStreet, String shippingCity, String shippingState,
531                            String shippingZip, String shippingCountry, String shippingPhone,
532                            String ccName, String ccType, String ccNumber, int ccExpMonth,
533                            int ccExpYear, String ccVerNumber, String comments)
534                    throws PortalException, SystemException {
535    
536                    ShoppingOrder order = getLatestOrder(userId, groupId);
537    
538                    return updateOrder(
539                            order.getOrderId(), billingFirstName, billingLastName,
540                            billingEmailAddress, billingCompany, billingStreet, billingCity,
541                            billingState, billingZip, billingCountry, billingPhone,
542                            shipToBilling, shippingFirstName, shippingLastName,
543                            shippingEmailAddress, shippingCompany, shippingStreet, shippingCity,
544                            shippingState, shippingZip, shippingCountry, shippingPhone, ccName,
545                            ccType, ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
546            }
547    
548            public ShoppingOrder updateOrder(
549                            long orderId, String ppTxnId, String ppPaymentStatus,
550                            double ppPaymentGross, String ppReceiverEmail, String ppPayerEmail)
551                    throws PortalException, SystemException {
552    
553                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
554                            orderId);
555    
556                    order.setModifiedDate(new Date());
557                    order.setPpTxnId(ppTxnId);
558                    order.setPpPaymentStatus(ppPaymentStatus);
559                    order.setPpPaymentGross(ppPaymentGross);
560                    order.setPpReceiverEmail(ppReceiverEmail);
561                    order.setPpPayerEmail(ppPayerEmail);
562    
563                    shoppingOrderPersistence.update(order, false);
564    
565                    return order;
566            }
567    
568            public ShoppingOrder updateOrder(
569                            long orderId, String billingFirstName, String billingLastName,
570                            String billingEmailAddress, String billingCompany,
571                            String billingStreet, String billingCity, String billingState,
572                            String billingZip, String billingCountry, String billingPhone,
573                            boolean shipToBilling, String shippingFirstName,
574                            String shippingLastName, String shippingEmailAddress,
575                            String shippingCompany, String shippingStreet, String shippingCity,
576                            String shippingState, String shippingZip, String shippingCountry,
577                            String shippingPhone, String ccName, String ccType, String ccNumber,
578                            int ccExpMonth, int ccExpYear, String ccVerNumber, String comments)
579                    throws PortalException, SystemException {
580    
581                    ShoppingOrder order = shoppingOrderPersistence.findByPrimaryKey(
582                            orderId);
583    
584                    ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
585                            order.getCompanyId(), order.getGroupId());
586    
587                    validate(
588                            shoppingPrefs, billingFirstName, billingLastName,
589                            billingEmailAddress, billingStreet, billingCity, billingState,
590                            billingZip, billingCountry, billingPhone, shipToBilling,
591                            shippingFirstName, shippingLastName, shippingEmailAddress,
592                            shippingStreet, shippingCity, shippingState, shippingZip,
593                            shippingCountry, shippingPhone, ccName, ccType, ccNumber,
594                            ccExpMonth, ccExpYear, ccVerNumber);
595    
596                    order.setModifiedDate(new Date());
597                    order.setBillingFirstName(billingFirstName);
598                    order.setBillingLastName(billingLastName);
599                    order.setBillingEmailAddress(billingEmailAddress);
600                    order.setBillingCompany(billingCompany);
601                    order.setBillingStreet(billingStreet);
602                    order.setBillingCity(billingCity);
603                    order.setBillingState(billingState);
604                    order.setBillingZip(billingZip);
605                    order.setBillingCountry(billingCountry);
606                    order.setBillingPhone(billingPhone);
607                    order.setShipToBilling(shipToBilling);
608    
609                    if (shipToBilling) {
610                            order.setShippingFirstName(billingFirstName);
611                            order.setShippingLastName(billingLastName);
612                            order.setShippingEmailAddress(billingEmailAddress);
613                            order.setShippingCompany(billingCompany);
614                            order.setShippingStreet(billingStreet);
615                            order.setShippingCity(billingCity);
616                            order.setShippingState(billingState);
617                            order.setShippingZip(billingZip);
618                            order.setShippingCountry(billingCountry);
619                            order.setShippingPhone(billingPhone);
620                    }
621                    else {
622                            order.setShippingFirstName(shippingFirstName);
623                            order.setShippingLastName(shippingLastName);
624                            order.setShippingEmailAddress(shippingEmailAddress);
625                            order.setShippingCompany(shippingCompany);
626                            order.setShippingStreet(shippingStreet);
627                            order.setShippingCity(shippingCity);
628                            order.setShippingState(shippingState);
629                            order.setShippingZip(shippingZip);
630                            order.setShippingCountry(shippingCountry);
631                            order.setShippingPhone(shippingPhone);
632                    }
633    
634                    order.setCcName(ccName);
635                    order.setCcType(ccType);
636                    order.setCcNumber(ccNumber);
637                    order.setCcExpMonth(ccExpMonth);
638                    order.setCcExpYear(ccExpYear);
639                    order.setCcVerNumber(ccVerNumber);
640                    order.setComments(comments);
641    
642                    shoppingOrderPersistence.update(order, false);
643    
644                    return order;
645            }
646    
647            protected String getNumber() throws SystemException {
648                    String number = PwdGenerator.getPassword(
649                            PwdGenerator.KEY1 + PwdGenerator.KEY2, 12);
650    
651                    try {
652                            shoppingOrderPersistence.findByNumber(number);
653    
654                            return getNumber();
655                    }
656                    catch (NoSuchOrderException nsoe) {
657                            return number;
658                    }
659            }
660    
661            protected void validate(
662                            ShoppingPreferences shoppingPrefs, String billingFirstName,
663                            String billingLastName, String billingEmailAddress,
664                            String billingStreet, String billingCity, String billingState,
665                            String billingZip, String billingCountry, String billingPhone,
666                            boolean shipToBilling, String shippingFirstName,
667                            String shippingLastName, String shippingEmailAddress,
668                            String shippingStreet, String shippingCity, String shippingState,
669                            String shippingZip, String shippingCountry, String shippingPhone,
670                            String ccName, String ccType, String ccNumber, int ccExpMonth,
671                            int ccExpYear, String ccVerNumber)
672                    throws PortalException {
673    
674                    if (Validator.isNull(billingFirstName)) {
675                            throw new BillingFirstNameException();
676                    }
677                    else if (Validator.isNull(billingLastName)) {
678                            throw new BillingLastNameException();
679                    }
680                    else if (!Validator.isEmailAddress(billingEmailAddress)) {
681                            throw new BillingEmailAddressException();
682                    }
683                    else if (Validator.isNull(billingStreet)) {
684                            throw new BillingStreetException();
685                    }
686                    else if (Validator.isNull(billingCity)) {
687                            throw new BillingCityException();
688                    }
689                    else if (Validator.isNull(billingState)) {
690                            throw new BillingStateException();
691                    }
692                    else if (Validator.isNull(billingZip)) {
693                            throw new BillingZipException();
694                    }
695                    else if (Validator.isNull(billingCountry)) {
696                            throw new BillingCountryException();
697                    }
698                    else if (Validator.isNull(billingPhone)) {
699                            throw new BillingPhoneException();
700                    }
701    
702                    if (!shipToBilling) {
703                            if (Validator.isNull(shippingFirstName)) {
704                                    throw new ShippingFirstNameException();
705                            }
706                            else if (Validator.isNull(shippingLastName)) {
707                                    throw new ShippingLastNameException();
708                            }
709                            else if (!Validator.isEmailAddress(shippingEmailAddress)) {
710                                    throw new ShippingEmailAddressException();
711                            }
712                            else if (Validator.isNull(shippingStreet)) {
713                                    throw new ShippingStreetException();
714                            }
715                            else if (Validator.isNull(shippingCity)) {
716                                    throw new ShippingCityException();
717                            }
718                            else if (Validator.isNull(shippingState)) {
719                                    throw new ShippingStateException();
720                            }
721                            else if (Validator.isNull(shippingZip)) {
722                                    throw new ShippingZipException();
723                            }
724                            else if (Validator.isNull(shippingCountry)) {
725                                    throw new ShippingCountryException();
726                            }
727                            else if (Validator.isNull(shippingPhone)) {
728                                    throw new ShippingPhoneException();
729                            }
730                    }
731    
732                    if ((!shoppingPrefs.usePayPal()) &&
733                            (shoppingPrefs.getCcTypes().length > 0)) {
734    
735                            if (Validator.isNull(ccName)) {
736                                    throw new CCNameException();
737                            }
738                            else if (Validator.isNull(ccType)) {
739                                    throw new CCTypeException();
740                            }
741                            else if (!CreditCard.isValidNumber(ccNumber, ccType)) {
742                                    throw new CCNumberException();
743                            }
744                            else if (!CreditCard.isValidExpirationDate(ccExpMonth, ccExpYear)) {
745                                    throw new CCExpirationException();
746                            }
747                    }
748            }
749    
750    }