3 import java.rmi.RemoteException;
6 import com.db4o.ObjectContainer;
7 import com.db4o.ObjectSet;
9 import dataAccess.DB4oManager;
11 import domain.RuralHouse;
12 import exceptions.BadDates;
13 import exceptions.OverlappingOfferExists;
15 public final class OfferManager {
17 private int offerNumber = 0;
18 dataAccess.DB4oManager dbMngr;
19 private static OfferManager theOfferManager;
21 public OfferManager() {
23 this.dbMngr = DB4oManager.getInstance();
24 } catch (Exception e) {
29 public static int getNumber() {
30 ObjectContainer db=DB4oManager.getContainer();
31 OfferManager o=getInstance();
39 * This method returns the instance of the OfferManager class
41 * @return the offer manager
43 public static OfferManager getInstance() {
44 ObjectContainer db=DB4oManager.getContainer();
45 OfferManager b = new OfferManager();
46 ObjectSet result = db.queryByExample(b);
47 if (!result.hasNext()){
48 theOfferManager = new OfferManager();
49 db.store(theOfferManager);
51 } else theOfferManager=(OfferManager)result.next();
52 return theOfferManager;
56 * This method creates an offer with a house number, first day, last day and price
59 * number, start day, last day and price
60 * @return the created offer, or null, or an exception
62 public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
63 float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
64 if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
66 boolean b = dbMngr.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
68 ruralHouse.createOffer(offerNumber, firstDay, lastDay, price);
69 return dbMngr.createOffer(ruralHouse,firstDay,lastDay,price);
74 public Offer modifyOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
75 float price, Offer offer) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
76 if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
77 offer.setFirstDay(firstDay);
78 offer.setLastDay(lastDay);
79 offer.setPrice(price);
81 return dbMngr.modifyOffer(offer);
84 public void deleteOffer(RuralHouse rh, Offer o) throws RemoteException, Exception{
85 rh.offers.removeElement(o);
86 dbMngr.deleteOffer(rh, o);