[seam-commits] Seam SVN: r10197 - in trunk/examples/wiki: src/etc and 3 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Mar 20 22:04:22 EDT 2009


Author: christian.bauer at jboss.com
Date: 2009-03-20 22:04:22 -0400 (Fri, 20 Mar 2009)
New Revision: 10197

Added:
   trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/BinaryBlobType.java
Modified:
   trunk/examples/wiki/README.txt
   trunk/examples/wiki/src/etc/META-INF/components-dev.xml
   trunk/examples/wiki/src/etc/META-INF/persistence-dev-war.xml
   trunk/examples/wiki/src/etc/META-INF/persistence-prod-war.xml
   trunk/examples/wiki/src/etc/wiki-dev-ds.xml
   trunk/examples/wiki/src/etc/wiki-prod-ds.xml
   trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/UserProfile.java
   trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUpload.java
   trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUploadImage.java
   trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/package-info.java
   trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/DBUnitImporter.java
Log:
Ported wiki dev deployment to PostgreSQL

Modified: trunk/examples/wiki/README.txt
===================================================================
--- trunk/examples/wiki/README.txt	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/README.txt	2009-03-21 02:04:22 UTC (rev 10197)
@@ -19,7 +19,7 @@
 - Upgrade/downgrade the Hibernate libraries to the ones bundled with this application:
   'ant upgradehibernate' will replace the libraries in server/default/lib of JBoss AS 4.2.3
   and also copy the required ehcache.jar.
-  (Sorry, but no other version works currently and the 4.2.2 bundled libraries are too old.)
+  (Sorry, but no other version works currently and the 4.2.3 bundled libraries are too old.)
 
 - Install MySQL 5.x and start it
 
@@ -34,6 +34,19 @@
   default login 'admin' with password 'admin'
 
 
+INSTALLATION WITH POSTGRESQL (development profile)
+==========================================================================================
+
+Follow the installation steps for MySQL but edit the following files and uncomment PostgreSQL 8.3 support:
+
+- src/etc/wiki-dev-ds.xml
+- src/etc/META-INF/components-dev.xml
+- src/etc/META-INF/persistence-dev-war.xml
+- src/main/org/jboss/seam/wiki/core/model/package-info.java
+
+For production packaging (see 'dist' target below), edit the src/etc/**/*-prod* files.
+
+
 INSTALLATION WITH MYSQL (production profile)
 ==========================================================================================
 

Modified: trunk/examples/wiki/src/etc/META-INF/components-dev.xml
===================================================================
--- trunk/examples/wiki/src/etc/META-INF/components-dev.xml	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/etc/META-INF/components-dev.xml	2009-03-21 02:04:22 UTC (rev 10197)
@@ -52,7 +52,12 @@
 
     <!-- Some DBUnit datasets we use in unit testing - let's also use them in dev mode when the app is deployed -->
     <component name="dbunitImporter" class="org.jboss.seam.wiki.util.DBUnitImporter" scope="APPLICATION" auto-create="true">
+
         <property name="database">mysql</property>
+        <!--
+        <property name="database">postgresql</property>
+        -->
+
         <property name="datasourceJndiName">java:/wikiDatasource</property>
         <property name="binaryDir">META-INF/testbinaries</property>
         <property name="datasets">

Modified: trunk/examples/wiki/src/etc/META-INF/persistence-dev-war.xml
===================================================================
--- trunk/examples/wiki/src/etc/META-INF/persistence-dev-war.xml	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/etc/META-INF/persistence-dev-war.xml	2009-03-21 02:04:22 UTC (rev 10197)
@@ -9,14 +9,21 @@
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <jta-data-source>java:/wikiDatasource</jta-data-source>
         <properties>
+
+            <!-- Enable the following block for MySQL 5.x support -->
             <property name="hibernate.dialect" value="org.jboss.seam.wiki.util.WikiMySQL5HibernateDialect"/>
