View Javadoc
1   /******************************************************************************
2    * DatabaseImpl.java - Implement the BuckoVidLib database 
3    * 
4    * BuckoVidLib - The BuckoSoft Video Library
5    * Copyright(c) 2014 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.BuckoVidLib.db;
9   
10  import java.io.UnsupportedEncodingException;
11  import java.net.URLDecoder;
12  import java.net.URLEncoder;
13  import java.util.List;
14  import java.util.Timer;
15  import java.util.TimerTask;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.hibernate.HibernateException;
20  import org.hibernate.Query;
21  import org.hibernate.Session;
22  
23  import com.buckosoft.BuckoVidLib.domain.Actor;
24  import com.buckosoft.BuckoVidLib.domain.Director;
25  import com.buckosoft.BuckoVidLib.domain.FailedToRip;
26  import com.buckosoft.BuckoVidLib.domain.Genre;
27  import com.buckosoft.BuckoVidLib.domain.LibrarySection;
28  import com.buckosoft.BuckoVidLib.domain.TVSeason;
29  import com.buckosoft.BuckoVidLib.domain.UserAttribute;
30  import com.buckosoft.BuckoVidLib.domain.Video;
31  import com.buckosoft.BuckoVidLib.domain.VideoBase;
32  import com.buckosoft.BuckoVidLib.domain.VideoTexts;
33  import com.buckosoft.BuckoVidLib.domain.WishList;
34  import com.buckosoft.BuckoVidLib.domain.Writer;
35  import com.buckosoft.BuckoVidLib.util.ConfigManager;
36  
37  /** Implement the Database
38   * @author dick
39   * @since 2014-10-17
40   */
41  public class DatabaseImpl implements Database {
42  	private final Log log = LogFactory.getLog(getClass());
43  	private	final boolean	logTruncate = ConfigManager.getBoolean("BuckoVidLib.logTruncate", true);
44  
45  	final int maxTitle	 = 64;
46  	final int maxTagline = 256;
47  	final int maxSummary = 3072;
48  	
49  	private HibernateUtil	hibernateUtil;
50  	private	Timer			dbTimer = new Timer("DBCheck");
51  	private	TimerTask		dbTimerTask = new DBTimerTask();
52  	private	int				dbTimerDelay = 60*60*6;			// 6 hours
53  	private	boolean			refreshNeeded = true;
54  	
55  	protected boolean		refreshed = false;				// for JUnit tests
56  
57  	public DatabaseImpl() {
58  		hibernateUtil = new HibernateUtil();
59  		dbTimerDelay = ConfigManager.getInt("BuckoVidLib.dbTimerDelay", 60*60*6);
60  		log.info("Set timer delay to " + dbTimerDelay + " seconds");
61  		dbTimer.schedule(dbTimerTask, dbTimerDelay*1000, dbTimerDelay*1000);
62  	}
63  
64  	/** Configure and initialize the database.
65  	 * This call is so that we can control actual initialization until 
66  	 * after all parms are configured.
67  	 * @param configFile "BuckoVidLib-test-hibernate.cfg.xml" or null to use the built-in config 
68  	 */
69  	public void setInit(String configFile) {
70  		log.info("Initializing with: " + configFile);
71  		hibernateUtil.initialize(configFile);
72  	}
73  
74  	class DBTimerTask extends TimerTask {
75  		@Override
76  		public void run() {
77  			if (refreshNeeded) {
78  				log.info("Refreshing db connection");
79  				getWishList();
80  				refreshed = true;
81  			}
82  			refreshNeeded = true;
83  		}
84  	}
85  
86  	/* (non-Javadoc)
87  	 * @see com.buckosoft.BuckoVidLib.db.Database#getFailedToRip()
88  	 */
89  	@Override
90  	public List<FailedToRip> getFailedToRip() {
91  		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
92  		session.getTransaction().begin();
93  		try {
94  			@SuppressWarnings("unchecked")
95  			List<FailedToRip> result = session.createQuery("from FailedToRip").list();
96  			refreshNeeded = false;
97  			return(result);
98  		} catch (HibernateException e) {
99  			e.printStackTrace();
100 		} finally {
101 			session.getTransaction().commit();
102 		}
103 		return(null);
104 	}
105 
106 	/* (non-Javadoc)
107 	 * @see com.buckosoft.BuckoVidLib.db.Database#getWishList()
108 	 */
109 	@Override
110 	public List<WishList> getWishList() {
111 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
112 		session.getTransaction().begin();
113 		try {
114 		@SuppressWarnings("unchecked")
115 		List<WishList> result = session.createQuery("from WishList order by name").list();
116 		refreshNeeded = false;
117 		return(result);
118 		} catch (HibernateException e) {
119 			e.printStackTrace();
120 		} finally {
121 			session.getTransaction().commit();
122 		}
123 		return(null);
124 	}
125 
126 	/* (non-Javadoc)
127 	 * @see com.buckosoft.BuckoVidLib.db.Database#addWishList(com.buckosoft.BuckoVidLib.domain.WishList)
128 	 */
129 	@Override
130 	public void addWishList(WishList wishList) {
131 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
132 		session.getTransaction().begin();
133 		try {
134 			session.saveOrUpdate(wishList);
135 			refreshNeeded = false;
136 		} catch (Exception e) {
137 			session.getTransaction().rollback();
138 			e.printStackTrace();
139 			return;
140 		} finally {
141 			session.getTransaction().commit();
142 		}
143 	}
144 
145 	/* (non-Javadoc)
146 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoCount()
147 	 */
148 	@Override
149 	public int getVideoCount() {
150 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
151 		session.getTransaction().begin();
152 		try {
153 			@SuppressWarnings("unchecked")
154 			List<Long> count = session.createQuery("select count(*) from Video").list();
155 			int c = count.get(0).intValue();
156 			refreshNeeded = false;
157 			return(c);
158 		} catch (Exception e) {
159 			e.printStackTrace();
160 		} finally {
161 			session.getTransaction().commit();
162 		}
163 		return(0);
164 	}
165 
166 
167 	/* (non-Javadoc)
168 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoIdsByActor(int)
169 	 */
170 	@Override
171 	public List<Integer> getVideoIdsByActor(int key) {
172 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
173 		session.getTransaction().begin();
174 		try {
175 			@SuppressWarnings("unchecked")
176 			List<Integer> list = session.createQuery("select v.videoId from Video_Actor v where v.actorId = :actorId")
177 									.setParameter("actorId", key)
178 									.list();
179 			refreshNeeded = false;
180 			return(list);
181 		} catch (Exception e) {
182 			e.printStackTrace();
183 		} finally {
184 			session.getTransaction().commit();
185 		}
186 		return(null);
187 	}
188 
189 	/* (non-Javadoc)
190 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoIdsByDirector(int)
191 	 */
192 	@Override
193 	public List<Integer> getVideoIdsByDirector(int key) {
194 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
195 		session.getTransaction().begin();
196 		try {
197 			@SuppressWarnings("unchecked")
198 			List<Integer> list = session.createQuery("select v.videoId from Video_Director v where v.directorId = :directorId")
199 									.setParameter("directorId", key)
200 									.list();
201 			refreshNeeded = false;
202 			return(list);
203 		} catch (Exception e) {
204 			e.printStackTrace();
205 		} finally {
206 			session.getTransaction().commit();
207 		}
208 		return(null);
209 	}
210 
211 
212 	/* (non-Javadoc)
213 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoIdsByWriter(int)
214 	 */
215 	@Override
216 	public List<Integer> getVideoIdsByWriter(int key) {
217 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
218 		session.getTransaction().begin();
219 		try {
220 			@SuppressWarnings("unchecked")
221 			List<Integer> list = session.createQuery("select v.videoId from Video_Writer v where v.writerId = :writerId")
222 									.setParameter("writerId", key)
223 									.list();
224 			refreshNeeded = false;
225 			return(list);
226 		} catch (Exception e) {
227 			e.printStackTrace();
228 		} finally {
229 			session.getTransaction().commit();
230 		}
231 		return(null);
232 	}
233 
234 	/* (non-Javadoc)
235 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoIdsByGenre(int)
236 	 */
237 	@Override
238 	public List<Integer> getVideoIdsByGenre(int key) {
239 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
240 		session.getTransaction().begin();
241 		try {
242 			@SuppressWarnings("unchecked")
243 			List<Integer> list = session.createQuery("select v.videoId from Video_Genre v where v.genreId = :genreId")
244 									.setParameter("genreId", key)
245 									.list();
246 			refreshNeeded = false;
247 			return(list);
248 		} catch (Exception e) {
249 			e.printStackTrace();
250 		} finally {
251 			session.getTransaction().commit();
252 		}
253 		return(null);
254 
255 	}
256 
257 	/* (non-Javadoc)
258 	 * @see com.buckosoft.BuckoVidLib.db.Database#truncateLibrary()
259 	 */
260 	@Override
261 	public void truncateLibrary() {
262 		if (logTruncate) {
263 			RuntimeException re = new RuntimeException("Trucating library");
264 			log.warn("Truncating the library", re);
265 		} else
266 			log.warn("Truncating the library");
267 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
268 		session.getTransaction().begin();
269 		try {
270 			Query query;
271 			query = session.createSQLQuery("truncate table actors");
272 			query.executeUpdate();
273 			query = session.createSQLQuery("truncate table directors");
274 			query.executeUpdate();
275 			query = session.createSQLQuery("truncate table genres");
276 			query.executeUpdate();
277 			query = session.createSQLQuery("truncate table librarysections");
278 			query.executeUpdate();
279 			query = session.createSQLQuery("truncate table tvseasons");
280 			query.executeUpdate();
281 			query = session.createSQLQuery("truncate table videos");
282 			query.executeUpdate();
283 			query = session.createSQLQuery("truncate table videotexts");
284 			query.executeUpdate();
285 			query = session.createSQLQuery("truncate table video_actors");
286 			query.executeUpdate();
287 			query = session.createSQLQuery("truncate table video_directors");
288 			query.executeUpdate();
289 			query = session.createSQLQuery("truncate table video_genres");
290 			query.executeUpdate();
291 			query = session.createSQLQuery("truncate table video_writers");
292 			query.executeUpdate();
293 			query = session.createSQLQuery("truncate table writers");
294 			query.executeUpdate();
295 		} catch (Exception e) {
296 			e.printStackTrace();
297 		} finally {
298 			session.getTransaction().commit();
299 		}
300 		
301 	}
302 
303 	/* (non-Javadoc)
304 	 * @see com.buckosoft.BuckoVidLib.db.Database#addVideo(com.buckosoft.BuckoVidLib.domain.Video)
305 	 */
306 	@Override
307 	public void saveVideo(Video video) {
308 		log.info("saveVideo " + video.getTitle() + "(" + video.getYear() + ")");
309 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
310 		session.getTransaction().begin();
311 		try {
312 			session.saveOrUpdate(video);
313 			saveVideoTexts(session, video);
314 		} catch (Exception e) {
315 			session.getTransaction().rollback();
316 			e.printStackTrace();
317 			return;
318 		} finally {
319 			session.getTransaction().commit();
320 		}
321 	}
322 
323 	private	void saveVideoTexts(Session session, Video video) throws Exception {
324 		if (video.getVideoTexts() != null) {
325 			VideoTexts vt = video.getVideoTexts();
326 			if (vt != null) {
327 				VideoTexts vts = new VideoTexts();		// save them encoded on disk, but decoded in memory.
328 				if (vt.getTagline() != null) {
329 					String s = URLEncoder.encode(vt.getTagline(), "utf-8");
330 					if (s.length() >= maxTagline) {
331 						log.warn("***************************************************");
332 						log.warn("Truncating tagline for " + video.getTitle() + "(" + video.getYear() + ")");
333 						s = s.substring(0, maxTagline-1);
334 					}
335 					vts.setTagline(s);
336 				}
337 				if (vt.getSummary() != null) {
338 					String s = URLEncoder.encode(vt.getSummary(), "utf-8");
339 					if (s.length() >= maxSummary) {
340 						log.warn("***************************************************");
341 						log.warn("Truncating summary for " + video.getTitle() + "(" + video.getYear() + ")");
342 						s = s.substring(0, maxSummary-1);
343 						log.warn("summary =\"" + s + "\"");
344 					}
345 					vts.setSummary(s);
346 				}
347 				vts.setVideoId(video.getId());
348 				session.saveOrUpdate(vts);
349 			}
350 		}
351 		
352 	}
353 
354 	/* (non-Javadoc)
355 	 * @see com.buckosoft.BuckoVidLib.db.Database#deleteVideo(com.buckosoft.BuckoVidLib.domain.VideoBase)
356 	 */
357 	@Override
358 	public void deleteVideo(VideoBase vb) {
359 		log.info("deleteVideo [" + vb.getId() + "] " + vb.getTitle() + "(" + vb.getYear() + ")");
360 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
361 		session.getTransaction().begin();
362 		try {
363 			session.createQuery("delete from VideoTexts where videoId = :videoId")
364 				.setParameter("videoId", vb.getId())
365 				.executeUpdate();
366 			session.createQuery("delete from Video_Actor where videoId = :videoId")
367 				.setParameter("videoId", vb.getId())
368 				.executeUpdate();
369 			session.createQuery("delete from Video_Actor where videoId = :videoId")
370 				.setParameter("videoId", vb.getId())
371 				.executeUpdate();
372 			session.createQuery("delete from Video_Actor where videoId = :videoId")
373 				.setParameter("videoId", vb.getId())
374 				.executeUpdate();
375 			session.createQuery("delete from Video_Actor where videoId = :videoId")
376 				.setParameter("videoId", vb.getId())
377 				.executeUpdate();
378 			session.createQuery("delete from Video where id = :id")
379 				.setParameter("id", vb.getId())
380 				.executeUpdate();
381 		} catch (Exception e) {
382 			session.getTransaction().rollback();
383 			e.printStackTrace();
384 			return;
385 		} finally {
386 			session.getTransaction().commit();
387 		}
388 	}
389 
390 	/* (non-Javadoc)
391 	 * @see com.buckosoft.BuckoVidLib.db.Database#getMaxVideoTextsSummaryLength()
392 	 */
393 	@Override
394 	public int getMaxVideoTextsSummaryLength() {
395 		return(maxSummary);
396 	}
397 
398 	/* (non-Javadoc)
399 	 * @see com.buckosoft.BuckoVidLib.db.Database#getMaxVideoTextsTaglineLength()
400 	 */
401 	@Override
402 	public int getMaxVideoTextsTaglineLength() {
403 		return(maxTagline);
404 	}
405 
406 	/* (non-Javadoc)
407 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideo(int)
408 	 */
409 	@Override
410 	public Video getVideo(int videoId) {
411 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
412 		session.getTransaction().begin();
413 		Video v;
414 		try {
415 			v = (Video)session.createQuery("from com.buckosoft.BuckoVidLib.domain.Video v where v.id = :vId")
416 									.setParameter("vId", videoId)
417 									.uniqueResult();
418 		} catch (HibernateException e) {
419 			log.error("Failed to load videoId " + videoId, e);
420 			e.printStackTrace();
421 			return(null);
422 		} finally {
423 			session.getTransaction().commit();
424 		}
425 		refreshNeeded = false;
426 		return(v);
427 	}
428 
429 	/* (non-Javadoc)
430 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideos()
431 	 */
432 	@Override
433 	public List<Video> getVideos() {
434 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
435 		session.getTransaction().begin();
436 		try {
437 			@SuppressWarnings("unchecked")
438 			List<Video> list = session.createQuery("from Video")
439 									.list();
440 			refreshNeeded = false;
441 			return(list);
442 		} catch (Exception e) {
443 			e.printStackTrace();
444 		} finally {
445 			session.getTransaction().commit();
446 		}
447 		return(null);
448 	}
449 
450 	/* (non-Javadoc)
451 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideos(int)
452 	 */
453 //	@Override
454 //	public List<Video> getVideos(int videoId) {
455 //		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
456 //		session.getTransaction().begin();
457 //		try {
458 //			@SuppressWarnings("unchecked")
459 //			List<Video> list = session.createQuery("from Video v where v.id = :vId")
460 //									.setParameter("vId", videoId)
461 //									.list();
462 //			refreshNeeded = false;
463 //			return(list);
464 //		} catch (Exception e) {
465 //			e.printStackTrace();
466 //		} finally {
467 //			session.getTransaction().commit();
468 //		}
469 //		return(null);
470 //	}
471 
472 	/* (non-Javadoc)
473 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoBase(int)
474 	 */
475 	@Override
476 	public VideoBase getVideoBase(int videoId) {
477 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
478 		session.getTransaction().begin();
479 		VideoBase v;
480 		try {
481 			v = (VideoBase)session.createQuery("from ShortVideo v where v.id = :vId")
482 									.setParameter("vId", videoId)
483 									.uniqueResult();
484 		} catch (HibernateException e) {
485 			e.printStackTrace();
486 			throw e;
487 		} finally {
488 			session.getTransaction().commit();
489 		}
490 		refreshNeeded = false;
491 		return(v);
492 	}
493 
494 	/* (non-Javadoc)
495 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoBases()
496 	 */
497 	@Override
498 	public List<VideoBase> getVideoBases() {
499 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
500 		session.getTransaction().begin();
501 		try {
502 			@SuppressWarnings("unchecked")
503 			List<VideoBase> list = session.createQuery("from ShortVideo")
504 									.list();
505 			refreshNeeded = false;
506 			return(list);
507 		} catch (Exception e) {
508 			e.printStackTrace();
509 		} finally {
510 			session.getTransaction().commit();
511 		}
512 		return(null);
513 	}
514 
515 
516 	/* (non-Javadoc)
517 	 * @see com.buckosoft.BuckoVidLib.db.Database#saveSeason(com.buckosoft.BuckoVidLib.domain.TVSeason)
518 	 */
519 	@Override
520 	public void saveTVSeason(TVSeason season) {
521 		if (season.getVideoId() == 0)
522 			throw new RuntimeException("videoId = 0");
523 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
524 		session.getTransaction().begin();
525 		try {
526 			session.saveOrUpdate(season);
527 			refreshNeeded = false;
528 		} catch (Exception e) {
529 			session.getTransaction().rollback();
530 			e.printStackTrace();
531 			return;
532 		} finally {
533 			session.getTransaction().commit();
534 		}
535 	}
536 
537 	/* (non-Javadoc)
538 	 * @see com.buckosoft.BuckoVidLib.db.Database#getSeason(int)
539 	 */
540 	@Override
541 	public TVSeason getTVSeason(int seasonId) {
542 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
543 		session.getTransaction().begin();
544 		try {
545 			TVSeason s = (TVSeason)session.createQuery("from TVSeason t where t.id = :tId")
546 									.setParameter("tId", seasonId)
547 									.uniqueResult();
548 			refreshNeeded = false;
549 			return(s);
550 		} catch (Exception e) {
551 			e.printStackTrace();
552 		} finally {
553 			session.getTransaction().commit();
554 		}
555 		return(null);
556 	}
557 
558 	/* (non-Javadoc)
559 	 * @see com.buckosoft.BuckoVidLib.db.Database#getSeasonFromKey(int)
560 	 */
561 	@Override
562 	public TVSeason getTVSeasonFromHashKey(int key) {
563 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
564 		session.getTransaction().begin();
565 		try {
566 			TVSeason s = (TVSeason)session.createQuery("from TVSeason t where t.hashKey = :tKey")
567 									.setParameter("tKey", key)
568 									.uniqueResult();
569 			refreshNeeded = false;
570 			return(s);
571 		} catch (Exception e) {
572 			log.error("getTVSeasonFromHashKey failed for key: " + key);
573 			e.printStackTrace();
574 		} finally {
575 			session.getTransaction().commit();
576 		}
577 		return(null);
578 	}
579 
580 	/* (non-Javadoc)
581 	 * @see com.buckosoft.BuckoVidLib.db.Database#getSeasons()
582 	 */
583 	@Override
584 	public List<TVSeason> getTVSeasons() {
585 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
586 		session.getTransaction().begin();
587 		try {
588 			@SuppressWarnings("unchecked")
589 			List<TVSeason> list = session.createQuery("from TVSeason")
590 									.list();
591 			refreshNeeded = false;
592 			return(list);
593 		} catch (Exception e) {
594 			e.printStackTrace();
595 		} finally {
596 			session.getTransaction().commit();
597 		}
598 		return(null);
599 	}
600 
601 	@Override
602 	public List<TVSeason> getTVSeasons(int videoId) {
603 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
604 		session.getTransaction().begin();
605 		try {
606 			@SuppressWarnings("unchecked")
607 			List<TVSeason> list = session.createQuery("from TVSeason where videoId = :vId")
608 									.setParameter("vId", videoId)
609 									.list();
610 			refreshNeeded = false;
611 			return(list);
612 		} catch (Exception e) {
613 			e.printStackTrace();
614 		} finally {
615 			session.getTransaction().commit();
616 		}
617 		return(null);
618 	}
619 
620 	/* (non-Javadoc)
621 	 * @see com.buckosoft.BuckoVidLib.db.Database#addDirector(com.buckosoft.BuckoVidLib.domain.Director)
622 	 */
623 	@Override
624 	public void addDirector(Director director) {
625 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
626 		session.getTransaction().begin();
627 		try {
628 			session.saveOrUpdate(director);
629 			refreshNeeded = false;
630 		} catch (Exception e) {
631 			session.getTransaction().rollback();
632 			e.printStackTrace();
633 			return;
634 		} finally {
635 			session.getTransaction().commit();
636 		}
637 	}
638 
639 	/* (non-Javadoc)
640 	 * @see com.buckosoft.BuckoVidLib.db.Database#getDirector(int)
641 	 */
642 	@Override
643 	public Director getDirector(int directorId) {
644 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
645 		session.getTransaction().begin();
646 		try {
647 			Director d = (Director)session.createQuery("from Director d where d.id = :dId")
648 									.setParameter("dId", directorId)
649 									.uniqueResult();
650 			refreshNeeded = false;
651 			return(d);
652 		} catch (Exception e) {
653 			e.printStackTrace();
654 		} finally {
655 			session.getTransaction().commit();
656 		}
657 		return(null);
658 	}
659 
660 	/* (non-Javadoc)
661 	 * @see com.buckosoft.BuckoVidLib.db.Database#getDirector(java.lang.String)
662 	 */
663 	@Override
664 	public Director getDirector(String directorName) {
665 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
666 		session.getTransaction().begin();
667 		try {
668 			Director d = (Director)session.createQuery("from Director d where d.name = :dName")
669 									.setParameter("dName", directorName)
670 									.uniqueResult();
671 			refreshNeeded = false;
672 			return(d);
673 		} catch (Exception e) {
674 			e.printStackTrace();
675 		} finally {
676 			session.getTransaction().commit();
677 		}
678 		return(null);
679 	}
680 
681 	/* (non-Javadoc)
682 	 * @see com.buckosoft.BuckoVidLib.db.Database#getDirectors()
683 	 */
684 	@Override
685 	public List<Director> getDirectors() {
686 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
687 		session.getTransaction().begin();
688 		try {
689 			@SuppressWarnings("unchecked")
690 			List<Director> list = session.createQuery("from Director")
691 									.list();
692 			refreshNeeded = false;
693 			return(list);
694 		} catch (Exception e) {
695 			e.printStackTrace();
696 		} finally {
697 			session.getTransaction().commit();
698 		}
699 		return(null);
700 	}
701 
702 	/* (non-Javadoc)
703 	 * @see com.buckosoft.BuckoVidLib.db.Database#findDirectors(java.lang.String, int)
704 	 */
705 	@Override
706 	public List<Director> findDirectors(String key, int limit) {
707 		key = "%" + key + "%";
708 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
709 		session.getTransaction().begin();
710 		try {
711 			@SuppressWarnings("unchecked")
712 			List<Director> list = session.createQuery("from Director d where d.name like :dKey")
713 									.setParameter("dKey", key)
714 									.setMaxResults(limit)
715 									.list();
716 			refreshNeeded = false;
717 			return(list);
718 		} catch (Exception e) {
719 			e.printStackTrace();
720 		} finally {
721 			session.getTransaction().commit();
722 		}
723 		return(null);
724 	}
725 
726 	/* (non-Javadoc)
727 	 * @see com.buckosoft.BuckoVidLib.db.Database#addWriter(com.buckosoft.BuckoVidLib.domain.Writer)
728 	 */
729 	@Override
730 	public void addWriter(Writer writer) {
731 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
732 		session.getTransaction().begin();
733 		try {
734 			session.saveOrUpdate(writer);
735 			refreshNeeded = false;
736 		} catch (Exception e) {
737 			session.getTransaction().rollback();
738 			e.printStackTrace();
739 			return;
740 		} finally {
741 			session.getTransaction().commit();
742 		}
743 	}
744 
745 	/* (non-Javadoc)
746 	 * @see com.buckosoft.BuckoVidLib.db.Database#getWriter(int)
747 	 */
748 	@Override
749 	public Writer getWriter(int writerId) {
750 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
751 		session.getTransaction().begin();
752 		try {
753 			Writer d = (Writer)session.createQuery("from Writer d where d.id = :wId")
754 									.setParameter("wId", writerId)
755 									.uniqueResult();
756 			refreshNeeded = false;
757 			return(d);
758 		} catch (Exception e) {
759 			e.printStackTrace();
760 		} finally {
761 			session.getTransaction().commit();
762 		}
763 		return(null);
764 	}
765 
766 	/* (non-Javadoc)
767 	 * @see com.buckosoft.BuckoVidLib.db.Database#getWriter(java.lang.String)
768 	 */
769 	@Override
770 	public Writer getWriter(String writerName) {
771 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
772 		session.getTransaction().begin();
773 		try {
774 			Writer d = (Writer)session.createQuery("from Writer w where w.name = :wName")
775 									.setParameter("wName", writerName)
776 									.uniqueResult();
777 			refreshNeeded = false;
778 			return(d);
779 		} catch (Exception e) {
780 			e.printStackTrace();
781 		} finally {
782 			session.getTransaction().commit();
783 		}
784 		return(null);
785 	}
786 
787 	/* (non-Javadoc)
788 	 * @see com.buckosoft.BuckoVidLib.db.Database#getWriters()
789 	 */
790 	@Override
791 	public List<Writer> getWriters() {
792 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
793 		session.getTransaction().begin();
794 		try {
795 			@SuppressWarnings("unchecked")
796 			List<Writer> list = session.createQuery("from Writer")
797 									.list();
798 			refreshNeeded = false;
799 			return(list);
800 		} catch (Exception e) {
801 			e.printStackTrace();
802 		} finally {
803 			session.getTransaction().commit();
804 		}
805 		return(null);
806 	}
807 
808 	/* (non-Javadoc)
809 	 * @see com.buckosoft.BuckoVidLib.db.Database#findWriters(java.lang.String, int)
810 	 */
811 	@Override
812 	public List<Writer> findWriters(String key, int limit) {
813 		key = "%" + key + "%";
814 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
815 		session.getTransaction().begin();
816 		try {
817 			@SuppressWarnings("unchecked")
818 			List<Writer> list = session.createQuery("from Writer w where w.name like :wKey")
819 									.setParameter("wKey", key)
820 									.setMaxResults(limit)
821 									.list();
822 			refreshNeeded = false;
823 			return(list);
824 		} catch (Exception e) {
825 			e.printStackTrace();
826 		} finally {
827 			session.getTransaction().commit();
828 		}
829 		return(null);
830 	}
831 
832 	/* (non-Javadoc)
833 	 * @see com.buckosoft.BuckoVidLib.db.Database#addGenre(com.buckosoft.BuckoVidLib.domain.Genre)
834 	 */
835 	@Override
836 	public void addGenre(Genre genre) {
837 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
838 		session.getTransaction().begin();
839 		try {
840 			session.saveOrUpdate(genre);
841 			refreshNeeded = false;
842 		} catch (Exception e) {
843 			session.getTransaction().rollback();
844 			e.printStackTrace();
845 			return;
846 		} finally {
847 			session.getTransaction().commit();
848 		}
849 	}
850 
851 	/* (non-Javadoc)
852 	 * @see com.buckosoft.BuckoVidLib.db.Database#getGenre(int)
853 	 */
854 	@Override
855 	public Genre getGenre(int genreId) {
856 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
857 		session.getTransaction().begin();
858 		try {
859 			Genre g = (Genre)session.createQuery("from Genre g where g.id = :gId")
860 									.setParameter("gId", genreId)
861 									.uniqueResult();
862 			refreshNeeded = false;
863 			return(g);
864 		} catch (Exception e) {
865 			e.printStackTrace();
866 		} finally {
867 			session.getTransaction().commit();
868 		}
869 		return(null);
870 	}
871 
872 	/* (non-Javadoc)
873 	 * @see com.buckosoft.BuckoVidLib.db.Database#getGenre(java.lang.String)
874 	 */
875 	@Override
876 	public Genre getGenre(String genreTag) {
877 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
878 		session.getTransaction().begin();
879 		try {
880 			Genre g = (Genre)session.createQuery("from Genre g where g.tag = :gTag")
881 									.setParameter("gTag", genreTag)
882 									.uniqueResult();
883 			refreshNeeded = false;
884 			return(g);
885 		} catch (Exception e) {
886 			e.printStackTrace();
887 		} finally {
888 			session.getTransaction().commit();
889 		}
890 		return(null);
891 	}
892 
893 	/* (non-Javadoc)
894 	 * @see com.buckosoft.BuckoVidLib.db.Database#getGenres()
895 	 */
896 	@Override
897 	public List<Genre> getGenres() {
898 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
899 		session.getTransaction().begin();
900 		try {
901 			@SuppressWarnings("unchecked")
902 			List<Genre> list = session.createQuery("from Genre")
903 									.list();
904 			refreshNeeded = false;
905 			return(list);
906 		} catch (Exception e) {
907 			e.printStackTrace();
908 		} finally {
909 			session.getTransaction().commit();
910 		}
911 		return(null);
912 	}
913 
914 	/* (non-Javadoc)
915 	 * @see com.buckosoft.BuckoVidLib.db.Database#findGenres(java.lang.String, int)
916 	 */
917 	@Override
918 	public List<Genre> findGenres(String key, int limit) {
919 		key = "%" + key + "%";
920 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
921 		session.getTransaction().begin();
922 		try {
923 			@SuppressWarnings("unchecked")
924 			List<Genre> list = session.createQuery("from Genre g where g.tag like :gKey")
925 									.setParameter("gKey", key)
926 									.setMaxResults(limit)
927 									.list();
928 			refreshNeeded = false;
929 			return(list);
930 		} catch (Exception e) {
931 			e.printStackTrace();
932 		} finally {
933 			session.getTransaction().commit();
934 		}
935 		return(null);
936 	}
937 
938 	/* (non-Javadoc)
939 	 * @see com.buckosoft.BuckoVidLib.db.Database#addActor(com.buckosoft.BuckoVidLib.domain.Actor)
940 	 */
941 	@Override
942 	public void addActor(Actor actor) {
943 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
944 		session.getTransaction().begin();
945 		try {
946 			session.saveOrUpdate(actor);
947 			refreshNeeded = false;
948 		} catch (Exception e) {
949 			session.getTransaction().rollback();
950 			e.printStackTrace();
951 			return;
952 		} finally {
953 			session.getTransaction().commit();
954 		}
955 	}
956 
957 	
958 	/* (non-Javadoc)
959 	 * @see com.buckosoft.BuckoVidLib.db.Database#getActor(int)
960 	 */
961 	@Override
962 	public Actor getActor(int actorId) {
963 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
964 		session.getTransaction().begin();
965 		try {
966 			Actor a = (Actor)session.createQuery("from Actor a where a.id = :aId")
967 									.setParameter("aId", actorId)
968 									.uniqueResult();
969 			refreshNeeded = false;
970 			return(a);
971 		} catch (Exception e) {
972 			e.printStackTrace();
973 		} finally {
974 			session.getTransaction().commit();
975 		}
976 		return(null);
977 	}
978 
979 	/* (non-Javadoc)
980 	 * @see com.buckosoft.BuckoVidLib.db.Database#getActor(java.lang.String)
981 	 */
982 	@Override
983 	public Actor getActor(String actorName) {
984 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
985 		session.getTransaction().begin();
986 		try {
987 			Actor a = (Actor)session.createQuery("from Actor a where a.name = :aName")
988 									.setParameter("aName", actorName)
989 									.uniqueResult();
990 			refreshNeeded = false;
991 			return(a);
992 		} catch (Exception e) {
993 			e.printStackTrace();
994 		} finally {
995 			session.getTransaction().commit();
996 		}
997 		return(null);
998 	}
999 
1000 	/* (non-Javadoc)
1001 	 * @see com.buckosoft.BuckoVidLib.db.Database#getActors()
1002 	 */
1003 	@Override
1004 	public List<Actor> getActors() {
1005 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1006 		session.getTransaction().begin();
1007 		try {
1008 			@SuppressWarnings("unchecked")
1009 			List<Actor> list = session.createQuery("from Actor")
1010 									.list();
1011 			refreshNeeded = false;
1012 			return(list);
1013 		} catch (Exception e) {
1014 			e.printStackTrace();
1015 		} finally {
1016 			session.getTransaction().commit();
1017 		}
1018 		return(null);
1019 	}
1020 
1021 
1022 	/* (non-Javadoc)
1023 	 * @see com.buckosoft.BuckoVidLib.db.Database#findActors(java.lang.String)
1024 	 */
1025 	@Override
1026 	public List<Actor> findActors(String key, int limit) {
1027 		key = "%" + key + "%";
1028 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1029 		session.getTransaction().begin();
1030 		try {
1031 			@SuppressWarnings("unchecked")
1032 			List<Actor> list = session.createQuery("from Actor a where a.name like :aKey")
1033 									.setParameter("aKey", key)
1034 									.setMaxResults(limit)
1035 									.list();
1036 			refreshNeeded = false;
1037 			return(list);
1038 		} catch (Exception e) {
1039 			e.printStackTrace();
1040 		} finally {
1041 			session.getTransaction().commit();
1042 		}
1043 		return(null);
1044 	}
1045 
1046 	/* (non-Javadoc)
1047 	 * @see com.buckosoft.BuckoVidLib.db.Database#addRole(com.buckosoft.BuckoVidLib.domain.Role)
1048 	 */
1049 /*	@Override -- No Roles for now. Use Actors. Maybe someday add roles, but I don't really care.
1050 	public void addRole(Role role) {
1051 		// Auto-generated method stub
1052 		
1053 	}
1054 */
1055 	/* (non-Javadoc)
1056 	 * @see com.buckosoft.BuckoVidLib.db.Database#getVideoTexts(int)
1057 	 */
1058 	@Override
1059 	public VideoTexts getVideoTexts(int id) {
1060 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1061 		session.getTransaction().begin();
1062 		VideoTexts vts = null;
1063 		try {
1064 			VideoTexts vt = (VideoTexts)session.createQuery("from VideoTexts vt where vt.id = :vtId")
1065 									.setParameter("vtId", id)
1066 									.uniqueResult();
1067 			refreshNeeded = false;
1068 			if (vt != null) {
1069 				vts = new VideoTexts();
1070 				try {
1071 					if (vt.getSummary() != null)
1072 						vts.setSummary(URLDecoder.decode(vt.getSummary(), "utf-8"));
1073 					if (vt.getTagline() != null)
1074 						vts.setTagline(URLDecoder.decode(vt.getTagline(), "utf-8"));
1075 				} catch (UnsupportedEncodingException | IllegalArgumentException e) {
1076 					log.error("Failed to read VideoTexts");
1077 					if (vt.getSummary() != null)
1078 						log.error("Summary = \"" + vt.getSummary() + "\"");
1079 					if (vt.getTagline() != null)
1080 						log.error("Tagline = \"" + vt.getTagline() + "\"");
1081 					e.printStackTrace();
1082 				}
1083 			}
1084 			return(vts);
1085 		} catch (Exception e) {
1086 			e.printStackTrace();
1087 		} finally {
1088 			session.getTransaction().commit();
1089 		}
1090 		return(null);
1091 	}
1092 
1093 	/* (non-Javadoc)
1094 	 * @see com.buckosoft.BuckoVidLib.db.Database#addLibrarySection(com.buckosoft.BuckoVidLib.domain.LibrarySection)
1095 	 */
1096 	@Override
1097 	public void addLibrarySection(LibrarySection librarySection) {
1098 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1099 		session.getTransaction().begin();
1100 		try {
1101 			session.saveOrUpdate(librarySection);
1102 			refreshNeeded = false;
1103 		} catch (Exception e) {
1104 			session.getTransaction().rollback();
1105 			e.printStackTrace();
1106 			return;
1107 		} finally {
1108 			session.getTransaction().commit();
1109 		}
1110 	}
1111 
1112 	/* (non-Javadoc)
1113 	 * @see com.buckosoft.BuckoVidLib.db.Database#getLibrarySection(int)
1114 	 */
1115 	@Override
1116 	public LibrarySection getLibrarySection(int key) {
1117 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1118 		session.getTransaction().begin();
1119 		try {
1120 			LibrarySection ls = (LibrarySection)session.createQuery("from LibrarySection ls where ls.key = :key")
1121 									.setParameter("key", key)
1122 									.uniqueResult();
1123 			refreshNeeded = false;
1124 			return(ls);
1125 		} catch (Exception e) {
1126 			e.printStackTrace();
1127 		} finally {
1128 			session.getTransaction().commit();
1129 		}
1130 		return(null);
1131 	}
1132 
1133 	/* (non-Javadoc)
1134 	 * @see com.buckosoft.BuckoVidLib.db.Database#getLibrarySections()
1135 	 */
1136 	@Override
1137 	public List<LibrarySection> getLibrarySections() {
1138 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1139 		session.getTransaction().begin();
1140 		try {
1141 			@SuppressWarnings("unchecked")
1142 			List<LibrarySection> list = session.createQuery("from LibrarySection")
1143 									.list();
1144 			refreshNeeded = false;
1145 			return(list);
1146 		} catch (Exception e) {
1147 			e.printStackTrace();
1148 		} finally {
1149 			session.getTransaction().commit();
1150 		}
1151 		return(null);
1152 	}
1153 
1154 	/* (non-Javadoc)
1155 	 * @see com.buckosoft.BuckoVidLib.db.Database#getUser(int)
1156 	 */
1157 	@Override
1158 	public List<UserAttribute> getUserAttributes(int userId) {
1159 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1160 		session.getTransaction().begin();
1161 		try {
1162 			@SuppressWarnings("unchecked")
1163 			List<UserAttribute> ls = session.createQuery("from UserAttribute ua where ua.userId = :uid")
1164 									.setParameter("uid", userId)
1165 									.list();
1166 			refreshNeeded = false;
1167 			return(ls);
1168 		} catch (Exception e) {
1169 			e.printStackTrace();
1170 		} finally {
1171 			session.getTransaction().commit();
1172 		}
1173 		return(null);
1174 	}
1175 
1176 	/* (non-Javadoc)
1177 	 * @see com.buckosoft.BuckoVidLib.db.Database#saveUser(com.buckosoft.BuckoVidLib.domain.User)
1178 	 */
1179 	@Override
1180 	public void saveUserAttributes(List<UserAttribute> list) {
1181 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
1182 		session.getTransaction().begin();
1183 		try {
1184 			for (UserAttribute ua : list)
1185 				session.saveOrUpdate(ua);
1186 			refreshNeeded = false;
1187 		} catch (Exception e) {
1188 			session.getTransaction().rollback();
1189 			e.printStackTrace();
1190 			return;
1191 		} finally {
1192 			session.getTransaction().commit();
1193 		}
1194 	}
1195 	
1196 }