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"); } } if (roDbpath == null || roDbpath == "" || roDbpath.ToLower() == @"..\ro") roDbpath = fstPath.Substring(0, fstPath.LastIndexOf('\\')) + @"\ro"; // first see if this rodb has been migrated (another dataset may have migrated it) rodb = RODb.GetByFolderPath(roDbpath); if (rodb == null) { DirectoryInfo di = new DirectoryInfo(roDbpath); // 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"); ROFst rofst = ROFst.GetByRODbID_DTS(rodb.RODbID, fi.LastWriteTimeUtc); if (rofst == null) { // Next read in the rofst & make the rofst record. FileStream fsIn = new FileStream(fstPath + @"\ro.fst", 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. 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(); rofstinfo = ROFstInfo.Get(rofst.ROFstID); // refresh the info record! rofst.Dispose(); // Now load any images in... type 8: // Intergrated Graphics RO type if (rofstinfo.ROFSTLookup != null) { for (int i = 0; i < rofstinfo.ROFSTLookup.myHdr.myDbs.Length; i++) { // walk through the rofst 'database' searching for // all nodes that are integrated graphics, i.e. type 8. if (rofstinfo.ROFSTLookup.myHdr.myDbs[i].children != null) MigrateRoFstGraphics(roDbpath, rofstinfo.ROFSTLookup.myHdr.myDbs[i].children, rodb); } } } 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... ROImage roImg = null; //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); using (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(); roImg = ROImage.MakeROImage(rodb, imgname, ab, null, fi.LastWriteTimeUtc, "Migration"); } } // 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); } } }