Git Repository Public Repository

RRRRHHHH_Code

URLs

Copy to Clipboard

Diff Revisions 556478 ... vs ad3773 ... for ruralHouses/src/dataAccess/DB4oManager.java

Diff revisions: vs.
  @@ -81,12 +81,10 @@
81 81 configuration = Db4oEmbedded.newConfiguration();
82 82 configuration.common().activationDepth(c.getActivationDepth());
83 83 configuration.common().updateDepth(c.getUpdateDepth());
84 + configuration.common().objectClass(Owner.class).cascadeOnUpdate(true);
84 85 configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
85 - configuration.common().objectClass(Booking.class).cascadeOnDelete(true);
86 86 configuration.common().objectClass(RuralHouse.class)
87 - .cascadeOnDelete(true);
88 - configuration.common().objectClass(Offer.class)
89 - .cascadeOnDelete(true);
87 + .cascadeOnUpdate(true);
90 88 configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
91 89 db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
92 90 }
  @@ -137,6 +135,7 @@
137 135 Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
138 136 alfredo);
139 137
138 +
140 139 Account admin = new Account("admin", "admin", true);
141 140 db.store(Administrator.getInstance());
142 141 db.store(jonAcc);
  @@ -209,7 +208,7 @@
209 208 try {
210 209 ObjectSet<Offer> of = db.queryByExample(offer);
211 210 RuralHouse rh = of.get(0).getRuralHouse();
212 - System.out.println(rh.getAllOffers().remove(of.get(0)));
211 + System.out.println(rh.offers.remove(of.get(0)));
213 212 db.store(rh);
214 213 db.commit();
215 214
  @@ -221,7 +220,23 @@
221 220 }
222 221 }
223 222
224 - public Administrator getAdminData() {
223 + public Vector<Offer> getRHsOffer(String name){
224 + if (c.isDatabaseLocal() == false)
225 + openSDB();
226 + else
227 + openDB();
228 +
229 + try {
230 + RuralHouse rh = (RuralHouse)db.queryByExample(new RuralHouse(name, null, null, null, null)).get(0);
231 + Offer proto = new Offer(0, rh, null, null, 0);
232 + ObjectSet<Offer> result = db.queryByExample(proto);
233 + return new Vector<Offer>(result);
234 + } finally {
235 + db.close();
236 + }
237 + }
238 +
239 + public Administrator getAdminData(){
225 240 if (c.isDatabaseLocal() == false)
226 241 openSDB();
227 242 else
  @@ -229,7 +244,8 @@
229 244
230 245 try {
231 246
232 - List<Administrator> admL = db.query(new Predicate<Administrator>() {
247 +
248 + List<Administrator> admL = db.query(new Predicate<Administrator>(){
233 249 private static final long serialVersionUID = 1L;
234 250
235 251 public boolean match(Administrator admin) {
  @@ -237,8 +253,11 @@
237 253 }
238 254 });
239 255
256 +
257 +
240 258 return admL.get(0);
241 - } finally {
259 + }
260 + finally {
242 261 db.close();
243 262
244 263 }
  @@ -296,7 +315,7 @@
296 315 else
297 316 openDB();
298 317
299 - Vector<Booking> book = new Vector<Booking>();
318 + Vector<Booking> book = new Vector<Booking>() ;
300 319
301 320 try {
302 321
  @@ -315,11 +334,11 @@
315 334 offer.createBooking(theDB4oManagerAux.bookingNumber++, cl);
316 335 db.store(theDB4oManagerAux); // To store the new value for
317 336 // bookingNumber
318 -
337 +
319 338 db.store(offer);
320 339 db.commit();
321 340 book = offer.getBookings();
322 -
341 +
323 342 }
324 343
325 344 } catch (com.db4o.ext.ObjectNotStorableException e) {
  @@ -356,6 +375,7 @@
356 375 }
357 376 }
358 377
378 +
359 379 public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
360 380 Exception {
361 381
  @@ -367,10 +387,7 @@
367 387 try {
368 388 RuralHouse proto = new RuralHouse(null, null, null, null, null);
369 389 ObjectSet<RuralHouse> result = db.queryByExample(proto);
370 - Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
371 - while (result.hasNext())
372 - ruralHouses.add((RuralHouse) result.next());
373 - return ruralHouses;
390 + return new Vector<RuralHouse>(result);
374 391 } finally {
375 392 db.close();
376 393 }
  @@ -495,10 +512,10 @@
495 512
496 513 }
497 514
498 - public Vector<RuralHouse> getRuralHouses(String name, String town,
515 + public Vector<RuralHouse> getRuralHouses(Owner ow, String name, String town,
499 516 int nBed, int nKit, int nBath, int nPark, int nLiv) {
500 517 HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
501 - RuralHouse rh = new RuralHouse(name, null, null, town, fea);
518 + RuralHouse rh = new RuralHouse(name, ow, null, town, fea);
502 519 if (c.isDatabaseLocal() == false)
503 520 openSDB();
504 521 else
  @@ -524,50 +541,27 @@
524 541 openDB();
525 542
526 543 try {
527 - ObjectSet<Account> result = db.queryByExample(new Account(acc
528 - .getUsername()));
529 - if (result.isEmpty()) {
530 - db.store(acc);
531 - db.commit();
532 - return true;
533 - }
544 + // TODO realize if there is another account with same username.
545 + // Quite difficult with the f***ing salt
546 + db.store(acc);
547 + db.commit();
534 548 } catch (Exception exc) {
535 549 exc.printStackTrace();
536 550 } finally {
537 551 db.close();
538 552 }
539 - return false;
553 + return true;
540 554 }
541 555
542 556 // TODO remove account
543 557
544 558 public boolean removeAccount(Account acc) {
545 - if (c.isDatabaseLocal() == false)
546 - openSDB();
547 - else
548 - openDB();
549 -
550 - try {
551 - ObjectSet<Account> result = db.queryByExample(new Account(acc
552 - .getUsername()));
553 - if (!result.isEmpty()) {
554 - db.delete(result.get(0));
555 - ;
556 - db.commit();
557 - return true;
558 - }
559 - } catch (Exception exc) {
560 - exc.printStackTrace();
561 - } finally {
562 - db.close();
563 - }
564 559 return false;
565 560 }
566 -
567 - public void acceptBooking(Offer of) {
568 - Offer off = new Offer(of.getOfferNumber(), new RuralHouse(of
569 - .getRuralHouse().getHouseName(), null, null, null, null), null,
570 - null, 0);
561 +
562 + //TODO this method should be improved.
563 + public void acceptBooking(Offer of){
564 + Offer off = new Offer(of.getOfferNumber(),of.getRuralHouse(),of.getFirstDay(),of.getLastDay(),of.getPrice());
571 565 if (c.isDatabaseLocal() == false)
572 566 openSDB();
573 567 else
  @@ -575,36 +569,32 @@
575 569
576 570 try {
577 571 ObjectSet<Offer> result = db.queryByExample(off);
578 - db.delete(result.get(0));
579 - RuralHouse rh = result.get(0).getRuralHouse();
580 - of.setRuralHouse(rh);
581 - rh.getAllOffers().remove(result.get(0));
582 - rh.getAllOffers().add(of);
583 - db.store(rh);
572 + this.deleteOffer(result.get(0));
573 + db.store(of);
584 574 db.close();
585 -
575 +
586 576 } catch (Exception e) {
587 - e.printStackTrace();
588 - ;
589 - }
577 + e.printStackTrace();;
578 + }
590 579 }
591 -
592 - public void removeBooking(Booking b) {
580 +
581 + public void removeBooking(Booking b){
593 582 if (c.isDatabaseLocal() == false)
594 583 openSDB();
595 584 else
596 585 openDB();
597 586 try {
598 587 ObjectSet<Booking> result = db.queryByExample(b);
599 - result.get(0).getOffer().getBookings().remove(b);
600 - db.store(result.get(0).getOffer());
588 + ObjectSet<Client> result2= db.queryByExample(b.getClient());
589 + db.delete(result.get(0));
590 + db.delete(result2.get(0));
601 591 db.commit();
602 592 } catch (Exception e) {
603 - e.printStackTrace();
604 - ;
605 - } finally {
593 + e.printStackTrace();;
594 + }finally{
606 595 db.close();
607 596 }
608 597
598 +
609 599 }
610 600 }