Coverage Report - com.buckosoft.fibs.BuckoFIBS.db.HibernateUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
HibernateUtil
75%
34/45
50%
5/10
2.667
 
 1  
 /******************************************************************************
 2  
  * HibernateUtil.java - Non-object access to the database 
 3  
  * $Id$
 4  
  * 
 5  
  * BuckoFIBS - Backgammon by BuckoSoft
 6  
  * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
 7  
  * 
 8  
  * $Log$
 9  
  * Revision 1.7  2011/05/31 19:36:38  dick
 10  
  * Privatize our private parts.
 11  
  *
 12  
  * Revision 1.6  2011/05/10 05:48:10  dick
 13  
  * Add support for an in-memory database.  Used by TEST.
 14  
  *
 15  
  * Revision 1.5  2011/05/09 17:09:39  dick
 16  
  * HibernateUtil goes from static to an object, so that during test we can keep
 17  
  * reinstantiating it.
 18  
  *
 19  
  * Revision 1.4  2011/05/08 21:41:40  dick
 20  
  * Add support for a test (in memory) database.
 21  
  *
 22  
  * Revision 1.3  2010/03/03 13:12:21  inim
 23  
  * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
 24  
  *
 25  
  * Revision 1.2  2010/03/03 12:19:48  inim
 26  
  * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
 27  
  *
 28  
  * Revision 1.1  2010/02/04 05:57:53  inim
 29  
  * Mavenized project folder layout
 30  
  *
 31  
  * Revision 1.1  2009/02/20 10:24:51  dick
 32  
  * Non-object access to the database.
 33  
  *
 34  
  */
 35  
 
 36  
 /* 
 37  
  * This program is free software: you can redistribute it and/or modify
 38  
  * it under the terms of the GNU General Public License as published by
 39  
  * the Free Software Foundation, either version 3 of the License, or
 40  
  * (at your option) any later version.
 41  
  *
 42  
  * This program is distributed in the hope that it will be useful,
 43  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 44  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 45  
  * GNU General Public License for more details.
 46  
  *
 47  
  * You should have received a copy of the GNU General Public License
 48  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 49  
  *
 50  
  * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
 51  
  * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
 52  
  */
 53  
 package com.buckosoft.fibs.BuckoFIBS.db;
 54  
 
 55  
 import java.io.File;
 56  
 
 57  
 import org.hibernate.Session;
 58  
 import org.hibernate.SessionFactory;
 59  
 import org.hibernate.cfg.Configuration;
 60  
 import org.hibernate.tool.hbm2ddl.SchemaExport;
 61  
 
 62  
 /** Non-object access to the database. <br>
 63  
  * This class is really private to DatabaseImpl.
 64  
  * @author Dick Balaska
 65  
  * @since 2009/02/18
 66  
  * @version $Revision$ <br> $Date$
 67  
  * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/db/HibernateUtil.java">cvs HibernateUtil.java</a>
 68  
  */
 69  11
 final class HibernateUtil {
 70  
         private SessionFactory sessionFactory;
 71  11
         private        Configuration configuration = null;
 72  
         
 73  
         /** for TEST, increment the database name on each instantation */
 74  1
         private        static int serialNumber = 0;
 75  
         
 76  
         private void initConfiguration(String url) {
 77  
                 try {
 78  
                         // Create the SessionFactory from hibernate.cfg.xml
 79  4
                         if (url == null) {
 80  0
                                 String s = "jdbc:hsqldb:file:" + getUserDatabaseDirectory() + "/BuckoFIBS";
 81  0
                                 configuration = new Configuration().configure().setProperty("hibernate.connection.url",
 82  
                                                 s);
 83  0
                         }
 84  4
                         else if (url.equals("mem"))
 85  4
                                 configuration = new Configuration().configure().setProperty("hibernate.connection.url",
 86  
                                                 "jdbc:hsqldb:mem:mymemdb" + serialNumber++);
 87  0
                         else if (url.equals("testfile"))
 88  0
                                 configuration = new Configuration().configure().setProperty("hibernate.connection.url",
 89  
                                                 "jdbc:hsqldb:file:db/test/BuckoFIBSTest" + serialNumber++);
 90  4
                         sessionFactory = configuration.buildSessionFactory();
 91  0
                 } catch (Throwable ex) {
 92  
                         // Make sure you log the exception, as it might be swallowed
 93  0
                         System.err.println("Initial SessionFactory creation failed." + ex);
 94  0
                         throw new ExceptionInInitializerError(ex);
 95  4
                 }
 96  4
         }
 97  
 
 98  
         protected SessionFactory getSessionFactory() {
 99  47
                 return sessionFactory;
 100  
         }
 101  
 
 102  
         protected void maybeCreateNewDatabase(String url) {
 103  4
                 initConfiguration(url);
 104  4
                 Session session = getSessionFactory().getCurrentSession();
 105  4
                 session.beginTransaction();
 106  4
                 Object o = null;
 107  
                 try {
 108  4
                         o = session.createQuery("from Player where id = ?")
 109  4
                                 .setInteger(0, 1).uniqueResult();
 110  4
                 } catch (Exception e) {
 111  4
                         System.out.println("maybeCreateNewDatabase needs to create database");
 112  
                         //e.printStackTrace();
 113  0
                 }
 114  4
                 session.getTransaction().commit();
 115  4
                 if (o != null)                // database ok.
 116  0
                         return;
 117  
 
 118  4
                 SchemaExport se = new SchemaExport(configuration);
 119  4
                 se.execute(true, true, false, true);
 120  4
         }
 121  
 
 122  
         /** Return the location for user data
 123  
          * @return The absolute string for {user.home}/jIBS
 124  
          */
 125  
         public static String getUserDataDirectory() {
 126  1
                 String s = "{user.home}" + File.separator + "BuckoFIBS";
 127  1
                 return(HibernateUtil.unwindProperties(s));
 128  
         }
 129  
 
 130  
         /** Return the location of our hsqldb database
 131  
          * @return The absolute string for {user.home}/jIBS/database
 132  
          */
 133  
         public static String getUserDatabaseDirectory() {
 134  0
                 return(getUserDataDirectory() + File.separator + "database");
 135  
         }
 136  
 
 137  
         /** Given a string, substitute any System properties in that string.
 138  
          * A property is denoted with {}, i.e. <code>{user.home}</code>
 139  
          * @param in The string to unwind
 140  
          * @return The resultant string.  The original string is returned if no left brace '{' is found.
 141  
          */
 142  
         public static String unwindProperties(String in) {
 143  2
                 int i = in.indexOf('{');
 144  2
                 if (i == -1)
 145  1
                         return(in);
 146  1
                 int j = in.indexOf('}');
 147  1
                 String out = in.substring(0, i);
 148  1
                 String s = System.getProperty(in.substring(i+1, j));
 149  1
                 out += s;
 150  1
                 out += in.substring(j+1);
 151  1
                 return(out);
 152  
         }
 153  
 }