public string wmRenameCodeblock(string sTaskID, string sOldCodeblockName, string sNewCodeblockName) { dataAccess dc = new dataAccess(); acUI.acUI ui = new acUI.acUI(); FunctionTemplates.HTMLTemplates ft = new FunctionTemplates.HTMLTemplates(); try { if (ui.IsGUID(sTaskID)) { // first make sure we are not trying to rename it something that already exists. string sErr = ""; string sSQL = "select count(*) from task_codeblock where task_id = '" + sTaskID + "'" + " and codeblock_name = '" + sNewCodeblockName + "'"; int iCount = 0; if (!dc.sqlGetSingleInteger(ref iCount, sSQL, ref sErr)) { throw new Exception("Unable to check codeblock names for task." + sErr); } if (iCount != 0) { return ("Codeblock Name already in use, choose another."); } // do it dataAccess.acTransaction oTrans = new dataAccess.acTransaction(ref sErr); //update the codeblock table sSQL = "update task_codeblock set codeblock_name = '" + sNewCodeblockName + "' where codeblock_name = '" + sOldCodeblockName + "' and task_id = '" + sTaskID + "'"; oTrans.Command.CommandText = sSQL; if (!oTrans.ExecUpdate(ref sErr)) { throw new Exception(sErr); } //and any steps in that codeblock sSQL = "update task_step set codeblock_name = '" + sNewCodeblockName + "' where codeblock_name = '" + sOldCodeblockName + "' and task_id = '" + sTaskID + "'"; oTrans.Command.CommandText = sSQL; if (!oTrans.ExecUpdate(ref sErr)) { throw new Exception(sErr); } //the fun part... rename it where it exists in any steps //but this must be in a loop of only the steps where that codeblock reference exists. sSQL = "select step_id from task_step" + " where task_id = '" + sTaskID + "'" + " and ExtractValue(function_xml, '//codeblock[1]') = '" + sOldCodeblockName + "'"; oTrans.Command.CommandText = sSQL; DataTable dtSteps = new DataTable(); if (!oTrans.ExecGetDataTable(ref dtSteps, ref sErr)) { throw new Exception("Unable to get steps referencing the Codeblock." + sErr); } foreach (DataRow dr in dtSteps.Rows) { ft.SetNodeValueinXMLColumn("task_step", "function_xml", "step_id = '" + dr["step_id"].ToString() + "'", "//codeblock[. = '" + sOldCodeblockName + "']", sNewCodeblockName); } //all done oTrans.Commit(); return sErr; } else { throw new Exception("Unable to get codeblocks for task. Missing or invalid task_id."); } } catch (Exception ex) { throw ex; } }
private string CopyTask(int iMode, string sSourceTaskID, string sNewTaskName, string sNewTaskCode) { //iMode 0=new task, 1=new major version, 2=new minor version dataAccess dc = new dataAccess(); acUI.acUI ui = new acUI.acUI(); string sErr = ""; string sSQL = ""; string sNewTaskID = ui.NewGUID(); int iIsDefault = 0; string sTaskName = ""; double dVersion = 1.000; double dMaxVer = 0.000; string sOTID = ""; //do it all in a transaction dataAccess.acTransaction oTrans = new dataAccess.acTransaction(ref sErr); //figure out the new name and selected version oTrans.Command.CommandText = "select task_name, version, original_task_id from task where task_id = '" + sSourceTaskID + "'"; DataRow dr = null; if (!oTrans.ExecGetDataRow(ref dr, ref sErr)) throw new Exception("Unable to find task for ID [" + sSourceTaskID + "]." + sErr); sTaskName = dr["task_name"].ToString(); dVersion = Convert.ToDouble(dr["version"]); sOTID = dr["original_task_id"].ToString(); //figure out the new version switch (iMode) { case 0: sTaskName = sNewTaskName; iIsDefault = 1; dVersion = 1.000; sOTID = sNewTaskID; break; case 1: //gotta get the highest version sSQL = "select max(version) from task where task_id = '" + sOTID + "'"; dc.sqlGetSingleDouble(ref dMaxVer, sSQL, ref sErr); if (sErr != "") { oTrans.RollBack(); throw new Exception(sErr); } dVersion = dMaxVer + 1; break; case 2: sSQL = "select max(version) from task where task_id = '" + sOTID + "'" + " and cast(version as unsigned) = " + Convert.ToInt32(dVersion); dc.sqlGetSingleDouble(ref dMaxVer, sSQL, ref sErr); if (sErr != "") { oTrans.RollBack(); throw new Exception(sErr); } dVersion = dMaxVer + 0.001; break; default: //a iMode is required throw new Exception("A mode required for this copy operation." + sErr); } //if we are versioning, AND there are not yet any 'Approved' versions, //we set this new version to be the default //(that way it's the one that you get taken to when you pick it from a list) if (iMode > 0) { sSQL = "select case when count(*) = 0 then 1 else 0 end" + " from task where original_task_id = '" + sOTID + "'" + " and task_status = 'Approved'"; dc.sqlGetSingleInteger(ref iIsDefault, sSQL, ref sErr); if (sErr != "") { oTrans.RollBack(); throw new Exception(sErr); } } //start copying oTrans.Command.CommandText = "create temporary table _copy_task" + " select * from task where task_id = '" + sSourceTaskID + "'"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //update the task_id oTrans.Command.CommandText = "update _copy_task set" + " task_id = '" + sNewTaskID + "'," + " original_task_id = '" + sOTID + "'," + " version = '" + dVersion + "'," + " task_name = '" + sTaskName + "'," + " default_version = " + iIsDefault.ToString() + "," + " task_status = 'Development'," + " created_dt = now()"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //update the task_code if necessary if (iMode == 0) { oTrans.Command.CommandText = "update _copy_task set task_code = '" + sNewTaskCode + "'"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); } //codeblocks oTrans.Command.CommandText = "create temporary table _copy_task_codeblock" + " select '" + sNewTaskID + "' as task_id, codeblock_name" + " from task_codeblock where task_id = '" + sSourceTaskID + "'"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //USING TEMPORARY TABLES... need a place to hold step ids while we manipulate them oTrans.Command.CommandText = "create temporary table _step_ids" + " select distinct step_id, uuid() as newstep_id" + " from task_step where task_id = '" + sSourceTaskID + "'"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //steps temp table oTrans.Command.CommandText = "create temporary table _copy_task_step" + " select step_id, '" + sNewTaskID + "' as task_id, codeblock_name, step_order, commented," + " locked, function_name, function_xml, step_desc, output_parse_type, output_row_delimiter," + " output_column_delimiter, variable_xml" + " from task_step where task_id = '" + sSourceTaskID + "'"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //update the step id oTrans.Command.CommandText = "update _copy_task_step a, _step_ids b" + " set a.step_id = b.newstep_id" + " where a.step_id = b.step_id"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //update steps with codeblocks that reference a step (embedded steps) oTrans.Command.CommandText = "update _copy_task_step a, _step_ids b" + " set a.codeblock_name = b.newstep_id" + " where b.step_id = a.codeblock_name"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //spin the steps and update any embedded step id's in the commands oTrans.Command.CommandText = "select step_id, newstep_id from _step_ids"; DataTable dtStepIDs = new DataTable(); if (!oTrans.ExecGetDataTable(ref dtStepIDs, ref sErr)) throw new Exception("Unable to get step ids." + sErr); foreach (DataRow drStepIDs in dtStepIDs.Rows) { oTrans.Command.CommandText = "update _copy_task_step" + " set function_xml = replace(lower(function_xml), '" + drStepIDs["step_id"].ToString().ToLower() + "', '" + drStepIDs["newstep_id"].ToString() + "')" + " where function_name in ('if','loop','exists')"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); } //finally, put the temp steps table in the real steps table oTrans.Command.CommandText = "insert into task select * from _copy_task"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); oTrans.Command.CommandText = "insert into task_codeblock select * from _copy_task_codeblock"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); oTrans.Command.CommandText = "insert into task_step select * from _copy_task_step"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); //finally, if we versioned up and we set this one as the new default_version, //we need to unset the other row if (iMode > 0 && iIsDefault == 1) { oTrans.Command.CommandText = "update task" + " set default_version = 0" + " where original_task_id = '" + sOTID + "'" + " and task_id <> '" + sNewTaskID + "'"; if (!oTrans.ExecUpdate(ref sErr)) throw new Exception(sErr); } oTrans.Commit(); return sNewTaskID; }
private bool ReIDSteps(ref dataAccess.acTransaction oTrans, string sUserID, string sTaskID, ref string sErr) { //We join this to task_step on step_id, and only issue new GUID's //to the rows that are in conflict. //saves time, and if no steps are in conflict, the imported version is an exact match of the //exported one. //( and yes, I realize I did not join task_step on the task_id. // thats because the step_id is a guid AND the PK on the table. not necessary to check // it against task_id too.) oTrans.Command.CommandText = "select its.step_id" + " from import_task_step its" + " join task_step ts on its.step_id = ts.step_id" + " where its.user_id = '" + sUserID + "'" + " and its.task_id = '" + sTaskID + "'"; DataTable dtSteps = new DataTable(); if (!oTrans.ExecGetDataTable(ref dtSteps, ref sErr)) { throw new Exception(sErr); } if (dtSteps.Rows.Count > 0) { foreach (DataRow drSteps in dtSteps.Rows) { //update each row by: //*) getting a new guid //*) searching for references to the old ID and replacing them // specifically in function_xml //*) updating the row with the new guid string sOrigStepID = drSteps["step_id"].ToString(); string sNewStepID = ui.NewGUID(); //this will update any references in function_xml with // the new guid of this step oTrans.Command.CommandText = "update import_task_step" + " set function_xml = replace(function_xml, '" + sOrigStepID + "', '" + sNewStepID + "')" + " where ifnull(ExtractValue(function_xml, '(//*[. = ''" + sOrigStepID + "''])'), '') <> ''"; if (!oTrans.ExecUpdate(ref sErr)) { throw new Exception(sErr); } //then finally, we will update the actual step rows with the new id oTrans.Command.CommandText = "update import_task_step" + " set step_id = '" + sNewStepID + "'" + " where step_id = '" + sOrigStepID + "'"; if (!oTrans.ExecUpdate(ref sErr)) { throw new Exception(sErr); } } } return(true); }