122 lines
4.9 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 Loader
{
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>();
frmMain.UpdateLabelsLibDocs(0, 0);
if (Directory.Exists(pth + "\\rtffiles"))
{
DirectoryInfo di = new DirectoryInfo(pth + "\\RTFFILES");
FileInfo[] fis = di.GetFiles("DOC_*.LIB");
frmMain.UpdateLabelsSetProc(fis.Length);
foreach (FileInfo fi in fis)
{
frmMain.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);
frmMain.UpdateLabelsSetSect(ds_doc.Tables[0].Rows.Count);
foreach (DataRow dr_doc in ds_doc.Tables[0].Rows)
{
frmMain.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;
ci.AddItem("Edit", "Initialized", "true");
string tmpRtfFileName = GetLibDocData(fi, ci, ref title);
int Docid = SaveWordDoc(tmpRtfFileName, title, null, ci, string.Empty);
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.LastWriteTimeUtc;
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);
br.Close();
fs.Close();
bw.Close();
tmpfile.Close();
WaitMS(wms); // give it some time to close the tempfile before adding section
File.SetLastWriteTimeUtc(tmpRtfFileName, dts);
}
return tmpRtfFileName;
}
}
}