Line | Hits | Source |
---|---|---|
1 | /******************************************************************************* | |
2 | * InternetCafe is a software solution that helps the management of Cybercafes | |
3 | * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005. | |
4 | * Copyright (C) 2006 Guido Angelo Ingenito | |
5 | ||
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | ||
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | *******************************************************************************/ | |
20 | package base; | |
21 | ||
22 | import java.io.File; | |
23 | import java.io.FileInputStream; | |
24 | import java.io.FileOutputStream; | |
25 | import java.io.IOException; | |
26 | import java.io.InputStream; | |
27 | import java.io.OutputStream; | |
28 | import java.util.Iterator; | |
29 | import java.util.Observable; | |
30 | ||
31 | import org.apache.log4j.Logger; | |
32 | ||
33 | import base.backup.Backup; | |
34 | import base.network.Network; | |
35 | import base.network.NetworkManager; | |
36 | import base.service.Service; | |
37 | import base.session.Session; | |
38 | import base.user.User; | |
39 | ||
40 | import com.db4o.Db4o; | |
41 | import com.db4o.ObjectContainer; | |
42 | import com.db4o.ObjectSet; | |
43 | ||
44 | 5 | public class InternetCafeManager extends Observable { |
45 | ||
46 | 5 | private static final transient Logger logger = Logger |
47 | 5 | .getLogger(InternetCafeManager.class.getName()); |
48 | ||
49 | private static InternetCafeManager instance; | |
50 | ||
51 | public static InternetCafeManager getInstance() { | |
52 | 52 | return instance == null ? instance = new InternetCafeManager() |
53 | 21 | : instance; |
54 | } | |
55 | ||
56 | 5 | private java.util.Hashtable<String, User> user = new java.util.Hashtable<String, User>(); |
57 | ||
58 | 5 | private java.util.Hashtable<String, Session> session = new java.util.Hashtable<String, Session>(); |
59 | ||
60 | 5 | private java.util.Hashtable<String, Service> service = new java.util.Hashtable<String, Service>(); |
61 | ||
62 | 5 | private java.util.Hashtable<String, Backup> backup = new java.util.Hashtable<String, Backup>(); |
63 | ||
64 | 5 | protected InternetCafeManager() { |
65 | ||
66 | 5 | } |
67 | ||
68 | private void deleteDB(ObjectContainer db) { | |
69 | 0 | ObjectSet result = db.get(new Object()); |
70 | 0 | while (result.hasNext()) |
71 | 0 | db.delete(result.next()); |
72 | 0 | db.commit(); |
73 | 0 | } |
74 | ||
75 | public void store() { | |
76 | ||
77 | 0 | if (!new File(ConfigurationManager.getInstance().getDataBasePath()) |
78 | 0 | .exists()) { |
79 | try { | |
80 | 0 | new File(ConfigurationManager.getInstance().getDataBasePath()) |
81 | 0 | .createNewFile(); |
82 | 0 | } catch (IOException e) { |
83 | // TODO Auto-generated catch block | |
84 | 0 | e.printStackTrace(); |
85 | 0 | } |
86 | } | |
87 | 0 | ObjectContainer db = Db4o.openFile(ConfigurationManager.getInstance() |
88 | 0 | .getDataBasePath()); |
89 | try { | |
90 | // Here we will uppdate all the images... | |
91 | 0 | updateImageReferences(); |
92 | ||
93 | // We must delete the current database and restore it to the fresh | |
94 | // values... | |
95 | 0 | deleteDB(db); |
96 | // This will store in the db all the users... | |
97 | 0 | Iterator iterator = user.values().iterator(); |
98 | 0 | while (iterator.hasNext()) { |
99 | 0 | User u = (User) iterator.next(); |
100 | 0 | if (u != null && u.getId() != -1) |
101 | 0 | db.set(u); |
102 | 0 | } |
103 | // This will store in the db the entire network... | |
104 | 0 | db.set(NetworkManager.getInstance().getNetwork()); |
105 | // This will store in the db all the sessions... | |
106 | 0 | iterator = session.values().iterator(); |
107 | 0 | while (iterator.hasNext()) |
108 | 0 | db.set((Session) iterator.next()); |
109 | ||
110 | // This will store in the db all the services... | |
111 | 0 | iterator = service.values().iterator(); |
112 | 0 | while (iterator.hasNext()) |
113 | 0 | db.set((Service) iterator.next()); |
114 | ||
115 | // This will store in the db all the backups... | |
116 | 0 | iterator = backup.values().iterator(); |
117 | 0 | while (iterator.hasNext()) |
118 | 0 | db.set((Backup) iterator.next()); |
119 | ||
120 | // This is the end of the operations on the db... | |
121 | 0 | db.commit(); |
122 | ||
123 | 0 | logger.debug("Internet Cafe Successfully Stored."); |
124 | 0 | } catch (Exception ex) { |
125 | 0 | ex.printStackTrace(); |
126 | 0 | } finally { |
127 | 0 | db.close(); |
128 | 0 | } |
129 | 0 | } |
130 | ||
131 | private void updateImageReferences() { | |
132 | // Now we must store each user and document image | |
133 | 0 | File userImageDirectory = new File(ConfigurationManager.getInstance() |
134 | 0 | .getUserImageDirectoryPath()); |
135 | 0 | File documentImageDirectory = new File(ConfigurationManager |
136 | 0 | .getInstance().getDocumentImageDirectoryPath()); |
137 | ||
138 | 0 | if (!userImageDirectory.exists()) { |
139 | 0 | boolean success = userImageDirectory.mkdirs(); |
140 | 0 | if (!success) |
141 | 0 | logger.error("Can't create the User's image directory!"); |
142 | } | |
143 | ||
144 | 0 | if (!documentImageDirectory.exists()) { |
145 | 0 | boolean success = documentImageDirectory.mkdirs(); |
146 | 0 | if (!success) |
147 | 0 | logger.error("Can't create the Document's image directory!"); |
148 | } | |
149 | ||
150 | // Here we must move all the new images to the correct directory... | |
151 | 0 | Iterator iterator = user.values().iterator(); |
152 | 0 | while (iterator.hasNext()) { |
153 | 0 | User u = (User) iterator.next(); |
154 | 0 | if (u.getImagePath() != null && new File(u.getImagePath()).exists()) { |
155 | 0 | File userImageFile = new File(u.getImagePath()); |
156 | ||
157 | 0 | String extension = userImageFile.getName().substring( |
158 | 0 | userImageFile.getName().lastIndexOf("."), |
159 | 0 | userImageFile.getName().length()); |
160 | // This is the new file renamed according with the concat | |
161 | // pattern "User" + UserId + "Image" + extension | |
162 | 0 | File newUserImageFile = new File(userImageDirectory, "User" |
163 | 0 | + u.getId() + "Image" + extension); |
164 | ||
165 | // CASE 0 - The user's image directory doesn't contains the | |
166 | // user's file... copy it | |
167 | 0 | if (!contains(userImageDirectory.list(), userImageFile |
168 | 0 | .getName())) { |
169 | try { | |
170 | 0 | copy(userImageFile, newUserImageFile); |
171 | 0 | u.setImagePath(ConfigurationManager.getInstance() |
172 | 0 | .getUserImageDirectoryPath() |
173 | 0 | + File.separatorChar |
174 | 0 | + newUserImageFile.getName()); |
175 | 0 | } catch (IOException e) { |
176 | 0 | logger |
177 | 0 | .error("Can't move the User's image file to the Users's images directory!"); |
178 | // TODO Auto-generated catch block | |
179 | 0 | e.printStackTrace(); |
180 | 0 | } |
181 | 0 | }// CASE 1 - The user's image directory contains the user's |
182 | // file and it has the same length... discard changes, | |
183 | // nothing to do... | |
184 | 0 | else if (userImageFile.length() != newUserImageFile.length()) { |
185 | // CASE 2 - The user's image directory contains the user's | |
186 | // file and it hasn't the same length... delete the previous | |
187 | // file and copy the new one | |
188 | try { | |
189 | 0 | newUserImageFile.delete(); |
190 | 0 | copy(userImageFile, newUserImageFile); |
191 | 0 | } catch (IOException e) { |
192 | 0 | logger |
193 | 0 | .error("Can't move the User's image file to the Users's images directory!"); |
194 | // TODO Auto-generated catch block | |
195 | 0 | e.printStackTrace(); |
196 | 0 | } |
197 | } | |
198 | ||
199 | } | |
200 | ||
201 | 0 | if (u.getDocument() != null |
202 | 0 | && u.getDocument().getImagePath() != null) { |
203 | 0 | File documentImageFile = new File(u.getDocument() |
204 | 0 | .getImagePath()); |
205 | 0 | if (documentImageFile.exists() |
206 | 0 | && !contains(documentImageDirectory.list(), |
207 | 0 | documentImageFile.getName())) { |
208 | 0 | String extension = documentImageFile.getName().substring( |
209 | 0 | documentImageFile.getName().lastIndexOf("."), |
210 | 0 | documentImageFile.getName().length()); |
211 | ||
212 | 0 | File newDocumentImageFile = new File( |
213 | 0 | documentImageDirectory, "Document" + u.getId() |
214 | 0 | + "Image" + extension); |
215 | try { | |
216 | 0 | copy(documentImageFile, newDocumentImageFile); |
217 | ||
218 | 0 | u.getDocument().setImagePath( |
219 | 0 | ConfigurationManager.getInstance() |
220 | 0 | .getDocumentImageDirectoryPath() |
221 | 0 | + File.separatorChar |
222 | 0 | + newDocumentImageFile.getName()); |
223 | 0 | } catch (IOException e) { |
224 | 0 | System.err |
225 | 0 | .println("Can't move the User's document image file to the Users's documents image directory!"); |
226 | // TODO Auto-generated catch block | |
227 | 0 | e.printStackTrace(); |
228 | 0 | } |
229 | ||
230 | } | |
231 | } | |
232 | ||
233 | 0 | } |
234 | 0 | } |
235 | ||
236 | void copy(File src, File dst) throws IOException { | |
237 | 0 | InputStream in = new FileInputStream(src); |
238 | 0 | OutputStream out = new FileOutputStream(dst); |
239 | ||
240 | // Transfer bytes from in to out | |
241 | 0 | byte[] buf = new byte[1024]; |
242 | int len; | |
243 | 0 | while ((len = in.read(buf)) > 0) { |
244 | 0 | out.write(buf, 0, len); |
245 | 0 | } |
246 | 0 | in.close(); |
247 | 0 | out.close(); |
248 | 0 | } |
249 | ||
250 | public static boolean contains(String[] list, String text) { | |
251 | 0 | for (int i = 0; i < list.length; i++) |
252 | 0 | if (list[i].equalsIgnoreCase(text)) |
253 | 0 | return true; |
254 | 0 | return false; |
255 | } | |
256 | ||
257 | public void retrieve() { | |
258 | ||
259 | 0 | if (!new File(ConfigurationManager.getInstance().getDataBasePath()) |
260 | 0 | .exists()) |
261 | 0 | return; |
262 | ||
263 | 0 | ObjectContainer db = Db4o.openFile(ConfigurationManager.getInstance() |
264 | 0 | .getDataBasePath()); |
265 | try { | |
266 | ||
267 | // This will retrieve all the users... | |
268 | 0 | user = new java.util.Hashtable<String, User>(); |
269 | 0 | ObjectSet os = db.get(User.class); |
270 | 0 | while (os.hasNext()) { |
271 | 0 | User u = (User) os.next(); |
272 | 0 | if (u != null && u.getId() != -1) { |
273 | // We must set the correct imagePath for each user image and | |
274 | // document... | |
275 | if (!ConfigurationManager.debugMode) { | |
276 | if (u.getImagePath() != "" && u.getImagePath() != null | |
277 | && u.getImagePath().contains("..")) { | |
278 | String imagePath = u.getImagePath().replace("..", | |
279 | "."); | |
280 | u.setImagePath(imagePath); | |
281 | } | |
282 | if (u.getDocument() != null | |
283 | && u.getDocument().getImagePath() != "" | |
284 | && u.getDocument().getImagePath() != null | |
285 | && u.getDocument().getImagePath() | |
286 | .contains("..")) { | |
287 | String imagePath = u.getDocument().getImagePath() | |
288 | .replace("..", "."); | |
289 | u.getDocument().setImagePath(imagePath); | |
290 | } | |
291 | } | |
292 | 0 | addUser(u); |
293 | } | |
294 | 0 | } |
295 | ||
296 | // This will retrieve the entire network... | |
297 | 0 | os = db.get(Network.class); |
298 | 0 | while (os.hasNext()) |
299 | 0 | NetworkManager.getInstance().setNetwork((Network) os.next()); |
300 | ||
301 | // This will retrieve all the sessions... | |
302 | 0 | os = db.get(Session.class); |
303 | 0 | while (os.hasNext()) |
304 | 0 | addSession((Session) os.next()); |
305 | ||
306 | // This will retrieve all the services... | |
307 | 0 | os = db.get(Service.class); |
308 | 0 | while (os.hasNext()) |
309 | 0 | addService((Service) os.next()); |
310 | ||
311 | // This will retrieve all the backups... | |
312 | 0 | os = db.get(Backup.class); |
313 | 0 | while (os.hasNext()) |
314 | 0 | addBackup((Backup) os.next()); |
315 | ||
316 | 0 | logger.debug("Internet Cafe Successfully Retrieved."); |
317 | 0 | } catch (Exception ex) { |
318 | 0 | ex.printStackTrace(); |
319 | 0 | } finally { |
320 | 0 | db.close(); |
321 | 0 | } |
322 | 0 | } |
323 | ||
324 | /** | |
325 | * This method simply adds a user. | |
326 | * | |
327 | * @param user | |
328 | * The user to add. | |
329 | */ | |
330 | public void addUser(User user) { | |
331 | 0 | this.setChanged(); |
332 | 0 | this.user.put(userKey(user), user); |
333 | 0 | this.notifyObservers(user); |
334 | 0 | } |
335 | ||
336 | /** | |
337 | * This method simply adds a backup. | |
338 | * | |
339 | * @param backup | |
340 | * The backup to add. | |
341 | */ | |
342 | public void addBackup(Backup backup) { | |
343 | 0 | this.setChanged(); |
344 | 0 | this.backup.put(backupKey(backup), backup); |
345 | 0 | this.notifyObservers(backup); |
346 | 0 | } |
347 | ||
348 | /** | |
349 | * This method simply adds a service. | |
350 | * | |
351 | * @param service | |
352 | * The service to add. | |
353 | */ | |
354 | public void addService(Service service) { | |
355 | 0 | this.setChanged(); |
356 | 0 | this.service.put(serviceKey(service), service); |
357 | 0 | this.notifyObservers(service); |
358 | 0 | } |
359 | ||
360 | /** | |
361 | * @return Returns the user. | |
362 | */ | |
363 | public User[] getUser() { | |
364 | 26 | return user.values().toArray(new User[0]); |
365 | } | |
366 | ||
367 | /** | |
368 | * @return Returns the backup. | |
369 | */ | |
370 | public Backup[] getBackup() { | |
371 | 0 | return backup.values().toArray(new Backup[0]); |
372 | } | |
373 | ||
374 | /** | |
375 | * @return Returns the user. | |
376 | */ | |
377 | public Service[] getService() { | |
378 | 0 | return service.values().toArray(new Service[0]); |
379 | } | |
380 | ||
381 | /** | |
382 | * @return Returns the session. | |
383 | */ | |
384 | public Session[] getSession() { | |
385 | 0 | return session.values().toArray(new Session[0]); |
386 | } | |
387 | ||
388 | /** | |
389 | * This method simply adds a session. | |
390 | * | |
391 | * @param session | |
392 | * The session to add. | |
393 | */ | |
394 | public void addSession(Session session) { | |
395 | 0 | this.setChanged(); |
396 | 0 | this.session.put(sessionKey(session), session); |
397 | 0 | this.notifyObservers(session); |
398 | 0 | } |
399 | ||
400 | /** | |
401 | * This method simply adds a set of sessions. | |
402 | * | |
403 | * @param sessionSet | |
404 | * The session set to add. | |
405 | */ | |
406 | public void addSessionSet(Session[] sessionSet) { | |
407 | 0 | for (int i = 0; i < sessionSet.length; i++) |
408 | 0 | addSession(sessionSet[i]); |
409 | 0 | } |
410 | ||
411 | /** | |
412 | * This method deletes the user. | |
413 | * | |
414 | * @param user | |
415 | * The user to be deleted. | |
416 | */ | |
417 | public void deleteUser(User user) { | |
418 | 0 | this.setChanged(); |
419 | 0 | this.user.remove(userKey(user)); |
420 | 0 | this.notifyObservers(user); |
421 | 0 | } |
422 | ||
423 | /** | |
424 | * This method deletes the service. | |
425 | * | |
426 | * @param service | |
427 | * The user to be deleted. | |
428 | */ | |
429 | public void deleteService(Service service) { | |
430 | 0 | this.setChanged(); |
431 | 0 | this.service.remove(serviceKey(service)); |
432 | 0 | this.notifyObservers(service); |
433 | 0 | } |
434 | ||
435 | /** | |
436 | * This method simply builds a key string for an instance of User. | |
437 | * | |
438 | * @param user | |
439 | * The user from wich build the key. | |
440 | * @return A key rappresentation for the user. | |
441 | */ | |
442 | protected static String userKey(User user) { | |
443 | 0 | String key = user.getId() + " " + user.getName() + " " |
444 | 0 | + user.getSurname() + " " + user.getBirthday(); |
445 | 0 | return key; |
446 | } | |
447 | ||
448 | /** | |
449 | * This method simply builds a key string for an instance of Backup. | |
450 | * | |
451 | * @param backup | |
452 | * The backup from wich build the key. | |
453 | * @return A key rappresentation for the backup. | |
454 | */ | |
455 | protected static String backupKey(Backup backup) { | |
456 | 0 | String key = backup.getId() + " " + backup.getName() + " " |
457 | 0 | + backup.getDate(); |
458 | 0 | return key; |
459 | } | |
460 | ||
461 | /** | |
462 | * This method simply builds a key string for an instance of Service. | |
463 | * | |
464 | * @param service | |
465 | * The service from wich build the key. | |
466 | * @return A key rappresentation for the service. | |
467 | */ | |
468 | protected static String serviceKey(Service service) { | |
469 | 0 | String key = service.getId() + " " + service.getName() + " " |
470 | 0 | + service.getRateType(); |
471 | 0 | return key; |
472 | } | |
473 | ||
474 | /** | |
475 | * This method simply builds a key string for an instance of Session. | |
476 | * | |
477 | * @param session | |
478 | * The session from wich build the key. | |
479 | * @return A key rappresentation for the session. | |
480 | */ | |
481 | protected static String sessionKey(Session session) { | |
482 | 0 | String key = session.getId() + " " + session.getUser().getNickname() |
483 | 0 | + " " + session.getWorkstation().getName() + " " |
484 | 0 | + session.getStartTime(); |
485 | 0 | return key; |
486 | } | |
487 | ||
488 | /** | |
489 | * This method deletes a session. | |
490 | * | |
491 | * @param session | |
492 | * The session to delete. | |
493 | */ | |
494 | public void deleteSession(Session session) { | |
495 | 0 | this.setChanged(); |
496 | 0 | this.session.remove(sessionKey(session)); |
497 | 0 | this.notifyObservers(session); |
498 | 0 | } |
499 | ||
500 | /** | |
501 | * This method retrieves a user specifying his id. | |
502 | * | |
503 | * @param userId | |
504 | * An id associated to the user to retrieve. | |
505 | * @return A user with id equals to userId, null if the specified userId is | |
506 | * not a valid user's id. | |
507 | */ | |
508 | public User getUserById(int userId) { | |
509 | 0 | User[] user = getUser(); |
510 | 0 | for (int i = 0; i < user.length; i++) |
511 | 0 | if (user[i].getId() == userId) |
512 | 0 | return user[i]; |
513 | 0 | return null; |
514 | } | |
515 | ||
516 | /** | |
517 | * This method retrieves a session specifying his id. | |
518 | * | |
519 | * @param sessionId | |
520 | * An id associated to the session to retrieve. | |
521 | * @return A session with id equals to sessionId, null if the specified | |
522 | * sessionId is not a valid session's id. | |
523 | */ | |
524 | public Session getSessionById(int sessionId) { | |
525 | 0 | Session[] session = getSession(); |
526 | 0 | for (int i = 0; i < session.length; i++) |
527 | 0 | if (session[i].getId() == sessionId) |
528 | 0 | return session[i]; |
529 | 0 | return null; |
530 | } | |
531 | ||
532 | /** | |
533 | * This method retrieves a service specifying his id. | |
534 | * | |
535 | * @param serviceId | |
536 | * An id associated to the service to retrieve. | |
537 | * @return A service with id equals to serviceId, null if the specified | |
538 | * serviceId is not a valid service's id. | |
539 | */ | |
540 | public Service getServiceById(int serviceId) { | |
541 | 0 | Service[] service = getService(); |
542 | 0 | for (int i = 0; i < service.length; i++) |
543 | 0 | if (service[i].getId() == serviceId) |
544 | 0 | return service[i]; |
545 | 0 | return null; |
546 | } | |
547 | ||
548 | /** | |
549 | * This method retrieves a backup specifying his id. | |
550 | * | |
551 | * @param backupId | |
552 | * An id associated to the backup to retrieve. | |
553 | * @return A backup with id equals to backupId, null if the specified | |
554 | * backupId is not a valid backup's id. | |
555 | */ | |
556 | public Backup getBackupById(int backupId) { | |
557 | 0 | Backup[] backup = getBackup(); |
558 | 0 | for (int i = 0; i < backup.length; i++) |
559 | 0 | if (backup[i].getId() == backupId) |
560 | 0 | return backup[i]; |
561 | 0 | return null; |
562 | } | |
563 | ||
564 | /** | |
565 | * This method deletes a backup from the system. | |
566 | * | |
567 | * @param backup | |
568 | * The backup to delete. | |
569 | */ | |
570 | public void deleteBackup(Backup backup) { | |
571 | 0 | this.setChanged(); |
572 | 0 | this.backup.remove(backupKey(backup)); |
573 | 0 | this.notifyObservers(backup); |
574 | 0 | } |
575 | ||
576 | /** | |
577 | * This method clears all the InternetCafe's data. Backups will not be | |
578 | * deleted. | |
579 | */ | |
580 | public void clear() { | |
581 | 0 | user.clear(); |
582 | 0 | NetworkManager.getInstance().setNetwork(null); |
583 | 0 | session.clear(); |
584 | 0 | service.clear(); |
585 | 0 | } |
586 | ||
587 | /** | |
588 | * This method retrieves a user specifying his nickname. | |
589 | * | |
590 | * @param userNickname | |
591 | * A nickname associated to the user to retrieve. | |
592 | * @return A user with nickname equals to userNickname, null if the specified userNickname is | |
593 | * not a valid user's nickname. | |
594 | */ | |
595 | public User getUserByNickname(String userNickname) { | |
596 | 0 | User[] user = getUser(); |
597 | 0 | for (int i = 0; i < user.length; i++) |
598 | 0 | if (user[i].getNickname().equals(userNickname)) |
599 | 0 | return user[i]; |
600 | 0 | return null; |
601 | } | |
602 | ||
603 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |