diff --git a/PROMS/VEPROMS.CSLA.Library/Extension/ROFSTExt.cs b/PROMS/VEPROMS.CSLA.Library/Extension/ROFSTExt.cs index ba4f0675..7bf153c1 100644 --- a/PROMS/VEPROMS.CSLA.Library/Extension/ROFSTExt.cs +++ b/PROMS/VEPROMS.CSLA.Library/Extension/ROFSTExt.cs @@ -232,7 +232,7 @@ namespace VEPROMS.CSLA.Library using (ROImageInfoList myROImages = ROImageInfoList.GetByRODbID(rdi.RODbID)) { Dictionary myExistingROImages = BuildROImagesList(myROImages); - List myChangedROImages = new List(); + List myUnChangedROImages = new List(); List myAddedROImages = new List(); rofst = ROFst.MakeROFst(rodb, ab, null, di.LastWriteTimeUtc, rdi.UserID); // Hook this into the current docversion by replacing the rofstid field in the doc version @@ -248,12 +248,12 @@ namespace VEPROMS.CSLA.Library { using (ROFstInfo rfi = ROFstInfo.Get(rofst.ROFstID)) { - rfi.MigrateRoFstGraphics(rdi, rofst.ROFSTLookup.myHdr.myDbs[i].children, rodb, rofst, myExistingROImages, myChangedROImages,myAddedROImages);// TODO: Need to add MyImages + rfi.MigrateRoFstGraphics(rdi, rofst.ROFSTLookup.myHdr.myDbs[i].children, rodb, rofst, myExistingROImages, myUnChangedROImages, myAddedROImages);// TODO: Need to add MyImages } } } - if(myChangedROImages.Count > 0) - using(FigureInfoList fil = FigureInfoList.AddByROFstIDImageIDs(rofst.ROFstID,buildImageIDString(myChangedROImages))); + if (myUnChangedROImages.Count > 0) + using (FigureInfoList fil = FigureInfoList.AddByROFstIDImageIDs(rofst.ROFstID, buildImageIDString(myUnChangedROImages))) ; return rofst; } } @@ -300,7 +300,7 @@ namespace VEPROMS.CSLA.Library using (ROImageInfoList myROImages = ROImageInfoList.GetByRODbIDNoData(rdi.RODbID)) { Dictionary myExistingROImages = BuildROImagesList(myROImages); - List myChangedROImages = new List(); + List myUnChangedROImages = new List(); List myAddedROImages = new List(); using (ROFstInfo rfi = ROFstInfo.Get(rofst.ROFstID)) { @@ -309,12 +309,13 @@ namespace VEPROMS.CSLA.Library // walk through the rofst 'database' searching for all nodes that are integrated graphics, type 8: if (rofst.ROFSTLookup.myHdr.myDbs[i].children != null) { - rfi.MigrateRoFstGraphics(rdi, rofst.ROFSTLookup.myHdr.myDbs[i].children, rodb, rofst, myExistingROImages, myChangedROImages,myAddedROImages); + rfi.MigrateRoFstGraphics(rdi, rofst.ROFSTLookup.myHdr.myDbs[i].children, rodb, rofst, myExistingROImages, myUnChangedROImages, myAddedROImages); } } } - if (myChangedROImages.Count > 0) - using(FigureInfoList fil = FigureInfoList.AddByROFstIDImageIDs(rofst.ROFstID,buildImageIDString(myChangedROImages))); + // Add references for existing RO Images to the Figure table (Figure table relates ro.fst with its images) + if (myUnChangedROImages.Count > 0) + using (FigureInfoList fil = FigureInfoList.AddByROFstIDImageIDs(rofst.ROFstID, buildImageIDString(myUnChangedROImages))) ; } // Now update the usages: compare old to new rofsts and update usages accordingly, i.e. modified // values, deleted ros, etc. @@ -498,15 +499,15 @@ namespace VEPROMS.CSLA.Library retval = string.Format("{0}_{1}", roName, iSuffix + 1); return retval; } - private void MigrateRoFstGraphics(RODbInfo rdi, ROFSTLookup.rochild[] rochild, RODb rodb, ROFst rofst, Dictionary myExistingROImages, List myChangedROImages, List myAddedROImages) + private void MigrateRoFstGraphics(RODbInfo rdi, ROFSTLookup.rochild[] rochild, RODb rodb, ROFst rofst, Dictionary myExistingROImages, List myUnChangedROImages, List myAddedROImages) { for (int i = 0; i < rochild.Length; i++) { - if (rochild[i].type == 8) this.AddGraphic(rdi, rochild[i].value, rodb, rofst, myExistingROImages, myChangedROImages, myAddedROImages); - if (rochild[i].children != null) this.MigrateRoFstGraphics(rdi, rochild[i].children, rodb, rofst, myExistingROImages, myChangedROImages, myAddedROImages); + if (rochild[i].type == 8) this.AddGraphic(rdi, rochild[i].value, rodb, rofst, myExistingROImages, myUnChangedROImages, myAddedROImages); + if (rochild[i].children != null) this.MigrateRoFstGraphics(rdi, rochild[i].children, rodb, rofst, myExistingROImages, myUnChangedROImages, myAddedROImages); } } - private void AddGraphic(RODbInfo rdi, string p, RODb rodb, ROFst rofst, Dictionary myExistingROImages, List myChangedROImages, List myAddedROImages) + private void AddGraphic(RODbInfo rdi, string p, RODb rodb, ROFst rofst, Dictionary myExistingROImages, List myUnChangedROImages, List myAddedROImages) { if (p == null) return; string imgname = p.Substring(0, p.IndexOf('\n')); @@ -529,8 +530,8 @@ namespace VEPROMS.CSLA.Library if (myExistingROImages.ContainsKey(key)) { int imageID = myExistingROImages[key]; - if(!myChangedROImages.Contains(imageID)) - myChangedROImages.Add(imageID); + if (!myUnChangedROImages.Contains(imageID)) + myUnChangedROImages.Add(imageID); } else { @@ -541,7 +542,17 @@ namespace VEPROMS.CSLA.Library byte[] ab = r.ReadBytes((int)fsIn.Length); r.Close(); fsIn.Close(); - using (roImg = ROImage.MakeROImage(rodb, imgname, ab, null, fi.LastWriteTimeUtc, "Migration")) + byte[] cmp = ROImageInfo.Compress(ab); + int existingImageID = FindExisting(myExistingROImages, imgname, imgfile, cmp, ab); + if (existingImageID != 0) + { + if (!myUnChangedROImages.Contains(existingImageID)) + myUnChangedROImages.Add(existingImageID); + return; + } + ROImageConfig rc = new ROImageConfig(); + rc.Image_Size = ab.Length.ToString(); + using (roImg = ROImage.MakeROImage(rodb, imgname, ROImageInfo.Compress(ab), rc.ToString(), fi.LastWriteTimeUtc, "Migration")) { using (Figure figure = Figure.GetByROFstID_ImageID(this.ROFstID, roImg.ImageID)) { @@ -555,6 +566,57 @@ namespace VEPROMS.CSLA.Library } } + private int FindExisting(Dictionary myExistingROImages, string imgname, string imgfile, byte[] cmpFileContents, byte[] fileContents) + { + // the reason the following check is necessary is that the DTS can be off by hours + // because of the way windows handles Daylight savings time for file DTS. + + FileInfo fi = new FileInfo(imgfile); + DateTime dts = fi.LastWriteTimeUtc; + + // loop through myExistingROImages looking for matching name. + foreach (string key in myExistingROImages.Keys) + { + // if found, compare the date/time stamp + string part1 = Regex.Replace(key, ":.*$", ""); + string part2 = Regex.Replace(key, "^.*?:", ""); + if (part1.ToUpper() == imgname.ToUpper()) + { + // if date/time stamps matches date, minutes & seconds, compare contents + string cmp1 = Regex.Replace(dts.ToString(), " [0-9]*:", " x:"); + cmp1 = Regex.Replace(cmp1, " [AP]", " x"); + string cmp2 = Regex.Replace(part2, " [0-9]*:", " x:"); + cmp2 = Regex.Replace(cmp2, " [AP]", " x"); + if (cmp1==cmp2) + { + // compare contents + int imgId = myExistingROImages[key]; + using (ROImageInfo roii = ROImageInfo.Get(imgId)) + { + ROImageConfig roicfg = new ROImageConfig(roii); + int size = Convert.ToInt32(roicfg.Image_Size); + byte[] tmpb = ROImageInfo.Decompress(roii.Content, size); + + if (ByteArrayCompare(roii.Content, cmpFileContents)) + return imgId; + if (ByteArrayCompare(tmpb, fileContents)) + return imgId; + } + } + } + } + return 0; + } + + private bool ByteArrayCompare(byte[] ba1, byte[] ba2) + { + // we wrote our own byte array comparinson because the system's 'equals' did not work! + if (ba1.Length != ba2.Length) return false; + for (int i = 0; i < ba1.Length; i++) + if (ba1[i] != ba2[i]) return false; + return true; + } + private ROImageInfo GetMyImage(Dictionary myImages, string imgname, DateTime dts) { string key = string.Format("{0}:{1}", imgname, dts);