KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > airsent > business > delivery > DeliveryImpl


1 /*
2  * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
3  * Reserved.
4  *
5  * This source code file is distributed by Lutris Technologies, Inc. for
6  * use only by licensed users of product(s) that include this source
7  * file. Use of this source file or the software that uses it is covered
8  * by the terms and conditions of the Lutris Enhydra Development License
9  * Agreement included with this product.
10  *
11  * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
12  * ANY KIND, either express or implied. See the License for the specific terms
13  * governing rights and limitations under the License.
14  *
15  * Contributor(s):
16  *
17  * $Id: DeliveryImpl.java,v 1.1 2004/08/16 09:33:11 slobodan Exp $
18  */

19
20 package com.lutris.airsent.business.delivery;
21
22
23 import com.lutris.airsent.spec.AirSentException;
24 import com.lutris.airsent.spec.delivery.Delivery;
25 import com.lutris.airsent.spec.delivery.Update;
26 import com.lutris.airsent.spec.messenger.Messenger;
27
28 import com.lutris.airsent.business.*;
29 import com.lutris.airsent.business.delivery.*;
30 import com.lutris.airsent.data.delivery.DeliveryDO;
31
32 import com.lutris.airsent.business.address.AddressManager;
33
34
35 import com.lutris.airsent.business.address.AddressImpl;
36 import com.lutris.airsent.business.address.AddressManager;
37
38 import com.lutris.airsent.spec.address.Address;
39 import com.lutris.airsent.spec.messenger.MessengerManager;
40
41 import com.lutris.airsent.spec.customer.Customer;
42
43 import com.lutris.airsent.business.customer.CustomerImpl;
44 import com.lutris.airsent.data.person.CustomerDO;
45 import com.lutris.airsent.business.messenger.*;
46 import com.lutris.airsent.data.person.MessengerDO;
47 import com.lutris.dods.builder.generator.query.DataObjectException;
48 import java.util.*;
49 import java.text.*;
50
51
52 /**
53  * Delivery Implementation
54  */

