ref #6589 finalize update script for
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / SchemaUpdaterStepBase.java
index 8fa250e0822fdfdcfc5435afb1ec9ab8acb652e9..0194cbbc351e5e696e3e668f6161e067fc623772 100644 (file)
@@ -101,24 +101,50 @@ public abstract class SchemaUpdaterStepBase implements ISchemaUpdaterStep {
        }\r
 \r
     /**\r
-     * Returns the smallest next free id, if includeAudit is <code>true</code> the audit table is also considered in computation\r
+     * Returns the smallest next free id, if includeAudit is <code>true</code> the audit\r
+     * table is also considered in computation\r
      * @throws NumberFormatException\r
      * @throws SQLException\r
      */\r
     protected int getMaxId1(ICdmDataSource datasource, String tableName, boolean includeAudit, IProgressMonitor monitor, CaseType caseType,\r
             SchemaUpdateResult result) throws SQLException {\r
 \r
-        String sql = "SELECT max(id) FROM " +caseType.transformTo(tableName);\r
+        return getMaxIdentifier(datasource, tableName, "id", includeAudit, monitor, caseType, result);\r
+    }\r
+\r
+    protected int getMaxIdentifier(ICdmDataSource datasource, String tableName, String idAttrName, boolean includeAudit, IProgressMonitor monitor, CaseType caseType,\r
+            SchemaUpdateResult result) throws SQLException {\r
+\r
+        String sql = "SELECT max("+idAttrName+") FROM " +caseType.transformTo(tableName);\r
         Integer maxId = getInteger(datasource, sql, 0);\r
 \r
         Integer maxIdAud = -1;\r
         if(includeAudit){\r
-            sql = "SELECT max(id) FROM " +caseType.transformTo(tableName + "_AUD");\r
+            sql = "SELECT max("+idAttrName+") FROM " +caseType.transformTo(tableName + "_AUD");\r
             maxIdAud = getInteger(datasource, sql, 0);\r
         }\r
         return Math.max(maxId, maxIdAud) + 1;\r
     }\r
 \r
+\r
+    /**\r
+     * Creates a new entry in the AuditEvent table\r
+     * @return the revision number of the the new audit event\r
+     */\r
+    protected long createAuditEvent(ICdmDataSource datasource, CaseType caseType, IProgressMonitor monitor,\r
+            SchemaUpdateResult result) throws SQLException {\r
+        String sql;\r
+        long rev;\r
+        sql = "INSERT INTO @@AuditEvent@@ (revisionnumber, date, timestamp, uuid) "\r
+                + " VALUES (%d, '%s', %d, '%s') ";\r
+        int newId = this.getMaxIdentifier(datasource, "AuditEvent", "revisionNumber", false, monitor, caseType, result);\r
+        long timeStamp = System.currentTimeMillis();\r
+        sql = caseType.replaceTableNames(String.format(sql, newId, this.getNowString(), timeStamp, UUID.randomUUID()));\r
+        datasource.executeUpdate(sql);\r
+        rev =  newId;\r
+        return rev;\r
+    }\r
+\r
     private Integer getInteger(ICdmDataSource datasource, String sql, int nullReplace) throws SQLException {\r
         Object value = datasource.getSingleValue(sql);\r
         if (value == null){\r