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

153 lines
5.7 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.MSWord;
using vlnObjectLibrary;
using vlnServerLibrary;
using Org.Mentalis.Files;
namespace DataLoader
{
public partial class frmLoader : Form
{
private void UpdateLabelsLibDocs(int incLib, int incUsages)
{
if (incLib == 0 && incUsages == 0)//Reset
{
lblTime.Tag = DateTime.Now;
pbProc.Value = 0;
pbSect.Value = 0;
}
else
{
pbProc.Value += incLib;
pbSect.Value += incUsages;
}
lblProc.Text = string.Format("{0} Lib Docs", pbProc.Value);
lblSection.Text = string.Format("{0} Usages", pbSect.Value);
lblStep.Text = "";
TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - ((DateTime)lblTime.Tag).Ticks);
lblTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2} Elapsed", ts.Hours, ts.Minutes, ts.Seconds);
Application.DoEvents();
}
private void MigrateLibDocs(OleDbConnection cn, string pth)
{
// Get all of the library documents - the first list has the entire list of files
// found within the rtffiles folder, the second list contains usages from the 'tran'
// file. During processing for procedures/sections occurs, the used library documents
// will be migrated. After that, any remaining library documents will be added to
// the section table without a reference from the structuretbl.
Dictionary<string, int> dicLibDocSect = new Dictionary<string, int>();
UpdateLabelsLibDocs(0, 0);
if (Directory.Exists(pth + "\\rtffiles"))
{
DirectoryInfo di = new DirectoryInfo(pth + "\\RTFFILES");
FileInfo[] fis = di.GetFiles("DOC_*.LIB");
pbProc.Maximum = fis.Length;
foreach (FileInfo fi in fis)
{
UpdateLabelsLibDocs(1, 0);
dicLibDocSect[fi.Name.Substring(0, 8).ToUpper()] = MigrateLibDoc(fi);
}
}
dicLibDocRef = new Dictionary<string, int>();
OleDbDataAdapter da_doc = new OleDbDataAdapter("select [FROMNUMBER], [FROMSEQUEN], [TONUMBER] from [tran] where [TONUMBER] LIKE 'doc_%' or [TONUMBER] like 'DOC_%'", cn);
DataSet ds_doc = new DataSet();
da_doc.Fill(ds_doc);
pbSect.Maximum = ds_doc.Tables[0].Rows.Count;
foreach (DataRow dr_doc in ds_doc.Tables[0].Rows)
{
UpdateLabelsLibDocs(0, 1);
string key = dr_doc["FROMNUMBER"].ToString().PadRight(20) + dr_doc["FROMSEQUEN"].ToString().PadRight(10);
if (!dicLibDocSect.ContainsKey(dr_doc["TONUMBER"].ToString().ToUpper()))
log.ErrorFormat("Error setting library document references: {0}", dr_doc["TONUMBER"].ToString().ToUpper());
else
dicLibDocRef[key] = dicLibDocSect[dr_doc["TONUMBER"].ToString().ToUpper()];
}
da_doc.Dispose();
}
private int MigrateLibDoc(FileInfo fi)
{
ConfigInfo ci = new ConfigInfo(null);
string title = null; // for docname, remove the '.lib', i.e. substring(0,8)
DateTime dts = DateTime.Now;
string tmpRtfFileName = GetLibDocData(fi, ci, ref title);
int Docid = SaveWordDoc(tmpRtfFileName, title, ci);
File.Delete(tmpRtfFileName);
return Docid;
}
private string LoadFromLib(BinaryReader br)
{
int nchar = br.ReadInt16();
if (nchar > 0)
{
string tmp = new string(br.ReadChars(nchar));
return tmp.Substring(0, tmp.Length - 1); // remove null at end.
}
return null;
}
private string GetLibDocData(FileInfo fi, ConfigInfo ci, ref string title)
{
title = null;
// get the number, title, etc from the file.
// use the path to open the file & read the title & comment
DateTime dts = fi.LastWriteTime;
FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(fs, System.Text.ASCIIEncoding.ASCII);
string tmpRtfFileName = Path.GetTempFileName();
FileStream tmpfile = new FileStream(tmpRtfFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(tmpfile, System.Text.Encoding.ASCII);
int cntPage = br.ReadInt16();
if (cntPage != -1)
{ // Not End of File
ci.AddItem("Section", "NumPages", cntPage.ToString());
string ldtitle = LoadFromLib(br);
if (ldtitle != null && ldtitle != "") title = ldtitle;
ci.AddItem("LibDoc", "Comment", LoadFromLib(br));
long l = br.BaseStream.Length - br.BaseStream.Position;
byte[] buf = new byte[l];
br.Read(buf, 0, (int)l);
bw.Write(buf, 0, (int)l);
// TODO: Check with KBR to see if she needed read/write this way
// I can't remember.
//byte ac;
//bool done = false;
//while (done == false)
//{
// if (br.PeekChar() > 0)
// {
// ac = br.ReadByte();
// bw.Write(ac);
// }
// else
// done = true;
//}
br.Close();
fs.Close();
bw.Close();
tmpfile.Close();
WaitMS(wms); // give it some time to close the tempfile before adding section
File.SetLastWriteTime(tmpRtfFileName, dts);
}
return tmpRtfFileName;
}
}
}