Git Repository Public Repository

RRRRHHHH_Code

URLs

Copy to Clipboard

Diff Revisions 578bec ... vs 25898b ... for ruralHouses/src/dataAccess/DB4oManager.java

Diff revisions: vs.
  @@ -135,7 +135,6 @@
135 135 Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
136 136 alfredo);
137 137
138 -
139 138 Account admin = new Account("admin", "admin", true);
140 139 db.store(Administrator.getInstance());
141 140 db.store(jonAcc);
  @@ -220,9 +219,7 @@
220 219 }
221 220 }
222 221
223 -
224 -
225 - public Administrator getAdminData(){
222 + public Administrator getAdminData() {
226 223 if (c.isDatabaseLocal() == false)
227 224 openSDB();
228 225 else
  @@ -230,8 +227,7 @@
230 227
231 228 try {
232 229
233 -
234 - List<Administrator> admL = db.query(new Predicate<Administrator>(){
230 + List<Administrator> admL = db.query(new Predicate<Administrator>() {
235 231 private static final long serialVersionUID = 1L;
236 232
237 233 public boolean match(Administrator admin) {
  @@ -239,11 +235,8 @@
239 235 }
240 236 });
241 237
242 -
243 -
244 238 return admL.get(0);
245 - }
246 - finally {
239 + } finally {
247 240 db.close();
248 241
249 242 }
  @@ -301,7 +294,7 @@
301 294 else
302 295 openDB();
303 296
304 - Vector<Booking> book = new Vector<Booking>() ;
297 + Vector<Booking> book = new Vector<Booking>();
305 298
306 299 try {
307 300
  @@ -320,11 +313,11 @@
320 313 offer.createBooking(theDB4oManagerAux.bookingNumber++, cl);
321 314 db.store(theDB4oManagerAux); // To store the new value for
322 315 // bookingNumber
323 -
316 +
324 317 db.store(offer);
325 318 db.commit();
326 319 book = offer.getBookings();
327 -
320 +
328 321 }
329 322
330 323 } catch (com.db4o.ext.ObjectNotStorableException e) {
  @@ -529,27 +522,50 @@
529 522 openDB();
530 523
531 524 try {
532 - // TODO realize if there is another account with same username.
533 - // Quite difficult with the f***ing salt
534 - db.store(acc);
535 - db.commit();
525 + ObjectSet<Account> result = db.queryByExample(new Account(acc
526 + .getUsername()));
527 + if (result.isEmpty()) {
528 + db.store(acc);
529 + db.commit();
530 + return true;
531 + }
536 532 } catch (Exception exc) {
537 533 exc.printStackTrace();
538 534 } finally {
539 535 db.close();
540 536 }
541 - return true;
537 + return false;
542 538 }
543 539
544 540 // TODO remove account
545 541
546 542 public boolean removeAccount(Account acc) {
543 + if (c.isDatabaseLocal() == false)
544 + openSDB();
545 + else
546 + openDB();
547 +
548 + try {
549 + ObjectSet<Account> result = db.queryByExample(new Account(acc
550 + .getUsername()));
551 + if (!result.isEmpty()) {
552 + db.delete(result.get(0));
553 + ;
554 + db.commit();
555 + return true;
556 + }
557 + } catch (Exception exc) {
558 + exc.printStackTrace();
559 + } finally {
560 + db.close();
561 + }
547 562 return false;
548 563 }
549 -
550 - //TODO this method should be improved.
551 - public void acceptBooking(Offer of){
552 - Offer off = new Offer(of.getOfferNumber(),of.getRuralHouse(),of.getFirstDay(),of.getLastDay(),of.getPrice());
564 +
565 + // TODO this method should be improved.
566 + public void acceptBooking(Offer of) {
567 + Offer off = new Offer(of.getOfferNumber(), of.getRuralHouse(),
568 + of.getFirstDay(), of.getLastDay(), of.getPrice());
553 569 if (c.isDatabaseLocal() == false)
554 570 openSDB();
555 571 else
  @@ -560,29 +576,30 @@
560 576 this.deleteOffer(result.get(0));
561 577 db.store(of);
562 578 db.close();
563 -
579 +
564 580 } catch (Exception e) {
565 - e.printStackTrace();;
566 - }
581 + e.printStackTrace();
582 + ;
583 + }
567 584 }
568 -
569 - public void removeBooking(Booking b){
585 +
586 + public void removeBooking(Booking b) {
570 587 if (c.isDatabaseLocal() == false)
571 588 openSDB();
572 589 else
573 590 openDB();
574 591 try {
575 592 ObjectSet<Booking> result = db.queryByExample(b);
576 - ObjectSet<Client> result2= db.queryByExample(b.getClient());
593 + ObjectSet<Client> result2 = db.queryByExample(b.getClient());
577 594 db.delete(result.get(0));
578 595 db.delete(result2.get(0));
579 596 db.commit();
580 597 } catch (Exception e) {
581 - e.printStackTrace();;
582 - }finally{
598 + e.printStackTrace();
599 + ;
600 + } finally {
583 601 db.close();
584 602 }
585 603
586 -
587 604 }
588 605 }