+            <property name="hibernate.binary_or_blob" value="blob"/>
+
+            <!-- Enable the following block for PostgreSQL 8.3 support
+            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
+            <property name="hibernate.binary_or_blob" value="binary"/>
+            -->
+
             <property name="hibernate.transaction.manager_lookup_class"
                       value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
 
             <!-- Automatic schema export and drop -->
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
-            <!--
-            -->
 
             <!-- Logging -->
             <property name="hibernate.show_sql" value="false"/>

Modified: trunk/examples/wiki/src/etc/META-INF/persistence-prod-war.xml
===================================================================
--- trunk/examples/wiki/src/etc/META-INF/persistence-prod-war.xml	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/etc/META-INF/persistence-prod-war.xml	2009-03-21 02:04:22 UTC (rev 10197)
@@ -9,7 +9,16 @@
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <jta-data-source>java:/wikiDatasource</jta-data-source>
         <properties>
+
+            <!-- Enable the following block for MySQL 5.x support -->
             <property name="hibernate.dialect" value="org.jboss.seam.wiki.util.WikiMySQL5HibernateDialect"/>
+            <property name="hibernate.binary_or_blob" value="blob"/>
+
+            <!-- Enable the following block for PostgreSQL 8.3 support
+            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
+            <property name="hibernate.binary_or_blob" value="binary"/>
+            -->
+
             <property name="hibernate.transaction.manager_lookup_class"
                       value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
 

Modified: trunk/examples/wiki/src/etc/wiki-dev-ds.xml
===================================================================
--- trunk/examples/wiki/src/etc/wiki-dev-ds.xml	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/etc/wiki-dev-ds.xml	2009-03-21 02:04:22 UTC (rev 10197)
@@ -1,18 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <datasources>
 
-    <!-- TODO: Queries are getting too complex for HSQL DB, see ForumQueries.hbm.xml
+    <!-- MySQL 5.x -->
     <local-tx-datasource>
-        <jndi-name>wikiDatasource</jndi-name>
-        <connection-url>jdbc:hsqldb:hsql://localhost:9001</connection-url>
-        <driver-class>org.hsqldb.jdbcDriver</driver-class>
-        <user-name>sa</user-name>
-        <password></password>
-    </local-tx-datasource>
-    -->
 
-    <local-tx-datasource>
-
         <jndi-name>wikiDatasource</jndi-name>
         <connection-url>jdbc:mysql://localhost/test?characterEncoding=UTF-8</connection-url>
         <driver-class>com.mysql.jdbc.Driver</driver-class>
@@ -36,4 +27,22 @@
 
     </local-tx-datasource>
 
+    <!-- PostgreSQL 8.3 TODO: Validate timeout/pool settings!
+    <local-tx-datasource>
+
+        <jndi-name>wikiDatasource</jndi-name>
+        <connection-url>jdbc:postgresql://localhost/test</connection-url>
+        <driver-class>org.postgresql.Driver</driver-class>
+        <user-name>postgres</user-name>
+        <password>postgres</password>
+
+        <min-pool-size>20</min-pool-size>
+        <max-pool-size>250</max-pool-size>
+
+        <blocking-timeout-millis>5000</blocking-timeout-millis>
+        <idle-timeout-minutes>2</idle-timeout-minutes>
+
+    </local-tx-datasource>
+    -->
+
 </datasources>

Modified: trunk/examples/wiki/src/etc/wiki-prod-ds.xml
===================================================================
--- trunk/examples/wiki/src/etc/wiki-prod-ds.xml	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/etc/wiki-prod-ds.xml	2009-03-21 02:04:22 UTC (rev 10197)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <datasources>
 
+    <!-- MySQL 5.x -->
     <local-tx-datasource>
         <jndi-name>wikiDatasource</jndi-name>
         <connection-url>jdbc:mysql://localhost/test?characterEncoding=UTF-8</connection-url>
