Changes to facilitate usage of Hibernate Tools
authorMartin Taal <martin.taal@openbravo.com>
Mon, 01 Feb 2010 11:36:47 +0100
changeset 5990 2fb77cf4313e
parent 5989 badd6aad2076
child 5991 0b2b6a389f51
child 6561 a55912716649
Changes to facilitate usage of Hibernate Tools
config/Openbravo.properties.template
config/eclipse/hibernate.cfg.xml
src-test/org/openbravo/test/base/BaseTest.java
src-test/org/openbravo/test/model/RuntimeModelTest.java
src-test/org/openbravo/test/model/UtilsTest.java
src/org/openbravo/base/session/OBPropertiesProvider.java
src/org/openbravo/dal/core/DalMappingGenerator.java
--- a/config/Openbravo.properties.template	Fri Jan 29 16:30:18 2010 +0100
+++ b/config/Openbravo.properties.template	Mon Feb 01 11:36:47 2010 +0100
@@ -135,3 +135,12 @@
 # with role System Administrator
 
 safe.mode=false
+
+##############
+# Hibernate #
+##############
+
+# if this property is set then the mapping will be written to the file
+# defined by this property, note the value should be the complete path 
+# including the file name and extension.
+# hibernate.hbm.file=/tmp/hibernate.hbm.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/eclipse/hibernate.cfg.xml	Mon Feb 01 11:36:47 2010 +0100
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo Public License 
+ * Version 1.1 (the "License"), being the Mozilla Public License 
+ * version 1.1  with a permitted attribution clause ; you may not use 
+ * this file except in compliance with the License. 
+ * You may obtain a copy of the License at  
+ * http://www.openbravo.com/legal/license.txt 
+ * Software distributed under the License is distributed on an 
+ * "AS IS" basis, WITHOUT WARRANTY OF  ANY KIND, either express or 
+ * implied. See the License for the specific language governing rights 
+ * and  limitations under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SL 
+ * All portions are Copyright (C) 2010 Openbravo SL 
+ * All Rights Reserved. 
+ * Contributor(s): Openbravo S.L.
+ ************************************************************************
+-->
+<!DOCTYPE hibernate-configuration PUBLIC
+                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+    <session-factory>
+    <!-- Postgress Settings -->
+        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
+        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
+        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/obpi</property>
+        <property name="hibernate.connection.username">tad</property>
+        <property name="hibernate.connection.password">TAD</property>
+        <property name="hibernate.cache.use_second_level_cache">false</property>
+    <!-- Oracle Settings
+        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
+        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
+        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ob</property>
+        <property name="hibernate.connection.username">pi</property>
+        <property name="hibernate.connection.password">tad</property>
+        <property name="hibernate.default_schema">PI</property>
+        <property name="hibernate.cache.use_second_level_cache">false</property>
+         -->
+    </session-factory> 
+</hibernate-configuration>
\ No newline at end of file
--- a/src-test/org/openbravo/test/base/BaseTest.java	Fri Jan 29 16:30:18 2010 +0100
+++ b/src-test/org/openbravo/test/base/BaseTest.java	Mon Feb 01 11:36:47 2010 +0100
@@ -19,8 +19,6 @@
 
 package org.openbravo.test.base;
 
-import java.io.File;
-import java.net.URL;
 import java.sql.SQLException;
 
 import junit.framework.TestCase;
@@ -31,7 +29,6 @@
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
-import org.openbravo.base.provider.OBConfigFileProvider;
 import org.openbravo.base.session.OBPropertiesProvider;
 import org.openbravo.base.structure.BaseOBObject;
 import org.openbravo.dal.core.DalLayerInitializer;
