diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 80:8447467cef86

Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT). artifacts/trunk@799 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 19 Mar 2010 09:34:40 +0000
parents f69e5b87f05f
children 72e2dd4feb31
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Tue Mar 16 16:03:06 2010 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Fri Mar 19 09:34:40 2010 +0000
@@ -37,6 +37,12 @@
     public static final String SQL_LOAD_BY_GID =
         SQL.get("artifacts.select.gid");
 
+    public static final String SQL_GET_ID =
+        SQL.get("artifacts.get.id");
+
+    public static final String SQL_REPLACE =
+        SQL.get("artifacts.replace");
+
     protected DatabaseCleaner cleaner;
 
     protected FactoryLookup   factoryLookup;
@@ -119,13 +125,25 @@
             insertDatabase(artifact, factory, ttl));
     }
 
+    public PersistentArtifact storeOrReplace(
+        Artifact        artifact,
+        ArtifactFactory factory,
+        Long            ttl
+    )
+    throws Exception
+    {
+        return new PersistentArtifact(
+            artifact,
+            factory.getSerializer(),
+            storeOrReplaceDatabase(artifact, factory, ttl));
+    }
+
     public interface ArtifactLoader {
 
         Object load(ArtifactFactory factory, byte [] bytes, int id);
 
     } // interface ArtifactLoader
 
-
     public PersistentArtifact getArtifact(String identifer) {
 
         return (PersistentArtifact)loadArtifact(
@@ -148,8 +166,6 @@
             });
     }
 
-
-
     public Object loadArtifact(String identifer, ArtifactLoader loader) {
 
         if (!StringUtils.checkUUID(identifer)) {
@@ -249,6 +265,120 @@
         return serializer.fromBytes(bytes);
     }
 
+    protected int storeOrReplaceDatabase(
+        Artifact        artifact,
+        ArtifactFactory factory,
+        Long            ttl
+    ) {
+        String uuid = artifact.identifier();
+
+        if (!StringUtils.checkUUID(uuid)) {
+            throw new RuntimeException("No valid UUID");
+        }
+
+        Connection        connection = null;
+        PreparedStatement stmnt      = null;
+        ResultSet         result     = null;
+
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            connection = dataSource.getConnection();
+            try {
+                connection.setAutoCommit(false);
+
+                stmnt = connection.prepareStatement(SQL_GET_ID);
+
+                stmnt.setString(1, uuid);
+                result = stmnt.executeQuery();
+
+                Integer ID = result.next()
+                    ? Integer.valueOf(result.getInt(1))
+                    : null;
+
+                result.close(); result = null;
+                stmnt.close();  stmnt  = null;
+
+                if (ID != null) { // already in database
+                    int id = ID.intValue();
+
+                    stmnt = connection.prepareStatement(SQL_REPLACE);
+
+                    if (ttl == null) {
+                        stmnt.setNull(1, Types.BIGINT);
+                    }
+                    else {
+                        stmnt.setLong(1, ttl.longValue());
+                    }
+
+                    stmnt.setString(2, factory.getName());
+                    stmnt.setBytes(
+                        3,
+                        factory.getSerializer().toBytes(artifact));
+                    stmnt.setInt(4, id);
+
+                    stmnt.execute();
+                    connection.commit();
+                    return id;
+                }
+
+                stmnt = connection.prepareStatement(SQL_NEXT_ID);
+                result = stmnt.executeQuery();
+
+                if (!result.next()) {
+                    throw new RuntimeException("No id generated");
+                }
+
+                int id = result.getInt(1);
+
+                result.close(); result = null;
+                stmnt.close();  stmnt  = null;
+
+                stmnt = connection.prepareStatement(SQL_INSERT);
+
+                stmnt.setInt(1, id);
+                stmnt.setString(2, uuid);
+                if (ttl == null) {
+                    stmnt.setNull(3, Types.BIGINT);
+                }
+                else {
+                    stmnt.setLong(3, ttl.longValue());
+                }
+
+                stmnt.setString(4, factory.getName());
+
+                stmnt.setBytes(
+                    5,
+                    factory.getSerializer().toBytes(artifact));
+
+                stmnt.execute();
+                connection.commit();
+                return id;
+            }
+            catch (SQLException sqle) {
+                connection.rollback();
+                throw sqle;
+            }
+        }
+        catch (SQLException sqle) {
+            logger.error(sqle.getLocalizedMessage(), sqle);
+        }
+        finally {
+            if (result != null) {
+                try { result.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (stmnt != null) {
+                try { stmnt.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (connection != null) {
+                try { connection.close(); }
+                catch (SQLException sqle) {}
+            }
+        }
+        throw new RuntimeException("failed insert artifact into database");
+    }
+
     protected int insertDatabase(
         Artifact        artifact,
         ArtifactFactory factory,

http://dive4elements.wald.intevation.org