From 44451eb517505374d4c8ea1c2c706307f9b0a382 Mon Sep 17 00:00:00 2001 From: camjan Date: Sun, 8 Mar 2015 13:59:14 +0100 Subject: [PATCH] Some minor changes and the logic for searching rural houses by the name of the town done --- .../src/businessLogic/HouseManager.java | 8 + .../businessLogic/HouseManagerInterface.java | 1 + ruralHouses/src/dataAccess/DB4oManager.java | 357 ++++++++++-------- ruralHouses/src/gui/DeleteHouseGUI.java | 4 +- ruralHouses/src/gui/NewHouseGUI.java | 12 +- 5 files changed, 214 insertions(+), 168 deletions(-) diff --git a/ruralHouses/src/businessLogic/HouseManager.java b/ruralHouses/src/businessLogic/HouseManager.java index 82eedfc..1463ad5 100644 --- a/ruralHouses/src/businessLogic/HouseManager.java +++ b/ruralHouses/src/businessLogic/HouseManager.java @@ -47,6 +47,14 @@ public class HouseManager implements HouseManagerInterface { this.dbMngr.removeHouse(houseNumber); } + + + @Override + public Vector getHousesByTown(String town) { + + return this.dbMngr.getRuralHousesByTown(town); + } + // For future implementation // @Override // public void modifyHouse(int houseNumber, Owner owner, String description, diff --git a/ruralHouses/src/businessLogic/HouseManagerInterface.java b/ruralHouses/src/businessLogic/HouseManagerInterface.java index 65e2baa..ca99f40 100644 --- a/ruralHouses/src/businessLogic/HouseManagerInterface.java +++ b/ruralHouses/src/businessLogic/HouseManagerInterface.java @@ -24,4 +24,5 @@ public interface HouseManagerInterface { public void removeHouse(int houseNumber); + public Vector getHousesByTown(String town); } diff --git a/ruralHouses/src/dataAccess/DB4oManager.java b/ruralHouses/src/dataAccess/DB4oManager.java index 45f847c..80bc080 100644 --- a/ruralHouses/src/dataAccess/DB4oManager.java +++ b/ruralHouses/src/dataAccess/DB4oManager.java @@ -4,7 +4,6 @@ import java.io.File; //import java.util.Enumeration; //import java.util.Vector; - import java.rmi.RemoteException; import java.sql.Date; import java.util.HashSet; @@ -25,97 +24,99 @@ import domain.Owner; import domain.RuralHouse; import exceptions.OfferCanNotBeBooked; import exceptions.OverlappingOfferExists; -public class DB4oManager { - private static ObjectContainer db; +public class DB4oManager { + + private static ObjectContainer db; private static EmbeddedConfiguration configuration; private static ClientConfiguration configurationCS; - private int bookingNumber=0; // if it is "static" then it is not serialized - private int offerNumber=0; // if it is "static" then it is not serialized - private static DB4oManager theDB4oManager=null; + private int bookingNumber = 0; // if it is "static" then it is not + // serialized + private int offerNumber = 0; // if it is "static" then it is not serialized + private static DB4oManager theDB4oManager = null; private static DB4oManagerAux theDB4oManagerAux; static ConfigXML c; private DB4oManager() throws Exception { - theDB4oManagerAux=new DB4oManagerAux(0,0); - c=ConfigXML.getInstance(); - System.out.println("Creating DB4oManager instance => isDatabaseLocal: "+c.isDatabaseLocal()+" getDatabBaseOpenMode: "+c.getDataBaseOpenMode()); + theDB4oManagerAux = new DB4oManagerAux(0, 0); + c = ConfigXML.getInstance(); + System.out.println("Creating DB4oManager instance => isDatabaseLocal: " + + c.isDatabaseLocal() + " getDatabBaseOpenMode: " + + c.getDataBaseOpenMode()); - - if ((c.getDataBaseOpenMode().equals("initialize")) && (c.isDatabaseLocal())) - new File(c.getDb4oFilename()).delete(); - - if (c.isDatabaseLocal()) - { + if ((c.getDataBaseOpenMode().equals("initialize")) + && (c.isDatabaseLocal())) + new File(c.getDb4oFilename()).delete(); + + if (c.isDatabaseLocal()) { configuration = Db4oEmbedded.newConfiguration(); configuration.common().activationDepth(c.getActivationDepth()); configuration.common().updateDepth(c.getUpdateDepth()); - db=Db4oEmbedded.openFile(configuration, c.getDb4oFilename()); + db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename()); System.out.println("DataBase opened"); - } - else // c.isDatabaseLocal==false - { + } else // c.isDatabaseLocal==false + { openObjectContainer(); } - if (c.getDataBaseOpenMode().equals("initialize")) - { initializeDB(); + if (c.getDataBaseOpenMode().equals("initialize")) { + initializeDB(); System.out.println("DataBase initialized"); + } else // c.getDataBaseOpenMode().equals("open") + + { + ObjectSet res = db.queryByExample(DB4oManagerAux.class); + ListIterator listIter = res.listIterator(); + if (listIter.hasNext()) + theDB4oManagerAux = (DB4oManagerAux) res.next(); } - else // c.getDataBaseOpenMode().equals("open") - - { - ObjectSet res =db.queryByExample(DB4oManagerAux.class); - ListIterator listIter = res.listIterator(); - if (listIter.hasNext()) theDB4oManagerAux = (DB4oManagerAux) res.next(); - } - } + } - private static void openDB(){ + private static void openDB() { configuration = Db4oEmbedded.newConfiguration(); configuration.common().activationDepth(c.getActivationDepth()); configuration.common().updateDepth(c.getUpdateDepth()); - db=Db4oEmbedded.openFile(configuration, c.getDb4oFilename()); + db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename()); } - - private void openObjectContainer(){ - + + private void openObjectContainer() { + configurationCS = Db4oClientServer.newClientConfiguration(); configurationCS.common().activationDepth(c.getActivationDepth()); configurationCS.common().updateDepth(c.getUpdateDepth()); configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true); - db = Db4oClientServer.openClient(configurationCS,c.getDatabaseNode(), - c.getDatabasePort(),c.getUser(),c.getPassword()); + db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(), + c.getDatabasePort(), c.getUser(), c.getPassword()); } - + class DB4oManagerAux { int bookingNumber; int offerNumber; - DB4oManagerAux(int bookingNumber,int offerNumber){ - this.bookingNumber=bookingNumber; - this.offerNumber=offerNumber; + + DB4oManagerAux(int bookingNumber, int offerNumber) { + this.bookingNumber = bookingNumber; + this.offerNumber = offerNumber; } } - public static DB4oManager getInstance() throws Exception{ - if (theDB4oManager==null) - theDB4oManager=new DB4oManager(); + public static DB4oManager getInstance() throws Exception { + if (theDB4oManager == null) + theDB4oManager = new DB4oManager(); return theDB4oManager; } - - - public void initializeDB(){ - + + public void initializeDB() { + try { Owner jon = new Owner("Jon"); Owner alfredo = new Owner("Alfredo"); jon.addRuralHouse(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3); jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4); jon.setBankAccount("1349 5677 21 2133567777"); - alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5, - 5, 5, 5); - alfredo.addRuralHouse(4,"", "Murgia", 6, 6, 6, 6, 6); + alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5, 5, 5, + 5); + alfredo.addRuralHouse(4, "", "Murgia", 6, 6, 6, 6, 6); alfredo.setBankAccount("4144 0087 23 9700002133"); Account jonAcc = new Account("userJon", "passJon", jon); Account alfredoAcc = new Account("userAlfredo", "passAlfredo", @@ -125,53 +126,54 @@ public class DB4oManager { db.store(jonAcc); db.store(alfredoAcc); db.commit(); - } - finally { - //db.close(); + } finally { + // db.close(); } } - + @SuppressWarnings("finally") - public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay, - float price) throws RemoteException, Exception { - Offer o = null; - try { - - if (c.isDatabaseLocal()==false) openObjectContainer(); - - - RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),null,null,null,null); - ObjectSet result = db.queryByExample(proto); - RuralHouse rh=(RuralHouse)result.next(); - o=rh.createOffer(theDB4oManagerAux.offerNumber++,firstDay, lastDay, price); - db.store(theDB4oManagerAux); // To store the new value for offerNumber - db.store(o); - db.commit(); - - } - catch (com.db4o.ext.ObjectNotStorableException e){ - System.out.println("Error: com.db4o.ext.ObjectNotStorableException in createOffer"); - } - finally { - return o; - } + public Offer createOffer(RuralHouse ruralHouse, Date firstDay, + Date lastDay, float price) throws RemoteException, Exception { + Offer o = null; + try { + + if (c.isDatabaseLocal() == false) + openObjectContainer(); + + RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), + null, null, null, null); + ObjectSet result = db.queryByExample(proto); + RuralHouse rh = (RuralHouse) result.next(); + o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay, + lastDay, price); + db.store(theDB4oManagerAux); // To store the new value for + // offerNumber + db.store(o); + db.commit(); + + } catch (com.db4o.ext.ObjectNotStorableException e) { + System.out + .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer"); + } finally { + return o; + } } - + public void deleteDB() { - + try { - Owner proto = new Owner(null,null); - ObjectSet result = db.queryByExample(proto); - Vector owners=new Vector(); - while(result.hasNext()) - { Owner o = (Owner)result.next(); - System.out.println("Deleted owner: "+o.toString()); - db.delete(o); - } - db.commit(); - } finally { - // db.close(); - } + Owner proto = new Owner(null, null); + ObjectSet result = db.queryByExample(proto); + Vector owners = new Vector(); + while (result.hasNext()) { + Owner o = (Owner) result.next(); + System.out.println("Deleted owner: " + o.toString()); + db.delete(o); + } + db.commit(); + } finally { + // db.close(); + } } /** @@ -181,103 +183,115 @@ public class DB4oManager { * day, last day, house number and telephone * @return a book */ - public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber) + public Booking createBooking(RuralHouse ruralHouse, Date firstDate, + Date lastDate, String bookTelephoneNumber) throws OfferCanNotBeBooked { - + try { - if (c.isDatabaseLocal()==false) openObjectContainer(); + if (c.isDatabaseLocal() == false) + openObjectContainer(); - RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),null,ruralHouse.getDescription(),ruralHouse.getTown(), null); - ObjectSet result = db.queryByExample(proto); - RuralHouse rh=(RuralHouse)result.next(); + RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), + null, ruralHouse.getDescription(), ruralHouse.getTown(), + null); + ObjectSet result = db.queryByExample(proto); + RuralHouse rh = (RuralHouse) result.next(); Offer offer; offer = rh.findOffer(firstDate, lastDate); - if (offer!=null) { - offer.createBooking(theDB4oManagerAux.bookingNumber++, bookTelephoneNumber); - db.store(theDB4oManagerAux); // To store the new value for bookingNumber + if (offer != null) { + offer.createBooking(theDB4oManagerAux.bookingNumber++, + bookTelephoneNumber); + db.store(theDB4oManagerAux); // To store the new value for + // bookingNumber db.store(offer); db.commit(); return offer.getBooking(); } return null; - } catch (com.db4o.ext.ObjectNotStorableException e){ - System.out.println("Error: com.db4o.ext.ObjectNotStorableException in createBooking"); - return null; - } - catch (Exception exc) { + } catch (com.db4o.ext.ObjectNotStorableException e) { + System.out + .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking"); + return null; + } catch (Exception exc) { exc.printStackTrace(); return null; } } - /** - * This method existing owners + * This method existing owners * */ - public Vector getOwners() throws RemoteException, - Exception { - - if (c.isDatabaseLocal()==false) openObjectContainer(); - - try { - Owner proto = new Owner(null,null); - ObjectSet result = db.queryByExample(proto); - Vector owners=new Vector(); - while(result.hasNext()) - owners.add((Owner)result.next()); - return owners; - } finally { - //db.close(); - } - } + public Vector getOwners() throws RemoteException, Exception { + + if (c.isDatabaseLocal() == false) + openObjectContainer(); + + try { + Owner proto = new Owner(null, null); + ObjectSet result = db.queryByExample(proto); + Vector owners = new Vector(); + while (result.hasNext()) + owners.add((Owner) result.next()); + return owners; + } finally { + // db.close(); + } + } + public Vector getAllRuralHouses() throws RemoteException, - Exception { - if (c.isDatabaseLocal()==false) openObjectContainer(); - - try { - RuralHouse proto = new RuralHouse(0,null,null,null,null); - ObjectSet result = db.queryByExample(proto); - Vector ruralHouses=new Vector(); - while(result.hasNext()) - ruralHouses.add((RuralHouse)result.next()); - return ruralHouses; - } finally { - // db.close(); - } + Exception { + if (c.isDatabaseLocal() == false) + openObjectContainer(); + + try { + RuralHouse proto = new RuralHouse(0, null, null, null, null); + ObjectSet result = db.queryByExample(proto); + Vector ruralHouses = new Vector(); + while (result.hasNext()) + ruralHouses.add((RuralHouse) result.next()); + return ruralHouses; + } finally { + // db.close(); + } } - - public boolean existsOverlappingOffer(RuralHouse rh,Date firstDay, Date lastDay) throws RemoteException, OverlappingOfferExists{ - try { - if (c.isDatabaseLocal()==false) openObjectContainer(); - - RuralHouse rhn = (RuralHouse) db.queryByExample(new RuralHouse(rh.getHouseNumber(),null,null,null,null)).next(); - if (rhn.overlapsWith(firstDay, lastDay)!=null) throw new OverlappingOfferExists(); - else return false; - } finally { - //db.close(); - } + + public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay, + Date lastDay) throws RemoteException, OverlappingOfferExists { + try { + if (c.isDatabaseLocal() == false) + openObjectContainer(); + + RuralHouse rhn = (RuralHouse) db + .queryByExample( + new RuralHouse(rh.getHouseNumber(), null, null, + null, null)).next(); + if (rhn.overlapsWith(firstDay, lastDay) != null) + throw new OverlappingOfferExists(); + else + return false; + } finally { + // db.close(); + } } - public static ObjectContainer getContainer(){ + public static ObjectContainer getContainer() { return db; } - - public void close(){ + + public void close() { db.close(); System.out.println("DataBase closed"); } - + public String toString() { - return "bookingNumber="+bookingNumber+" offerNumber="+offerNumber; + return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber; } - - /** * @param usr * @param pwd @@ -287,7 +301,8 @@ public class DB4oManager { */ public Vector getAccount(String usr, String pwd) throws RemoteException, Exception { - + if (c.isDatabaseLocal() == false) + openObjectContainer(); try { Account proto = new Account(usr, pwd, new Owner(null, null)); ObjectSet result = db.queryByExample(proto); @@ -296,7 +311,7 @@ public class DB4oManager { accounts.add((Account) result.next()); return accounts; } finally { - //db.close(); + // db.close(); } } @@ -304,9 +319,10 @@ public class DB4oManager { * @param rh */ public boolean storeRuralHouses(RuralHouse rh) { - DB4oManager.openDB(); +// DB4oManager.openDB(); + if (c.isDatabaseLocal() == false) + openObjectContainer(); boolean stored = false; - ObjectContainer db = DB4oManager.getContainer(); RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null, null, null); try { @@ -317,13 +333,15 @@ public class DB4oManager { stored = true; } } finally { - db.close(); +// db.close(); } return stored; } public void removeHouse(int houseNumber) { - DB4oManager.openDB(); +// DB4oManager.openDB(); + if (c.isDatabaseLocal() == false) + openObjectContainer(); RuralHouse house = new RuralHouse(houseNumber, null, null, null, null); try { ObjectSet result = db.queryByExample(house); @@ -335,9 +353,24 @@ public class DB4oManager { } catch (Exception exc) { exc.printStackTrace(); } finally { - db.close(); +// db.close(); } } -} + public Vector getRuralHousesByTown(String town){ + RuralHouse rh = new RuralHouse(0,null,null,town,null); + if (c.isDatabaseLocal() == false) + openObjectContainer(); + try{ + ObjectSet result = db.queryByExample(rh); + Vector ruralHouses = new Vector(); + while(result.hasNext()) + ruralHouses.add(result.next()); + return ruralHouses; + }finally{ + //db.close(); + } + + } +} diff --git a/ruralHouses/src/gui/DeleteHouseGUI.java b/ruralHouses/src/gui/DeleteHouseGUI.java index 0395a15..ce2b10b 100644 --- a/ruralHouses/src/gui/DeleteHouseGUI.java +++ b/ruralHouses/src/gui/DeleteHouseGUI.java @@ -16,7 +16,7 @@ import javax.swing.JButton; import businessLogic.FacadeImplementation; import businessLogic.HouseManager; - +import businessLogic.HouseManagerInterface; import domain.Owner; import domain.RuralHouse; @@ -85,7 +85,7 @@ public class DeleteHouseGUI extends JFrame { private void actionListenerButton(ActionEvent e){ RuralHouse toDel = (RuralHouse)comboBox.getSelectedItem(); - HouseManager hm = new HouseManager(); + HouseManagerInterface hm = new HouseManager(); hm.removeHouse(toDel.getHouseNumber()); } } diff --git a/ruralHouses/src/gui/NewHouseGUI.java b/ruralHouses/src/gui/NewHouseGUI.java index bb72996..cb5ef2b 100644 --- a/ruralHouses/src/gui/NewHouseGUI.java +++ b/ruralHouses/src/gui/NewHouseGUI.java @@ -18,12 +18,15 @@ import javax.swing.JTextField; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.SwingConstants; import javax.swing.JButton; + import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; + import javax.swing.JTextPane; import businessLogic.HouseManager; +import businessLogic.HouseManagerInterface; public class NewHouseGUI extends JFrame { @@ -119,7 +122,8 @@ public class NewHouseGUI extends JFrame { btnRegister.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { - HouseManager hm = new HouseManager(); + HouseManagerInterface hm = new HouseManager(); + //TODO when the house is not added show a warning to the user. Method below returns a boolean stating that. hm.registerNewHouse(Integer.parseInt(Code_f.getText()), owner, description_f.getText(), Town_f.getText() ,Integer.parseInt(rooms_f.getText()), @@ -178,11 +182,11 @@ public class NewHouseGUI extends JFrame { .addGap(18) .addComponent(rooms_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))) .addGroup(gl_contentPane.createSequentialGroup() - .addGap(26) + .addGap(18) .addComponent(lblParkings, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(parkings_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) - .addContainerGap(885, Short.MAX_VALUE)) + .addContainerGap(97, Short.MAX_VALUE)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) @@ -219,7 +223,7 @@ public class NewHouseGUI extends JFrame { .addGroup(gl_contentPane.createSequentialGroup() .addGap(3) .addComponent(lblKitchen))) - .addPreferredGap(ComponentPlacement.RELATED, 206, Short.MAX_VALUE) + .addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(3) -- 2.20.1