From c16ebcf2edb470f2ae49738d2b8450d43472e276 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 15 Mar 2019 19:03:07 +0000 Subject: [PATCH] B2019-035 Added better memory management to the import procedure set functionality, with focus on importing Word documents and Library documents. Also, when a library document is added via an import, the modified date/time of the library document is appended to the library document name --- .../VEPROMS User Interface/dlgExportImport.cs | 94 ++++++++++++------- 1 file changed, 62 insertions(+), 32 deletions(-) diff --git a/PROMS/VEPROMS User Interface/dlgExportImport.cs b/PROMS/VEPROMS User Interface/dlgExportImport.cs index 8975e1ae..775cd6ca 100644 --- a/PROMS/VEPROMS User Interface/dlgExportImport.cs +++ b/PROMS/VEPROMS User Interface/dlgExportImport.cs @@ -709,6 +709,7 @@ namespace VEPROMS Old2NewItem = new Dictionary(); Old2NewContent = new Dictionary(); Old2NewLibDoc = new Dictionary(); + GetExistingLibDocsList(); // B2019-035 better memory management PendingTransitions = new XmlDocument(); XmlElement xe = PendingTransitions.CreateElement("transitions"); PendingTransitions.AppendChild(xe); @@ -738,6 +739,7 @@ namespace VEPROMS Old2NewItem = new Dictionary(); Old2NewContent = new Dictionary(); Old2NewLibDoc = new Dictionary(); + GetExistingLibDocsList(); // B2019-035 better memory management PendingTransitions = new XmlDocument(); XmlElement xe = PendingTransitions.CreateElement("transitions"); PendingTransitions.AppendChild(xe); @@ -777,6 +779,7 @@ namespace VEPROMS Old2NewItem = new Dictionary(); Old2NewContent = new Dictionary(); Old2NewLibDoc = new Dictionary(); + GetExistingLibDocsList(); // B2019-035 better memory management PendingTransitions = new XmlDocument(); XmlElement xe = PendingTransitions.CreateElement("transitions"); PendingTransitions.AppendChild(xe); @@ -866,6 +869,7 @@ namespace VEPROMS DirectoryInfo[] dis = di.GetDirectories(); for (int d = 0; d < dis.Length; d++) dis[d].Delete(true); + di.Delete(); lblImportStatus.Text = "Updating Transitions"; AddTransitions(); FixFloatingFoldouts(); @@ -1705,6 +1709,7 @@ namespace VEPROMS Old2NewItem = new Dictionary(); Old2NewContent = new Dictionary(); Old2NewLibDoc = new Dictionary(); + GetExistingLibDocsList(); // B2019-035 better memory management PendingTransitions = new XmlDocument(); FileInfo fi = new FileInfo(ofd.FileName); string dn; @@ -3336,6 +3341,7 @@ namespace VEPROMS private Dictionary Old2NewItem; private Dictionary Old2NewContent; private Dictionary Old2NewLibDoc; + private Dictionary ExistingLibDocs; // B2019-035 better memory management private XmlDocument PendingTransitions; private RODb MyRODb = null; @@ -3921,18 +3927,20 @@ namespace VEPROMS } private void AddAnnotations(int itemID, XmlNode xn) { - Item itm = Item.Get(itemID); - foreach (XmlNode nd in xn.SelectNodes("annotation")) + using (Item itm = Item.Get(itemID)) //B2019-035 better memory management { - int typeid = int.Parse(nd.Attributes.GetNamedItem("typeid").InnerText); - string rtftext = nd.Attributes.GetNamedItem("rtftext").InnerText; - string searchtext = nd.Attributes.GetNamedItem("searchtext").InnerText; - string config = nd.Attributes.GetNamedItem("config").InnerText; - string userid = nd.Attributes.GetNamedItem("userid").InnerText; - DateTime dts = DateTime.Parse(nd.Attributes.GetNamedItem("dts").InnerText); - AnnotationType annType = AnnotationType.GetByNameOrCreate(nd.Attributes.GetNamedItem("typename").InnerText); - Annotation ann = Annotation.MakeAnnotation(itm, annType, rtftext, searchtext, config, dts, userid); - ann.Save(); + foreach (XmlNode nd in xn.SelectNodes("annotation")) + { + int typeid = int.Parse(nd.Attributes.GetNamedItem("typeid").InnerText); + string rtftext = nd.Attributes.GetNamedItem("rtftext").InnerText; + string searchtext = nd.Attributes.GetNamedItem("searchtext").InnerText; + string config = nd.Attributes.GetNamedItem("config").InnerText; + string userid = nd.Attributes.GetNamedItem("userid").InnerText; + DateTime dts = DateTime.Parse(nd.Attributes.GetNamedItem("dts").InnerText); + AnnotationType annType = AnnotationType.GetByNameOrCreate(nd.Attributes.GetNamedItem("typename").InnerText); + Annotation ann = Annotation.MakeAnnotation(itm, annType, rtftext, searchtext, config, dts, userid); + ann.Save(); + } } } private void AddParts(XmlNode myNode, ItemInfo parentInfo) @@ -4566,7 +4574,6 @@ namespace VEPROMS lblImportSection.Text = string.Format("{0} of {1} Sections", pbImportSection.Value.ToString(), pbImportSection.Maximum.ToString()); Application.DoEvents(); XmlNode xc = xn.SelectSingleNode("content"); - Section sect; string number = xc.Attributes.GetNamedItem("number").InnerText; string text = xc.Attributes.GetNamedItem("text").InnerText; int sectiontype = int.Parse(xc.Attributes.GetNamedItem("type").InnerText); @@ -4577,7 +4584,7 @@ namespace VEPROMS CheckForFloatingFoldout(contentid, config); string userid = xc.Attributes.GetNamedItem("userid").InnerText; DateTime dts = DateTime.Parse(xc.Attributes.GetNamedItem("dts").InnerText); - using (sect = Section.MakeSection(parentInfo, prevInfo, number, text, sectiontype)) + using (Section sect = Section.MakeSection(parentInfo, prevInfo, number, text, sectiontype)) // B2019-035 better memory management { sect.DTS = dts; sect.UserID = userid; @@ -4750,9 +4757,11 @@ namespace VEPROMS XmlNode nd = xc.SelectSingleNode("entry"); string userid = nd.Attributes.GetNamedItem("userid").InnerText; DateTime dts = DateTime.Parse(nd.Attributes.GetNamedItem("dts").InnerText); - Document dd = AddDocument(nd.SelectSingleNode("document")); - Entry ee = Entry.MakeEntry(content, dd, dts, userid); - ee.Save(); + using (Document dd = AddDocument(nd.SelectSingleNode("document"))) // B2019-035 better memory management + { + Entry ee = Entry.MakeEntry(content, dd, dts, userid); + ee.Save(); + } } // jsj 2016Feb16 - This appears to not be used //private void AddDocument(XmlReader xr, Dictionary dic) @@ -4772,36 +4781,57 @@ namespace VEPROMS // userid = dic["userid"]; // Entry ee = Entry.MakeEntry(content, dd, dts, userid); //} + + // B2019-035 This is called only once to get the existing library document info. + // when it was in the AddDocument() logic, it would use up (and not free up) memory each time + // a Word section was added during the import + private void GetExistingLibDocsList() + { + ExistingLibDocs = new Dictionary(); + using (DocumentInfoList dil = DocumentInfoList.GetLibraries(true)) + { + foreach (DocumentInfo di in dil) + { + string keystr = di.LibTitle+"_"+di.DTS.ToString(); + if (!ExistingLibDocs.ContainsKey(keystr)) + ExistingLibDocs.Add(keystr, di.DocID); + di.Dispose(); + } + } + + } private Document AddDocument(XmlNode xn) { - Document d; + Document d=null; int docid = int.Parse(xn.Attributes.GetNamedItem("docid").InnerText); if (Old2NewLibDoc.ContainsKey(docid)) { docid = Old2NewLibDoc[docid]; d = Document.Get(docid); } - else + else // see if it is in the existing database { string libtitle = xn.Attributes.GetNamedItem("libtitle").InnerText; DateTime dts = DateTime.Parse(xn.Attributes.GetNamedItem("dts").InnerText); if (libtitle != "") { - DocumentInfoList dil = DocumentInfoList.GetLibraries(true); - foreach (DocumentInfo di in dil) - { - if (di.LibTitle == libtitle && di.DTS == dts) - return di.Get(); // found library document in exiting database - } + // B2019-035 better memory management. Prior logic would eat up memory (and not free it). + string libkey = libtitle + "_" + dts.ToString(); + if (ExistingLibDocs.ContainsKey(libkey)) + d = Document.Get(ExistingLibDocs[libkey]); // found library document in existing database + } + if (d == null) // not found in existing database, create it + { + byte[] doccontent = Convert.FromBase64String(xn.Attributes.GetNamedItem("doccontent").InnerText); + string docascii = xn.Attributes.GetNamedItem("docascii").InnerText; + string config = xn.Attributes.GetNamedItem("config").InnerText; + string userid = xn.Attributes.GetNamedItem("userid").InnerText; + string fileextension = xn.Attributes.GetNamedItem("fileextension").InnerText; + if (libtitle != "") libtitle = libtitle + "_" + dts.ToString(); // if a lib document, append the date/time to the title incase there are duplicate titles + d = Document.MakeDocument(libtitle, doccontent, docascii, config, dts, userid, fileextension); + d.Save(); + Old2NewLibDoc.Add(docid, d.DocID); } - byte[] doccontent = Convert.FromBase64String(xn.Attributes.GetNamedItem("doccontent").InnerText); - string docascii = xn.Attributes.GetNamedItem("docascii").InnerText; - string config = xn.Attributes.GetNamedItem("config").InnerText; - string userid = xn.Attributes.GetNamedItem("userid").InnerText; - string fileextension = xn.Attributes.GetNamedItem("fileextension").InnerText; - d = Document.MakeDocument(libtitle, doccontent, docascii, config, dts, userid, fileextension); - d.Save(); - Old2NewLibDoc.Add(docid, d.DocID); } return d; }