ea782a0c3a17a3772a0a2246604700773632b142
[RRRRHHHH_Code] / ruralHouses / src / businessLogic / OfferManager.java
1 package businessLogic;
2
3 import java.rmi.RemoteException;
4 import java.sql.Date;
5
6 import com.db4o.ObjectContainer;
7 import com.db4o.ObjectSet;
8
9 import dataAccess.DB4oManager;
10 import domain.Booking;
11 import domain.Offer;
12 import domain.RuralHouse;
13 import exceptions.BadDates;
14 import exceptions.OfferCanNotBeBooked;
15 import exceptions.OverlappingOfferExists;
16
17 public final class OfferManager {
18
19         private int offerNumber = 0;
20         dataAccess.DB4oManager dbMngr;
21         private static OfferManager theOfferManager;
22
23         public OfferManager() {
24                 try {
25                         this.dbMngr = DB4oManager.getInstance();
26                 } catch (Exception e) {
27                         e.printStackTrace();
28                 }
29         }
30
31         public static int getNumber() {
32                 ObjectContainer db=DB4oManager.getContainer();
33                 OfferManager o=getInstance();
34                 o.offerNumber++;
35                 db.store(o);
36                 db.commit();
37                 return o.offerNumber;
38         }
39
40         /**
41          * This method returns the instance of the OfferManager class 
42          * 
43          * @return the offer manager
44          */
45         public static OfferManager getInstance()  {
46                 ObjectContainer db=DB4oManager.getContainer();
47                 OfferManager b = new OfferManager();
48                 ObjectSet result = db.queryByExample(b);
49                 if (!result.hasNext()){
50                         theOfferManager = new OfferManager();
51                         db.store(theOfferManager);
52                         db.commit();
53                 } else theOfferManager=(OfferManager)result.next();
54                 return theOfferManager;
55         }
56         
57         /**
58          * This method creates an offer with a house number, first day, last day and price
59          * 
60          * @param House
61          *            number, start day, last day and price
62          * @return the created offer, or null, or an exception
63          */
64         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
65                         float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
66                 if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
67
68                 boolean b = dbMngr.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
69                 if (!b) return dbMngr.createOffer(ruralHouse,firstDay,lastDay,price);                   
70                 return null;
71         }
72
73         
74 }