8c6fb3e179b007a69c4531a47874d35a92b8ace4
[RRRRHHHH_Code] / ruralHouses / src / businessLogic / OfferManager.java
1 package businessLogic;
2
3 import java.rmi.RemoteException;
4 import java.sql.Date;
5
6 import dataAccess.DB4oManager;
7 import domain.Offer;
8 import domain.RuralHouse;
9 import exceptions.BadDates;
10 import exceptions.OverlappingOfferExists;
11
12 public final class OfferManager {
13
14         private int offerNumber = 0;
15         dataAccess.DB4oManager dbMngr;
16
17         public OfferManager() {
18                 try {
19                         this.dbMngr = DB4oManager.getInstance();
20                 } catch (Exception e) {
21                         e.printStackTrace();
22                 }
23         }
24
25
26         
27         /**
28          * This method creates an offer with a house number, first day, last day and price
29          * 
30          * @param House
31          *            number, start day, last day and price
32          * @return the created offer, or null, or an exception
33          */
34         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
35                         float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
36                 if (firstDay.after(lastDay)||firstDay==null||lastDay==null)
37                         throw new BadDates();
38
39                 boolean b = dbMngr.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
40                 if (!b) {
41                         dbMngr.createOffer(ruralHouse,firstDay,lastDay,price);
42                         return ruralHouse.createOffer(offerNumber, firstDay, lastDay, price);                   
43                 }
44                 return null;
45         }
46
47         
48         public void deleteOffer(RuralHouse rh, Offer o) throws RemoteException, Exception{
49                 rh.offers.removeElement(o);
50                 dbMngr.deleteOffer( o);
51         }
52
53 }