Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBActivateTask.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBActivateTask.java,v diff -u -r1.2 -r1.3 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBActivateTask.java 5 Apr 2005 05:43:08 -0000 1.2 +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBActivateTask.java 6 Apr 2005 07:13:16 -0000 1.3 @@ -6,23 +6,23 @@ package org.lamsfoundation.lams.tool.deploy; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.PreparedStatement; +import org.apache.commons.dbutils.DbUtils; -import org.apache.tools.ant.taskdefs.JDBCTask; -import org.apache.tools.ant.BuildException; -import java.io.File; -import java.io.IOException; /** * Ant task runs the creates & inserts for the LAMS aand Tool DB tables * @author chris * - * + * */ public class ToolDBActivateTask extends DBTask { private long defaultContentId; private long toolId; - + /** * Holds value of property learningLibraryId. */ @@ -33,44 +33,112 @@ { } - public void execute() throws BuildException + public void execute() throws DeployException { - //TODO - //run tool create db script - - //-->transaction - //insert into tool content table - //get tool content id (for use as default content id - //insert into tool table - //get tool id - //insert into library and activity table - - //update tool insert script with tool id and default content id - - //execute tool insert script - //<--end transaction + Connection conn = getConnection(); + try + { + activateTool(toolId, conn); + activateLibrary(learningLibraryId, conn); + activateActivity(learningLibraryId, conn); + conn.commit(); + } + catch (SQLException sqlex) + { + try + { + DbUtils.rollback(conn); + } + catch (SQLException sqlex2) + { + throw new DeployException("Attempted to rollback db activate 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 db activate because of "+dex+" but failed - cleanup maybe required (see root cause)", sqlex); + } + throw dex; + } + finally + { + DbUtils.closeQuietly(conn); + } } - + /** * Setter for property toolId. * @param toolId New value of property toolId. */ public void setToolId(long toolId) { - + this.toolId = toolId; } - + /** * Setter for property learningLibraryId. * @param learningLibraryId New value of property learningLibraryId. */ public void setLearningLibraryId(long learningLibraryId) { - + this.learningLibraryId = learningLibraryId; } + private void activateTool(long toolId, Connection conn) throws SQLException + { + PreparedStatement stmt = null; + try + { + stmt = conn.prepareStatement("UPDATE lams_tool SET valid_flag = 1 WHERE tool_id = ?"); + stmt.setLong(1, toolId); + stmt.execute(); + } + finally + { + DbUtils.closeQuietly(stmt); + } + + } + private void activateLibrary(long libraryId, Connection conn) throws SQLException + { + PreparedStatement stmt = null; + try + { + stmt = conn.prepareStatement("UPDATE lams_learning_library SET valid_flag = 1 WHERE learning_library_id = ?"); + stmt.setLong(1, libraryId); + stmt.execute(); + } + finally + { + DbUtils.closeQuietly(stmt); + } + + } + private void activateActivity(long libraryId, Connection conn) throws SQLException + { + PreparedStatement stmt = null; + try + { + stmt = conn.prepareStatement("UPDATE lams_learning_activity SET valid_flag = 1 WHERE learning_library_id = ?"); + stmt.setLong(1, libraryId); + stmt.execute(); + } + finally + { + DbUtils.closeQuietly(stmt); + } + + } + } Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java,v diff -u -r1.3 -r1.4 --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java 6 Apr 2005 06:23:05 -0000 1.3 +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/ToolDBDeployTask.java 6 Apr 2005 07:13:16 -0000 1.4 @@ -110,7 +110,7 @@ defaultContentId = getNewToolContentId(toolId, conn); //add the default content id to the tool record - insertDefaultContentId(conn); + updateToolDefaultContentId(toolId, defaultContentId, conn); //put the tool id into the tool library script Map replacementMap = new HashMap(1); @@ -121,6 +121,9 @@ //run tool library script and get the library id back learningLibraryId = runLibraryScript(libraryScriptSQL, conn); + //update tool record to include library id + updateToolLibraryId(toolId, learningLibraryId, conn); + //put the library id into the activity insert script replacementMap = new HashMap(1); replacementMap.put("tool_id", Long.toString(toolId)); @@ -237,7 +240,7 @@ } } - private void insertDefaultContentId(Connection conn) throws DeployException + private void updateToolDefaultContentId(long toolId, long defaultContentId, Connection conn) throws DeployException { PreparedStatement stmt = null; try @@ -250,14 +253,35 @@ } catch (SQLException sqlex) { - throw new DeployException("Could not insert defualt content id", sqlex); + throw new DeployException("Could not update default content id into tool", sqlex); } finally { DbUtils.closeQuietly(stmt); } } + private void updateToolLibraryId(long toolId, long libraryId, Connection conn) throws DeployException + { + PreparedStatement stmt = null; + try + { + stmt = conn.prepareStatement("UPDATE lams_tool SET learning_library_id = ? WHERE tool_id = ?"); + stmt.setLong(1, libraryId); + stmt.setLong(2, toolId); + stmt.execute(); + + } + catch (SQLException sqlex) + { + throw new DeployException("Could not update library id into tool", sqlex); + } + finally + { + DbUtils.closeQuietly(stmt); + } + } + private void runScript(final String scriptSQL, final Connection conn) throws DeployException { ScriptRunner runner = new ScriptRunner(scriptSQL, conn);