Git Repository Public Repository

RRRRHHHH_Code

URLs

Copy to Clipboard
 
5ad0ccf52e0a1772c424ac983a7b6e1c00cec879
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
package dataAccess;

import java.io.File;
//import java.util.Enumeration;
//import java.util.Vector;

import java.rmi.RemoteException;
import java.sql.Date;
import java.util.HashSet;
import java.util.ListIterator;
import java.util.Vector;

import com.db4o.*;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.cs.Db4oClientServer;
import com.db4o.cs.config.ClientConfiguration;

import configuration.ConfigXML;
import domain.Account;
import domain.Booking;
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 res = db.queryByExample(DB4oManagerAux.class);
			ListIterator 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);
		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");
			Owner alfredo = new Owner("Alfredo");
			jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
			jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
			jon.setBankAccount("1349 5677 21 2133567777");
			alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
					5);
			alfredo.addRuralHouse("Murgoitz", "", "Murgia", 6, 6, 6, 6, 6);
			alfredo.setBankAccount("4144 0087 23 9700002133");
			Account jonAcc = new Account("userJon", "passJon", jon);
			Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
					alfredo);
			db.store(jon);
			db.store(alfredo);
			db.store(jonAcc);
			db.store(alfredoAcc);
			db.commit();
		} finally {
			db.close();
		}
	}

	public void deleteDB() {

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

		try {
			Owner proto = new Owner(null, null);
			ObjectSet result = db.queryByExample(proto);
			Vector<Owner> owners = new Vector<Owner>();
			while (result.hasNext()) {
				Owner o = (Owner) result.next();
				System.out.println("Deleted owner: " + o.toString());
				db.delete(o);
			}
			db.commit();
		} finally {
			db.close();
		}
	}
	
	
	@SuppressWarnings("finally")
	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 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;
	}

	@SuppressWarnings("finally")
	public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
		if (c.isDatabaseLocal() == false)
			openSDB();
		else
			openDB();

		try {

			
			db.store(offer);
			db.commit();

		} catch (com.db4o.ext.ObjectNotStorableException e) {
			System.out
					.println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
		} finally {
			db.close();
		}
		return offer;
	}
	
	@SuppressWarnings("finally")
	public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, Exception {
		if (c.isDatabaseLocal() == false)
			openSDB();
		else
			openDB();

		try {

			db.store(rh);
			db.delete(offer);
			db.commit();

		} catch (com.db4o.ext.ObjectNotStorableException e) {
			System.out
					.println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
		} 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 Booking createBooking(RuralHouse ruralHouse, Date firstDate,
			Date lastDate, String bookTelephoneNumber)
			throws OfferCanNotBeBooked {

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

		Booking bok = null;

		try {

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

			RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
					null, ruralHouse.getDescription(), ruralHouse.getDistrict(),
					null);
			ObjectSet result = db.queryByExample(proto);
			RuralHouse rh = (RuralHouse) result.next();

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

			if (offer != null) {
				offer.createBooking(theDB4oManagerAux.bookingNumber++,
						bookTelephoneNumber);
				db.store(theDB4oManagerAux); // To store the new value for
												// bookingNumber
				db.store(offer);
				db.commit();
				bok = offer.getBooking();
			}

		} 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 bok;
	}

	/**
	 * 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);
			ObjectSet 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 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 pwd
	 * @return
	 * @throws RemoteException
	 * @throws Exception
	 */
	public Vector<Account> getAccount(String usr, String pwd)
			throws RemoteException, Exception {

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

		try {
			Account proto = new Account(usr, pwd, new Owner(null, null));
			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<Owner> result = db.queryByExample(house);
			if (result.isEmpty()) {
				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<RuralHouse> result = db.queryByExample(rh);
			if (!result.isEmpty()) {
				RuralHouse found = (RuralHouse) result.get(0);
//				db.delete(found.getOwner());
				db.store(owner);
				db.delete(found);
				db.commit();
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		} finally {
			db.close();
		}

	}
	
	public Vector<RuralHouse> getRuralHousesByTown(String town){
		RuralHouse rh = new RuralHouse(null,null,null,town,null);
		
		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());
			return ruralHouses;
		} finally {
			db.close();
		}

	}
	public RuralHouse getRuralHouseByName(String name){
		RuralHouse rh = new RuralHouse(name,null,null,null,null);
		
		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.get(0);
		} catch (NullPointerException e){
			return null;
		}
		
		
		
	}
	
	public Vector<RuralHouse> getRuralHouses(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(null,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;
		}
		
		
		
	}
}

Commits for RRRRHHHH_CoderuralHouses/src/dataAccess/DB4oManager.java

Diff revisions: vs.
Revision Author Commited Message
5ad0cc ... Diff Diff Eneko Pinzolas Murua Wed 01 Apr, 2015 17:46:59 +0000

ModifyHouseGUI created and its link from HouseRelatedOwnerGUI reestablished

afdc66 ... Diff Diff camjan Wed 25 Mar, 2015 16:43:35 +0000

Villatripas de arriba districs added and the option of searching houses with diferent parameters

5dd541 ... Diff Diff camjan Wed 11 Mar, 2015 22:59:01 +0000

GUI added to show list of available houses

95c4ff ... Diff Diff pinene picture pinene Tue 10 Mar, 2015 18:36:52 +0000

deleted: ruralHouses/hs_err_pid6014.log
modified: ruralHouses/src/businessLogic/OfferManager.java
modified: ruralHouses/src/dataAccess/DB4oManager.java
renamed: ruralHouses/src/gui/SetAvailability2GUI.java -> ruralHouses/src/gui/AddOffersGUI.java
modified: ruralHouses/src/gui/DeleteHouseGUI.java
new file: ruralHouses/src/gui/DeleteOfferGUI.java
deleted: ruralHouses/src/gui/SetAvailabilityGUI.java

82c870 ... Diff Diff pinene picture pinene Tue 10 Mar, 2015 13:32:30 +0000

DeleteHouses bug debugged

bb8482 ... Diff Diff Eneko Pinzolas Murua Tue 10 Mar, 2015 13:23:13 +0000

temp

bd03dd ... Diff Diff camjan Tue 10 Mar, 2015 13:06:22 +0000

Administrator class created delete debbuging started

4a87ac ... Diff Diff camjan Tue 10 Mar, 2015 12:19:47 +0000

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

Conflicts:
ruralHouses/src/dataAccess/DB4oManager.java

eac0e6 ... Diff Diff camjan Tue 10 Mar, 2015 12:16:52 +0000

minor changes

7c0734 ... Eneko Pinzolas Murua Tue 10 Mar, 2015 12:15:11 +0000

house code changed to name