Git Repository Public Repository

RRRRHHHH_Code

URLs

Copy to Clipboard

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

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