View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   // all rights reserved
4   /*
5    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
6    *
7    * By using this Software, You acknowledge that the Software is a valuable asset
8    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9    * third party supplier of IDSI and constitutes confidential and proprietary
10   * information.
11   *
12   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
21  */
22  package net.sourceforge.addam.ddlrun.custom;
23  
24  
25  import java.sql.Connection;
26  import java.sql.PreparedStatement;
27  import java.sql.SQLException;
28  import java.sql.Timestamp;
29  
30  import net.sourceforge.addam.ddlrun.utils.RunLogger;
31  
32  /**
33   * Logs events into a script table and updates the start point based on the group resource in another table.
34   * The statements must include
35   *
36   * @author TIM3
37   * @since Mar 6, 2005
38   */
39  public class IDSRunLogger implements RunLogger {
40  
41      public static final String INSERT_SCRIPT_LOG = "INSERT INTO SCRIPT_LOG(SCL_SCRIPT,SCL_RUN_DATE) VALUES(?,?)";
42      public static final String UPDATE_VERSION = "UPDATE VERSION SET VRS_VERSION=?, VRS_START_FOLDER=?, VRS_MODIFIED_DATE=? WHERE VRS_TYPE='PF'";
43      public static final String UNDEFINE_VERSION = "UPDATE VERSION SET VRS_VERSION='undefined', VRS_MODIFIED_DATE=? WHERE VRS_TYPE='PF'";
44      private PreparedStatement undefineVersionStatement;
45      private PreparedStatement insertScriptLogStatement;
46      private PreparedStatement updateVersionStatement;
47      private final Connection connection;
48      private final String dbversion;
49  
50      /**
51       * constructs a IDSRunLogger
52       */
53      public IDSRunLogger(Connection connection, String version) {
54          this.connection = connection;
55          this.dbversion = (version != null) ? version : "not provided";
56      }
57  
58      public void logRunScript(String script, long time) throws SQLException {
59          if (insertScriptLogStatement == null) {
60              insertScriptLogStatement = connection.prepareStatement(INSERT_SCRIPT_LOG);
61          }
62          insertScriptLogStatement.setString(1, script);
63          insertScriptLogStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
64          insertScriptLogStatement.execute();
65          if (undefineVersionStatement == null) {
66              // only mark the version undefined the first time you record a script run
67              // it will later be marked with the correct version when we're done
68              undefineVersionStatement = connection.prepareStatement(UNDEFINE_VERSION);
69              undefineVersionStatement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
70              undefineVersionStatement.execute();
71          }
72          this.connection.commit();
73      }
74  
75      public void logRunComplete(String group, long time) throws Exception {
76          if (updateVersionStatement == null) {
77              updateVersionStatement = connection.prepareStatement(UPDATE_VERSION);
78          }
79          updateVersionStatement.setString(1, dbversion);
80          updateVersionStatement.setString(2, group);
81          updateVersionStatement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
82          updateVersionStatement.execute();
83          this.connection.commit();
84      }
85  
86      public void logRunFailure(String group, String script, Exception e) {
87          System.out.println("Execution failed for " + group + "/" + script);
88      }
89  }