55 public class DeliveryImpl implements Delivery {
56
57     DeliveryDO deliveryDO;
58     private Update update;
59     private AddressImpl pickUpAddress;
60     private AddressImpl dropOffAddress;
61     private Customer customer;
62     private Messenger messenger;
63     
64     /**
65      * Constructs DeliveryImpl object
66      *
67      *
68      * @exception if an error occurs
69      */

70     DeliveryImpl(Update update, Customer customer)
71         throws AirSentException {
72
73         try {
74             deliveryDO = DeliveryDO.createVirgin();
75             AddressManager af = HomeManagerImpl.getInstance().getAddressManager();
76             pickUpAddress = af.create();
77             deliveryDO.setPickup(pickUpAddress.getDataObject());
78             dropOffAddress = af.create();
79             deliveryDO.setDropoff(dropOffAddress.getDataObject());
80             setCustomer(customer);
81             this.update = update;
82         } catch (Exception JavaDoc e) {
83             throw new AirSentException(e);
84         }
85     }
86      
87     /**
88      * Constructs DeliveryImpl object
89      *
90      *
91      * @exception if an error occurs
92      */

93     DeliveryImpl(Update update, DeliveryDO d)
94         throws AirSentBusinessException {
95         if (d == null) {
96             throw new AirSentBusinessException("Error: data object is null");
97         }
98     try {
99         deliveryDO = d;
100         this.update = update;
101         this.customer = new CustomerImpl(deliveryDO.getCustomerID());
102     } catch (Exception JavaDoc e) {
103         throw new AirSentBusinessException(e);
104         }
105     }
106     
107     /**
108      * Constructs DeliveryImpl object
109      *
110      *
111      * @exception if an error occurs
112      */

113     DeliveryImpl(Update update, String JavaDoc id)
114         throws AirSentException, AirSentBusinessException {
115         if (id == null) {
116             throw new AirSentBusinessException("Error: data object is null");
117         }
118         try {
119             deliveryDO = DeliveryDO.createExisting(id);
120             this.update = update;
121         this.customer = new CustomerImpl(deliveryDO.getCustomerID());
122         } catch (Exception JavaDoc e) {
123             throw new AirSentBusinessException(e);
124         }
125     }
126  
127     /**
128      * Gets the handle.
129      *
130      * @return handle
131      * @exception if an error occurs
132      */

133     public String JavaDoc getHandle()
134         throws AirSentException {
135     try {
136         return deliveryDO.getHandle();
137     } catch (Exception JavaDoc e) {
138         throw new AirSentException(e);
139     }
140     }
141
142     /**
143      * Gets pick up address for the order.
144      *
145      * @return pick up address
146      * @exception if an error occurs
147      */

148     public Address getPickUp()
149         throws AirSentException {
150         if (pickUpAddress == null) {
151             try {
152                 AddressManager af = HomeManagerImpl.getInstance().getAddressManager();
153                 pickUpAddress = af.create(deliveryDO.getPickup());
154             } catch (Exception JavaDoc e) {
155                 throw new AirSentException(e);
156             }
157         }
158         return pickUpAddress;
159     }
160     
161     /**
162      * Gets the drop off address for the order
163      *
164      * @return drop off address
165      * @exception if an error occurs
166      */

167     public Address getDropOff()
168         throws AirSentException {
169         if (dropOffAddress == null) {
170             try {
171                 AddressManager af = HomeManagerImpl.getInstance().getAddressManager();
172                 dropOffAddress = af.create(deliveryDO.getDropoff());
173             } catch (Exception JavaDoc e) {
174                 throw new AirSentException(e);
175             }
176         }
177         return dropOffAddress;
178     }
179
180     /**
181      * gets the customer
182      *
183      * @return customer
184      * @exception if an error occurs
185      */

186     public Customer getCustomer()
187         throws AirSentException {
188         if (customer == null) {
189             throw new AirSentException("customer is null");
190         }
191         return customer;
192     }
193     
194     /*
195      *
196      */

197     private void setCustomer(Customer c)
198         throws AirSentException, AirSentBusinessException {
199         try {
200             deliveryDO.setCustomerID(CustomerDO.createExisting(c.getHandle()));
201             customer = c;
202         } catch (DataObjectException doe) {
203             // object can't be found
204
throw new AirSentBusinessException(doe);
205         } catch (Exception JavaDoc e) {
206             throw new AirSentException(e);
207         }
208     }
209
210     /**
211      * Gets the messenger.
212      *
213      * @return messenger
214      * @exception if an error occurs
215      */

216     public Messenger getMessenger()
217         throws AirSentException {
218     try {
219         if (messenger == null) {
220                 if (deliveryDO.getMessengerID() != null) {
221                     MessengerManager mf = HomeManagerImpl.getInstance().getMessengerManager();
222                     messenger = mf.create(deliveryDO.getMessengerID().getHandle());
223                 }
224         }
225             return messenger;
226     } catch (Exception JavaDoc e) {
227         throw new AirSentException(e);
228     }
229     }
230
231     /**
232      * Sets the messenger.
233      *
234      * @exception if an error occurs
235      * @exception if a business rule is violated
236      */

237     public void setMessenger(Messenger m)
238         throws AirSentException, AirSentBusinessException {
239     try {
240         deliveryDO.setMessengerID(MessengerDO.createExisting(m.getHandle()));
241             messenger = m;
242         } catch (DataObjectException doe) {
243             // object can't be found
244
throw new AirSentBusinessException(doe);
245     } catch (Exception JavaDoc e) {
246         throw new AirSentException(e);
247     }
248     }
249
250     /**
251      * gets the fragile flag
252      *
253      * @return true if fragile
254      * @exception if an error occurs
255      */

256     public boolean isFragile()
257         throws AirSentException {
258     try {
259         return deliveryDO.getFragile();
260     } catch (Exception JavaDoc e) {
261         throw new AirSentException(e);
262     }
263     }
264
265     /**
266      * sets the fragile flag
267      *
268      * @param fragile true if fragile
269      * @exception if an error occurs
270      */

271     public void setIsFragile(boolean fragile)
272         throws AirSentException {
273     try {
274         this.deliveryDO.setFragile(fragile);
275     } catch (Exception JavaDoc e) {
276         throw new AirSentException(e);
277     }
278     }
279
280     /**
281      * gets the urgent flag
282      *
283      * @return true if urgent
284      * @exception if an error occurs
285      */

286     public boolean isUrgent()
287         throws AirSentException {
288     try {
289         return deliveryDO.getUrgent();
290     } catch (Exception JavaDoc e) {
291         throw new AirSentException(e);
292     }
293     }
294
295     /**
296      * sets the urgent flag
297      *
298      * @param urgent true if urgent
299      * @exception if an error occurs
300      */

301     public void setIsUrgent(boolean urgent)
302         throws AirSentException {
303     try {
304         deliveryDO.setUrgent(urgent);
305     } catch (Exception JavaDoc e) {
306         throw new AirSentException(e);
307     }
308     }
309
310     /**
311      * gets the size description
312      *
313      * @return size
314      * @exception if an error occurs
315      */

316     public String JavaDoc getSize()
317         throws AirSentException {
318     try {
319         return deliveryDO.getSize();
320     } catch (Exception JavaDoc e) {
321         throw new AirSentException(e);
322     }
323     }
324
325     /**
326      * sets the size for the order
327      *
328      * @param size size description
329      * @exception if an error occurs
330      */

331     public void setSize(String JavaDoc size)
332         throws AirSentException, AirSentBusinessException {
333         if (size.length() > Delivery.MAX_DESC) {
334             throw new AirSentBusinessException("Exceeds maximum size");
335         }
336     try {
337         deliveryDO.setSize(size);
338     } catch (Exception JavaDoc e) {
339         throw new AirSentException(e);
340     }
341     }
342
343     /**
344      * gets the descriton of the order
345      *
346      * @return descriotn
347      * @exception if an error occurs
348      */

349     public String JavaDoc getDescription()
350         throws AirSentException {
351     try {
352         return deliveryDO.getDescription();
353     } catch (Exception JavaDoc e) {
354         throw new AirSentException(e);
355     }
356     }
357
358     /**
359      * sets the order description
360      *
361      * @param description order description
362      * @exception if an error occurs
363      * @exception if the description is invalid
364      */

365     public void setDescription(String JavaDoc description)
366         throws AirSentException, AirSentBusinessException {
367         if (description.length() > Delivery.MAX_DESC) {
368             throw new AirSentBusinessException("Exceeds maximum size");
369         }
370     try {
371         deliveryDO.setDescription(description);
372     } catch (Exception JavaDoc e) {
373         throw new AirSentException(e);
374     }
375     }
376
377     /**
378      * saves the order
379      *
380      * @exception if an error occurs
381      */

382     public void save()
383         throws AirSentException {
384     try {
385             deliveryDO.commit();
386     } catch (Exception JavaDoc e) {
387             e.printStackTrace();
388         throw new AirSentException(e);
389     }
390     update.setUpdateState(System.currentTimeMillis());
391     }
392
393     /**
394      * deletes the order
395      *
396      * @exception if an error occurs
397      */

398     public void delete()
399         throws AirSentException {
400     try {
401         deliveryDO.delete();
402     } catch (Exception JavaDoc e) {
403         throw new AirSentException(e);
404     }
405     update.setUpdateState(System.currentTimeMillis());
406     }
407
408     /**
409      * returns the pick up time
410      *
411      * @return pick up time
412      * @exception if an error occurs
413      */

414     public String JavaDoc getPickedUpTime()
415         throws AirSentException {
416     long time = 0;
417     try {
418         if ((time = deliveryDO.getPickedUpTime()) == 0) {
419         return "-";
420         } else {
421         return formatTime(time);
422         }
423     } catch (Exception JavaDoc e) {
424         throw new AirSentException(e);
425     }
426     }
427
428     /**
429      * returns the drop off time
430      *
431      * @return drop off time
432      * @exception if an error occurs
433      */

434     public String JavaDoc getDroppedOffTime()
435         throws AirSentException {
436     long time = 0;
437     try {
438         if ((time = deliveryDO.getDroppedOffTime()) == 0) {
439         return "-";
440         } else {
441         return formatTime(time);
442         }
443     } catch (Exception JavaDoc e) {
444         throw new AirSentException(e);
445     }
446     }
447
448     /**
449      * Chnages the order to be 'picked up'
450      *
451      * @exception if an error occurs
452      */

453     public void pickUp()
454         throws AirSentException {
455     try {
456         deliveryDO.setIsPickedUp(true);
457         deliveryDO.setPickedUpTime(System.currentTimeMillis());
458         save();
459     } catch (Exception JavaDoc e) {
460         throw new AirSentException(e);
461     }
462     }
463
464     /**
465      * Changes the order to be in the 'drop off' state
466      *
467      * @exception if an error occurs
468      */

469     public void dropOff()
470         throws AirSentException {
471     try {
472         deliveryDO.setIsDroppedOff(true);
473         deliveryDO.setDroppedOffTime(System.currentTimeMillis());
474         save();
475     } catch (Exception JavaDoc e) {
476         throw new AirSentException(e);
477     }
478     }
479
480     /**
481      * returns the pick up statues
482      *
483      * @return true if order has been pickup
484      * @exception if an error occurs
485      */

486     public boolean isPickedUp()
487         throws AirSentException {
488     try {
489         return deliveryDO.getIsPickedUp();
490     } catch (Exception JavaDoc e) {
491         throw new AirSentException(e);
492     }
493     }
494
495     /**
496      * returns the drop off status
497      *
498      * @return true if the order has been dropped off
499      * @exception if an error occurs
500      */

501     public boolean isDroppedOff()
502         throws AirSentException {
503     try {
504         return deliveryDO.getIsDroppedOff();
505     } catch (Exception JavaDoc e) {
506         throw new AirSentException(e);
507     }
508     }
509
510
511     /*
512      * Get invoice number.
513      */

514     public int getInvoice()
515         throws AirSentException {
516     try {
517         return deliveryDO.getInvoice();
518     } catch (Exception JavaDoc e) {
519         throw new AirSentException(e);
520     }
521     }
522
523     /*
524      * Set invoice number.
525      */

526     public void setInvoice(int invoice)
527         throws AirSentException {
528     try {
529         deliveryDO.setInvoice(invoice);
530     } catch (Exception JavaDoc e) {
531         throw new AirSentException(e);
532     }
533     }
534
535     /*
536      * Format the time for the day.
537      *
538      * @param long mSeconds
539      * @return String
540      */

541     private String JavaDoc formatTime(long time) {
542     return new Date(time).toGMTString();
543     }
544 }
545
546
Popular Tags