2009-06-12 20:17:03 +00:00

383 lines
14 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 VEPROMS.CSLA.Library;
namespace DataLoader
{
public partial class Loader
{
private Item AddStep(OleDbConnection cn, string StepType, string Textm, string Recid, string stpseq, string structtype, Item FromItem, DateTime dts, string userid, bool conv_caret, string pth, DocVersion docver)
{
Content content = null;
Item item = null;
frmMain.UpdateLabels(0, 0, 1);
ConfigInfo ci = new ConfigInfo(null);
string stptext = null;
if (userid == null || userid == "") userid = "Migration";
int tok = -1;
//char[] chrarr = { '\x1', '\x2', '\x3', '\x5' };
char[] chrarr = { '\x1', '\x2', '\x3' };
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);
// This will find a continuous action flag without finding a hanging indent
if (tok == 0 && Textm[Textm.Length - 1] == '\x5')
tok = Textm.Length - 1;
if (tok < 0)
stptext = TextConvert.ConvertText(Textm, conv_caret);
else
stptext = TextConvert.ConvertText(Textm.Substring(0, tok), conv_caret);
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);
}
string newstptyp = null;
bool ManualPagebreak = false;
char cbittst = StepType.PadRight(2)[0];
if (cbittst != ' ' && (cbittst & 0x80) > 1)
{
ManualPagebreak = true;
//if (ci == null) ci = new ConfigInfo(null);
ci.AddItem("Step", "ManualPagebreak", "True");
newstptyp = StepType.Substring(1, 1);
}
// Need the content record for the RO & transitions.
//content = Content.MakeContent(null, stptext, 20000 + int.Parse(newstptyp!=null?newstptyp:StepType),null, ManualPagebreak?ci.ToString():null, dts, userid);
// 20000 flags step type item & 1 adjusts for 'base' in format files.
int contenttype = (structtype == "R") ? 20040 : 20001 + int.Parse(newstptyp != null ? newstptyp : StepType);
content = Content.New(null, stptext, contenttype, null, ManualPagebreak ? ci.ToString() : null, dts, userid);
content.MyZContent.OldStepSequence = ProcNumber + "|" + stpseq;
// Before we save it, handle RO & Transitions tokens.
int tokrt = Textm.IndexOf('\x15');
bool txtdirty = false;
if (tokrt > -1)
{
txtdirty = true;
stptext = MigrateRos(cn, stptext, seqcvt, content, docver);
}
// 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);
_TransitionMigrationErrors = new List<string>();
if (tokrt > -1)
{
txtdirty = true;
stptext = MigrateTrans(cn, stptext, seqcvt, content, pth);
}
if (txtdirty)
{
content.Text = stptext;
//content.Save();
}
if (!content.IsSavable) ErrorRpt.ErrorReport(content);
List<string> migrationerrors = null;
if (_ContentMigrationErrors.ContainsKey(content.ContentID))
migrationerrors = _ContentMigrationErrors[content.ContentID];
content.Save();
// check if already created thru new during transition migration...
if (dicTrans_ItemIds.ContainsKey(ProcNumber + "|" + seqcvt))
{
item = dicTrans_ItemIds[ProcNumber + "|" + seqcvt];
item.MyPrevious = FromItem;
item.MyContent = content;
item.DTS = dts;
item.UserID = userid;
if (!item.IsSavable) ErrorRpt.ErrorReport(item);
item.Save();
dicTrans_ItemIds.Remove(ProcNumber + "|" + seqcvt);
dicTrans_MigrationErrors.Remove(ProcNumber + "|" + seqcvt);
}
else
item = Item.MakeItem(FromItem, content, content.DTS, content.UserID);
if (migrationerrors != null)
{
foreach (string str in migrationerrors)
{
ItemAnnotation ia = item.ItemAnnotations.Add(MigrationErrorType);
ia.SearchText = str;
}
if (!item.IsSavable) ErrorRpt.ErrorReport(item);
item.Save();
}
if (_TransitionMigrationErrors.Count > 0)
{
foreach (string str in _TransitionMigrationErrors)
{
if (!dicTrans_MigrationErrors.ContainsKey(str))
dicTrans_MigrationErrors.Add(str, new List<Item>());
dicTrans_MigrationErrors[str].Add(item);
}
}
dicTrans_ItemDone[ProcNumber + "|" + seqcvt] = item;
dicOldStepSequence[content] = 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);
item = 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 (only if last char in string)
// This assumes that these tokens are stored in reverse order that
// they are processed.
bool recdirty = false;
try
{
if (tok >= 0 && tok != Textm.Length)
{
string tkstring = Textm.Substring(tok);
int nxttok = 0;
if (tkstring[tkstring.Length - 1] == '\x5') // Continuous Action Summary
{
recdirty = true;
//if (ci == null) ci = new ConfigInfo(null);
ci.AddItem("Step", "ContActSum", "True");
tkstring = tkstring.Substring(0, tkstring.Length - 1); // strip off Continuous Action Token
}
if ((nxttok = tkstring.IndexOf("\x3\x3")) > -1) // Enhanced Override Tab
{
Console.WriteLine("Override Tab: {0}", tkstring.Substring(nxttok + 1));
ci.AddItem("Step", "OverrideTab", tkstring.Substring(nxttok + 2));
recdirty = true;
//recdirty |= AddContentDetail(content, STP_OVR_TAB, tkstring.Substring(nxttok + 2));
tkstring = tkstring.Substring(0, nxttok);
}
if ((nxttok = tkstring.IndexOf("\x3")) > -1) //Linked Sequence
{
Console.WriteLine("Linked Seq: {0}", tkstring.Substring(nxttok + 1));
ci.AddItem("Step", "LinkedSeq", TextConvert.ConvertSeq(tkstring.Substring(nxttok + 1)));
recdirty = true;
//recdirty |= AddContentDetail(content, STP_LNK_SEQ, TextConvert.ConvertSeq(tkstring.Substring(nxttok + 1)));
tkstring = tkstring.Substring(0, nxttok);
}
if ((nxttok = tkstring.IndexOf("\x2")) > -1) // Multiple Change Ids
{
Console.WriteLine("Multiple Change ID: {0}", tkstring.Substring(nxttok + 1));
ci.AddItem("Step", "MultipleChangeID", tkstring.Substring(nxttok + 1));
recdirty = true;
//recdirty |= AddContentDetail(content, STP_MULT_CHGID, tkstring.Substring(nxttok + 1));
tkstring = tkstring.Substring(0, nxttok);
}
if ((nxttok = tkstring.IndexOf("\x1")) > -1) // Comment
{
// add the comment to annotation table. Set type to comment.
Console.WriteLine("Comment text: {0}", tkstring.Substring(nxttok + 1));
Annotation annot = Annotation.MakeAnnotation(item, CommentType, null, tkstring.Substring(nxttok + 1), null, dts, userid);
}
}
// also see if a check-off needs added.
if (Recid[0] != '0')
{
recdirty = true;
string chkindx = Recid[0].ToString();
//if (ci == null) ci = new ConfigInfo(null);
ci.AddItem("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 (recdirty)
{
if (ci.ItemCount != 0) content.Config = ci.ToString();
if (!content.IsSavable) ErrorRpt.ErrorReport(content);
content.Save();
}
}
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 item;
}
//private static bool AddContentDetail(Content content, int type, string strn)
//{
// if (strn != null && strn.Trim() != "" )
// {
// content.ContentDetails.Add(type, strn);
// return true;
// }
// return false;
//}
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 Item MigrateStep(OleDbConnection cn, DataTable dt, DataRowView drv, Item FromItem, bool conv_caret, string pth, DocVersion docver)
{
try
{
string sType = GetStructType(drv["CSequence"].ToString());
Item item = 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", FromItem // was str
, GetDTS(drv["Date"].ToString(), drv["Time"].ToString()), drv["Initials"].ToString(), conv_caret, pth, docver);
//Content cont = Content.MakeContent(null,(drv["textm"] == DBNull.Value ? drv["Text"].ToString() : drv["Textm"].ToString()),drv["Type"]+20000,null,null,
// GetDTS(drv["Date"].ToString(), drv["Time"].ToString()), drv["Initials"].ToString());
//Item item = Item.MakeItem(FromItem, cont, cont.DTS, cont.UserID);
//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
// , GetDTS(drv["Date"].ToString(), drv["Time"].ToString()), drv["Initials"].ToString());
Dictionary<string, Item> dicStep = new Dictionary<string, Item>();
dicStep[drv["CSequence"].ToString()] = item;
Dictionary<string, Dictionary<string, Item>> dicStruct = new Dictionary<string, Dictionary<string, Item>>();
Dictionary<string, Item> dicBase = new Dictionary<string, Item>();
dicStruct[drv["CSequence"].ToString()] = dicBase;
dicBase[""] = item;
// 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"]);
int FrType = 0; // type of relationship (not type of step)
Item FrItem = item;
foreach (DataRowView drvs in dv)
{
//Console.WriteLine(">{0}",drvs["CStep"]);
string sParent = GetParent(drvs["CSequence"].ToString());
if (dicStep.ContainsKey(sParent))
{
Item itemp = dicStep[sParent];
sType = GetStructType(drvs["CSequence"].ToString());
Dictionary<string,Item> dicStr = dicStruct[sParent];
// check if a step type of 'sType' exists to see if child/sibling
//Content contc = Content.MakeContent(null,(drvs["textm"] == DBNull.Value ? drvs["Text"].ToString() : drvs["Textm"].ToString()),drvs["Type"]+20000,null,null,
// GetDTS(drvs["Date"].ToString(), drvs["Time"].ToString()), drvs["Initials"].ToString()));
Item itemc = null;
if (dicStr.ContainsKey(sType)) // next sibling
{
FrItem = dicStr[sType];
FrType = 0;
}
else // child of a node
{
FrItem = null;
FrType = (3 + ("CNRST".IndexOf(sType)));
}
itemc = 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()), FrItem
, GetDTS(drvs["Date"].ToString(), drvs["Time"].ToString()), drvs["Initials"].ToString(), conv_caret, pth, docver);
if (FrType > 0 )
{
itemp.MyContent.ContentParts.Add(FrType, itemc);
if (!itemp.MyContent.IsSavable) ErrorRpt.ErrorReport(itemp.MyContent);
itemp.MyContent.Save();
}
dicStr[sType] = itemc;
dicStruct[drvs["CSequence"].ToString()] = new Dictionary<string, Item>();
dicStep[drvs["CSequence"].ToString()] = itemc;
}
else
{
log.ErrorFormat("Parent {0} Could not be found for {1}", sParent, drvs["sequence"].ToString());
}
}
return item;
}
catch (Exception ex)
{
log.Error("PROCESS STEP");
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
log.ErrorFormat(ex.StackTrace);
return null;
}
}
}
}