Index: lams_tool_deploy/build.xml =================================================================== diff -u -r5a1e4424a7fee54a37abf1504b699077bd9f41fa -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/build.xml (.../build.xml) (revision 5a1e4424a7fee54a37abf1504b699077bd9f41fa) +++ lams_tool_deploy/build.xml (.../build.xml) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -87,7 +87,7 @@ - + @@ -96,6 +96,7 @@ + Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DBTask.java =================================================================== diff -u -ra174a94fcb9db22380a89926f460afbef0216c91 -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DBTask.java (.../DBTask.java) (revision a174a94fcb9db22380a89926f460afbef0216c91) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DBTask.java (.../DBTask.java) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -6,6 +6,14 @@ package org.lamsfoundation.lams.tool.deploy; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Driver; +import java.sql.SQLException; +import java.io.File; +import java.io.IOException; +import org.apache.commons.io.FileUtils; + /** * * @author chris @@ -14,14 +22,14 @@ { /** - * Holds value of property jdbcDriverClass. + * Holds value of property dbDriverClass. */ - private String jdbcDriverClass; + private String dbDriverClass; /** - * Holds value of property jdbcUrl. + * Holds value of property dbDriverUrl. */ - private String jdbcUrl; + private String dbDriverUrl; /** * Holds value of property dbUsername. @@ -44,21 +52,23 @@ * Setter for property jdbcDriver. * @param jdbcDriver New value of property jdbcDriver. */ - public void setJdbcDriverClass(java.lang.String jdbcDriverClass) + public void setDbDriverClass(java.lang.String dbDriverClass) + { - this.jdbcDriverClass = jdbcDriverClass; + this.dbDriverClass = dbDriverClass; } /** * Setter for property jdbcUrl. * @param jdbcUrl New value of property jdbcUrl. */ - public void setJdbcUrl(String jdbcUrl) + public void setDbDriverUrl(java.lang.String dbDriverUrl) + { - this.jdbcUrl = jdbcUrl; + this.dbDriverUrl = dbDriverUrl; } /** @@ -81,4 +91,36 @@ this.dbPassword = dbPassword; } + /** + * Get a JDBC Connection from the set properties. + * @return Connection + * @throws DeployException if the connection cannot be established. + */ + protected Connection getConnection() throws DeployException + { + try + { + Class.forName(dbDriverClass); + return DriverManager.getConnection(dbDriverUrl, dbUsername, dbPassword); + } + catch (Exception ex) + { + throw new DeployException("Could not get connection", ex); + } + } + + protected String readFile(File file) throws DeployException + { + + try + { + return FileUtils.readFileToString(file, "UTF8"); + } + catch (IOException ioex) + { + throw new DeployException("Could not read file", ioex); + } + + } + } Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployConfig.java =================================================================== diff -u -r5a1e4424a7fee54a37abf1504b699077bd9f41fa -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployConfig.java (.../DeployConfig.java) (revision 5a1e4424a7fee54a37abf1504b699077bd9f41fa) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployConfig.java (.../DeployConfig.java) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -17,14 +17,14 @@ */ public class DeployConfig { - //public static final String PROPERTIES_FILE_NAME = "/home/chris/workspace/lams_tool_deploy/test/file/deploy.properties"; + //public static final String PROPERTIES_FILE_PATH = "/home/chris/workspace/lams_tool_deploy/test/file/deploy.properties"; public static final String TOOL_WEB_URI_KEY = "toolWebUri"; public static final String TOOL_CONTEXT_KEY = "toolContext"; public static final String LAMS_EAR_PATH_KEY = "lamsEarPath"; - public static final String TOOL_INSERT_SCRIPT_NAME_KEY = "toolInsertScriptName"; - public static final String TOOL_LIBRARY_INSERT_SCRIPT_NAME_KEY = "toolLibraryInsertScriptName"; - public static final String TOOL_ACTIVITY_INSERT_SCRIPT_NAME_KEY = "toolActivityInsertScriptName"; - public static final String TOOL_TABLES_SCRIPT_NAME_KEY = "toolTablesScriptName"; + public static final String TOOL_INSERT_SCRIPT_PATH_KEY = "toolInsertScriptPath"; + public static final String TOOL_LIBRARY_INSERT_SCRIPT_PATH_KEY = "toolLibraryInsertScriptPath"; + public static final String TOOL_ACTIVITY_INSERT_SCRIPT_PATH_KEY = "toolActivityInsertScriptPath"; + public static final String TOOL_TABLES_SCRIPT_PATH_KEY = "toolTablesScriptPath"; public static final String DB_USERNAME_KEY = "dbUsername"; public static final String DB_PASSWORD_KEY = "dbPassword"; public static final String DB_DRIVER_CLASS_KEY = "dbDriverClass"; @@ -48,24 +48,24 @@ private String lamsEarPath; /** - * Holds value of property toolInsertScriptName. + * Holds value of property toolInsertScriptPath. */ - private String toolInsertScriptName; + private String toolInsertScriptPath; /** - * Holds value of property toolLibraryInsertScriptName. + * Holds value of property toolLibraryInsertScriptPath. */ - private String toolLibraryInsertScriptName; + private String toolLibraryInsertScriptPath; /** - * Holds value of property toolActivityInsertScriptName. + * Holds value of property toolActivityInsertScriptPath. */ - private String toolActivityInsertScriptName; + private String toolActivityInsertScriptPath; /** - * Holds value of property toolTablesScriptName. + * Holds value of property toolTablesScriptPath. */ - private String toolTablesScriptName; + private String toolTablesScriptPath; /** * Holds value of property deployFiles. @@ -101,10 +101,10 @@ toolWebUri = getProperty(TOOL_WEB_URI_KEY); toolContextRoot = getProperty(TOOL_CONTEXT_KEY); lamsEarPath = getProperty(LAMS_EAR_PATH_KEY); - toolInsertScriptName = getProperty(TOOL_INSERT_SCRIPT_NAME_KEY); - toolLibraryInsertScriptName = getProperty(TOOL_LIBRARY_INSERT_SCRIPT_NAME_KEY); - toolActivityInsertScriptName = getProperty(TOOL_ACTIVITY_INSERT_SCRIPT_NAME_KEY); - toolTablesScriptName = getProperty(TOOL_TABLES_SCRIPT_NAME_KEY); + toolInsertScriptPath = getProperty(TOOL_INSERT_SCRIPT_PATH_KEY); + toolLibraryInsertScriptPath = getProperty(TOOL_LIBRARY_INSERT_SCRIPT_PATH_KEY); + toolActivityInsertScriptPath = getProperty(TOOL_ACTIVITY_INSERT_SCRIPT_PATH_KEY); + toolTablesScriptPath = getProperty(TOOL_TABLES_SCRIPT_PATH_KEY); dbUsername = getProperty(DB_USERNAME_KEY); dbPassword = getProperty(DB_PASSWORD_KEY); dbDriverClass = getProperty(DB_DRIVER_CLASS_KEY); @@ -119,8 +119,8 @@ } /** - * Getter for property toolWebAppName. - * @return Value of property toolWebAppName. + * Getter for property toolWebAppPath. + * @return Value of property toolWebAppPath. */ public String getToolWebUri() @@ -154,46 +154,46 @@ * Getter for property toolIinsertScriptPath. * @return Value of property toolIinsertScriptPath. */ - public String getToolInsertScriptName() + public String getToolInsertScriptPath() { - return this.toolInsertScriptName; + return this.toolInsertScriptPath; } /** * Getter for property libraryInsertScriptPath. * @return Value of property libraryInsertScriptPath. */ - public String getToolLibraryInsertScriptName() + public String getToolLibraryInsertScriptPath() { - return this.toolLibraryInsertScriptName; + return this.toolLibraryInsertScriptPath; } /** * Getter for property toolActivityInsertScriptPath. * @return Value of property toolActivityInsertScriptPath. */ - public String getToolActivityInsertScriptName() + public String getToolActivityInsertScriptPath() { - return this.toolActivityInsertScriptName; + return this.toolActivityInsertScriptPath; } /** * Getter for property toolTablesScriptPath. * @return Value of property toolTablesScriptPath. */ - public String getToolTablesScriptName() + public String getToolTablesScriptPath() { - return this.toolTablesScriptName; + return this.toolTablesScriptPath; } /** Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployFilesTask.java =================================================================== diff -u -r5a1e4424a7fee54a37abf1504b699077bd9f41fa -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployFilesTask.java (.../DeployFilesTask.java) (revision 5a1e4424a7fee54a37abf1504b699077bd9f41fa) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployFilesTask.java (.../DeployFilesTask.java) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -14,45 +14,12 @@ * Copies the required file to the lams ear directory. * @author chris */ -public class DeployFilesTask implements Task +public class DeployFilesTask extends FilesTask { - /** - * Holds value of property lamsEarPath. - */ - private String lamsEarPath; + /** - * Holds value of property deployFiles. - */ - private List deployFiles; - - /** Creates a new instance of DeployWarTask */ - public DeployFilesTask() - { - } - - /** - * Setter for property lamsEarPath. - * @param lamsEarPath New value of property lamsEarPath. - */ - public void setLamsEarPath(String lamsEarPath) - { - - this.lamsEarPath = lamsEarPath; - } - - /** - * Setter for property deployFiles. - * @param deployFiles New value of property deployFiles. - */ - public void setDeployFiles(List deployFiles) - { - - this.deployFiles = deployFiles; - } - - /** *Executes the task */ public void execute() throws DeployException Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/FileTokenReplacer.java =================================================================== diff -u -ra174a94fcb9db22380a89926f460afbef0216c91 -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/FileTokenReplacer.java (.../FileTokenReplacer.java) (revision a174a94fcb9db22380a89926f460afbef0216c91) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/FileTokenReplacer.java (.../FileTokenReplacer.java) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -8,14 +8,13 @@ import java.io.File; import java.io.IOException; -import java.io.BufferedReader; -import java.io.FileReader; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.regex.PatternSyntaxException; import java.util.Map; import java.util.Set; import java.util.Iterator; +import org.apache.commons.io.FileUtils; /** * Parsers a file and does token replacement * @author chris @@ -109,36 +108,16 @@ protected String readFile() throws DeployException { - StringBuffer buf = new StringBuffer(); - BufferedReader reader = null; + try { - reader = new BufferedReader(new FileReader(file)); - String line = null; - while((line = reader.readLine()) != null) - { - buf.append(line); - } + return FileUtils.readFileToString(file, "UTF8"); } catch (IOException ioex) { throw new DeployException("Could not read file", ioex); } - finally - { - if (reader != null) - { - try - { - reader.close(); - } - catch (IOException ioex) - { - //not much we can do here - } - } - } - return buf.toString(); + } } Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ScriptRunner.java =================================================================== diff -u --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ScriptRunner.java (revision 0) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ScriptRunner.java (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -0,0 +1,99 @@ +/* + * ScriptRunner.java + * + * Created on 06 April 2005, 12:55 + */ + +package org.lamsfoundation.lams.tool.deploy; + + +import java.sql.SQLException; +import java.sql.Connection; +import java.sql.Statement; +import java.util.List; +import java.util.ArrayList; +import org.apache.commons.dbutils.DbUtils; +/** + * Used to Run a SQL script. + * Expects statements to be terminated by ; + * and comment to start with -- or #. + * Also note that there can be no SELECT statements in the script!. + * @author chris + */ +public class ScriptRunner +{ + public static final String STATEMENT_DELIMITER = ";"; + public static final String COMMENT_PATTERN = "[#|\\-\\-].*"; + public static final String LINE_BREAK_PATTERN = "\\r\\n|\\n|\\r|\\u0085|\\u2028|\\u2029"; + public static final String LARGE_WHITE_SPACE_PATTERN = "\\s{2,}"; + + protected Connection conn; + protected String sqlScript; + + /** Creates a new instance of ScriptRunner */ + public ScriptRunner(final String sqlScript, final Connection conn) + { + this.conn = conn; + this.sqlScript = sqlScript; + } + + /** + * Runs the script. + * Note that it will *WILL NOT* close the connection, commit or roll back. + * @throws DeployException if it fails. + */ + public void run() throws DeployException + { + String[] statements = parseScript(sqlScript); + executeStatements(statements, conn); + } + + /** + * Parses the script into statements + * suitable for using with JDBC Statement interface. + * @return String[] and array of the statements. + * @throws DeployException if the script file in not suitable. + */ + protected String[] parseScript(String script) throws DeployException + { + //remove comments (lines beginning with --) + script = script.replaceAll(COMMENT_PATTERN,""); + + //remove line breaks + script = script.replaceAll(LINE_BREAK_PATTERN, ""); + + //remove excess whitespace + script = script.replaceAll(LARGE_WHITE_SPACE_PATTERN, " "); + + //split on ; + return script.split(STATEMENT_DELIMITER); + + } + + /** + * Executes an array of statements againt a connection. + * Note that it will *WILL NOT* close the connection, commit or roll back. + */ + protected void executeStatements(String[] statements, Connection conn) throws DeployException + { + Statement stmt = null; + try + { + stmt = conn.createStatement(); + for (int i = 0, length = statements.length; i < length; i++) + { + stmt.addBatch(statements[i]); + } + stmt.executeBatch(); + } + catch (SQLException sqlex) + { + throw new DeployException("Could not execute statements", sqlex); + } + finally + { + DbUtils.closeQuietly(stmt); + } + } + +} Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java =================================================================== diff -u -r5a1e4424a7fee54a37abf1504b699077bd9f41fa -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java (.../ToolDBDeployTask.java) (revision 5a1e4424a7fee54a37abf1504b699077bd9f41fa) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java (.../ToolDBDeployTask.java) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -6,155 +6,331 @@ package org.lamsfoundation.lams.tool.deploy; +import java.util.Map; +import java.util.HashMap; import java.io.File; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.ResultSet; +import java.sql.PreparedStatement; +import org.apache.commons.dbutils.DbUtils; -//tool_id BIGINT(20) NOT NULL AUTO_INCREMENT -// , tool_signature VARCHAR(64) NOT NULL -// , service_name VARCHAR(255) NOT NULL -// , tool_display_name VARCHAR(255) NOT NULL -// , description TEXT -// , learning_library_id BIGINT(20) -// , default_tool_content_id BIGINT(20) NOT NULL -// , valid_flag TINYINT(1) NOT NULL DEFAULT 1 -// , grouping_support_type_id INT(3) NOT NULL -// , supports_define_later_flag TINYINT(1) NOT NULL DEFAULT 0 -// , supports_run_offline_flag TINYINT(1) NOT NULL -// , supports_moderation_flag TINYINT(1) NOT NULL -// , supports_contribute_flag TINYINT(1) NOT NULL -// , learner_url TEXT NOT NULL -// , author_url TEXT NOT NULL -// , define_later_url TEXT -// , export_portfolio_url TEXT NOT NULL -// , monitor_url TEXT NOT NULL -// , contribute_url TEXT -// , moderation_url TEXT -// , create_date_time DATETIME NOT NULL - /** - * + * Task creates all the necessary db items for the tool, but leaves them + * inactive (use ToolDBActivateTask to activate the tool). * @author chris */ public class ToolDBDeployTask extends DBTask { - + /** * Holds value of property toolInsertScript. */ private File toolInsertScript; - + /** * Holds value of property toolLibraryInsertScript. */ private File toolLibraryInsertScript; - + /** - * Holds value of property toolTablesCreatScript. + * Holds value of property toolTablesScript. */ - private File toolTablesCreatScript; - + private File toolTablesScript; + /** * Holds value of property toolId. */ private long toolId; - + /** * Holds value of property learningLibraryId. */ private long learningLibraryId; - + /** * Holds value of property toolActivityInsertScript. */ private File toolActivityInsertScript; - + private long defaultContentId; + /** Creates a new instance of ToolDBActivateTask */ public ToolDBDeployTask() { } - + /** * Setter for property toolInsertFile. * @param toolInsertFile New value of property toolInsertFile. */ public void setToolInsertScript(java.io.File toolInsertScript) { - + this.toolInsertScript = toolInsertScript; } - + /** * Setter for property toolLibraryInsertFile. * @param toolLibraryInsertFile New value of property toolLibraryInsertFile. */ public void setToolLibraryInsertScript(java.io.File toolLibraryInsertScript) { - + this.toolLibraryInsertScript = toolLibraryInsertScript; } - + /** * Setter for property toolTablesCreatScript. * @param toolTablesCreatScript New value of property toolTablesCreatScript. */ - public void setToolTablesCreatScript(File toolTablesCreatScript) + public void setToolTablesScript(java.io.File toolTablesScript) + + { - this.toolTablesCreatScript = toolTablesCreatScript; + this.toolTablesScript = toolTablesScript; } - + public void execute() throws DeployException { - //get a tool content id + //get a connection + Connection conn = getConnection(); - //put the tool content id into the tool insert file - //run tool insert and get tool id back - - //put the tool id into the tool library script - - //run tool library script and get the library id back - - //put the library id into the activity insert script - - //run the activity insert script - - //put the tool id and and defualt content id into - //the tool tables script - - //run the tool table script + try + { + conn.setAutoCommit(false); + //run tool insert and get tool id back + toolId = runToolInsertScript(readFile(toolInsertScript), conn); + + //get a default tool content id + defaultContentId = getNewToolContentId(toolId, conn); + + //add the default content id to the tool record + insertDefaultContentId(conn); + + //put the tool id into the tool library script + Map replacementMap = new HashMap(1); + replacementMap.put("tool_id", Long.toString(toolId)); + FileTokenReplacer libraryScriptReplacer = new FileTokenReplacer(toolLibraryInsertScript, replacementMap); + String libraryScriptSQL = libraryScriptReplacer.replace(); + + //run tool library script and get the library id back + learningLibraryId = runLibraryScript(libraryScriptSQL, conn); + + //put the library id into the activity insert script + replacementMap = new HashMap(1); + replacementMap.put("tool_id", Long.toString(toolId)); + replacementMap.put("learning_library_id", Long.toString(learningLibraryId)); + FileTokenReplacer activityScriptReplacer = new FileTokenReplacer(toolActivityInsertScript, replacementMap); + String activityScriptSQL = activityScriptReplacer.replace(); + + //run the activity insert script + runScript(activityScriptSQL, conn); + + //put the tool id and and defualt content id into + //the tool tables script + replacementMap = new HashMap(1); + replacementMap.put("tool_id", Long.toString(toolId)); + replacementMap.put("default_content_id", Long.toString(defaultContentId)); + FileTokenReplacer toolTablesScriptReplacer = new FileTokenReplacer(toolTablesScript, replacementMap); + String toolTablesScriptSQL = toolTablesScriptReplacer.replace(); + + //run the tool table script + runScript(toolTablesScriptSQL, conn); + + //commit transaction + conn.commit(); + } + catch (SQLException sqlex) + { + try + { + DbUtils.rollback(conn); + } + catch (SQLException sqlex2) + { + throw new DeployException("Attempted to rollback because of "+sqlex+" but failed - cleanup maybe required (see root cause)", sqlex2); + } + throw new DeployException("Execute failed", sqlex); + } + catch (DeployException dex) + { + try + { + DbUtils.rollback(conn); + } + catch (SQLException sqlex) + { + throw new DeployException("Attempted to rollback because of "+dex+" but failed - cleanup maybe required (see root cause)", sqlex); + } + throw dex; + } + finally + { + DbUtils.closeQuietly(conn); + } } - + /** * Getter for property toolId. * @return Value of property toolId. */ public long getToolId() { - + return this.toolId; } - + /** * Getter for property learningLibraryId. * @return Value of property learningLibraryId. */ public long getLearningLibraryId() { - + return this.learningLibraryId; } - + /** * Setter for property toolActivityInsertScript. * @param toolActivityInsertScript New value of property toolActivityInsertScript. */ public void setToolActivityInsertScript(File toolActivityInsertScript) { - + this.toolActivityInsertScript = toolActivityInsertScript; } + private long getNewToolContentId(long toolId, Connection conn) throws DeployException + { + PreparedStatement stmt = null; + ResultSet results = null; + try + { + stmt = conn.prepareStatement("INSERT INTO lams_tool_content (tool_id) VALUES (?)"); + stmt.setLong(1, toolId); + stmt.execute(); + stmt = conn.prepareStatement("SELECT LAST_INSERT_ID() FROM lams_tool_content"); + results = stmt.executeQuery(); + if (results.next()) + { + return results.getLong("LAST_INSERT_ID()"); + } + else + { + throw new DeployException("No tool content id found"); + } + + } + catch (SQLException sqlex) + { + throw new DeployException("Could not get new tool content id", sqlex); + } + finally + { + DbUtils.closeQuietly(stmt); + DbUtils.closeQuietly(results); + } + } + + private void insertDefaultContentId(Connection conn) throws DeployException + { + PreparedStatement stmt = null; + try + { + stmt = conn.prepareStatement("UPDATE lams_tool SET default_content_id = ? WHERE tool_id = ?"); + stmt.setLong(1, defaultContentId); + stmt.setLong(2, toolId); + stmt.execute(); + + } + catch (SQLException sqlex) + { + throw new DeployException("Could not insert defualt content id", sqlex); + } + finally + { + DbUtils.closeQuietly(stmt); + } + } + + private void runScript(final String scriptSQL, final Connection conn) throws DeployException + { + ScriptRunner runner = new ScriptRunner(scriptSQL, conn); + runner.run(); + } + + private long runLibraryScript (final String scriptSQL, final Connection conn) throws DeployException + { + runScript(scriptSQL, conn); + PreparedStatement stmt = null; + ResultSet results = null; + try + { + stmt = conn.prepareStatement("SELECT LAST_INSERT_ID() FROM lams_learning_library"); + results = stmt.executeQuery(); + if (results.next()) + { + return results.getLong("LAST_INSERT_ID()"); + } + else + { + throw new DeployException("Could not get learning_library_id"); + } + } + catch (SQLException sqlex) + { + throw new DeployException("Failed to run learning library script",sqlex); + } + finally + { + DbUtils.closeQuietly(stmt); + DbUtils.closeQuietly(results); + } + + } + + private long runToolInsertScript (final String scriptSQL, final Connection conn) throws DeployException + { + runScript(scriptSQL, conn); + PreparedStatement stmt = null; + ResultSet results = null; + try + { + stmt = conn.prepareStatement("SELECT LAST_INSERT_ID() FROM lams_tool"); + results = stmt.executeQuery(); + if (results.next()) + { + return results.getLong("LAST_INSERT_ID()"); + } + else + { + throw new DeployException("Could not get learning_library_id"); + } + } + catch (SQLException sqlex) + { + throw new DeployException("Failed to run tool insert script",sqlex); + } + finally + { + DbUtils.closeQuietly(stmt); + DbUtils.closeQuietly(results); + } + + } + + /** + * Getter for property defaultContentId. + * @return Value of property defaultContentId. + */ + public long getDefaultContentId() + { + + return this.defaultContentId; + } } Index: lams_tool_deploy/test/file/deploy.properties =================================================================== diff -u -r5a1e4424a7fee54a37abf1504b699077bd9f41fa -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/test/file/deploy.properties (.../deploy.properties) (revision 5a1e4424a7fee54a37abf1504b699077bd9f41fa) +++ lams_tool_deploy/test/file/deploy.properties (.../deploy.properties) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -18,16 +18,16 @@ deployFiles=lams_tool_imscp.war,lams_tool_imscp.jar #nameof SQL script file to create and populate the tool tables -toolTablesScriptName=create_imscp_db.sql +toolTablesScriptPath=create_imscp_db.sql -#Name of SQL script file to create the tool library activity -toolActivityInsertScriptName=insert_imscp_activity.sql +#Path of SQL script file to create the tool library activity +toolActivityInsertScriptPath=insert_imscp_activity.sql -#Name of SQL script to insert the library record -toolLibraryInsertScriptName=insert_imscp_library.sql +#Path of SQL script to insert the library record +toolLibraryInsertScriptPath=insert_imscp_library.sql #name of SQL script that inserts the lams_tool record -toolInsertScriptName=insert_imscp_tool.sql +toolInsertScriptPath=insert_imscp_tool.sql #path to lams ear dir lamsEarPath=/var/jboss/server/default/deploy/lams.ear Index: lams_tool_deploy/test/file/sql/test-script.sql =================================================================== diff -u --- lams_tool_deploy/test/file/sql/test-script.sql (revision 0) +++ lams_tool_deploy/test/file/sql/test-script.sql (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -0,0 +1,30 @@ +# Connection: ROOT LOCAL +# Host: localhost +# Saved: 2005-04-06 15:44:33 +# +# Connection: ROOT LOCAL +# Host: localhost +# Saved: 2005-04-06 15:15:40 +# +-- SCRIPT RUNNER TESTING SCRIPT +-- IF YOU ALTER THIS YOU WILL BREAK THE TEST! +CREATE TABLE TEST_1 ( +TEST_1_INT INT, +TEST_1_STR VARCHAR(255), #asdadasdsd +TEST_1_DATE DATETIME +) TYPE=InnoDB; + +-- more comments +INSERT INTO TEST_1 VALUES (1, 'foo', NOW()); + +-- comments +-- comments +ALTER TABLE TEST_1 -- asdasdasd +ADD INDEX IDX_TEST_1 (TEST_1_INT); + +-- -- double comments +UPDATE TEST_1 SET TEST_1_STR = 'bar' WHERE TEST_1_INT = 1; + + +-- more comments! +DROP TABLE TEST_1; \ No newline at end of file Index: lams_tool_deploy/test/file/test.properties =================================================================== diff -u --- lams_tool_deploy/test/file/test.properties (revision 0) +++ lams_tool_deploy/test/file/test.properties (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -0,0 +1,13 @@ +# Properties file for junbit test classes + +#Password for Install DB +dbPassword=dag.Quiz + +#user for install db +dbUsername=root + +#Driver Connection URL +dbDriverUrl=jdbc:mysql://localhost:3306/scratch + +#Classname of JDBC driver +dbDriverClass=com.mysql.jdbc.Driver \ No newline at end of file Index: lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/DBConnector.java =================================================================== diff -u --- lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/DBConnector.java (revision 0) +++ lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/DBConnector.java (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -0,0 +1,149 @@ +/* + * DBConnector.java + * + * Created on 06 April 2005, 13:39 + */ + +package org.lamsfoundation.lams.tool.deploy; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +/** + * Utility class for getting a DB connection. + * INTENDED ONLY TO BE USED WITH JUNIT!. + * @author chris + */ +public class DBConnector +{ + + public static final String DB_USERNAME_KEY = "dbUsername"; + public static final String DB_PASSWORD_KEY = "dbPassword"; + public static final String DB_DRIVER_CLASS_KEY = "dbDriverClass"; + public static final String DB_DRIVER_URL_KEY = "dbDriverUrl"; + /** + * Holds value of property dbUsername. + */ + private String dbUsername; + + /** + * Holds value of property dbPassword. + */ + private String dbPassword; + + /** + * Holds value of property dbDriverClass. + */ + private String dbDriverClass; + + /** + * Holds value of property dbDriverUrl. + */ + private String dbDriverUrl; + + /** Creates a new instance of DBConnector */ + public DBConnector(String propsFilePath) throws ConfigurationException + { + PropertiesConfiguration config = new PropertiesConfiguration(propsFilePath); + setDbUsername(config.getString(DB_USERNAME_KEY)); + setDbPassword(config.getString(DB_PASSWORD_KEY)); + setDbDriverUrl(config.getString(DB_DRIVER_URL_KEY)); + setDbDriverClass(config.getString(DB_DRIVER_CLASS_KEY)); + } + + /** Creates a new instance of DBConnector */ + public DBConnector() + { + + } + + /** + * Getter for property dbUsername. + * @return Value of property dbUsername. + */ + public String getDbUsername() + { + + return this.dbUsername; + } + + /** + * Setter for property dbUsername. + * @param dbUsername New value of property dbUsername. + */ + public void setDbUsername(String dbUsername) + { + + this.dbUsername = dbUsername; + } + + /** + * Getter for property dbPassword. + * @return Value of property dbPassword. + */ + public String getDbPassword() + { + + return this.dbPassword; + } + + /** + * Setter for property dbPassword. + * @param dbPassword New value of property dbPassword. + */ + public void setDbPassword(String dbPassword) + { + + this.dbPassword = dbPassword; + } + + /** + * Getter for property dbDriverClass. + * @return Value of property dbDriverClass. + */ + public String getDbDriverClass() + { + + return this.dbDriverClass; + } + + /** + * Setter for property dbDriverClass. + * @param dbDriverClass New value of property dbDriverClass. + */ + public void setDbDriverClass(String dbDriverClass) + { + + this.dbDriverClass = dbDriverClass; + } + + /** + * Getter for property dbDriverUrl. + * @return Value of property dbDriverUrl. + */ + public String getDbDriverUrl() + { + + return this.dbDriverUrl; + } + + /** + * Setter for property dbDriverUrl. + * @param dbDriverUrl New value of property dbDriverUrl. + */ + public void setDbDriverUrl(String dbDriverUrl) + { + + this.dbDriverUrl = dbDriverUrl; + } + + + public Connection connect() throws SQLException, ClassNotFoundException + { + Class.forName(dbDriverClass); + return DriverManager.getConnection(dbDriverUrl, dbUsername, dbPassword); + } + +} Index: lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/DeployConfigTest.java =================================================================== diff -u -r5a1e4424a7fee54a37abf1504b699077bd9f41fa -r3e1c3cc5291e781560fe935140c6c985229df7e4 --- lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/DeployConfigTest.java (.../DeployConfigTest.java) (revision 5a1e4424a7fee54a37abf1504b699077bd9f41fa) +++ lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/DeployConfigTest.java (.../DeployConfigTest.java) (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -21,9 +21,9 @@ { private DeployConfig config = null; - public DeployConfigTest(String testName) + public DeployConfigTest(String testPath) { - super(testName); + super(testPath); } protected void setUp() throws java.lang.Exception @@ -78,30 +78,30 @@ } //#nameof SQL script file to create and populate the tool tables - //toolTablesScriptName=create_imscp_db.sql + //toolTablesScriptPath=create_imscp_db.sql - public void testGetToolTablesScriptName() + public void testGetToolTablesScriptPath() { - assertEquals(config.getToolTablesScriptName(), "create_imscp_db.sql"); + assertEquals(config.getToolTablesScriptPath(), "create_imscp_db.sql"); } - //#Name of SQL script file to create the tool library activity - //toolActivityInsertScriptName=insert_imscp_activity.sql - public void testGetToolActivityInsertScriptName() + //#Path of SQL script file to create the tool library activity + //toolActivityInsertScriptPath=insert_imscp_activity.sql + public void testGetToolActivityInsertScriptPath() { - assertEquals(config.getToolActivityInsertScriptName(), "insert_imscp_activity.sql"); + assertEquals(config.getToolActivityInsertScriptPath(), "insert_imscp_activity.sql"); } - //#Name of SQL script to insert the library record - //toolLibraryInsertScriptName=insert_imscp_library.sql - public void testGetToolLibraryInsertScriptName() + //#Path of SQL script to insert the library record + //toolLibraryInsertScriptPath=insert_imscp_library.sql + public void testGetToolLibraryInsertScriptPath() { - assertEquals(config.getToolLibraryInsertScriptName(), "insert_imscp_library.sql"); + assertEquals(config.getToolLibraryInsertScriptPath(), "insert_imscp_library.sql"); } //#name of SQL script that inserts the lams_tool record - //toolInsertScriptName=insert_imscp_tool.sql - public void testGetToolInsertScriptName() + //toolInsertScriptPath=insert_imscp_tool.sql + public void testGetToolInsertScriptPath() { - assertEquals(config.getToolInsertScriptName(), "insert_imscp_tool.sql"); + assertEquals(config.getToolInsertScriptPath(), "insert_imscp_tool.sql"); } //#path to lams ear dir //lamsEarPath=/var/jboss/server/default/deploy/lams.ear Index: lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/ScriptRunnerTest.java =================================================================== diff -u --- lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/ScriptRunnerTest.java (revision 0) +++ lams_tool_deploy/test/java/org/lamsfoundation/lams/tool/deploy/ScriptRunnerTest.java (revision 3e1c3cc5291e781560fe935140c6c985229df7e4) @@ -0,0 +1,104 @@ +/* + * ScriptRunnerTest.java + * JUnit based test + * + * Created on 06 April 2005, 13:36 + */ + +package org.lamsfoundation.lams.tool.deploy; + +import junit.framework.*; +import java.sql.SQLException; +import java.sql.Connection; +import java.sql.Statement; +import java.util.List; +import java.util.ArrayList; +import org.apache.commons.dbutils.DbUtils; +import java.io.File; +import org.apache.commons.io.FileUtils; +/** + * + * @author chris + */ +public class ScriptRunnerTest extends TestCase +{ + private Connection conn = null; + private String testScript = null; + private ScriptRunner runner = null; + private String[] testStatements = + {"CREATE TABLE TEST_3 (TEST_3_INT INT, TEST_3_STR VARCHAR(255), TEST_3_DATE DATETIME) TYPE=InnoDB", + "INSERT INTO TEST_3 VALUES(1, 'foo', NOW())", + "ALTER TABLE TEST_3 ADD INDEX IDX_TEST_3 (TEST_3_INT)", + "UPDATE TEST_3 SET TEST_3_STR = 'bar' WHERE TEST_3_INT = 1", + "DROP TABLE TEST_3"}; + + public ScriptRunnerTest(String testName) + { + super(testName); + } + + protected void setUp() throws java.lang.Exception + { + try + { + testScript = loadTestScript("test/file/sql/test-script.sql"); + DBConnector connector = new DBConnector("test/file/test.properties"); + conn = connector.connect(); + conn.setAutoCommit(false); + runner = new ScriptRunner(testScript, conn); + } + catch (Exception ex) + { + DbUtils.closeQuietly(conn); + System.out.println("SET UP FAILED"); + ex.printStackTrace(); + throw ex; + } + } + + protected void tearDown() throws java.lang.Exception + { + conn.commit(); + DbUtils.closeQuietly(conn); + } + + public static junit.framework.Test suite() + { + junit.framework.TestSuite suite = new junit.framework.TestSuite(ScriptRunnerTest.class); + + return suite; + } + + public void testExecuteStatements() throws Exception + { + + runner.executeStatements(testStatements, conn); + + } + + public void testParseScript() throws Exception + { + String[] statements = runner.parseScript(testScript); + System.out.println("PARSED SCRIPT"); + for (int i = 0, length = statements.length; i < length; i++) + { + System.out.println(statements[i]); + } + assertNotNull(statements); + assertEquals(statements.length, 5); + + } + + public void testRun() throws Exception + { + runner.run(); + } + + + private String loadTestScript(String scriptPath) throws Exception + { + File file = new File(scriptPath); + return FileUtils.readFileToString(file, "UTF8"); + } + +}