/********************************************************************************************* * Copyright 2004 - Volian Enterprises, Inc. All rights reserved. * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE * ------------------------------------------------------------------------------ * $Workfile: LibDoc.cs $ $Revision: 6 $ * $Author: Kathy $ $Date: 5/31/06 9:47a $ * * $History: LibDoc.cs $ * * ***************** Version 6 ***************** * User: Kathy Date: 5/31/06 Time: 9:47a * Updated in $/LibSource/VEObject * Fixed B2006-023 - library document usages, duplicates and missing * procedure titles * * ***************** Version 5 ***************** * User: Kathy Date: 2/08/06 Time: 10:22a * Updated in $/LibSource/VEObject * Fix B2006-004 - usages not found * * ***************** Version 4 ***************** * User: Kathy Date: 5/31/05 Time: 12:42p * Updated in $/LibSource/VEObject * crash on save if no comment * * ***************** Version 3 ***************** * User: Kathy Date: 5/25/05 Time: 10:32a * Updated in $/LibSource/VEObject * Allow edits for tmpchg * * ***************** Version 2 ***************** * User: Kathy Date: 1/31/05 Time: 11:05a * Updated in $/LibSource/VEObject * Fix B2005-005 - this just fixed empty icon usage * * ***************** Version 1 ***************** * User: Kathy Date: 7/27/04 Time: 8:53a * Created in $/LibSource/VEObject *********************************************************************************************/ using System; using System.IO; using System.Windows.Forms; using System.ComponentModel; using System.Collections; using System.Data; using VDB_TransUsage; using Utils; namespace VEObject { /// /// Summary description for LibDoc. /// public class VEO_LibDoc : VEO_Base { string _Comment; public ArrayList usages; public string ldname; private string tmpRtfFileName; protected bool changeTitle; protected bool changeComment; protected string tmpTitle; protected string tmpComment; public VEO_LibDoc(string ipath, VEO_DummyLibDoc ld, bool inew) { iconStates = new int[5] {15,15,15,15,15}; string loctitle=null; string loccomment=null; _Location = ipath; VEObjectType = (int)VEObjectTypesDefs.LibraryDoc; parentObj = ld; usages = new ArrayList(); isNew = inew; // use the path to open the file & read the title & comment FileInfo fi = new FileInfo(ipath); FileStream fs=null; fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite ); BinaryReader br = new BinaryReader(fs,System.Text.ASCIIEncoding.ASCII); int cntPage = br.ReadInt16(); if(cntPage != -1) { // Not End of File int nchar = br.ReadInt16(); string tmp = new string(br.ReadChars(nchar)); loctitle = tmp.Substring(0,tmp.Length-1); // remove null at end. nchar = br.ReadInt16(); if (nchar>0) { tmp = new String(br.ReadChars(nchar)); loccomment = tmp.Substring(0,tmp.Length-1); // remove null at end. } } br.Close(); fs.Close(); if (loctitle!=null) _Title = loctitle; else _Title=ipath; _Comment = loccomment; VEO_Base tmp1 = (VEO_Base) ld.parentObj; usrRunTime = tmp1.usrRunTime; string pthnm = this._Location.Substring(0,_Location.ToUpper().IndexOf(".LIB")); ldname = pthnm.Substring(_Location.LastIndexOf("\\")+1,8); LoadLockInfo(); icon = iconStates[(int) Lock.LockStatus]; } [Description("Location"),Category("Library Document"),ReadOnly(true)]public string Location { get{return _Location;} set{_Location=value;} } [Description("Title"),Category("Library Document")]public string Title { get { if(!changeTitle) return _Title; else return tmpTitle; } set { changeTitle=true; tmpTitle=value; } } [Description("Comment"),Category("Library Document")]public string Comment { get { if(!changeComment) return _Comment; else return tmpComment; } set { changeComment=true; tmpComment=value; } } public override bool PropertiesDlg(Object parent) { LDProperties propdlg = new LDProperties(parent, this); if (propdlg.ShowDialog() == DialogResult.OK) return true; // for New libdoc, a file was created in order for dialog to work, // remove it on cancel. if (this.isNew && File.Exists(_Location)) File.Delete(_Location); return false; } public void GetUsages() { usages.Clear(); VEO_DummyLibDoc dl = (VEO_DummyLibDoc) this.parentObj; VEO_ProcSet ps = (VEO_ProcSet) dl.parentObj; string pth = ps._curDirectory+"\\"+ps.Location+"\\tran.dbf"; vdb_TransUsage transusg = new vdb_TransUsage(pth); DataSet ds = transusg.GetSortedByToTrans("[TONUMBER] = \'"+ldname.ToUpper() + "\' OR [TONUMBER] = \'" + ldname.ToLower()+"\'"); VEO_DummySet dset = (VEO_DummySet) ps.Children[1]; if (ds.Tables[0].Rows.Count !=0) { foreach(DataRow dr in ds.Tables[0].Rows) { string usg = dr["FROMNUMBER"].ToString(); string titl = null; if (dset.Children.Count==0) dset.Read(false); foreach(Object obj in dset.Children) { VEO_Proc prc = (VEO_Proc) obj; if (usg == prc._Prcnum) { titl = prc._Title; break; } } usages.Add(usg + " " + titl); } } transusg = null; } public override bool SaveNew(string dummy) { // since the file was created before creating this object, use the 'Write' method // to save it. bool success = Write(); isNew=false; // the parent icon cannot be empty. VEO_DummyLibDoc ld = (VEO_DummyLibDoc) parentObj; ld.icon = ld.iconStates[0]; ld.IsEmpty=false; return success; } public override bool Write() { string origfile=null; if (isNew||changeTitle||changeComment) { try { // rename file extension to bak & rewrite it. origfile = _Location.Substring(0,_Location.Length-3) + "BAK"; File.Delete(origfile); File.Move(_Location,origfile); FileStream chgfile = new FileStream(_Location, FileMode.Create); BinaryWriter bw = new BinaryWriter(chgfile,System.Text.Encoding.ASCII); // Write out header to the new file short j=0; bw.Seek(0,SeekOrigin.Begin); bw.Write(j); // temporary page count value (reset on pagination) j=(short)(Title.Length+1); bw.Write(j); char [] xbuf = new char[Title.Length]; xbuf = Title.ToCharArray(); bw.Write(xbuf); bw.Write((byte)0); // add null for end of string j=0; if (Comment != null) j=(short)(Comment.Length+1); bw.Write(j); if(j>0) { xbuf = new char[Comment.Length]; xbuf = Comment.ToCharArray(); bw.Write(xbuf); bw.Write((byte)0); } // Now open the original file for reading data, get past the header info // first. FileStream oldfile = new FileStream(origfile, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(oldfile,System.Text.ASCIIEncoding.ASCII); int cntPage = br.ReadInt16(); if(cntPage != -1) { // Not End of File int nchar = br.ReadInt16(); string tmp = new string(br.ReadChars(nchar)); nchar = br.ReadInt16(); if (nchar>0)tmp = new String(br.ReadChars(nchar)); } byte ac; bool done = false; while(done==false) { if (br.PeekChar() >0) { ac = br.ReadByte(); bw.Write(ac); } else done=true; } br.Close(); bw.Close(); oldfile.Close(); chgfile.Close(); } catch (Exception e) { MessageBox.Show(e.Message,"Error on library document save."); if (origfile!=null) { File.Delete(_Location); File.Move(origfile,_Location); } return false; } if(origfile!=null)File.Delete(origfile); if(changeComment) { _Comment=tmpComment; changeComment=false; } if(changeTitle) { _Title=tmpTitle; changeTitle=false; } } return true; } private bool canDelete() { if (usages.Count == 0) GetUsages(); if (usages.Count > 0) { MessageBox.Show("Cannot delete library document, it is used."); return false; } return true; } public override bool Delete() { // first see if the library document can be deleted (if it is used, it cannot // be deleted. if (canDelete()==false) return false; File.Delete(_Location); // if this is the only library document. then set the parents, icon to empty VEO_DummyLibDoc ld = (VEO_DummyLibDoc) this.parentObj; if (ld.Children.Count==1) IsEmpty=true; // just this one. return true; } public string GetContentsFile() { return tmpRtfFileName; } public void ClearContentsFile() { if(tmpRtfFileName!=null)File.Delete(tmpRtfFileName); tmpRtfFileName=null; } public bool ReadContentsFile() { try { // Open the file for reading data, get past the header info first. FileStream ldfile = new FileStream(_Location, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(ldfile,System.Text.ASCIIEncoding.ASCII); 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) { int nchar = br.ReadInt16(); string tmp = new string(br.ReadChars(nchar)); nchar = br.ReadInt16(); if (nchar>0)tmp = new String(br.ReadChars(nchar)); } byte ac; bool done = false; while(done==false) { if (br.PeekChar() >0) { ac = br.ReadByte(); bw.Write(ac); } else done=true; } br.Close(); ldfile.Close(); bw.Close(); tmpfile.Close(); } catch (Exception e) { MessageBox.Show(e.Message,"Error reading Library Document"); return false; } return true; } public override void AddToTree(TreeNode parentnd) { } public override bool Expand(TreeNode parentnd) { return true; } public override bool mnuAllowNew() { return false; } public override bool mnuAllowDelete() { return AllowMods(); } public override bool canEdit() { return AllowMods(); } private bool AllowMods() { VEO_DummyLibDoc ds = (VEO_DummyLibDoc) this.parentObj; VEO_ProcSet ps = (VEO_ProcSet) ds.parentObj; if (ps.isApproved == true) return false; if (amILockedByMe()==false) return false; if ((ps.accessFlags&Security.LIBRARYDOCS)==Security.LIBRARYDOCS) return true; return false; } } }