tmp
[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          * This method creates an offer with a house number, first day, last day and price
28          * 
29          * @param House
30          *            number, start day, last day and price
31          * @return the created offer, or null, or an exception
32          */
33         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
34                         float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
35                 if (firstDay.after(lastDay)||firstDay==null||lastDay==null)
36                         throw new BadDates();
37
38                 boolean b = dbMngr.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
39                 if (!b) {
40                         dbMngr.createOffer(ruralHouse,firstDay,lastDay,price);
41                         return ruralHouse.createOffer(offerNumber, firstDay, lastDay, price);                   
42                 }
43                 return null;
44         }
45
46         
47         public void deleteOffer(RuralHouse rh, Offer o) throws RemoteException, Exception{
48                 rh.offers.removeElement(o);
49                 dbMngr.deleteOffer( o);
50         }
51
52 }