307 lines
11 KiB
C#
307 lines
11 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Collections;
|
|
using System.ComponentModel;
|
|
using System.Windows.Forms;
|
|
using System.Data;
|
|
using System.Data.OleDb;
|
|
using System.Collections.Specialized;
|
|
using System.Collections.Generic;
|
|
using System.Xml;
|
|
using System.IO;
|
|
using System.Text;
|
|
using Volian.CSLA.Library;
|
|
|
|
namespace DataLoader
|
|
{
|
|
public partial class frmLoader : Form
|
|
{
|
|
private Step AddStep(OleDbConnection cn, string StepType, string Textm, string Recid, string stpseq, string structtype, int structid, DateTime dts, string userid)
|
|
{
|
|
Step stp = null;
|
|
UpdateLabels(0, 0, 1);
|
|
bool hasxml = false;
|
|
ConfigInfo ci = new ConfigInfo(null);
|
|
string stptext = null;
|
|
int tok = -1;
|
|
char[] chrarr = { '\x1', '\x2', '\x3', '\x5' };
|
|
try
|
|
{
|
|
// the textm field has step text, but also may have other text stored
|
|
// with it with a 'token' as a separator (see below).
|
|
tok = Textm.IndexOfAny(chrarr);
|
|
if (tok < 0)
|
|
stptext = TextConvert.ConvertText(Textm);
|
|
else
|
|
stptext = TextConvert.ConvertText(Textm.Substring(0, tok));
|
|
|
|
string seqcvt = TextConvert.ConvertSeq(stpseq);
|
|
|
|
// Figure marker - it should NEVER get here!!!
|
|
int tokfig = Textm.IndexOf('\xE8');
|
|
if (tokfig > -1)
|
|
{
|
|
log.Error("Found a old style figure!");
|
|
log.ErrorFormat("oldstepsequence = {0}", stpseq);
|
|
}
|
|
// Before we save it, handle RO & Transitions tokens.
|
|
int tokrt = Textm.IndexOf('\x15');
|
|
if (tokrt > -1) stptext = MigrateRos(cn, stptext, seqcvt, structid);
|
|
|
|
// 16-bit code has the following two defines.
|
|
// #define TransitionMarker 0xC2
|
|
// #define ReferenceMarker 0xCB
|
|
// these two characters get converted (to unicode) from ado.net
|
|
// use the unicode chars.
|
|
char[] chrrotrn = { '\x252C', '\x2566' };
|
|
tokrt = Textm.IndexOfAny(chrrotrn);
|
|
if (tokrt > -1) stptext = MigrateTrans(cn, stptext, seqcvt, structid);
|
|
TextM tm = TextM.MakeTextM(stptext);
|
|
stp = Step.MakeStep(StepType, tm.TextMID, null, dts, userid);
|
|
dicOldStepSequence[stp] = seqcvt;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("Save Step");
|
|
log.ErrorFormat("oldstepsequence = {0}", stpseq);
|
|
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
|
|
log.ErrorFormat(ex.StackTrace);
|
|
stp = null;
|
|
}
|
|
|
|
// now add on any support pieces of text associated with the step.
|
|
// These include:
|
|
// '\1' comment
|
|
// '\2' multiple change ids and/or change message
|
|
// '\3' linked sequence
|
|
// '\3\3'override tab
|
|
// '\5' continuous action summary flag
|
|
|
|
try
|
|
{
|
|
while (tok >= 0 && tok != Textm.Length)
|
|
{
|
|
int nxttok = Textm.IndexOfAny(chrarr, tok + 1);
|
|
char chr = Textm[tok];
|
|
int typ = 0;
|
|
if (chr == '\x1')
|
|
typ = STP_COMMENT;
|
|
else if (chr == '\x2')
|
|
typ = STP_MULT_CHGID;
|
|
else if (chr == '\x3')
|
|
{
|
|
typ = STP_LNK_SEQ;
|
|
// check for a double \3 - override tab.
|
|
if (tok + 1 < Textm.Length)
|
|
{
|
|
char nxttokchr = Textm[tok + 1];
|
|
if (nxttokchr == '\x3')
|
|
{
|
|
typ = STP_OVR_TAB;
|
|
tok++; // this was a double \3, get past 1st one.
|
|
nxttok = Textm.IndexOfAny(chrarr, tok + 1);
|
|
}
|
|
}
|
|
}
|
|
else if (chr == '\x5')
|
|
{
|
|
ci.AddItem("Step", "ContActSum", "True");
|
|
//hasxml = SetXml(xmldoc, topElement, "Step", "ContActSum", "True");
|
|
if (nxttok < 0) nxttok = Textm.Length;
|
|
}
|
|
|
|
// if not xml, i.e. chr=='\x5', make a textm element
|
|
if (chr != '\x5')
|
|
{
|
|
if (nxttok < 0) nxttok = Textm.Length;
|
|
// if this is a sequence number - may need to convert hi-end chars
|
|
string str = null;
|
|
if (typ == STP_LNK_SEQ)
|
|
str = TextConvert.ConvertSeq(Textm.Substring(tok + 1, nxttok - tok - 1));
|
|
else
|
|
str = Textm.Substring(tok + 1, nxttok - tok - 1);
|
|
//StepText stptxt = StepText.MakeStepText(typ, str, true);
|
|
//stptxt.ItemType = typ;
|
|
//stptxt.Textm = str;
|
|
//stp.StepStepTexts.Add(
|
|
}
|
|
tok = nxttok;
|
|
}
|
|
|
|
// also see if a check-off needs added.
|
|
if (Recid[0] != '0')
|
|
{
|
|
string chkindx = Recid[0].ToString();
|
|
ci.AddItem("Step", "CheckOffIndex", chkindx);
|
|
//hasxml = SetXml(xmldoc, topElement, "Step", "CheckOffIndex", chkindx);
|
|
}
|
|
|
|
// here's where it knows if it's a linked step (or in processstep)
|
|
if (Recid[1] != '0')
|
|
{
|
|
// do linked step stuff.
|
|
}
|
|
|
|
// if checkoffs or the continuous action summary flag, save the xml.
|
|
if (hasxml)
|
|
{
|
|
stp.Config = ci.ToString();
|
|
stp.Save(true);
|
|
}
|
|
// if this has associated steptext, such as tab override or comment,
|
|
// save it.
|
|
//else if (stp.StepStepTexts.Count > 0)
|
|
// stp.Save(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("Save Step part 2");
|
|
log.ErrorFormat("oldstepsequence = {0}", stpseq);
|
|
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
|
|
log.ErrorFormat(ex.StackTrace);
|
|
}
|
|
return stp;
|
|
}
|
|
|
|
// private void ProcessSubStep(DataTable dt,DataRowView drv,StepTbl stpP)
|
|
// {
|
|
// // TODO: Need logic for TextM Support
|
|
// StepTbl stp = AddStep(stpP,drv["Type"].ToString(),drv["Text"].ToString(),drv["Step"].ToString()+drv["sequence"].ToString());
|
|
// // TODO: Logic to add Sub-steps
|
|
// string sPre = drv["Sequence"].ToString();
|
|
// ProcessSubSteps(dt,drv["Step"].ToString(),sPre+"[*!]?",stp);// Cautions and Notes
|
|
// ProcessSubSteps(dt,drv["Step"].ToString(),sPre+"$",stp);// RNOs
|
|
// ProcessSubSteps(dt,drv["Step"].ToString(),sPre+"?",stp);// Substeps
|
|
// //ProcessSubSteps(dt,drv["Step"],"S_",stp);// Tables
|
|
// }
|
|
// private void ProcessSubSteps(DataTable dt,string step,string lookfor,StepTbl stp)
|
|
// {
|
|
// DataView dv = new DataView(dt,"Step='" + step + "' and Sequence like'" + lookfor + "'",
|
|
// "sequence",DataViewRowState.OriginalRows);
|
|
// foreach(DataRowView drv in dv)
|
|
// {
|
|
// ProcessSubStep(dt,drv,stp);
|
|
// }
|
|
// }
|
|
private string GetParent(string s)
|
|
{
|
|
string retval = "S";
|
|
if (s.Length > 1)
|
|
{
|
|
int l = s.Length;
|
|
if ("!*".IndexOf(s[l - 2]) > -1)
|
|
{
|
|
if (l > 2) retval = s.Substring(0, l - 2);
|
|
}
|
|
else
|
|
{
|
|
retval = s.Substring(0, l - 1);
|
|
}
|
|
}
|
|
return retval;
|
|
}
|
|
private string GetStructType(string s)
|
|
{
|
|
string retval = "S";
|
|
if (s.Length > 1)
|
|
{
|
|
int l = s.Length;
|
|
if ("!*".IndexOf(s[l - 2]) > -1)
|
|
{
|
|
if (s[l - 2] == '!') retval = "C";
|
|
else retval = "N";
|
|
}
|
|
else
|
|
{
|
|
if (s[l - 1] == '$') retval = "R";
|
|
if (s[l - 1] == '#') retval = "T";
|
|
}
|
|
}
|
|
return retval;
|
|
}
|
|
private Int32 MigrateStep(OleDbConnection cn, DataTable dt, DataRowView drv, Byte FromType, Int32 FromID)
|
|
{
|
|
try
|
|
{
|
|
int tmpid = 1;
|
|
|
|
// Do the structure record first because usages from the step require a structure
|
|
// id.
|
|
string sType = GetStructType(drv["CSequence"].ToString());
|
|
// Structures str = AddStructure(FromType, FromID, (byte)(3 + ("CNRST".IndexOf(sType))), tmpid, drv["CStep"].ToString() + drv["CSequence"].ToString(),
|
|
Structure str = AddStructure(FromType, FromID, 3, tmpid, drv["CStep"].ToString() + drv["CSequence"].ToString(),
|
|
GetDTS(drv["Date"].ToString(), drv["Time"].ToString()), drv["Initials"].ToString());
|
|
Step stp = AddStep(cn, drv["Type"].ToString()
|
|
, (drv["textm"] == DBNull.Value ? drv["Text"].ToString() : drv["Textm"].ToString())
|
|
, drv["Recid"].ToString(), drv["CStep"].ToString() + drv["CSequence"].ToString(), "S", str.StructureID
|
|
, GetDTS(drv["Date"].ToString(), drv["Time"].ToString()), drv["Initials"].ToString());
|
|
str.ContentID = stp.StepID;
|
|
str.Save(true);
|
|
Dictionary<string, Step> dicStep = new Dictionary<string, Step>();
|
|
dicStep[drv["CSequence"].ToString()] = stp;
|
|
Dictionary<string, Dictionary<string, int>> dicStruct = new Dictionary<string, Dictionary<string, int>>();
|
|
Dictionary<string, int> dicBase = new Dictionary<string, int>();
|
|
dicStruct[drv["CSequence"].ToString()] = dicBase;
|
|
dicBase[""] = str.StructureID;
|
|
// Logic to add Sub-steps
|
|
string sQry = "CStep = '" + drv["CStep"].ToString() + "' and CSequence <> 'S'";
|
|
// sort order - for sections use currentrows.
|
|
DataView dv = new DataView(dt, sQry, "StepNo,Level,SubStepNo", DataViewRowState.CurrentRows);
|
|
//dataGrid1.DataSource=dv;
|
|
//Loop through DataView and add Steps one at a time
|
|
//Console.WriteLine("={0}",drv["Step"]);
|
|
Byte FrType = 0;
|
|
Int32 FrID = str.StructureID;
|
|
foreach (DataRowView drvs in dv)
|
|
{
|
|
//Console.WriteLine(">{0}",drvs["CStep"]);
|
|
|
|
string sParent = GetParent(drvs["CSequence"].ToString());
|
|
if (dicStep.ContainsKey(sParent))
|
|
{
|
|
Step stpp = dicStep[sParent];
|
|
sType = GetStructType(drvs["CSequence"].ToString());
|
|
Dictionary<string,int> dicStr = dicStruct[sParent];
|
|
if (dicStr.ContainsKey(sType))
|
|
{
|
|
FrID = (Int32)dicStr[sType];
|
|
FrType = 0;
|
|
}
|
|
else
|
|
{
|
|
FrID = (Int32)dicStr[""];
|
|
FrType = (byte)(3 + ("CNRST".IndexOf(sType)));
|
|
}
|
|
Structure str1 = AddStructure(FrType, FrID, 3, tmpid++, drvs["CStep"].ToString() + drvs["CSequence"].ToString(),
|
|
GetDTS(drvs["Date"].ToString(), drvs["Time"].ToString()), drvs["Initials"].ToString());
|
|
Step stpc = AddStep(cn, drvs["Type"].ToString()
|
|
, (drvs["textm"] == DBNull.Value ? drvs["Text"].ToString() : drvs["Textm"].ToString())
|
|
, drv["Recid"].ToString(), drvs["CStep"].ToString() + drvs["CSequence"].ToString(), GetStructType(drvs["sequence"].ToString()), str1.StructureID
|
|
, GetDTS(drvs["Date"].ToString(), drvs["Time"].ToString()), drvs["Initials"].ToString());
|
|
str1.ContentID = stpc.StepID;
|
|
str1.Save(true);
|
|
dicStep[drvs["CSequence"].ToString()] = stpc;
|
|
|
|
dicBase = new Dictionary<string, int>();
|
|
dicStruct[drvs["CSequence"].ToString()] = dicBase;
|
|
dicBase[""] = str1.StructureID;
|
|
dicStr[sType] = str1.StructureID;
|
|
}
|
|
else
|
|
{
|
|
log.ErrorFormat("Parent {0} Could not be found for {1}", sParent, drvs["sequence"].ToString());
|
|
}
|
|
}
|
|
return str.StructureID;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("PROCESS STEP");
|
|
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
|
|
log.ErrorFormat(ex.StackTrace);
|
|
return 0;
|
|
}
|
|
//return 0;
|
|
}
|
|
}
|
|
} |