@@ -79,38 +76,16 @@
    */
   protected void initializeDalLayer() throws Exception {
     if (!DalLayerInitializer.getInstance().isInitialized()) {
-      setConfigPropertyFiles();
       DalLayerInitializer.getInstance().initialize(true);
     }
   }
 
   /**
    * Reads the configuration properties from the property files.
+   * 
+   * @deprecated not used anymore, behavior has been implemented in the {@link OBPropertiesProvider}
    */
   protected void setConfigPropertyFiles() {
-    // get the location of the current class file
-    final URL url = this.getClass().getResource(getClass().getSimpleName() + ".class");
-    File f = new File(url.getPath());
-    File propertiesFile = null;
-    while (f.getParentFile() != null && f.getParentFile().exists()) {
-      f = f.getParentFile();
-      final File configDirectory = new File(f, "config");
-      if (configDirectory.exists()) {
-        propertiesFile = new File(configDirectory, "Openbravo.properties");
-        if (propertiesFile.exists()) {
-          // found it and break
-          break;
-        }
-      }
-    }
-    if (propertiesFile == null) {
-      throw new OBException("The testrun assumes that it is run from "
-          + "within eclipse and that the Openbravo.properties "
-          + "file is located as a grandchild of the 7th ancestor " + "of this class");
-    }
-    OBPropertiesProvider.getInstance().setProperties(propertiesFile.getAbsolutePath());
-    OBConfigFileProvider.getInstance().setFileLocation(
-        propertiesFile.getParentFile().getAbsolutePath());
   }
 
   /**
--- a/src-test/org/openbravo/test/model/RuntimeModelTest.java	Fri Jan 29 16:30:18 2010 +0100
+++ b/src-test/org/openbravo/test/model/RuntimeModelTest.java	Mon Feb 01 11:36:47 2010 +0100
@@ -47,7 +47,6 @@
   // don't initialize dal layer for model tests
   @Override
   protected void setUp() throws Exception {
-    setConfigPropertyFiles();
     super.setUp();
   }
 
--- a/src-test/org/openbravo/test/model/UtilsTest.java	Fri Jan 29 16:30:18 2010 +0100
+++ b/src-test/org/openbravo/test/model/UtilsTest.java	Mon Feb 01 11:36:47 2010 +0100
@@ -46,7 +46,6 @@
   // don't initialize dal layer for model tests
   @Override
   protected void setUp() throws Exception {
-    setConfigPropertyFiles();
     super.setUp();
   }
 
--- a/src/org/openbravo/base/session/OBPropertiesProvider.java	Fri Jan 29 16:30:18 2010 +0100
+++ b/src/org/openbravo/base/session/OBPropertiesProvider.java	Mon Feb 01 11:36:47 2010 +0100
@@ -19,12 +19,15 @@
 
 package org.openbravo.base.session;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.Properties;
 
 import org.apache.log4j.Logger;
 import org.openbravo.base.exception.OBException;
+import org.openbravo.base.provider.OBConfigFileProvider;
 import org.openbravo.base.util.Check;
 
 /**
@@ -49,6 +52,9 @@
   }
 
   public Properties getOpenbravoProperties() {
+    if (obProperties == null) {
+      readPropertiesFromDevelopmentProject();
+    }
     return obProperties;
   }
 
@@ -84,4 +90,30 @@
       throw new OBException(e);
     }
   }
+
+  // tries to read the properties from the openbravo development project
+  private void readPropertiesFromDevelopmentProject() {
+    // get the location of the current class file
+    final URL url = this.getClass().getResource(getClass().getSimpleName() + ".class");
+    File f = new File(url.getPath());
+    File propertiesFile = null;
+    while (f.getParentFile() != null && f.getParentFile().exists()) {
+      f = f.getParentFile();
+      final File configDirectory = new File(f, "config");
+      if (configDirectory.exists()) {
+        propertiesFile = new File(configDirectory, "Openbravo.properties");
+        if (propertiesFile.exists()) {
+          // found it and break
+          break;
+        }
+      }
+    }
+    if (propertiesFile == null) {
+      return;
+    }
+    setProperties(propertiesFile.getAbsolutePath());
+    OBConfigFileProvider.getInstance().setFileLocation(
+        propertiesFile.getParentFile().getAbsolutePath());
+  }
+
 }
\ No newline at end of file
--- a/src/org/openbravo/dal/core/DalMappingGenerator.java	Fri Jan 29 16:30:18 2010 +0100
+++ b/src/org/openbravo/dal/core/DalMappingGenerator.java	Mon Feb 01 11:36:47 2010 +0100
@@ -33,6 +33,7 @@
 import org.openbravo.base.model.Property;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.base.provider.OBSingleton;
+import org.openbravo.base.session.OBPropertiesProvider;
 import org.openbravo.base.util.Check;
 
 /**
@@ -45,6 +46,8 @@
 public class DalMappingGenerator implements OBSingleton {
   private static final Logger log = Logger.getLogger(DalMappingGenerator.class);
 
+  private final static String HIBERNATE_FILE_PROPERTY = "hibernate.hbm.file";
+
   private final static String TEMPLATE_FILE = "template.hbm.xml";
   private final static String MAIN_TEMPLATE_FILE = "template_main.hbm.xml";
   // private final static char TAB = '\t';
@@ -89,9 +92,12 @@
       log.debug(result);
     }
 
-    if (false) {
+    final String hibernateFileLocation = OBPropertiesProvider.getInstance()
+        .getOpenbravoProperties().getProperty(HIBERNATE_FILE_PROPERTY);
+
+    if (hibernateFileLocation != null) {
       try {
-        final File f = new File("/tmp/hibernate.hbm.xml");
+        final File f = new File(hibernateFileLocation);
         if (f.exists()) {
           f.delete();
         }
@@ -100,7 +106,8 @@
         fw.write(result);
         fw.close();
       } catch (final Exception e) {
-        throw new OBException(e);
+        // ignoring exception for the rest
+        log.error("Exception when saving hibernate mapping in " + hibernateFileLocation, e);
       }
     }
     return result;