// ======================================================================== // 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, bool appending) { // MessageBox.Show("Before LibDocs"); frmMain.AddInfo("Before MigrateLibDocs{0}\r\n{1}", GC.GetTotalMemory(true), VEPROMS.CSLA.Library.CSLACache.UsageAll); // 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 dicLibDocSect = new Dictionary(); VEPROMS.CSLA.Library.DocumentInfoList docList = VEPROMS.CSLA.Library.DocumentInfoList.Get(); 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) { string fName = fi.Name.Substring(0, 8).ToUpper(); frmMain.UpdateLabelsLibDocs(1, 0); if (appending) { foreach (VEPROMS.CSLA.Library.DocumentInfo docInfo in docList) { if(docInfo.Config.Contains(string.Format("\"{0}.LIB\"", fName))) dicLibDocSect[fName] = docInfo.DocID; } } else dicLibDocSect[fName] = MigrateLibDoc(fi); } } dicLibDocRef = new Dictionary(); 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) + TextConvert.ConvertSeq(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(); frmMain.AddInfo("After MigrateLibDocs{0}\r\n{1}", GC.GetTotalMemory(true), VEPROMS.CSLA.Library.CSLACache.UsageAll); // MessageBox.Show("After LibDocs"); } private int MigrateLibDoc(FileInfo fi) { ConfigInfo ci = new ConfigInfo(null); string title = null; // for docname, remove the '.lib', i.e. substring(0,8) ci.AddItem("Edit", "Initialized", "true"); ci.AddItem("History", "OriginalFileName", fi.Name); string tmpRtfFileName = GetLibDocData(fi, ci, ref title); int Docid = SaveWordDoc(tmpRtfFileName, title, null, false, ci, string.Empty,fi.LastWriteTimeUtc); 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; } } }