2006-11-14 15:58:03 +00:00

383 lines
14 KiB
C#

// ========================================================================
// Copyright 2006 - Volian Enterprises, Inc. All rights reserved.
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
// ------------------------------------------------------------------------
// $Workfile: $ $Revision: $
// $Author: $ $Date: $
//
// $History: $
// ========================================================================
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 Section AddSection(string Number, string Title, DateTime Dts, string Userid, ConfigInfo ci, string stpseq, string fmt, int libdocid, string pth)
{
UpdateLabels(0, 1, 0);
try
{
string Format = null;
// do the format field, an xpath for the format & last part is column
// mode if a step section.
try
{
if (fmt != null && fmt != "")
{
if (fmt.IndexOf(' ') > -1) // will have spaces if it's a user format
{
string part1 = "/" + fmt.Substring(0, fmt.IndexOf(' ')) + "/";
string part2 = "USER=" + fmt.Substring(fmt.LastIndexOf(' ') + 1, 2);
Format = part1 + part2;
}
else
Format = "/" + fmt;
}
}
catch (Exception ex)
{
log.ErrorFormat("Error getting format {0}", ex.Message);
}
// tack on the column mode:
if (stpseq != null && stpseq.Substring(1, 1) == "0" && stpseq.Substring(5, 1) != " ") Format = Format + "/COL=" + stpseq.Substring(5, 1);
// find rtf file (or use the library document temp file) & read it into the field
// acccontent
int Contentid=0;
byte ContentType=0;
if (libdocid != 0 || stpseq.Substring(1, 1) != "0")
{
string fname = null;
if (libdocid != 0)
{
Contentid = libdocid;
ContentType = 2;
}
else
{
int num = Convert.ToInt32(stpseq[0]) - 64;
string thenum = num.ToString("d2");
fname = string.Format("{0}\\rtffiles\\{1}.A{2}", pth, ProcFileName, thenum);
Application.DoEvents();
SaveSectionDocument(fname, stpseq, ref ContentType, ref Contentid);
}
}
Section sec = Section.MakeSection(Number, Title,ContentType, Contentid, Format, ci.ToString(), Dts, Userid);
dicOldStepSequence[sec] = stpseq;
return sec;
}
catch (Exception ex)
{
log.Error("Save Section");
log.ErrorFormat("oldstepsequence = {0}", stpseq);
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
log.ErrorFormat(ex.StackTrace);
}
return null;
}
private string SectTitle(OleDbConnection cn, DataRowView dr)
{
string tbuff = null;
string menustr = null;
bool UseMultiLineSectionTitle = false; // TODO KBR: format flag
if (UseMultiLineSectionTitle)
{
bool titleInMemo = false;
// for accessory pages...
if (dr["Step"].ToString().Substring(1, 1) != "0")
{
// The long section title is stored on a record with the "~" character.
// This was done since originally the actual accessory page data was stored in the memo
// field, so the long title could not be stored there, another record had to be used.
OleDbDataAdapter da = new OleDbDataAdapter("select * from " + ProcFileName + " where [Step] like '" + dr["Step"].ToString().Substring(0, 1) + "~';", cn);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 1)
{
DataRow row = ds.Tables[0].Rows[0];
tbuff = TextConvert.ConvertText(row["Textm"].ToString());
if (tbuff != null || tbuff[0] != '\0') titleInMemo = true;
}
else // no long section title existed for this accessory page
tbuff = TextConvert.ConvertText(dr["Text"].ToString().PadRight(130, ' ').Substring(0, 75).TrimEnd());
}
catch (Exception ex)
{
log.ErrorFormat("Error getting long section title {0}", ex.Message);
}
}
// For step sections, the long section title is stored on the section record
// (see above comment for accessory pages to see the difference)
else
{
tbuff = TextConvert.ConvertText(dr["TextM"].ToString().Trim());
}
//// TESTS were run & it looked like that whitespace was removed before saving,
//// so, put up a message box if find out otherwise....
int nl = tbuff.IndexOf("\n");
if (nl > -1)
MessageBox.Show("multiline text for section title, fix this!!");
//// remove newlines & any other escape/whitespace chars.
//int nl = tbuff.IndexOf("\n");
//if (nl > -1 || titleInMemo)
//{
// string tmpstr = tbuff.Replace("\r", "");
// tmpstr = tmpstr.Replace("\t", "");
// tmpstr = tmpstr.Replace("\n", " ");
// // get rid of multiple spaces
// while (tmpstr.IndexOf(" ") > -1) tmpstr = tmpstr.Replace(" ", " ");
// tbuff = tmpstr;
// if (tbuff.Substring(tbuff.Length-1, 1) == " ") tbuff = tbuff.Substring(0, tbuff.Length - 1);
//}
//menustr = tbuff;
}
else
{ // format does not include long section title
menustr = TextConvert.ConvertText(dr["Text"].ToString().PadRight(80, ' ').Substring(0, 75).TrimEnd());
}
return menustr;
}
private Int32 MigrateSection(Procedure prc, OleDbConnection cn, DataRowView dr, DataTable dt, Byte FromType, Int32 FromID, bool isSubSection, string pth)
{
Int32 thesectid = 0;
bool islibdoc = false;
//bool hasxml = false;
string s = dr["text"].ToString().PadRight(130, ' ');
string num = s.Substring(85, 20).TrimEnd();
string fmt = s.Substring(75, 10).TrimEnd();
string title = SectTitle(cn, dr);
string init = dr["initials"].ToString().Trim();
string sequence = dr["CSequence"].ToString().PadRight(10);
string step = dr["CStep"].ToString();
int libDocid = 0;
DateTime dts = GetDTS(dr["Date"].ToString(), dr["Time"].ToString());
ConfigInfo ci = new ConfigInfo(null);
// for steps sections...
// Step Section Header Format:
// A0 1X2S51 &Y
// ^^^^^^^^^^^^
// |||||||||||||
// ||||||||||||`- 'Y','N', or blank signals to print section header - lib/section/addsec.c
// |||||||||||`-- (bits) Auto Indent, Editable Data, Checkoff Header Type - lib/section/addsec.c
// ||||||||||`--- blank
// ||||||||`----- Link With Enhanced Document ' '-Default(not determined) 0-NO 1-YES
// |||||||`------ MetaSection - number of subsections is given
// ||||||`------- S-Separate(PageBreak); T-Continuous; ' '-Default
// |||||`-------- Column mode (1,2,3,' '-default)
// ||||`--------- X -only proc section; x -orig. proc; ' ' -other
// |||`---------- Position within the procedure
// ||`----------- ALWAYS leave blank
// |`------------ Step Section Header marker
// `------------- Internal section number (starts at A)
if (step.Substring(1, 1) == "0")
{
// if this section has the original edit section flag (sequence[2]) save the id.
// set pagination, continuous, separate. If blank, don't create attribute - uses format default.
if (sequence.Substring(4, 1) == "T")
{
ci.AddItem("Section", "Pagination", "C");
//hasxml = SetXml(xmldoc, topElement, "Section", "Pagination", "C");
}
else if (sequence.Substring(4, 1) == "S")
{
ci.AddItem("Section", "Pagination", "S");
//hasxml = SetXml(xmldoc, topElement, "Section", "Pagination", "S");
}
// Step: linked to enhanced (!exist = N)
if (sequence.Substring(7, 1) == "1")
{
ci.AddItem("Step", "LnkEnh", "Y");
//hasxml = SetXml(xmldoc, topElement, "Step", "LnkEnh", "Y");
}
char cbittst = sequence.PadRight(10)[8];
if (cbittst == ' ') cbittst = '\0';
// determine if TOC element (!exist = N)
if ((cbittst & TOC) > 1)
{
ci.AddItem("Section", "TOC", "Y");
//hasxml = SetXml(xmldoc, topElement, "Section", "TOC", "Y");
}
// determine if autogenerated section (!exist = N)
if ((cbittst & AUTOGEN) > 1)
{
ci.AddItem("Section", "AutoGen", "Y");
//hasxml = SetXml(xmldoc, topElement, "Section", "AutoGen", "Y");
}
// Here are subsection flags, i.e. the following are only set if this
// is a subsection.
bool didsub = false;
if (isSubSection)
{
// Subsection: editable (!exist = Y)
if ((cbittst & EDDATA) > 0)
{
ci.AddItem("SubSection", "Edit", "N");
//SetXml(xmldoc, topElement, "SubSection", "Edit", "N");
}
// Subsection: print section headers (!exist = Y)
if ((cbittst & PH) > 0)
{
didsub = true;
ci.AddItem("SubSection", "PH", "N");
//SetXml(xmldoc, topElement, "SubSection", "PH", "N");
}
// Subsection: autoindent (!exist = Y)
if ((cbittst & AUTOIND) > 0)
{
ci.AddItem("SubSection", "AutoIndent", "N");
//SetXml(xmldoc, topElement, "SubSection", "AutoIndent", "N");
}
}
if (!didsub && sequence.Substring(4, 1) == "N")
{
ci.AddItem("SubSection", "PH", "N");
//SetXml(xmldoc, topElement, "SubSection", "PH", "N");
}
}
else
{
// Accessory Section Format:
// AI 1 2
// ^^ ^ ^
// || | |
// || | `- # of pages (ASCII value minus 48)
// || `--- Position within the procedure
// |`----- Acc. page type (A,I, or F)
// `------ Internal section number (starts at A)
ci.AddItem("Section", "NumPages", sequence.Substring(3, 1));
//hasxml = SetXml(xmldoc, topElement, "Section", "NumPages", sequence.Substring(3, 1));
// see if it's a libdoc too.0
string thekey = prc.Number.PadRight(20) + step.Substring(0, 1).PadRight(10);
if (dicLibDocRef.ContainsKey(thekey))
{
// if it is a library document, see if the section record has already been
// saved. If it has, just use this section id, otherwise, create the
// section record with info from the library document file.
libDocid = dicLibDocRef[thekey];
islibdoc = true;
}
}
Section sec = AddSection(num, title, dts, init, ci, step + sequence, fmt, libDocid, pth);
thesectid = sec.SectID;
// if this section has the original edit section flag (sequence[2]) save the id.
if (!islibdoc && step[1] == '0' && (sequence[2] == 'x' || sequence[2] == 'X'))
EditSectId = thesectid;
// ContentType (2 in the following call) are:
// 0 = structure,
// 1 = procedure,
// 2 = section,
// 3 = step
// 4 = branch
// fromtype values are (see steps.cs too)
// 0 = next of same type
// 1 = procedure,
// 2 = section,
// 3 = caution
// 4 = note
// 5 = RNO
// 6 = substep
// 7 = table
Structure str = AddStructure(FromType, FromID, 2, thesectid, step + sequence, dts, init);
// Process the Data Table - First look for High Level Steps
string sQry = string.Format("Step like '[{0}]%' and Sequence='S'", dr["Step"].ToString().Substring(0, 1));
DataView dv = new DataView(dt, sQry, "StepNo", DataViewRowState.CurrentRows);
Byte FrType = 6;
Int32 FrID = 0;
pbStep.Maximum = dt.Rows.Count;
pbStep.Value = 0;
foreach (DataRowView drv in dv)
{
FrID = MigrateStep(cn, dt, drv, FrType, FrID);
if (sec.ContentID == 0)
{
sec.ContentID = FrID;
sec.ContentType = 1;
sec.Save(true);
}
FrType = 0;
}
return str.StructureID;
}
private void LoadSection(DataSet ds, OleDbDataAdapter da, string FileName)
{
try
{
da.Fill(ds, "Sections");
DataTable dt = ds.Tables["Sections"];
dt.CaseSensitive = true;
dt.Columns.Add("CStep", System.Type.GetType("System.String"));
dt.Columns.Add("CSequence", System.Type.GetType("System.String"));
// set the cstep & csequence - couldn't do it in the add because it needed a sql function
foreach (DataRow drw in ds.Tables["Sections"].Rows)
{
drw["CStep"] = TextConvert.ConvertSeq(drw["Step"].ToString());
drw["CSequence"] = TextConvert.ConvertSeq(drw["Sequence"].ToString());
}
dt.Columns.Add("StepNo", System.Type.GetType("System.Int32"), "Convert(Convert(Substring(CStep,2,1),'System.Char'),'System.Int32')-48");
dt.Columns.Add("Level", System.Type.GetType("System.Int32"), "Len(CSequence)");
dt.Columns.Add("SubStepNo", System.Type.GetType("System.Int32"), "Convert(Convert(Substring(CSequence,Len(CSequence),1),'System.Char'),'System.Int32')-48");
dt.Columns.Add("locb", System.Type.GetType("System.Int32"), "Convert(Convert(Substring(CSequence,2,1),'System.Char'),'System.Int32')-48");
}
catch (Exception ex)
{
FileInfo fi;
switch (ex.Message)
{
case "Index file not found.":// then delete inf file
fi = new FileInfo(tbSource.Text + "\\" + FileName + ".inf");
fi.Delete();
LoadSection(ds, da, FileName);// Try Again
break;
case "External table is not in the expected format.": // then pad dbt file with 128 zeros.
fi = new FileInfo(tbSource.Text + "\\" + FileName + ".dbt");
FileStream fs = fi.OpenWrite();
fs.Position = fs.Length;
byte[] buf = new byte[128];
for (int i = 0; i < 128; i++) buf[i] = 0;
fs.Write(buf, 0, 128);
fs.Close();
LoadSection(ds, da, FileName);// Try Again
break;
default: // Unrecognized error
log.ErrorFormat("File - {0}.DBF\r\n\r\n{1}\r\n\r\n{2}", FileName, ex.Message, ex.InnerException);
break;
}
}
}
}
}