View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.order.model.facade;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import net.sf.oness.common.all.BaseObject;
28  import net.sf.oness.common.model.bo.AuditableBusinessObject;
29  import net.sf.oness.common.model.dao.Dao;
30  import net.sf.oness.common.model.temporal.Date;
31  import net.sf.oness.common.model.util.CollectionUtils;
32  import net.sf.oness.common.model.util.PaginatedList;
33  import net.sf.oness.inventory.model.facade.InventoryFacadeDelegate;
34  import net.sf.oness.inventory.model.product.bo.Price;
35  import net.sf.oness.inventory.model.product.bo.Product;
36  import net.sf.oness.order.model.bo.DeliveryDocket;
37  import net.sf.oness.order.model.bo.DeliveryDocketItem;
38  import net.sf.oness.order.model.bo.Invoice;
39  import net.sf.oness.order.model.bo.InvoiceItem;
40  import net.sf.oness.order.model.bo.Order;
41  import net.sf.oness.order.model.bo.OrderItem;
42  import net.sf.oness.order.model.to.DeliveryDocketItemWithProduct;
43  import net.sf.oness.order.model.to.DeliveryDocketWithParty;
44  import net.sf.oness.order.model.to.InvoiceItemWithProduct;
45  import net.sf.oness.order.model.to.InvoiceWithParty;
46  import net.sf.oness.order.model.to.OrderItemWithProduct;
47  import net.sf.oness.order.model.to.OrderWithParty;
48  import net.sf.oness.party.model.facade.PartyFacadeDelegate;
49  import net.sf.oness.party.model.party.bo.Party;
50  
51  /***
52   * @author Carlos Sanchez
53   * @version $Revision: 1.20 $
54   */
55  public class OrderSpringFacadeDelegate extends BaseObject implements
56          OrderFacadeDelegate {
57  
58      private Dao orderDao, orderItemDao, deliveryDocketDao,
59              deliveryDocketItemDao, invoiceDao, invoiceItemDao;
60  
61      private PartyFacadeDelegate partyFacadeDelegate;
62  
63      private InventoryFacadeDelegate inventoryFacadeDelegate;
64  
65      /***
66       * @param partyFacadeDelegate
67       *            The partyFacadeDelegate to set.
68       */
69      public void setPartyFacadeDelegate(PartyFacadeDelegate partyFacadeDelegate) {
70          this.partyFacadeDelegate = partyFacadeDelegate;
71      }
72  
73      /***
74       * @return Returns the partyFacadeDelegate.
75       */
76      public PartyFacadeDelegate getPartyFacadeDelegate() {
77          return partyFacadeDelegate;
78      }
79  
80      /***
81       * @param inventoryFacadeDelegate
82       *            The inventoryFacadeDelegate to set.
83       */
84      public void setInventoryFacadeDelegate(
85              InventoryFacadeDelegate inventoryFacadeDelegate) {
86          this.inventoryFacadeDelegate = inventoryFacadeDelegate;
87      }
88  
89      /***
90       * @return Returns the inventoryFacadeDelegate.
91       */
92      public InventoryFacadeDelegate getInventoryFacadeDelegate() {
93          return inventoryFacadeDelegate;
94      }
95  
96      /***
97       * @param orderDao
98       *            The orderDao to set.
99       */
100     public void setOrderDao(Dao orderDao) {
101         this.orderDao = orderDao;
102     }
103 
104     /***
105      * @return Returns the orderDao.
106      */
107     public Dao getOrderDao() {
108         return orderDao;
109     }
110 
111     /***
112      * @param orderItemDao
113      *            The orderItemDao to set.
114      */
115     public void setOrderItemDao(Dao orderItemDao) {
116         this.orderItemDao = orderItemDao;
117     }
118 
119     /***
120      * @return Returns the orderItemDao.
121      */
122     public Dao getOrderItemDao() {
123         return orderItemDao;
124     }
125 
126     /***
127      * @param deliveryDocketDao
128      *            The deliveryDocketDao to set.
129      */
130     public void setDeliveryDocketDao(Dao deliveryDocketDao) {
131         this.deliveryDocketDao = deliveryDocketDao;
132     }
133 
134     /***
135      * @return Returns the deliveryDocketDao.
136      */
137     public Dao getDeliveryDocketDao() {
138         return deliveryDocketDao;
139     }
140 
141     /***
142      * @param deliveryDocketItemDao
143      *            The deliveryDocketItemDao to set.
144      */
145     public void setDeliveryDocketItemDao(Dao deliveryDocketItemDao) {
146         this.deliveryDocketItemDao = deliveryDocketItemDao;
147     }
148 
149     /***
150      * @return Returns the deliveryDocketItemDao.
151      */
152     public Dao getDeliveryDocketItemDao() {
153         return deliveryDocketItemDao;
154     }
155 
156     /***
157      * @param invoiceDao
158      *            The invoiceDao to set.
159      */
160     public void setInvoiceDao(Dao invoiceDao) {
161         this.invoiceDao = invoiceDao;
162     }
163 
164     /***
165      * @return Returns the invoiceDao.
166      */
167     public Dao getInvoiceDao() {
168         return invoiceDao;
169     }
170 
171     /***
172      * @param invoiceItemDao
173      *            The invoiceItemDao to set.
174      */
175     public void setInvoiceItemDao(Dao invoiceItemDao) {
176         this.invoiceItemDao = invoiceItemDao;
177     }
178 
179     /***
180      * @return Returns the invoiceItemDao.
181      */
182     public Dao getInvoiceItemDao() {
183         return invoiceItemDao;
184     }
185 
186     /***
187      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#findOrderWithDetails(java.lang.Long)
188      */
189     public OrderWithParty findOrderWithDetails(Long id) {
190         OrderWithParty orderWithParty = new OrderWithParty();
191         orderWithParty.setAmount(new BigDecimal(0));
192         Order order = (Order) getOrderDao().findWithDetails(id);
193         orderWithParty.setOrder(order);
194         orderWithParty.setParty(getPartyFacadeDelegate().findPartyWithDetails(
195                 order.getPartyId()));
196         int itemsSize = order.getItems().size();
197         orderWithParty.setItems(new ArrayList(itemsSize));
198 
199         Collection productIds = new ArrayList(itemsSize);
200         Iterator iter = orderWithParty.getOrder().getItems().iterator();
201         while (iter.hasNext()) {
202             OrderItem orderItem = (OrderItem) iter.next();
203             productIds.add(orderItem.getProductId());
204         }
205         Map products = getInventoryFacadeDelegate().findProducts(productIds);
206 
207         iter = orderWithParty.getOrder().getItems().iterator();
208         while (iter.hasNext()) {
209             OrderItem orderItem = (OrderItem) iter.next();
210             OrderItemWithProduct orderItemWithProduct = new OrderItemWithProduct();
211             BigDecimal delivered = new BigDecimal("0");
212             if (orderItem.getDeliveryDocketItems() != null) {
213                 Iterator deliveredIter = orderItem.getDeliveryDocketItems()
214                         .iterator();
215                 while (deliveredIter.hasNext()) {
216                     DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) deliveredIter
217                             .next();
218                     delivered = delivered.add(deliveryDocketItem.getQuantity());
219                 }
220             }
221             orderItemWithProduct.setDelivered(delivered);
222             orderItemWithProduct.setOrderItem(orderItem);
223             orderItemWithProduct.setProduct((Product) products.get(orderItem
224                     .getProductId()));
225             orderWithParty.setAmount(orderWithParty.getAmount().add(
226                     orderItem.getPrice().multiply(orderItem.getQuantity())));
227             orderWithParty.getItems().add(orderItemWithProduct);
228         }
229 
230         return orderWithParty;
231     }
232 
233     /***
234      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#createOrder(net.sf.oness.order.model.bo.Order)
235      */
236     public OrderWithParty createOrder(Order order) {
237         order.setItems(new ArrayList(0));
238         order.setDeliveryDockets(new ArrayList(0));
239         Order newOrder = (Order) getOrderDao().create(order);
240         return findOrderWithDetails(newOrder.getId());
241     }
242 
243     /***
244      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#deleteOrder(java.lang.Long)
245      */
246     public void deleteOrder(Long id) {
247         getOrderDao().delete(id);
248     }
249 
250     /***
251      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#findOrder(net.sf.oness.order.model.bo.Order,
252      *      int, int)
253      */
254     public PaginatedList findOrder(Order order, int firstElement,
255             int maxElements) {
256         PaginatedList paginatedList = getOrderDao().find(order, firstElement,
257                 maxElements);
258         List list = new ArrayList(paginatedList.getPageSize());
259         Iterator iter = paginatedList.getList().iterator();
260         while (iter.hasNext()) {
261             list.add(findOrderWithDetails(((Order) iter.next()).getId()));
262         }
263         return new PaginatedList(list, firstElement, paginatedList
264                 .getPageSize(), paginatedList.size());
265     }
266 
267     /***
268      * @todo don't update those objects that didn't change
269      * 
270      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#updateOrder(net.sf.oness.order.model.bo.Order,
271      *      java.util.Collection)
272      */
273     public OrderWithParty updateOrder(Order order, Collection items) {
274         Order oldOrder = (Order) getOrderDao().findWithDetails(order.getId());
275         oldOrder.setDate(order.getDate());
276         oldOrder.setComments(order.getComments());
277 
278         Set orderItemIds = new HashSet();
279         Collection orderItemsToUpdate = new ArrayList(items.size());
280         Iterator iter = items.iterator();
281         while (iter.hasNext()) {
282             OrderItem orderItem = (OrderItem) iter.next();
283             OrderItem oldOrderItem = (OrderItem) getOrderItemDao()
284                     .findWithDetails(orderItem.getId());
285             orderItemIds.add(oldOrderItem.getId());
286             /*
287              * if the order item is not from the order throw an exception it
288              * should be null
289              */
290             if (!oldOrderItem.getOrder().getId().equals(order.getId()))
291                 throw new RuntimeException(
292                         "Tried to update an order item from another order");
293             oldOrderItem.setPrice(orderItem.getPrice());
294             oldOrderItem.setQuantity(orderItem.getQuantity());
295             oldOrderItem.setProductId(orderItem.getProductId());
296             orderItemsToUpdate.add(oldOrderItem);
297         }
298 
299         AuditableBusinessObject newOrder = getOrderDao().update(oldOrder);
300         iter = orderItemsToUpdate.iterator();
301         while (iter.hasNext()) {
302             OrderItem orderItem = (OrderItem) iter.next();
303             orderItem.getOrders().add(newOrder);
304             getOrderItemDao().update(orderItem);
305         }
306 
307         /* update items to point to the order updated */
308         iter = oldOrder.getItems().iterator();
309         while (iter.hasNext()) {
310             OrderItem orderItem = (OrderItem) iter.next();
311             /* only those not already updated */
312             if (!orderItemIds.contains(orderItem.getId())) {
313                 orderItem.getOrders().add(newOrder);
314                 OrderItem newOrderItem = (OrderItem) getOrderItemDao().update(
315                         orderItem);
316 
317                 /* update deliveryDocketItems to point to the orderItem updated */
318                 Iterator iter2 = orderItem.getDeliveryDocketItems().iterator();
319                 while (iter2.hasNext()) {
320                     DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) iter2
321                             .next();
322                     deliveryDocketItem.getOrderItems().add(newOrderItem);
323                     getDeliveryDocketItemDao().update(deliveryDocketItem);
324                 }
325             }
326         }
327 
328         return findOrderWithDetails(newOrder.getId());
329     }
330 
331     /***
332      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#createOrderItem(net.sf.oness.order.model.bo.OrderItem,
333      *      java.lang.Long)
334      */
335     public OrderWithParty createOrderItem(OrderItem orderItem, Long orderId) {
336         Order order = (Order) getOrderDao().findWithDetails(orderId);
337         Product product = (Product) getInventoryFacadeDelegate().findProducts(
338                 CollectionUtils.newList(orderItem.getProductId())).get(
339                 orderItem.getProductId());
340         Party party = getPartyFacadeDelegate().findPartyWithDetails(
341                 order.getPartyId());
342         createOrderItem(orderItem, product, order, party);
343         return findOrderWithDetails(orderId);
344     }
345 
346     /***
347      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#createOrderItems(java.util.Collection,
348      *      java.lang.Long)
349      */
350     public OrderWithParty createOrderItems(Collection orderItems, Long orderId) {
351         Order order = (Order) getOrderDao().findWithDetails(orderId);
352         Collection productIds = new ArrayList(orderItems.size());
353         Iterator iter = orderItems.iterator();
354         while (iter.hasNext()) {
355             OrderItem orderItem = (OrderItem) iter.next();
356             productIds.add(orderItem.getProductId());
357         }
358         Map products = getInventoryFacadeDelegate().findProducts(productIds);
359         Party party = getPartyFacadeDelegate().findPartyWithDetails(
360                 order.getPartyId());
361         iter = orderItems.iterator();
362         while (iter.hasNext()) {
363             OrderItem orderItem = (OrderItem) iter.next();
364             Product product = (Product) products.get(orderItem.getProductId());
365             createOrderItem(orderItem, product, order, party);
366         }
367         return findOrderWithDetails(orderId);
368     }
369 
370     private void createOrderItem(OrderItem orderItem, Product product,
371             Order order, Party party) {
372         Iterator iter = order.getItems().iterator();
373         while (iter.hasNext()) {
374             OrderItem item = (OrderItem) iter.next();
375             /* if the product is already in the order */
376             if (item.getProductId().equals(orderItem.getProductId())) {
377                 item.setQuantity(item.getQuantity()
378                         .add(orderItem.getQuantity()));
379                 getOrderItemDao().update(item);
380                 return;
381             }
382         }
383         iter = product.getPrices().iterator();
384         while (iter.hasNext()) {
385             Price price = (Price) iter.next();
386             if (price.getFare().getId().equals(party.getFareId())) {
387                 orderItem.setPrice(price.getAmount());
388                 break;
389             }
390         }
391         orderItem.setOrders(CollectionUtils.newList(order));
392         getOrderItemDao().create(orderItem);
393     }
394 
395     /***
396      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#createDeliveryDocket(net.sf.oness.order.model.bo.DeliveryDocket,
397      *      java.util.Map)
398      */
399     public DeliveryDocketWithParty createDeliveryDocket(
400             DeliveryDocket deliveryDocket, Map map) {
401         deliveryDocket.setItems(new ArrayList(0));
402         // TODO set orders
403         deliveryDocket.setOrders(new ArrayList(0));
404         deliveryDocket.setInvoices(new ArrayList(0));
405         DeliveryDocket newDeliveryDocket = (DeliveryDocket) getDeliveryDocketDao()
406                 .create(deliveryDocket);
407         return createDeliveryDocketItems(map, newDeliveryDocket.getId());
408     }
409 
410     /***
411      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#findDeliveryDocketWithDetails(java.lang.Long)
412      */
413     public DeliveryDocketWithParty findDeliveryDocketWithDetails(Long id) {
414         DeliveryDocketWithParty deliveryDocketWithParty = new DeliveryDocketWithParty();
415         DeliveryDocket deliveryDocket = (DeliveryDocket) getDeliveryDocketDao()
416                 .findWithDetails(id);
417         deliveryDocketWithParty.setDeliveryDocket(deliveryDocket);
418         deliveryDocketWithParty.setParty(getPartyFacadeDelegate()
419                 .findPartyWithDetails(deliveryDocket.getPartyId()));
420         int itemsSize = deliveryDocket.getItems().size();
421         deliveryDocketWithParty.setItems(new ArrayList(itemsSize));
422 
423         Collection productIds = new ArrayList(itemsSize);
424         Iterator iter = deliveryDocketWithParty.getDeliveryDocket().getItems()
425                 .iterator();
426         while (iter.hasNext()) {
427             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) iter
428                     .next();
429             productIds.add(deliveryDocketItem.getProductId());
430         }
431         Map products = getInventoryFacadeDelegate().findProducts(productIds);
432 
433         iter = deliveryDocketWithParty.getDeliveryDocket().getItems()
434                 .iterator();
435         while (iter.hasNext()) {
436             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) iter
437                     .next();
438             DeliveryDocketItemWithProduct deliveryDocketItemWithProduct = new DeliveryDocketItemWithProduct();
439             deliveryDocketItemWithProduct
440                     .setDeliveryDocketItem(deliveryDocketItem);
441             deliveryDocketItemWithProduct.setProduct((Product) products
442                     .get(deliveryDocketItem.getProductId()));
443             deliveryDocketWithParty.getItems().add(
444                     deliveryDocketItemWithProduct);
445         }
446 
447         return deliveryDocketWithParty;
448     }
449 
450     /***
451      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#createDeliveryDocketItems(java.util.Collection,
452      *      java.lang.Long)
453      */
454     public DeliveryDocketWithParty createDeliveryDocketItems(
455             Map deliveryDocketItems, Long deliveryDocketId) {
456         DeliveryDocket deliveryDocket = (DeliveryDocket) getDeliveryDocketDao()
457                 .findWithDetails(deliveryDocketId);
458         Collection productIds = new ArrayList(deliveryDocketItems.size());
459         Iterator iter = deliveryDocketItems.entrySet().iterator();
460         while (iter.hasNext()) {
461             Map.Entry entry = (Map.Entry) iter.next();
462             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) entry
463                     .getValue();
464             productIds.add(deliveryDocketItem.getProductId());
465         }
466         iter = deliveryDocketItems.entrySet().iterator();
467         while (iter.hasNext()) {
468             Map.Entry entry = (Map.Entry) iter.next();
469             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) entry
470                     .getValue();
471             Long orderItemId = (Long) entry.getKey();
472             createDeliveryDocketItem(deliveryDocketItem, deliveryDocket,
473                     orderItemId);
474         }
475         return findDeliveryDocketWithDetails(deliveryDocketId);
476     }
477 
478     private void createDeliveryDocketItem(
479             DeliveryDocketItem deliveryDocketItem,
480             DeliveryDocket deliveryDocket, Long orderItemId) {
481 
482         deliveryDocketItem.setDeliveryDockets(CollectionUtils
483                 .newList(deliveryDocket));
484         deliveryDocketItem.setOrderItems(CollectionUtils
485                 .newList(getOrderItemDao().findById(orderItemId)));
486         getDeliveryDocketItemDao().create(deliveryDocketItem);
487     }
488 
489     /***
490      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#findDeliveryDocket(net.sf.oness.order.model.bo.DeliveryDocket,
491      *      int, int)
492      */
493     public PaginatedList findDeliveryDocket(DeliveryDocket deliveryDocket,
494             int firstElement, int maxElements) {
495         PaginatedList paginatedList = getDeliveryDocketDao().find(
496                 deliveryDocket, firstElement, maxElements);
497         List list = new ArrayList(paginatedList.getPageSize());
498         Iterator iter = paginatedList.getList().iterator();
499         while (iter.hasNext()) {
500             list.add(findDeliveryDocketWithDetails(((DeliveryDocket) iter
501                     .next()).getId()));
502         }
503         return new PaginatedList(list, firstElement, paginatedList
504                 .getPageSize(), paginatedList.size());
505     }
506 
507     /***
508      * @todo don't update those objects that didn't change
509      * 
510      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#updateDeliveryDocket(net.sf.oness.order.model.bo.DeliveryDocket,
511      *      java.util.Collection)
512      */
513     public DeliveryDocketWithParty updateDeliveryDocket(
514             DeliveryDocket deliveryDocket, Collection items) {
515         DeliveryDocket oldDeliveryDocket = (DeliveryDocket) getDeliveryDocketDao()
516                 .findWithDetails(deliveryDocket.getId());
517         oldDeliveryDocket.setDate(deliveryDocket.getDate());
518         oldDeliveryDocket.setComments(deliveryDocket.getComments());
519 
520         Set deliveryDocketItemIds = new HashSet();
521         Collection deliveryDocketItemsToUpdate = new ArrayList(items.size());
522         Iterator iter = items.iterator();
523         while (iter.hasNext()) {
524             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) iter
525                     .next();
526             DeliveryDocketItem oldDeliveryDocketItem = (DeliveryDocketItem) getDeliveryDocketItemDao()
527                     .findWithDetails(deliveryDocketItem.getId());
528             deliveryDocketItemIds.add(oldDeliveryDocketItem.getId());
529             /*
530              * if the deliveryDocket item is not from the deliveryDocket throw
531              * an exception it should be null
532              */
533             if (!oldDeliveryDocketItem.getDeliveryDocket().getId().equals(
534                     deliveryDocket.getId()))
535                 throw new RuntimeException(
536                         "Tried to update an deliveryDocket item from another deliveryDocket");
537             oldDeliveryDocketItem.setQuantity(deliveryDocketItem.getQuantity());
538             oldDeliveryDocketItem.setProductId(deliveryDocketItem
539                     .getProductId());
540             deliveryDocketItemsToUpdate.add(oldDeliveryDocketItem);
541         }
542 
543         AuditableBusinessObject newDeliveryDocket = getDeliveryDocketDao()
544                 .update(oldDeliveryDocket);
545         iter = deliveryDocketItemsToUpdate.iterator();
546         while (iter.hasNext()) {
547             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) iter
548                     .next();
549             deliveryDocketItem.getDeliveryDockets().add(newDeliveryDocket);
550             getDeliveryDocketItemDao().update(deliveryDocketItem);
551         }
552 
553         /* update items to point to the deliveryDocket updated */
554         iter = oldDeliveryDocket.getItems().iterator();
555         while (iter.hasNext()) {
556             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) iter
557                     .next();
558             /* only those not already updated */
559             if (!deliveryDocketItemIds.contains(deliveryDocketItem.getId())) {
560                 deliveryDocketItem.getDeliveryDockets().add(newDeliveryDocket);
561                 getDeliveryDocketItemDao().update(deliveryDocketItem);
562             }
563         }
564 
565         return findDeliveryDocketWithDetails(newDeliveryDocket.getId());
566     }
567 
568     /***
569      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#deleteDeliveryDocket(java.lang.Long)
570      */
571     public void deleteDeliveryDocket(Long id) {
572         getDeliveryDocketDao().delete(id);
573     }
574 
575     /***
576      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#createInvoices(java.util.Collection)
577      */
578     public void createInvoices(Collection deliveryDocketIds) {
579         Iterator iter = deliveryDocketIds.iterator();
580         while (iter.hasNext()) {
581             DeliveryDocket deliveryDocket = (DeliveryDocket) getDeliveryDocketDao()
582                     .findById((Long) iter.next());
583             Collection deliveryDocketItems = deliveryDocket.getItems();
584             deliveryDocket.setInvoiced(Boolean.TRUE);
585             deliveryDocket = (DeliveryDocket) getDeliveryDocketDao().update(
586                     deliveryDocket);
587             deliveryDocket = (DeliveryDocket) getDeliveryDocketDao()
588                     .findWithDetails(deliveryDocket.getId());
589             Invoice invoice = new Invoice();
590             invoice.setDate(Date.now());
591             //invoice.setItems(createInvoiceItems(deliveryDocket.getItems()));
592             invoice.setDeliveryDockets(CollectionUtils.newList(deliveryDocket));
593             invoice.setPartyId(deliveryDocket.getPartyId());
594             invoice.setType(deliveryDocket.getType());
595             Invoice newInvoice = (Invoice) getInvoiceDao().create(invoice);
596             Party party = getPartyFacadeDelegate().findPartyWithDetails(
597                     invoice.getPartyId());
598             createInvoiceItems(deliveryDocketItems, newInvoice, party
599                     .getFareId());
600         }
601     }
602 
603     private Collection createInvoiceItems(Collection deliveryDocketItems,
604             Invoice invoice, Long fareId) {
605         Collection items = new ArrayList(deliveryDocketItems.size());
606         Iterator iter = deliveryDocketItems.iterator();
607         while (iter.hasNext()) {
608             DeliveryDocketItem deliveryDocketItem = (DeliveryDocketItem) getDeliveryDocketItemDao()
609                     .findWithDetails(((DeliveryDocketItem) iter.next()).getId());
610             InvoiceItem invoiceItem = new InvoiceItem();
611             invoiceItem.setProductId(deliveryDocketItem.getProductId());
612             invoiceItem.setQuantity(deliveryDocketItem.getQuantity());
613             invoiceItem.setInvoices(CollectionUtils.newList(invoice));
614 
615             /* set price */
616             Product product = (Product) getInventoryFacadeDelegate()
617                     .findProductWithDetails(invoiceItem.getProductId());
618             invoiceItem.setPrice(new BigDecimal(0));
619             Iterator productIter = product.getPrices().iterator();
620             while (productIter.hasNext()) {
621                 Price price = (Price) productIter.next();
622                 if (price.getFare().getId().equals(fareId)) {
623                     invoiceItem.setPrice(price.getAmount());
624                     break;
625                 }
626             }
627             items.add(getInvoiceItemDao().create(invoiceItem));
628         }
629         return items;
630     }
631 
632     /***
633      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#findInvoice(net.sf.oness.order.model.bo.Invoice,
634      *      int, int)
635      */
636     public PaginatedList findInvoice(Invoice invoice, int firstElement,
637             int maxElements) {
638         PaginatedList paginatedList = getInvoiceDao().find(invoice,
639                 firstElement, maxElements);
640         List list = new ArrayList(paginatedList.getPageSize());
641         Iterator iter = paginatedList.getList().iterator();
642         while (iter.hasNext()) {
643             list.add(findInvoiceWithDetails(((Invoice) iter.next()).getId()));
644         }
645         return new PaginatedList(list, firstElement, paginatedList
646                 .getPageSize(), paginatedList.size());
647     }
648 
649     /***
650      * @see net.sf.oness.order.model.facade.OrderFacadeDelegate#findInvoiceWithDetails(java.lang.Long)
651      */
652     public InvoiceWithParty findInvoiceWithDetails(Long id) {
653         InvoiceWithParty invoiceWithParty = new InvoiceWithParty();
654         invoiceWithParty.setAmount(new BigDecimal(0));
655         Invoice invoice = (Invoice) getInvoiceDao().findWithDetails(id);
656         invoiceWithParty.setInvoice(invoice);
657         invoiceWithParty.setParty(getPartyFacadeDelegate()
658                 .findPartyWithDetails(invoice.getPartyId()));
659         int itemsSize = invoice.getItems().size();
660         invoiceWithParty.setItems(new ArrayList(itemsSize));
661 
662         Collection productIds = new ArrayList(itemsSize);
663         Iterator iter = invoiceWithParty.getInvoice().getItems().iterator();
664         while (iter.hasNext()) {
665             InvoiceItem invoiceItem = (InvoiceItem) iter.next();
666             productIds.add(invoiceItem.getProductId());
667         }
668         Map products = getInventoryFacadeDelegate().findProducts(productIds);
669 
670         iter = invoiceWithParty.getInvoice().getItems().iterator();
671         while (iter.hasNext()) {
672             InvoiceItem invoiceItem = (InvoiceItem) iter.next();
673             InvoiceItemWithProduct invoiceItemWithProduct = new InvoiceItemWithProduct();
674             invoiceItemWithProduct.setInvoiceItem(invoiceItem);
675             invoiceItemWithProduct.setProduct((Product) products
676                     .get(invoiceItem.getProductId()));
677             invoiceWithParty.getItems().add(invoiceItemWithProduct);
678             invoiceWithParty
679                     .setAmount(invoiceWithParty.getAmount().add(
680                             invoiceItem.getPrice().multiply(
681                                     invoiceItem.getQuantity())));
682         }
683 
684         return invoiceWithParty;
685     }
686 
687 }