SourceCode/PROMS/DataLoader/Documents.cs
2008-11-19 13:53:59 +00:00

141 lines
4.3 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 VEPROMS.CSLA.Library;
namespace DataLoader
{
public partial class Loader
{
private void SaveSectionDocument(string fname, string stpseq, string stype, ref int cid)
{
int docid = SaveWordDoc(fname, stype);
switch (docid)
{
case 0:
// could add the following back in - but many of these may be put out in the log, and
// don't supply any pertinent info.
//log.InfoFormat("Timing problem for save of word document, will retry. oldstepsequence = {0}", stpseq);
break;
case -1:
log.ErrorFormat("Could not complete save of word document, oldstepsequence = {0}", stpseq);
break;
default:
cid = docid;
break;
}
}
private int SaveWordDoc(string fname, string title, string stype, ConfigInfo ci)
{
int docid = 0;
if (System.IO.File.Exists(fname))
{
if (frmMain.cbSaveDocChecked)
{
WordDoc d = new WordDoc(fname);
/* 16-bit's type[1] used the following codes to represent the respective lpi setting
*
* char far typestr[] = "*pP46f7L";
*
* char * far printtypes[] = {
* "Compressed, 8 lines per inch",
* "Elite, 6 lines per inch",
* "Pica, 6 lines per inch",
* "Default font, 4 Lines Per Inch",
* "Default font, 6 Lines Per Inch",
* "Compressed 6 LPI",
* "Default font, 7 Lines Per Inch",
* "Special Landscape, Elite, 6 lines per inch"
*/
if (stype != null)
{
// stype[1] == 'p' or 'P' or '6' 'f' or 'L' get spc = 6
int lpi = 6;
if (stype[1] == '*') lpi = 8;
else if (stype[1] == '4') lpi = 4;
else if (stype[1] == '7') lpi = 7;
// if need landscape set too: bool landscape = (stype[1] == 'L');
d.SetLineSpacing(lpi); // if need landscape set too: , landscape); ;
}
string temppath = Path.GetTempFileName();
string s = d.Save(temppath);
d.Close();
WaitMS(wms);
docid = SaveDoc(temppath, title, ci);
File.Delete(temppath);
}
else
{
if (frmMain.cbSaveRTFChecked)
docid = SaveDoc(fname, title, ci);
}
}
else
log.ErrorFormat("Missing rtf file: {0}", fname);
return docid;
}
private int SaveWordDoc(string temppath, string stype)
{
return SaveWordDoc(temppath, String.Empty, stype, null);
}
private int SaveTheDoc(string temppath, string title, ConfigInfo ci)
{
try
{
FileStream fs = File.Open(temppath, FileMode.Open, FileAccess.Read, FileShare.None);
long len = fs.Length + 1;
byte[] ByteArray = new byte[len];
int nBytesRead = fs.Read(ByteArray, 0, (int)len);
fs.Close();
string t1 = (title == null || title == "") ? null : title;
Document doc = Document.MakeDocument(t1, ByteArray, null, ci == null ? null : ci.ToString(), DateTime.Now, "Migration");
return doc.DocID;
}
// for an io exception, keep trying
catch (IOException)
{
Wait(2);
return 0;
}
catch (Exception ex)
{
log.Error("Save Word Doc");
log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
return -1;
}
}
private int SaveDoc(string temppath, string title, ConfigInfo ci)
{
int done = 0;
int ntry = 0;
while (done == 0 && ntry < 4)
{
ntry++;
done = SaveTheDoc(temppath, title, ci);
}
return done;
}
}
}