@@ -25,5 +26,23 @@
 
     </local-tx-datasource>
 
+    <!-- PostgreSQL 8.3 TODO: Validate timeout/pool settings!
+    <local-tx-datasource>
+
+        <jndi-name>wikiDatasource</jndi-name>
+        <connection-url>jdbc:postgresql://localhost/test</connection-url>
+        <driver-class>org.postgresql.Driver</driver-class>
+        <user-name>postgres</user-name>
+        <password>postgres</password>
+
+        <min-pool-size>20</min-pool-size>
+        <max-pool-size>250</max-pool-size>
+
+        <blocking-timeout-millis>5000</blocking-timeout-millis>
+        <idle-timeout-minutes>2</idle-timeout-minutes>
+
+    </local-tx-datasource>
+    -->
+
 </datasources>
 

Modified: trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/UserProfile.java
===================================================================
--- trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/UserProfile.java	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/UserProfile.java	2009-03-21 02:04:22 UTC (rev 10197)
@@ -50,7 +50,7 @@
     private String signature;
 
     // SchemaExport needs length.. MySQL has "tinyblob", "mediumblob" and other such nonsense types
-    @Lob
+    @org.hibernate.annotations.Type(type = "org.jboss.seam.wiki.util.BinaryBlobType")
     @Column(name = "IMAGE_DATA", nullable = true, length = 200000)
     @Basic(fetch = FetchType.LAZY) // Lazy loaded through bytecode instrumentation
     private byte[] image;
@@ -59,7 +59,7 @@
     private String imageContentType;
 
     // SchemaExport needs length.. MySQL has "tinyblob", "mediumblob" and other such nonsense types
-    @Lob
+    @org.hibernate.annotations.Type(type = "org.jboss.seam.wiki.util.BinaryBlobType")
     @Column(name = "SMALL_IMAGE_DATA", nullable = true, length = 200000)
     @Basic(fetch = FetchType.LAZY) // Lazy loaded through bytecode instrumentation
     private byte[] smallImage;

Modified: trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUpload.java
===================================================================
--- trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUpload.java	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUpload.java	2009-03-21 02:04:22 UTC (rev 10197)
@@ -21,7 +21,7 @@
     private int filesize;
 
     // SchemaExport needs length.. MySQL has "tinyblob", "mediumblob" and other such nonsense types
-    @Lob
+    @org.hibernate.annotations.Type(type = "org.jboss.seam.wiki.util.BinaryBlobType")
     @Column(name = "FILEDATA", nullable = false, length = 10000000)
     @Basic(fetch = FetchType.LAZY) // Lazy loaded through bytecode instrumentation
     private byte[] data;

Modified: trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUploadImage.java
===================================================================
--- trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUploadImage.java	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUploadImage.java	2009-03-21 02:04:22 UTC (rev 10197)
@@ -28,9 +28,8 @@
     @Column(name = "THUMBNAIL")
     private char thumbnail = Thumbnail.MEDIUM.getFlag(); // Medium size thumbnail by default, not attached
 
-    // TODO: SchemaExport needs length.. MySQL has "tinyblob", "mediumblob" and other such nonsense types, this
-    // is a best-guess value
-    @Lob
+    // SchemaExport needs length.. MySQL has "tinyblob", "mediumblob" and other such nonsense types
+    @org.hibernate.annotations.Type(type = "org.jboss.seam.wiki.util.BinaryBlobType")
     @Column(name = "THUMBNAIL_DATA", length = 1000000, nullable = true)
     @Basic(fetch = FetchType.LAZY) // Lazy loaded through bytecode instrumentation
     private byte[] thumbnailData;

Modified: trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/package-info.java
===================================================================
--- trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/package-info.java	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/main/org/jboss/seam/wiki/core/model/package-info.java	2009-03-21 02:04:22 UTC (rev 10197)
@@ -1,3 +1,6 @@
+/*
+    MySQL 5.x requires a fake sequence which Hibernate can provide with a custom table.
+ */
 @GenericGenerator(
     name = "wikiSequenceGenerator",
     strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
@@ -8,6 +11,19 @@
     }
 )
 
