using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; using System.Text.RegularExpressions; using VEPROMS.CSLA.Library; using Config; namespace DataLoader { public partial class Loader { public void MigrateROFST(string fstPath, DocVersion docver) { //if (!File.Exists(fstPath + @"\ro.fst")) //{ // log.ErrorFormat("RO FST Does not exist: {0}", fstPath); // return; //} // get the proc.ini value for the ropath. If it doesn't exist, use "..\" ConfigFile cfg = new ConfigFile(); XmlDocument d = cfg.IniToXml(fstPath + @"\proc.ini"); string roDbpath = null; string sp_prefix = null; string img_prefix = null; if (d != null) { XmlElement xml = d.DocumentElement; XmlNode rodef = xml.SelectSingleNode("//RODefaults"); XmlElement ele = rodef as XmlElement; //string tmp = null; if (ele != null) { roDbpath = ele.GetAttribute("ROPATH"); sp_prefix = ele.GetAttribute("Setpoint"); img_prefix = ele.GetAttribute("Graphics"); } } DirectoryInfo di2 = new DirectoryInfo(fstPath); roDbpath = roDbpath ?? ""; if (roDbpath == "") roDbpath = @"..\ro"; if (roDbpath.StartsWith(@"..\")) { while (roDbpath.StartsWith(@"..\")) { di2 = di2.Parent; roDbpath = roDbpath.Substring(3); } DirectoryInfo[] dis = di2.GetDirectories(roDbpath); if (dis.Length > 0) roDbpath = dis[0].FullName; } //if (roDbpath == null || roDbpath == "") // roDbpath = fstPath.Substring(0, fstPath.LastIndexOf('\\')) + @"\ro"; // first see if this rodb has been migrated (another dataset may have migrated it) DirectoryInfo di = new DirectoryInfo(roDbpath); rodb = RODb.GetByFolderPath(di.FullName); if (rodb == null) { // There may be more than 1 'ro' as the 'ROName' field (ROName is derived from the ropath). // Get new name be incrementing, if so. string newname = NewROName(di.Name); // If there is default graphic file extension data in the roapp.ini, use this as config // data for the rodb, otherwise set it to 'cstring' for now. cfg = new ConfigFile(); d = cfg.LoadRoAppIni(roDbpath); rodb = RODb.MakeRODb(di.Name, di.FullName, "cstring", d==null?null:d.InnerXml); } // now see if the same ro.fst has been migrated. To determine this check if a record // exists for the rodb and with the same dts as the file. FileInfo fi = new FileInfo(fstPath + @"\ro.fst"); if (!fi.Exists) fi = new FileInfo(roDbpath + @"\ro.fst"); ROFst rofst = ROFst.GetByRODbID_DTS(rodb.RODbID, fi.LastWriteTimeUtc); if (rofst == null) // if not already in our database, then add it. { // Next read in the rofst & make the rofst record. FileStream fsIn = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); // Create an instance of StreamReader that can read characters from the FileStream. BinaryReader r = new BinaryReader(fsIn); byte[] ab = r.ReadBytes((int)fsIn.Length); //r.Close(); fsIn.Close(); // get the date time stamp, need to create the record with the file's dts. rofst = ROFst.MakeROFst(rodb, ab, null, fi.LastWriteTimeUtc, "Migration"); } // Next hook the rofst to the docversion using the associations table. bool appending = false; if (docver.DocVersionAssociationCount == 0) { DocVersionAssociation dva = docver.DocVersionAssociations.Add(rofst); AssociationConfig assoc_cfg = new AssociationConfig(); assoc_cfg.RODefaults_graphicsprefix = img_prefix; assoc_cfg.RODefaults_setpointprefix = sp_prefix; dva.Config = assoc_cfg.ToString(); docver.Save(); rofst.Dispose(); } else appending = true; rofstinfo = ROFstInfo.Get(rofst.ROFstID); // refresh the info record! // Now load any images in... type 8: // Intergrated Graphics RO type ROFSTLookup mylookup = rofstinfo.GetROFSTLookup(DocVersionInfo.Get(docver.VersionID)); if (!appending && mylookup != null) { for (int i = 0; i < mylookup.myHdr.myDbs.Length; i++) { // walk through the rofst 'database' searching for // all nodes that are integrated graphics, i.e. type 8. if (mylookup.myHdr.myDbs[i].children != null) MigrateRoFstGraphics(roDbpath, mylookup.myHdr.myDbs[i].children, rodb); } } ROImageInfo.FlagImagesZipped(); } private string NewROName(string roName) { string retval = roName; int iSuffix = -1; RODbInfoList rodblist = RODbInfoList.Get(); foreach (RODbInfo rdi in rodblist) { if (rdi.ROName.StartsWith(roName)) { if (rdi.ROName == roName) iSuffix = 0; else if (Regex.IsMatch(rdi.ROName, roName + "[_][0-9]+")) { int ii = int.Parse(rdi.ROName.Substring(1 + roName.Length)); if (ii > iSuffix) iSuffix = ii; } } } if (iSuffix >= 0) retval = string.Format("{0}_{1}", roName, iSuffix + 1); return retval; } private void MigrateRoFstGraphics(string rodbpath, ROFSTLookup.rochild[] rochild, RODb rodb) { for (int i = 0; i < rochild.Length; i++) { if (rochild[i].type == 8) AddGraphic(rodbpath, rochild[i].value, rodb); if (rochild[i].children != null) MigrateRoFstGraphics(rodbpath, rochild[i].children, rodb); } } private void AddGraphic(string rodbpath, string p, RODb rodb) { if (p == null) return; string imgname = p.Substring(0, p.IndexOf('\n')); int thedot = imgname.LastIndexOf('.'); string fname = imgname; if (thedot == -1 || (thedot != (imgname.Length - 4))) { RODbConfig roDbCfg = new RODbConfig(rodb.Config); fname += string.Format(".{0}", roDbCfg.GetDefaultGraphicExtension()); } string imgfile = rodbpath + @"\" + fname; if (File.Exists(imgfile)) { FileInfo fi = new FileInfo(imgfile); frmMain.Status = "Processing Image " + fname; // if the roimage record exists, don't create a new one... //Console.WriteLine("ROImage Key {0} List {1} ID {2}", ROImage.CacheCountPrimaryKey, ROImage.CacheCountList, ROImage.CacheCountByRODbID); //Console.WriteLine("ROImageInfo Key {0} List {1}", ROImageInfo.CacheCountPrimaryKey, ROImageInfo.CacheCountList); //Console.WriteLine("ROFst Key {0} List {1} ID {2}", ROFst.CacheCountPrimaryKey, ROFst.CacheCountList, ROFst.CacheCountByRODbID_DTS); //Console.WriteLine("ROFstInfo Key {0} List {1}", ROFstInfo.CacheCountPrimaryKey, ROFstInfo.CacheCountList); frmMain.AddInfo("Adding RO Image {0}", imgname); //using (roImg = ROImage.GetByRODbID_FileName_DTS(rodb.RODbID, imgname, fi.LastWriteTimeUtc)) //{ ROImage roImg = null; roImg = GetImageFromAvailable(rodb.RODbID, imgname, fi.LastWriteTimeUtc); if (roImg == null) { roImg = ROImage.GetByRODbID_FileName_DTS(rodb.RODbID, imgname, fi.LastWriteTimeUtc); if (roImg == null) { using (FileStream fsIn = new FileStream(imgfile, FileMode.Open, FileAccess.Read, FileShare.Read)) { // Create an instance of StreamReader that can read characters from the FileStream. BinaryReader r = new BinaryReader(fsIn); byte[] ab = r.ReadBytes((int)fsIn.Length); r.Close(); fsIn.Close(); ConfigInfo ci = new ConfigInfo(null); ci.AddItem("Image", "Size", ab.Length.ToString()); roImg = ROImage.MakeROImage(rodb, imgname, ROImageInfo.Compress(ab), ci.ToString(), fi.LastWriteTimeUtc, "Migration"); } } AddToAvailable(roImg, rodb.RODbID, imgname, fi.LastWriteTimeUtc); } // see if it's already linked to the current rofst.. //Figure figure = Figure.GetByROFstID_ImageID(rofstinfo.ROFstID, roImg.ImageID); using (Figure figure = Figure.GetByROFstID_ImageID(rofstinfo.ROFstID, roImg.ImageID)) { if (figure != null) return; //using (ROFst rofst = rofstinfo.Get()) ROFst rofst = rofstinfo.Get(); // Use the cached ROFST rather than creating and disposing //figure = Figure.MakeFigure(rofst, roImg, null); using (Figure tfig = Figure.MakeFigure(rofst, roImg, null)) ; } //roImg.Dispose(); } else frmMain.AddError("Cannot Find Image File {0}", imgfile); } private void AddToAvailable(ROImage roImg, int dbid, string imgname, DateTime dateTime) { string key = AvailableKey(dbid, imgname, dateTime); _AvaiableROImages.Add(key, roImg); } private string AvailableKey(int dbid, string imgname, DateTime dateTime) { return string.Format("{0}|{1}|{2}", dbid, imgname, dateTime.ToString("yyyyMMdd HHmmss")); } private ROImage GetImageFromAvailable(int dbid, string imgname, DateTime dateTime) { string key = AvailableKey(dbid, imgname, dateTime); if (_AvaiableROImages.ContainsKey(key)) return _AvaiableROImages[key]; return null; } private Dictionary _AvaiableROImages = new Dictionary(); } }