unification with the actual initial project. Some things are new now, but there has...
[RRRRHHHH_Code] / ruralHouses / src / businessLogic / FacadeImplementation.java
index 0caab61..b97613d 100644 (file)
@@ -2,94 +2,112 @@ package businessLogic;
 
 import java.rmi.RemoteException;
 import java.rmi.server.UnicastRemoteObject;
-import java.util.Date;
-import java.util.Vector;
+import java.sql.Date;
 import java.sql.SQLException;
+import java.util.Vector;
 
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
 
-import configuration.Config;
+import configuration.ConfigXML;
 import dataAccess.DB4oManager;
 import domain.Booking;
 import domain.Offer;
 import domain.Owner;
 import domain.RuralHouse;
+import exceptions.BadDates;
+import exceptions.DB4oManagerCreationException;
 import exceptions.OfferCanNotBeBooked;
+import exceptions.OverlappingOfferExists;
+
 
 public class FacadeImplementation extends UnicastRemoteObject implements ApplicationFacadeInterface {
 
+       /**
+        * 
+        */
        private static final long serialVersionUID = 1L;
-       DB4oManager dbMngr;
 
-       public FacadeImplementation() throws RemoteException, InstantiationException,
-       IllegalAccessException, ClassNotFoundException, SQLException {
-               dbMngr = DB4oManager.getInstance();
-       }
+       Vector<Owner> owners;
+       Vector<RuralHouse> ruralHouses;
+       DB4oManager dB4oManager;
 
-
-       public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
-                       float price) throws RemoteException, Exception {                 
-               return dbMngr.createOffer(ruralHouse, firstDay, lastDay, price);
+       public FacadeImplementation() throws RemoteException, InstantiationException,
+                       IllegalAccessException, ClassNotFoundException, SQLException, DB4oManagerCreationException {
+               owners=null;
+               ruralHouses=null;
+               try{
+                       dB4oManager=DB4oManager.getInstance();
+               }
+               catch (com.db4o.ext.DatabaseFileLockedException e) {
+                       System.out.println("Error in FacadeImplementation: "+e.toString());
+                       throw e;
+               }
+               catch (Exception e) {
+                       System.out.println("Error in FacadeImplementation: "+e.toString());
+                       throw new DB4oManagerCreationException();
+               }
        }
+       
 
        /**
-        * This method obtains available offers for a concrete house in a certain period 
+        * This method creates an offer with a house number, first day, last day and price
         * 
-        * @param houseNumber, the house number where the offers must be obtained 
-        * @param firstDay, first day in a period range 
-        * @param lastDay, last day in a period range
-        * @return a vector of offers(Offer class)  available  in this period
+        * @param House
+        *            number, start day, last day and price
+        * @return the created offer, or null, or an exception
         */
-       public Vector<Offer> getOffers(RuralHouse house,Date firstDay, Date lastDay) throws RemoteException,
-       Exception {             
-               return house.getOffers(firstDay, lastDay);
-
+       public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
+                       float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
+               if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
+               ruralHouses=null;
+               owners=null;
+               boolean b = dB4oManager.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
+               if (!b) return dB4oManager.createOffer(ruralHouse,firstDay,lastDay,price);                      
+               return null;
        }
+
        /**
-        * This method finds all existing  owners 
+        * This method creates a book with a corresponding parameters
         * 
+        * @param First
+        *            day, last day, house number and telephone
+        * @return a book
         */
-       public Vector<Owner> getOwners() throws RemoteException,
-       Exception {
-               return dbMngr.getOwners();
-       } 
+       public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber)
+                       throws OfferCanNotBeBooked {
+               ruralHouses=null;
+               owners=null;
+               return dB4oManager.createBooking(ruralHouse,firstDate,lastDate,bookTelephoneNumber);
+       }
+
 
        /**
-        * This method obtains an owner's rural house
+        * This method existing  owner
         * 
-        * @param owner object
-        *            
-        * @return a vector of Rural Houses
         */
-       public Vector<RuralHouse> getRuralHouses(Owner owner)
-                       throws RemoteException {
-               return owner.getRuralHouses();          
+       public Vector<Owner> getOwners() throws RemoteException,
+                       Exception {
+               
+               if (owners!=null) { System.out.println("Owners obtained directly from business logic layer");
+                                                       return owners; }
+               else return owners=dB4oManager.getOwners();
        }
-
-
+               
        public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
-       Exception {             
-               return dbMngr.getAllRuralHouses();              
-       }
+       Exception {
+               
+               if (ruralHouses!=null) { System.out.println("RuralHouses obtained directly from business logic layer");
+                                                                return ruralHouses; }
+               else return ruralHouses=dB4oManager.getAllRuralHouses();
 
+       }
        
+       public void close() throws RemoteException{
+               dB4oManager.close();
 
-       public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber)
-                       throws OfferCanNotBeBooked {
-               try {
-                       RuralHouse rh=dbMngr.getRuralHouse(ruralHouse);
-                       Offer offer = rh.findOffer(firstDate, lastDate);
-                       Booking b=dbMngr.createBooking(offer,bookTelephoneNumber);
-                       return b;
-               } catch (Exception exc) {
-                       exc.printStackTrace();
-                       return null;
-               }
-               
        }
 
-       
-
-}
+       }