+/*
+    PostgreSQL 8.3 supports real sequences
+
+ at GenericGenerator(
+    name = "wikiSequenceGenerator",
+    strategy = "sequence",
+    parameters = {
+        @Parameter(name = "sequence", value = "WIKI_SEQUENCE"),
+        @Parameter(name = "parameters", value = "increment by 1 start with 1000")
+    }
+)
+*/
+
 package org.jboss.seam.wiki.core.model;
 
 import org.hibernate.annotations.GenericGenerator;

Added: trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/BinaryBlobType.java
===================================================================
--- trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/BinaryBlobType.java	                        (rev 0)
+++ trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/BinaryBlobType.java	2009-03-21 02:04:22 UTC (rev 10197)
@@ -0,0 +1,298 @@
+/*
+ *  Copyright 2004 Blandware (http://www.blandware.com)
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.jboss.seam.wiki.util;
+
+import org.hibernate.Hibernate;
+import org.hibernate.HibernateException;
+import org.hibernate.cfg.Environment;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.type.Type;
+import org.hibernate.usertype.CompositeUserType;
+import org.hibernate.util.PropertiesHelper;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Serializable;
+import java.sql.Blob;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * <p>This is wrapper for both BinaryType and BlobType in order to give developer the ability to switch them via config</p>
+ * <p>Returned class is <code>byte[]</code>, that's why we should make conversion of BLOB</p>
+ * <p>User hibernate.binary_or_blob hibernate property in order to manage behaviour</p>
+ * <p/>
+ * <p><a href="BinaryBlobType.java.html"><i>View Source</i></a></p>
+ *
+ * @author Andrey Grebnev <a href="mailto:andrey.grebnev at blandware.com">&lt;andrey.grebnev at blandware.com&gt;</a>
+ * @version $Revision: 1.2 $ $Date: 2006/03/12 14:06:47 $
+ */
+public class BinaryBlobType implements CompositeUserType {
+
+    /**
+     * Default bufer size in order to copy InputStream to byte[]
+     */
+    protected static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+
+    /**
+     * If it is </code>true</code> we use <code>BlobType</code>, otherwise <code>BinaryType</code>
+     */
+    protected boolean isBlob = false;
+
+    /**
+     * Creates new instance of BinaryBlobType
+     */
+    public BinaryBlobType() {
+        isBlob = "blob".equalsIgnoreCase(PropertiesHelper.getString("hibernate.binary_or_blob", Environment.getProperties(), "binary"));
+    }
+
+
+    /**
+     * Get the "property names" that may be used in a
+     * query.
+     *
+     * @return an array of "property names"
+     */
+    public String[] getPropertyNames() {
+        return new String[]{"value"};
+    }
+
+    /**
+     * Get the corresponding "property types".
+     *
+     * @return an array of Hibernate types
+     */
+    public Type[] getPropertyTypes() {
+        if (isBlob)
+            return new Type[]{Hibernate.BLOB};
+        else
+            return new Type[]{Hibernate.BINARY};
+    }
+
+    /**
+     * Get the value of a property.
+     *
+     * @param component an instance of class mapped by this "type"
+     * @param property
+     * @return the property value
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public Object getPropertyValue(Object component, int property) throws HibernateException {
+        return component;
+    }
+
+    /**
+     * Set the value of a property.
+     *
+     * @param component an instance of class mapped by this "type"
+     * @param property
+     * @param value     the value to set
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
+    }
+
+    /**
+     * The class returned by <tt>nullSafeGet()</tt>.
+     *
+     * @return Class
+     */
+    public Class returnedClass() {
+        return Hibernate.BINARY.getReturnedClass();
+    }
+
+    /**
+     * Compare two instances of the class mapped by this type for persistence "equality".
+     * Equality of the persistent state.
+     *
+     * @param x
+     * @param y
+     * @return boolean
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public boolean equals(Object x, Object y) throws HibernateException {
+        return Hibernate.BINARY.isEqual(x, y);
+    }
+
+    /**
+     * Get a hashcode for the instance, consistent with persistence "equality"
+     */
+    public int hashCode(Object x) throws HibernateException {
+        return Hibernate.BINARY.getHashCode(x, null);
+    }
+
+    /**
+     * Retrieve an instance of the mapped class from a JDBC resultset. Implementors
+     * should handle possibility of null values.
+     *
+     * @param rs      a JDBC result set
+     * @param names   the column names
+     * @param session
+     * @param owner   the containing entity
+     * @return Object
+     * @throws org.hibernate.HibernateException
+     *
+     * @throws java.sql.SQLException
+     */
+    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
+        if (isBlob) {
+            Blob blob = (Blob)Hibernate.BLOB.nullSafeGet(rs, names, session, owner);
+            if (blob == null)
+                return null;
+            else
+                return copyData(blob.getBinaryStream());
+        } else {
+            return Hibernate.BINARY.nullSafeGet(rs, names, session, owner);
+        }
+    }
+
+    /**
+     * Write an instance of the mapped class to a prepared statement. Implementors
+     * should handle possibility of null values. A multi-column type should be written
+     * to parameters starting from <tt>index</tt>.
+     *
+     * @param st      a JDBC prepared statement
+     * @param value   the object to write
+     * @param index   statement parameter index
+     * @param session
+     * @throws org.hibernate.HibernateException
+     *
+     * @throws java.sql.SQLException
+     */
+    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
+        if (isBlob) {
+            if (value == null)
+                Hibernate.BLOB.nullSafeSet(st, value, index, session);
+            else {
+                Blob blob = Hibernate.createBlob((byte[])value);
+                Hibernate.BLOB.nullSafeSet(st, blob, index, session);
+            }
+        } else {
+            Hibernate.BINARY.nullSafeSet(st, value, index, session);
+        }
+    }
+
+    /**
+     * Return a deep copy of the persistent state, stopping at entities and at collections.
+     *
+     * @param value generally a collection element or entity field
+     * @return Object a copy
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public Object deepCopy(Object value) throws HibernateException {
+        return Hibernate.BINARY.deepCopy(value, null, null);
+    }
+
+    /**
+     * Check if objects of this type mutable.
+     *
+     * @return boolean
+     */
+    public boolean isMutable() {
+        return Hibernate.BINARY.isMutable();
+    }
+
+    /**
+     * Transform the object into its cacheable representation. At the very least this
+     * method should perform a deep copy. That may not be enough for some implementations,
+     * however; for example, associations must be cached as identifier values. (optional
+     * operation)
+     *
+     * @param value   the object to be cached
+     * @param session
+     * @return a cachable representation of the object
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
+        return Hibernate.BINARY.disassemble(value, session, null);
+    }
+
+    /**
+     * Reconstruct an object from the cacheable representation. At the very least this
+     * method should perform a deep copy. (optional operation)
+     *
+     * @param cached  the object to be cached
+     * @param session
+     * @param owner   the owner of the cached object
+     * @return a reconstructed object from the cachable representation
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
+        return Hibernate.BINARY.assemble(cached, session, owner);
+    }
+
+    /**
+     * During merge, replace the existing (target) value in the entity we are merging to
+     * with a new (original) value from the detached entity we are merging. For immutable
+     * objects, or null values, it is safe to simply return the first parameter. For
+     * mutable objects, it is safe to return a copy of the first parameter. However, since
+     * composite user types often define component values, it might make sense to recursively
+     * replace component values in the target object.
+     *
+     * @param original
+     * @param target
+     * @param session
+     * @param owner
+     * @return first parameter
+     * @throws org.hibernate.HibernateException
+     *
+     */
+    public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException {
+        return Hibernate.BINARY.replace(original, target, session, owner, null);
+    }
+
+
+    /**
+     * Copy data from InputStream into byte[]
+     *
+     * @param input source
+     * @return the resulted array
+     */
+    protected byte[] copyData(InputStream input) {
+        ByteArrayOutputStream output = null;
+        try {
+            output = new ByteArrayOutputStream();
+            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+            int n = 0;
+            while (-1 != (n = input.read(buffer))) {
+                output.write(buffer, 0, n);
+            }
+            return output.toByteArray();
+
+        } catch (IOException ex) {
+            throw new RuntimeException("Cannot copy data from InputStream into byte[]", ex);
+        } finally {
+            try {
+                input.close();
+            } catch (IOException ex2) {
+                //do nothing
+            }
+            try {
+                output.close();
+            } catch (IOException ex2) {
+                //do nothing
+            }
+        }
+    }
+}
\ No newline at end of file

