1 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 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 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 e) { 83 throw new AirSentException(e); 84 } 85 } 86 87 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 e) { 103 throw new AirSentBusinessException(e); 104 } 105 } 106 107 113 DeliveryImpl(Update update, String 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 e) { 123 throw new AirSentBusinessException(e); 124 } 125 } 126 127 133 public String getHandle() 134 throws AirSentException { 135 try { 136 return deliveryDO.getHandle(); 137 } catch (Exception e) { 138 throw new AirSentException(e); 139 } 140 } 141 142 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 e) { 155 throw new AirSentException(e); 156 } 157 } 158 return pickUpAddress; 159 } 160 161 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 e) { 174 throw new AirSentException(e); 175 } 176 } 177 return dropOffAddress; 178 } 179 180 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 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 throw new AirSentBusinessException(doe); 205 } catch (Exception e) { 206 throw new AirSentException(e); 207 } 208 } 209 210 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 e) { 227 throw new AirSentException(e); 228 } 229 } 230 231 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 throw new AirSentBusinessException(doe); 245 } catch (Exception e) { 246 throw new AirSentException(e); 247 } 248 } 249 250 256 public boolean isFragile() 257 throws AirSentException { 258 try { 259 return deliveryDO.getFragile(); 260 } catch (Exception e) { 261 throw new AirSentException(e); 262 } 263 } 264 265 271 public void setIsFragile(boolean fragile) 272 throws AirSentException { 273 try { 274 this.deliveryDO.setFragile(fragile); 275 } catch (Exception e) { 276 throw new AirSentException(e); 277 } 278 } 279 280 286 public boolean isUrgent() 287 throws AirSentException { 288 try { 289 return deliveryDO.getUrgent(); 290 } catch (Exception e) { 291 throw new AirSentException(e); 292 } 293 } 294 295 301 public void setIsUrgent(boolean urgent) 302 throws AirSentException { 303 try { 304 deliveryDO.setUrgent(urgent); 305 } catch (Exception e) { 306 throw new AirSentException(e); 307 } 308 } 309 310 316 public String getSize() 317 throws AirSentException { 318 try { 319 return deliveryDO.getSize(); 320 } catch (Exception e) { 321 throw new AirSentException(e); 322 } 323 } 324 325 331 public void setSize(String 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 e) { 339 throw new AirSentException(e); 340 } 341 } 342 343 349 public String getDescription() 350 throws AirSentException { 351 try { 352 return deliveryDO.getDescription(); 353 } catch (Exception e) { 354 throw new AirSentException(e); 355 } 356 } 357 358 365 public void setDescription(String 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 e) { 373 throw new AirSentException(e); 374 } 375 } 376 377 382 public void save() 383 throws AirSentException { 384 try { 385 deliveryDO.commit(); 386 } catch (Exception e) { 387 e.printStackTrace(); 388 throw new AirSentException(e); 389 } 390 update.setUpdateState(System.currentTimeMillis()); 391 } 392 393 398 public void delete() 399 throws AirSentException { 400 try { 401 deliveryDO.delete(); 402 } catch (Exception e) { 403 throw new AirSentException(e); 404 } 405 update.setUpdateState(System.currentTimeMillis()); 406 } 407 408 414 public String 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 e) { 424 throw new AirSentException(e); 425 } 426 } 427 428 434 public String 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 e) { 444 throw new AirSentException(e); 445 } 446 } 447 448 453 public void pickUp() 454 throws AirSentException { 455 try { 456 deliveryDO.setIsPickedUp(true); 457 deliveryDO.setPickedUpTime(System.currentTimeMillis()); 458 save(); 459 } catch (Exception e) { 460 throw new AirSentException(e); 461 } 462 } 463 464 469 public void dropOff() 470 throws AirSentException { 471 try { 472 deliveryDO.setIsDroppedOff(true); 473 deliveryDO.setDroppedOffTime(System.currentTimeMillis()); 474 save(); 475 } catch (Exception e) { 476 throw new AirSentException(e); 477 } 478 } 479 480 486 public boolean isPickedUp() 487 throws AirSentException { 488 try { 489 return deliveryDO.getIsPickedUp(); 490 } catch (Exception e) { 491 throw new AirSentException(e); 492 } 493 } 494 495 501 public boolean isDroppedOff() 502 throws AirSentException { 503 try { 504 return deliveryDO.getIsDroppedOff(); 505 } catch (Exception e) { 506 throw new AirSentException(e); 507 } 508 } 509 510 511 514 public int getInvoice() 515 throws AirSentException { 516 try { 517 return deliveryDO.getInvoice(); 518 } catch (Exception e) { 519 throw new AirSentException(e); 520 } 521 } 522 523 526 public void setInvoice(int invoice) 527 throws AirSentException { 528 try { 529 deliveryDO.setInvoice(invoice); 530 } catch (Exception e) { 531 throw new AirSentException(e); 532 } 533 } 534 535 541 private String formatTime(long time) { 542 return new Date(time).toGMTString(); 543 } 544 } 545 546 | Popular Tags |