Git Repository Public Repository

RRRRHHHH_Code

URLs

Copy to Clipboard
 
6a5d4dad58d39bd2f78b516a2e2062472716eeef
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
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 = 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());

		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).cascadeOnUpdate(true);
		configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
		configuration.common().objectClass(RuralHouse.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);
		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.offers.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 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,
					ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
			ObjectSet<RuralHouse> result = db.queryByExample(proto);
			RuralHouse rh = (RuralHouse) result.next();

			Offer offer;
			offer = rh.findOffer(firstDate, lastDate);

			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 Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
			Exception {

		if (c.isDatabaseLocal() == false)
			openSDB();
		else
			openDB();

		try {
			RuralHouse proto = new RuralHouse(null, null, null, null, null);
			ObjectSet<RuralHouse> result = db.queryByExample(proto);
			Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
			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 {

		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 static ObjectContainer getContainer() {
		return db;
	}

	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);
			if (result.isEmpty()) {
				Owner ow = (Owner) db.queryByExample(rh.getOwner()).get(0);
				rh.setOwner(ow);
				ow.addRuralHouse(rh);
				db.store(rh);
				db.commit();
				stored = true;
			} else {
				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(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 = new RuralHouse(name, null, 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 {
			// TODO realize if there is another account with same username.
			// Quite difficult with the f***ing salt
			db.store(acc);
			db.commit();
		} catch (Exception exc) {
			exc.printStackTrace();
		} finally {
			db.close();
		}
		return true;
	}

	// TODO remove account

	public boolean removeAccount(Account acc) {
		return false;
	}
	
	//TODO this method should be improved.
	public void acceptBooking(Offer of){
		Offer off = new Offer(of.getOfferNumber(),of.getRuralHouse(),of.getFirstDay(),of.getLastDay(),of.getPrice());
		if (c.isDatabaseLocal() == false)
			openSDB();
		else
			openDB();

		try {
			ObjectSet<Offer> result = db.queryByExample(off);
			this.deleteOffer(result.get(0));
			db.store(of);
			db.close();
			
		} catch (Exception e) {
			e.printStackTrace();;
		}		
	}
	
	public void removeBooking(Booking b){
		if (c.isDatabaseLocal() == false)
			openSDB();
		else
			openDB();
		try {
			ObjectSet<Booking> result = db.queryByExample(b);
			ObjectSet<Client> result2= db.queryByExample(b.getClient());
			db.delete(result.get(0));
			db.delete(result2.get(0));
			db.commit();
		} catch (Exception e) {
			e.printStackTrace();;
		}finally{
			db.close();
		}

		
	}
}

Commits for RRRRHHHH_CoderuralHouses/src/dataAccess/DB4oManager.java

Diff revisions: vs.
Revision Author Commited Message
6a5d4d ... Diff Diff camjan Sun 17 May, 2015 20:35:45 +0000

Booking confirmation and e-mail service added. Some bugs to be solved.

7bf57b ... Diff Diff camjan Sun 17 May, 2015 11:59:22 +0000

Possibility of registering new owners added

0f75b2 ... Diff Diff camjan Sat 16 May, 2015 14:34:49 +0000

Username is saved hashed and password hashed and salted

d7fd17 ... Diff Diff Eneko Pinzolas Murua Mon 27 Apr, 2015 12:01:38 +0000

Started creating the booking interface for the owners.

153110 ... Diff Diff Eneko Pinzolas Murua Sun 19 Apr, 2015 21:47:53 +0000

debugging

bdf772 ... Diff Diff pinene picture pinene Sun 19 Apr, 2015 19:24:55 +0000

debugging

dd0098 ... Diff Diff pinene picture pinene Sun 19 Apr, 2015 15:23:19 +0000

database management

520867 ... Diff Diff pinene picture pinene Sun 19 Apr, 2015 14:09:12 +0000

tmp

ba6020 ... Diff Diff camjan Wed 15 Apr, 2015 15:47:17 +0000

Merge branch ‘master’ of https://xp-dev.com/git/RRRRHHHH_Code

Conflicts:
ruralHouses/src/dataAccess/DB4oManager.java
ruralHouses/src/gui/ModifyHouseGUI.java

5761bc ... camjan Wed 15 Apr, 2015 15:27:07 +0000

Bugs when deleting houses and offers fixed and GUI’s adapated for empty cases