Modified: trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/DBUnitImporter.java
===================================================================
--- trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/DBUnitImporter.java	2009-03-21 01:42:55 UTC (rev 10196)
+++ trunk/examples/wiki/src/main/org/jboss/seam/wiki/util/DBUnitImporter.java	2009-03-21 02:04:22 UTC (rev 10197)
@@ -30,7 +30,7 @@
 public class DBUnitImporter {
 
     public enum Database {
-        hsql, mysql
+        hsql, mysql, postgresql
     }
 
     protected Database database;
@@ -82,7 +82,9 @@
             con = getConnection();
             disableReferentialIntegrity(con);
             for (DataSetOperation op : dataSetOperations) {
+                prepareExecution(con, op);
                 op.execute(con);
+                afterExecution(con, op);
             }
             enableReferentialIntegrity(con);
         } finally {
@@ -134,6 +136,8 @@
                 con.getConnection().prepareStatement("set referential_integrity FALSE").execute(); // HSQL DB
             } else if (database.equals(Database.mysql)) {
                 con.getConnection().prepareStatement("set foreign_key_checks=0").execute(); // MySQL > 4.1.1
+            } else if (database.equals(Database.postgresql)) {
+                // See prepareExecution() and afterExecution()
             }
         } catch (Exception ex) {
             throw new RuntimeException(ex);
@@ -203,6 +207,44 @@
         return url.toString();
     }
 
