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