KickJava   Java API By Example, From Geeks To Geeks.

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


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: DeliveryManagerImpl.java,v 1.1 2004/05/19 18:15:19
18  */

19
20 package com.lutris.airsent.business.delivery;
21
22
23 import com.lutris.airsent.spec.AirSentException;
24
25 import com.lutris.airsent.business.*;
26
27 import com.lutris.airsent.business.delivery.*;
28 import com.lutris.airsent.spec.delivery.DeliveryManager;
29
30 import com.lutris.airsent.spec.delivery.*;
31 import com.lutris.airsent.data.delivery.*;
32 import com.lutris.airsent.spec.address.Address;
33 import com.lutris.airsent.spec.messenger.Messenger;
34 import com.lutris.airsent.data.person.MessengerDO;
35 import com.lutris.airsent.spec.customer.Customer;
36
37 import com.lutris.airsent.data.person.CustomerDO;
38 import com.lutris.appserver.server.sql.*;
39 import com.lutris.dods.builder.generator.query.*;
40 import java.sql.Date JavaDoc;
41
42 /**
43  * Implementation of the delivery manger.
44  */

45 public class DeliveryManagerImpl implements DeliveryManager {
46
47     private Update update = null;
48     
49     /**
50      * creates the Manager
51      */

52     public DeliveryManagerImpl() {
53     this.update = new UpdateImpl(System.currentTimeMillis());
54     }
55
56     /**
57      * Gets the last update time.
58      *
59      * @param browserState
60      * @param wait
61      * @return update time
62      * @exception if an error occurs
63      */

64     public long getUpdate(long browserState, long wait)
65         throws AirSentBusinessException {
66     try {
67         return update.needUpdate(browserState, wait);
68     } catch (Exception JavaDoc e) {
69         throw new AirSentBusinessException(e);
70     }
71     }
72
73     /**
74      * Create a new delivery.
75      *
76      * @param customer customer associated with the order
77      * @return delivey object
78      * @exception if an error occurs
79      */

80     public Delivery create(Customer customer)
81         throws AirSentBusinessException {
82     try {
83             return new DeliveryImpl(update, customer);
84     } catch (Exception JavaDoc e) {
85         throw new AirSentBusinessException(e);
86     }
87     }
88
89     /**
90      * Create a delivery based on an order.
91      *
92      * @return delivey object
93      * @exception if an error occurs
94      * @exception if business rules are violated
95      */

96     public Delivery create(Customer customer, OrderForm orderForm)
97         throws AirSentException, AirSentBusinessException {
98     try {
99             Delivery d;
100             if (orderForm.getOrderId() == null) {
101                 d = new DeliveryImpl(update, customer);
102             } else {
103                 d = new DeliveryImpl(update, orderForm.getOrderId());
104             }
105             d.setIsFragile(orderForm.getFragile());
106             d.setIsUrgent(orderForm.getUrgent());
107             d.setSize(orderForm.getSize());
108             d.setDescription(orderForm.getDescription());
109
110             Address pua = d.getPickUp();
111             pua.setName(orderForm.getPickUpName());
112             pua.setStreet1(orderForm.getPickUpAddress1());
113             pua.setLocalNumber(orderForm.getPickUpPhone());
114             pua.setDirections(orderForm.getPickUpDirections());
115
116             Address doa = d.getDropOff();
117             doa.setName(orderForm.getDropOffName());
118             doa.setStreet1(orderForm.getDropOffAddress1());
119             doa.setLocalNumber(orderForm.getDropOffPhone());
120             doa.setDirections(orderForm.getDropOffDirections());
121             return d;
122     } catch (AirSentBusinessException be) {
123         throw be;
124     } catch (Exception JavaDoc e) {
125         throw new AirSentException(e);
126     }
127     }
128
129     /**
130      * Find deliver by database id
131      *
132      * @param handle database id
133      * @return delivery
134      * @exception if an error occurs
135      */

136     public Delivery findByHandle(String JavaDoc handle)
137             throws AirSentBusinessException {
138         try {
139             DeliveryQuery query = new DeliveryQuery();
140             query.requireUniqueInstance();
141             query.setQueryOId(new ObjectId(handle));
142             DeliveryDO theDeliveryDO = query.getNextDO();
143             return new DeliveryImpl(update, theDeliveryDO);
144         } catch (Exception JavaDoc e) {
145             throw new AirSentBusinessException(e);
146         }
147     }
148
149     /**
150      * Find deliveries associated with a given customer
151      *
152      * @param customer the customer
153      * @return delivery
154      * @exception if an error occurs
155      */

156     public Delivery[] findByCustomer(Customer customer)
157         throws AirSentBusinessException {
158
159         Delivery[] theDeliveryArray = null;
160         try {
161             DeliveryQuery query = new DeliveryQuery();
162             QueryBuilder qb = query.getQueryBuilder();
163             qb.addEndClause(" order by oid");
164             query.setQueryCustomerID(CustomerDO.createExisting(customer.getHandle()));
165             DeliveryDO[] DOarray = query.getDOArray();
166             theDeliveryArray = new Delivery[DOarray.length];
167             for (int i = 0; i < DOarray.length; i++) {
168                 theDeliveryArray[i] = new DeliveryImpl(update, DOarray[i]);
169             }
170         } catch (Exception JavaDoc e) {
171             throw new AirSentBusinessException(e);
172         }
173         return theDeliveryArray;
174     }
175
176     /**
177      * Find all the deliveries.
178      *
179      * @return delivery objects
180      * @exception if an error occurs
181      */

182     public Delivery[] findAll()
183         throws AirSentBusinessException {
184     Delivery[] theDeliveryArray = null;
185     try {
186         DeliveryQuery query = new DeliveryQuery();
187             QueryBuilder qb = query.getQueryBuilder();
188             qb.addEndClause(" order by oid");
189         DeliveryDO[] DOarray = query.getDOArray();
190         theDeliveryArray = new Delivery[DOarray.length];
191         for (int i = 0; i < DOarray.length; i++) {
192         theDeliveryArray[i] = new DeliveryImpl(update, DOarray[i]);
193         }
194     } catch (Exception JavaDoc e) {
195         throw new AirSentBusinessException(e);
196     }
197     return theDeliveryArray;
198     }
199
200
201     /**
202      * Find the specified number the deliveries.
203      *
204      * @param subset desired number of deliveries
205      * @return delivery objects
206      * @exception if an error occurs
207      */

208     public Delivery[] findSubset(int subset)
209         throws AirSentBusinessException {
210     Delivery[] theDeliveryArray = null;
211     try {
212         DeliveryQuery query = new DeliveryQuery();
213             QueryBuilder qb = query.getQueryBuilder();
214             qb.addEndClause(" order by oid");
215         DeliveryDO[] DOarray = query.getDOArray();
216         int count = 0;
217         if(DOarray.length <= subset) {
218         count = DOarray.length;
219         } else {
220         count = subset;
221         }
222         theDeliveryArray = new Delivery[count];
223         for (int i = 0; i < count; i++) {
224         theDeliveryArray[i] = new DeliveryImpl(update, DOarray[i]);
225         }
226     } catch (Exception JavaDoc e) {
227         throw new AirSentBusinessException(e);
228     }
229     return theDeliveryArray;
230     }
231
232     /**
233      * find all deliveries associated with a given messenger
234      *
235      * @param messenger messenger
236      * @return delivery objects
237      * @exception if an error occurs
238      */

239     public Delivery[] findByMessenger(Messenger messenger)
240         throws AirSentBusinessException {
241     Delivery[] theDeliveryArray = null;
242     try {
243         DeliveryQuery query = new DeliveryQuery();
244         query.setQueryMessengerID(MessengerDO.createExisting(messenger.getHandle()));
245         //SV query.openParen();
246
//SV query.setQueryIsPickedUp(false);
247
//SV query.or();
248
//SV query.setQueryIsDroppedOff(false);
249
//SV query.closeParen();
250
DeliveryDO[] DOarray = query.getDOArray();
251         int len=0;
252         for (int i = 0; i < DOarray.length; i++) {
253               if ((!DOarray[i].getIsPickedUp()) || (!DOarray[i].getIsDroppedOff())){
254                 len=len+1;
255               }
256         }
257         theDeliveryArray = new Delivery[len];
258         int j=0;
259         for (int i = 0; i < DOarray.length; i++) {
260               if ((!DOarray[i].getIsPickedUp()) || (!DOarray[i].getIsDroppedOff())){
261                 theDeliveryArray[j] = new DeliveryImpl(update, DOarray[i]);
262                 j=j+1;
263               }
264         }
265     } catch (Exception JavaDoc e) {
266       e.printStackTrace();
267         throw new AirSentBusinessException(e);
268     }
269     return theDeliveryArray;
270     }
271
272     // -----------------------------------------------------------------
273
// not currenty used
274
// -----------------------------------------------------------------
275

276     /**
277      * find all delivery objects not delivered
278      *
279      * @return delivery objects
280      * @exception if an error occurs
281      */

282     public Delivery[] findUndelivered()
283         throws AirSentBusinessException {
284     Delivery[] theDeliveryArray = null;
285     try {
286         DeliveryQuery query = new DeliveryQuery();
287         query.setQueryIsPickedUp(false);
288         query.or();
289         query.setQueryIsDroppedOff(false);
290         DeliveryDO[] DOarray = query.getDOArray();
291         theDeliveryArray = new Delivery[DOarray.length];
292         for (int i = 0; i < DOarray.length; i++) {
293         theDeliveryArray[i] = new DeliveryImpl(update, DOarray[i]);
294         }
295     } catch (Exception JavaDoc e) {
296         throw new AirSentBusinessException(e);
297     }
298     return theDeliveryArray;
299     }
300
301     /**
302      * Find deliveries by invoice
303      *
304      * @param invoice invoice id
305      * @return delivery
306      * @exception if an error occurs
307      */

308     public Delivery findByInvoice(int invoice)
309         throws AirSentBusinessException {
310     Delivery theDelivery = null;
311     try {
312         DeliveryQuery query = new DeliveryQuery();
313         query.requireUniqueInstance();
314         query.setQueryInvoice(invoice);
315         DeliveryDO theDeliveryDO = query.getNextDO();
316         theDelivery = new DeliveryImpl(update, theDeliveryDO);
317         return theDelivery;
318     } catch (Exception JavaDoc e) {
319         throw new AirSentBusinessException(e);
320     }
321     }
322
323     /**
324      * Find all the deliveries after a given time.
325      *
326      * @param data time
327      * @return delivery objects
328      * @exception if an error occurs
329      */

330     public Delivery[] findAllAfter(long date)
331         throws AirSentBusinessException {
332     Delivery[] theDeliveryArray = null;
333     Date JavaDoc searchDate = new Date JavaDoc(date);
334     try {
335         DeliveryQuery query = new DeliveryQuery();
336         QueryBuilder builder = query.getQueryBuilder();
337         builder.addWhere(DeliveryDO.PickedUpTime, searchDate, QueryBuilder.GREATER_THAN);
338         builder.addWhereOr();
339         builder.addWhere(DeliveryDO.DroppedOffTime, searchDate, QueryBuilder.GREATER_THAN);
340         DeliveryDO[] DOarray = query.getDOArray();
341         theDeliveryArray = new Delivery[DOarray.length];
342         for (int i = 0; i < DOarray.length; i++) {
343         theDeliveryArray[i] = new DeliveryImpl(update, DOarray[i]);
344         }
345     } catch (Exception JavaDoc e) {
346         throw new AirSentBusinessException(e);
347     }
348     return theDeliveryArray;
349     }
350 }
351
352
Popular Tags