+    /**
+     * Callback for each operation, useful if extra preparation of data/tables is necessary.
+     *
+     * @param con A DBUnit connection wrapper
+     * @param operation The operation to be executed, call <tt>getDataSet()</tt> to access the data.
+     */
+    protected void prepareExecution(IDatabaseConnection con, DataSetOperation operation) {
+        try {
+            // PostgreSQL has no global switch to turn off foreign key checks, needs to be toggled on each table
+            if (database.equals(Database.postgresql)) {
+                for (String tableName : operation.getDataSet().getTableNames()) {
+                    con.getConnection().prepareStatement("alter table " + tableName + " disable trigger all").execute();
+                }
+            }
+        } catch (Exception ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
+    /**
+     * Callback for each operation, useful if extra preparation of data/tables is necessary.
+     *
+     * @param con A DBUnit connection wrapper
+     * @param operation The operation that was executed, call <tt>getDataSet()</tt> to access the data.
+     */
+    protected void afterExecution(IDatabaseConnection con, DataSetOperation operation) {
+        try {
+            // PostgreSQL has no global switch to turn off foreign key checks, needs to be toggled on each table
+            if (database.equals(Database.postgresql)) {
+                for (String tableName : operation.getDataSet().getTableNames()) {
+                    con.getConnection().prepareStatement("alter table " + tableName + " enable trigger all").execute();
+                }
+            }
+        } catch (Exception ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
     protected class DataSetOperation {
         String dataSetLocation;
         ReplacementDataSet dataSet;




More information about the seam-commits mailing list