using System; using System.Collections.Generic; using System.Text; using System.IO; using Csla; using Csla.Data; using System.Data; using System.Data.SqlClient; using EPocalipse.IFilter; namespace VEPROMS.CSLA.Library { public partial class Document { public string DocumentTitle { get { if (_LibTitle == "") return string.Format("Document {0}", _DocID); return _LibTitle; } } public void UpdateDocAscii(string fileName) { try { DocAscii = null; // Opening the file directly gives a sharing violation. // Therefore, the file is copied and the copy is used and then deleted. FileInfo myFile = new FileInfo(fileName); FileInfo tempFile = new FileInfo(myFile.DirectoryName + "\\tmp_" + myFile.Name); myFile.CopyTo(tempFile.FullName); // Copy to the temporary file using (FilterReader reader = new FilterReader(tempFile.FullName)) // Open the IFilter Reader { DocAscii = FixString(reader.ReadToEnd()); // Read the ascii text reader.Close(); // Close the reader } tempFile.Delete(); } catch (Exception ex) { // Handle exceptions } } /// /// FixString processes the string returned and changes any symbols (0xF0??) to normal characters /// /// /// private static string FixString(string str) { StringBuilder results = new StringBuilder(); foreach (char c in str) { if ((c & 0xFF00) == 0xF000) results.Append((char)(c & 0xFF)); else results.Append((char)(c)); } return results.ToString(); } } public partial class DocumentInfo { public string DocumentTitle { get { if (_LibTitle == "") return string.Format("Document {0}", _DocID); return _LibTitle; } } public string LibraryDocumentUsage { get { StringBuilder sb = new StringBuilder(); string sep = "\r\nUsed In:\r\n "; if (DocumentEntries == null) sb.Append("None"); else { foreach (EntryInfo myEntry in DocumentEntries) { foreach (ItemInfo myItem in myEntry.MyContent.ContentItems) { ItemInfo proc = myItem.MyProcedure; sb.Append(sep + proc.MyContent.Number + " - " + proc.MyContent.Text); sep = "\r\n "; } } } return sb.ToString(); } } public ItemInfoList LibraryDocumentUsageList { get { bool first = true; ItemInfoList iil = null; if (DocumentEntries == null) return null; foreach (EntryInfo myEntry in DocumentEntries) { foreach (ItemInfo myitem in myEntry.MyContent.ContentItems) { if (first) { iil = new ItemInfoList(myitem); first = false; } else iil.AddItem(myitem); } } return iil; } } } public partial class DocumentInfoList { public static DocumentInfoList GetLibraries(bool forceload) { try { if (!forceload && _DocumentInfoList != null) return _DocumentInfoList; DocumentInfoList tmp = DataPortal.Fetch(new LibraryCriteria(true)); DocumentInfo.AddList(tmp); tmp.AddEvents(); _DocumentInfoList = tmp; return tmp; } catch (Exception ex) { throw new DbCslaException("Error on DocumentInfoList.Get", ex); } } [Serializable()] protected class LibraryCriteria { private bool _IsLibrary; public bool IsLibrary { get { return _IsLibrary; } set { _IsLibrary = value; } } public LibraryCriteria(bool islibrary) { _IsLibrary = islibrary; } } private void DataPortal_Fetch(LibraryCriteria criteria) { this.RaiseListChangedEvents = false; if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] DocumentInfoList.DataPortal_Fetch", GetHashCode()); try { using (SqlConnection cn = Database.VEPROMS_SqlConnection) { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "getLibraryDocuments"; using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { IsReadOnly = false; while (dr.Read()) this.Add(new DocumentInfo(dr)); IsReadOnly = true; } } } } catch (Exception ex) { if (_MyLog.IsErrorEnabled) _MyLog.Error("DocumentInfoList.DataPortal_Fetch", ex); throw new DbCslaException("DocumentInfoList.DataPortal_Fetch", ex); } this.RaiseListChangedEvents = true; } } public class DSOFile : IDisposable { private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region Fields private bool _IsDisposed; private static string _TemporaryFolder = null; private DocumentInfo _MyDocument = null; private FileInfo _MyFile = null; private string _Extension = "DOC"; #endregion #region Properties public static string TemporaryFolder { get { if (_TemporaryFolder == null) { // This will create a Temp\VE-PROMS folder in the LocalSettings Folder. //XP - C:\Documents and Settings\{user}\Local Settings\Application Data\Temp\VE-PROMS //Vista - C:\Users\{user}\AppData\Local\Temp\VE-PROMS _TemporaryFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Temp"; if (!Directory.Exists(TemporaryFolder)) Directory.CreateDirectory(TemporaryFolder); _TemporaryFolder += @"\VE-PROMS"; if (!Directory.Exists(TemporaryFolder)) Directory.CreateDirectory(TemporaryFolder); } return _TemporaryFolder; } } public DocumentInfo MyDocument { get { return _MyDocument; } set { TryDelete(); _MyDocument = value; CreateFile(); } } public FileInfo MyFile { get { return _MyFile; } } public string Extension { get { return _Extension; } set { _Extension = value; } } #endregion #region Private Methods private void TryDelete() { if (_MyDocument == null) return; if (_MyFile == null) return; if (_MyFile.Exists) { try { _MyFile.Delete(); } catch (IOException ex) { _MyLog.Error("TryDelete",ex); } finally { _MyFile = null; _MyDocument = null; } } } private bool _Created = false; private int _Unique = 0; private string Unique { get { string retval = ""; if (_Unique != 0) retval = "_" + _Unique.ToString(); _Unique++; return retval; } } private void CreateFile() { while (!_Created) CreateTemporaryFile(); } private void CreateTemporaryFile() { try { if (_MyDocument != null) { _MyFile = new FileInfo(string.Format(@"{0}\tmp_{1}{2}{3}", TemporaryFolder, MyDocument.DocID, Unique, MyDocument.FileExtension)); FileStream fs = _MyFile.Create(); fs.Write(MyDocument.DocContent, 0, MyDocument.DocContent.Length); fs.Close(); _MyFile.CreationTime = _MyDocument.DTS; _MyFile.LastWriteTime = _MyDocument.DTS; _Created = true; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public string FullName { get { return _MyFile.FullName; } set { if(FullName != value) _MyFile = new FileInfo(value); } } public void SaveFile() { // TODO: Add Try & Catch logic if (_MyDocument == null) return; Document doc = _MyDocument.Get(); FileStream fs = _MyFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Byte[] buf = new byte[_MyFile.Length]; fs.Read(buf,0,buf.Length); fs.Close(); doc.FileExtension = MyFile.Extension; doc.DocContent = buf; doc.UpdateDocAscii(_MyFile.FullName); doc.UserID = Environment.UserName; doc.DTS = _MyFile.LastWriteTime; doc.Save(); } #endregion #region Constructors public DSOFile(DocumentInfo myDocument) { MyDocument = myDocument; } #endregion #region Destructor ~DSOFile() { Dispose(false); } public void Dispose() { Dispose(false); GC.SuppressFinalize(this); } protected void Dispose(bool disposing) { if (!_IsDisposed) { _IsDisposed = true; TryDelete(); } } #endregion } }