RRRRHHHH_Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
package dataAccess; import java.io.File; //import java.util.Enumeration; //import java.util.Vector; import java.rmi.RemoteException; import java.util.Date; import java.util.List; import java.util.ListIterator; import java.util.Vector; import com.db4o.Db4oEmbedded; import com.db4o.ObjectContainer; import com.db4o.ObjectSet; import com.db4o.config.EmbeddedConfiguration; import com.db4o.cs.Db4oClientServer; import com.db4o.cs.config.ClientConfiguration; import com.db4o.query.Predicate; import configuration.ConfigXML; import domain.Account; import domain.Administrator; import domain.Booking; import domain.Client; import domain.HouseFeatures; import domain.Offer; //import dataModel.Offer; import domain.Owner; import domain.RuralHouse; import exceptions.OfferCanNotBeBooked; import exceptions.OverlappingOfferExists; public class DB4oManager { private static ObjectContainer db; private static EmbeddedConfiguration configuration; private static ClientConfiguration configurationCS; private int bookingNumber = 1; // if it is "static" then it is not // serialized private int offerNumber = 1; // 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(1, 1); 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()) { openDB(); System.out.println("DataBase opened"); } else // c.isDatabaseLocal==false { openSDB(); System.out.println("Remote DataBase opened"); } if (c.getDataBaseOpenMode().equals("initialize")) { initializeDB(); System.out.println("DataBase initialized"); } else // c.getDataBaseOpenMode().equals("open") { ObjectSet<DB4oManagerAux> res = db .queryByExample(DB4oManagerAux.class); ListIterator<DB4oManagerAux> listIter = res.listIterator(); if (listIter.hasNext()) theDB4oManagerAux = (DB4oManagerAux) res.next(); } } private static void openDB() { configuration = Db4oEmbedded.newConfiguration(); configuration.common().activationDepth(c.getActivationDepth()); configuration.common().updateDepth(c.getUpdateDepth()); configuration.common().objectClass(Owner.class).cascadeOnDelete(true); configuration.common().objectClass(Booking.class).cascadeOnDelete(true); configuration.common().objectClass(RuralHouse.class) .cascadeOnDelete(true); configuration.common().objectClass(Account.class).cascadeOnDelete(true); configuration.common().objectClass(Offer.class).cascadeOnDelete(true); configuration.common().objectClass(Owner.class).cascadeOnUpdate(true); configuration.common().objectClass(Booking.class).cascadeOnUpdate(true); configuration.common().objectClass(RuralHouse.class) .cascadeOnUpdate(true); configuration.common().objectClass(Account.class).cascadeOnUpdate(true); configuration.common().objectClass(Offer.class).cascadeOnUpdate(true); configuration.common().objectClass(Account.class).cascadeOnUpdate(true); db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename()); } private void openSDB() { configurationCS = Db4oClientServer.newClientConfiguration(); configurationCS.common().activationDepth(c.getActivationDepth()); configurationCS.common().updateDepth(c.getUpdateDepth()); configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true); configurationCS.common().objectClass(Booking.class) .cascadeOnDelete(true); configurationCS.common().objectClass(RuralHouse.class) .cascadeOnDelete(true); configurationCS.common().objectClass(Account.class) .cascadeOnDelete(true); configurationCS.common().objectClass(Offer.class).cascadeOnDelete(true); configurationCS.common().objectClass(Owner.class).cascadeOnUpdate(true); configurationCS.common().objectClass(Booking.class) .cascadeOnUpdate(true); configurationCS.common().objectClass(RuralHouse.class) .cascadeOnUpdate(true); configurationCS.common().objectClass(Account.class) .cascadeOnUpdate(true); configurationCS.common().objectClass(Offer.class).cascadeOnUpdate(true); configurationCS.common().objectClass(Account.class) .cascadeOnUpdate(true); 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; } } public static DB4oManager getInstance() throws Exception { if (theDB4oManager == null) theDB4oManager = new DB4oManager(); return theDB4oManager; } public void initializeDB() { try { Owner jon = new Owner("Jon", "1349 5677 21 2133567777", "Jon@gmail.com"); Owner alfredo = new Owner("Alfredo", "4144 0087 23 9700002133", "alfredo@gmail.com"); jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3, 3, 3); jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4, 4, 4, 4); alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5, 5, 5, 5, 5); alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6); Account jonAcc = new Account("1", "1", jon); Account alfredoAcc = new Account("userAlfredo", "passAlfredo", alfredo); Account admin = new Account("admin", "admin", true); db.store(Administrator.getInstance()); db.store(jonAcc); db.store(alfredoAcc); db.store(admin); db.commit(); } finally { db.close(); } } public void deleteDB() { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { Owner proto = new Owner(null, null, null); ObjectSet<Owner> result = db.queryByExample(proto); while (result.hasNext()) { Owner o = (Owner) result.next(); System.out.println("Deleted owner: " + o.toString()); db.delete(o); } db.commit(); } finally { db.close(); } } public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay, float price) throws RemoteException, Exception { Offer o = null; if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null, null, null, null); ObjectSet<RuralHouse> 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 { db.close(); } return o; } public void deleteOffer(Offer offer) throws RemoteException, Exception { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<Offer> of = db.queryByExample(offer); RuralHouse rh = of.get(0).getRuralHouse(); System.out.println(rh.getAllOffers().remove(of.get(0))); db.store(rh); db.commit(); } catch (com.db4o.ext.ObjectNotStorableException e) { System.out .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer"); } finally { db.close(); } } public Vector<Offer> getRHsOffer(String name) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { RuralHouse rh = (RuralHouse) db.queryByExample( new RuralHouse(name, null, null, null, null)).get(0); Offer proto = new Offer(0, rh, null, null, 0); ObjectSet<Offer> result = db.queryByExample(proto); return new Vector<Offer>(result); } finally { db.close(); } } public Administrator getAdminData() { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { List<Administrator> admL = db.query(new Predicate<Administrator>() { private static final long serialVersionUID = 1L; public boolean match(Administrator admin) { return true; } }); return admL.get(0); } finally { db.close(); } } public void storeAdmin() { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { List<Administrator> admL = db.query(new Predicate<Administrator>() { /** * */ private static final long serialVersionUID = 1L; public boolean match(Administrator admin) { return true; } }); admL.get(0).setAddRequest( Administrator.getInstance().getAddRequest()); admL.get(0).setRemoveRequest( Administrator.getInstance().getRemoveRequest()); admL.get(0).setNewOwnerRequest( Administrator.getInstance().getNewOwnerRequest()); db.commit(); } catch (Exception e) { } finally { db.close(); } } /** * This method creates a book with a corresponding parameters * * @param First * day, last day, house number and telephone * @return a book */ public Vector<Booking> createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, Client cl) throws OfferCanNotBeBooked { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); Vector<Booking> book = new Vector<Booking>(); try { if (c.isDatabaseLocal() == false) openSDB(); RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null, null, null, null); ObjectSet<RuralHouse> result = db.queryByExample(proto); RuralHouse rh = (RuralHouse) result.next(); Offer offer; offer = (Offer) db.queryByExample( new Offer(0, rh, firstDate, lastDate, 0)).get(0); if (offer != null) { offer.createBooking(theDB4oManagerAux.bookingNumber++, cl); db.store(theDB4oManagerAux); // To store the new value for // bookingNumber db.store(offer); db.commit(); book = offer.getBookings(); } } catch (com.db4o.ext.ObjectNotStorableException e) { System.out .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking"); } catch (Exception exc) { exc.printStackTrace(); } finally { db.close(); } return book; } /** * This method existing owners * */ public Vector<Owner> getOwners() throws RemoteException, Exception { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { Owner proto = new Owner(null, null, null); ObjectSet<Owner> result = db.queryByExample(proto); Vector<Owner> owners = new Vector<Owner>(); while (result.hasNext()) owners.add((Owner) result.next()); return owners; } finally { db.close(); } } public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay, Date lastDay) throws RemoteException, OverlappingOfferExists { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { RuralHouse rhn = (RuralHouse) db.queryByExample( new RuralHouse(rh.getHouseName(), null, null, null, null)) .next(); if (rhn.overlapsWith(firstDay, lastDay) != null) throw new OverlappingOfferExists(); else return false; } finally { db.close(); } } public void close() { db.close(); System.out.println("DataBase closed"); } public String toString() { return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber; } /** * @param usr * @param ps * @return * @throws RemoteException * @throws Exception */ public Vector<Account> getAccount(Account proto) throws RemoteException, Exception { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<Account> result = db.queryByExample(proto); Vector<Account> accounts = new Vector<Account>(); while (result.hasNext()) accounts.add((Account) result.next()); return accounts; } finally { db.close(); } } /** * @param rh */ public boolean storeRuralHouses(RuralHouse rh) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); boolean stored = false; RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null, null); try { ObjectSet<RuralHouse> result = db.queryByExample(house); Owner own = new Owner(rh.getOwner().getName(),rh.getOwner().getBankAccount(),rh.getOwner().getMailAccount()); Owner ow = (Owner) db.queryByExample(own).get(0); rh.setOwner(ow); if (result.isEmpty()) { ow.addRuralHouse(rh); db.store(rh); db.commit(); stored = true; } else { ow.getRuralHouses().remove(result.get(0)); result.get(0).setOwner(null); ow.addRuralHouse(rh); db.store(result.get(0)); db.delete(result.get(0)); db.store(rh); db.commit(); stored = true; } } finally { db.close(); } return stored; } public void removeHouse(RuralHouse rh, Owner owner) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<Owner> result = db.queryByExample(owner); ObjectSet<RuralHouse> rhs = db.queryByExample(rh); if (!rhs.isEmpty()) { Owner found = result.get(0); found.getRuralHouses().remove(rhs.get(0)); db.delete(rhs.get(0)); db.store(found); db.commit(); } } catch (Exception exc) { exc.printStackTrace(); } finally { db.close(); } } public Vector<RuralHouse> getRuralHouses(Owner ow, String name, String town, int nBed, int nKit, int nBath, int nPark, int nLiv) { HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark); RuralHouse rh; if (ow != null) { Owner own = new Owner(ow.getName(), ow.getBankAccount(), ow.getMailAccount()); rh = new RuralHouse(name, own, null, town, fea); }else{ rh = new RuralHouse(name, ow, null, town, fea); } if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<RuralHouse> result = db.queryByExample(rh); Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>(); while (result.hasNext()) ruralHouses.add(result.next()); db.close(); return ruralHouses; } catch (NullPointerException e) { return null; } } public boolean addAccount(Account acc) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<Account> result = db.queryByExample(new Account(acc .getUsername())); if (result.isEmpty()) { db.store(acc); db.commit(); return true; } } catch (Exception exc) { exc.printStackTrace(); } finally { db.close(); } return false; } public boolean removeAccount(Owner own) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<Account> result = db.queryByExample(new Account(own)); if (!result.isEmpty()) { db.delete(result.get(0)); db.commit(); return true; } } catch (Exception exc) { exc.printStackTrace(); } finally { db.close(); } return false; } public void acceptBooking(Offer of) { Offer off = new Offer(of.getOfferNumber(), new RuralHouse(of .getRuralHouse().getHouseName(), null, null, null, null), null, null, 0); if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { ObjectSet<Offer> result = db.queryByExample(off); db.delete(result.get(0)); RuralHouse rh = result.get(0).getRuralHouse(); of.setRuralHouse(rh); rh.getAllOffers().remove(result.get(0)); rh.getAllOffers().add(of); db.store(rh); db.close(); } catch (Exception e) { e.printStackTrace(); ; } } public void removeBooking(Booking b) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { Booking book = new Booking(b.getBookNumber(), new Offer(b .getOffer().getOfferNumber(), new RuralHouse(b.getOffer() .getRuralHouse().getHouseName(), null, null, null, null), null, null, 0), b.getClient(), b.getBookDate()); Booking delete = (Booking) db.queryByExample(book).get(0); delete.setOffer(null); db.store(delete); db.delete(delete); db.commit(); } catch (Exception e) { e.printStackTrace(); } finally { db.close(); } } public Vector<Booking> getOfBok(Offer o) { if (c.isDatabaseLocal() == false) openSDB(); else openDB(); try { Offer of = (Offer) db.queryByExample( new Offer(0, new RuralHouse(o .getRuralHouse().getHouseName(), null, null, null, null), o.getFirstDay(), o.getLastDay(), 0)).get(0); Booking proto = new Booking(0, of, null, null); ObjectSet<Booking> result = db.queryByExample(proto); return new Vector<Booking>(result); } finally { db.close(); } } } |
Commits for RRRRHHHH_CoderuralHouses/src/dataAccess/DB4oManager.java
Revision | Author | Commited | Message |
---|---|---|---|
076d76 ... Diff | camjan | Fri 22 May, 2015 22:22:23 +0000 | The bug found in the presentation that we forgot to review has been fixed |
8012b0 ... Diff | camjan | Thu 21 May, 2015 17:19:07 +0000 | Merged the two branches, DB separated |
3db092 ... Diff | camjan | Thu 21 May, 2015 10:41:31 +0000 | Separate DB almost fixed |
be3833 ... Diff | camjan | Wed 20 May, 2015 22:55:11 +0000 | DB server side improved some problems when showing bookings remain |
08aaf3 ... Diff | camjan | Wed 20 May, 2015 21:26:51 +0000 | Started with the separated DB with the given code |
3a0296 ... Diff | pinene | Wed 20 May, 2015 16:57:47 +0000 | bookings were made to be taken from server per time they are searched |
ccac99 ... Diff | pinene | Wed 20 May, 2015 16:42:17 +0000 | Merge branch ‘master’ of ssh://xp-dev.com/RRRRHHHH_Code Conflicts: |
d30bcc ... Diff | pinene | Wed 20 May, 2015 16:32:33 +0000 | cleaning |
1d7e49 ... Diff | camjan | Wed 20 May, 2015 09:15:52 +0000 | Booking deletion definite debbuging |
85d983 ... | camjan | Wed 20 May, 2015 09:05:53 +0000 | Booking deletion fixed some minor problems remain |