286 lines
9.0 KiB
C#
286 lines
9.0 KiB
C#
// ========================================================================
|
|
// Copyright 2006 - Volian Enterprises, Inc. All rights reserved.
|
|
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
|
|
// ------------------------------------------------------------------------
|
|
// $Workfile: $ $Revision: $
|
|
// $Author: $ $Date: $
|
|
//
|
|
// $History: $
|
|
// ========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
using System.Xml;
|
|
using System.Xml.XPath;
|
|
using System.Text.RegularExpressions;
|
|
//using Config;
|
|
|
|
namespace VEPROMS.CSLA.Library
|
|
{
|
|
public partial class ROFst
|
|
{
|
|
[NonSerialized]
|
|
private ROFSTLookup _ROFSTLookup;
|
|
public ROFSTLookup ROFSTLookup
|
|
{
|
|
get
|
|
{
|
|
if (_ROFSTLookup == null)
|
|
{
|
|
_ROFSTLookup = new ROFSTLookup(this);
|
|
}
|
|
return _ROFSTLookup;
|
|
}
|
|
}
|
|
}
|
|
public partial class ROFstInfo
|
|
{
|
|
#region Log4Net
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
#endregion
|
|
#region PropertiesAndData
|
|
private DocVersion _docVer;
|
|
public DocVersion docVer
|
|
{
|
|
get
|
|
{
|
|
if (_docVer == null) _docVer = DocVersion.Get(this.ROFstAssociations[0].MyDocVersion.VersionID);
|
|
return _docVer;
|
|
}
|
|
set
|
|
{
|
|
_docVer = value;
|
|
|
|
}
|
|
}
|
|
[NonSerialized]
|
|
private ROFSTLookup _ROFSTLookup;
|
|
public ROFSTLookup ROFSTLookup
|
|
{
|
|
get
|
|
{
|
|
if (_ROFSTLookup == null)
|
|
{
|
|
_ROFSTLookup = new ROFSTLookup(this);
|
|
}
|
|
return _ROFSTLookup;
|
|
}
|
|
}
|
|
#endregion
|
|
#region AppSupport
|
|
//public static ROImageInfo Get(RODbInfo rodbinfo, string filename)
|
|
//{
|
|
// if (rodbinfo.RODbROImageCount != 0)
|
|
// {
|
|
// foreach (ROImageInfo ri in rodbinfo.RODbROImages)
|
|
// {
|
|
// if (ri.FileName == filename) return ri;
|
|
// }
|
|
// }
|
|
// return null;
|
|
//}
|
|
public string GetDefaultROPrefix()
|
|
{
|
|
if (docVer != null)
|
|
return docVer.DocVersionConfig.RODefaults_setpointprefix;
|
|
else
|
|
return "SP1"; // Not Sure about this...
|
|
}
|
|
public string GetDefaultGraphicsPrefix()
|
|
{
|
|
if (docVer != null)
|
|
return docVer.DocVersionConfig.RODefaults_graphicsprefix;
|
|
else
|
|
return "IG1"; // Not Sure about this...
|
|
}
|
|
#region Add New Ro Fst
|
|
/// <summary>
|
|
/// Adds an ro.fst into a sql database.
|
|
/// </summary>
|
|
/// <param name="rdi" - the Rodb to use as the path for updating the ro.fst, i.e.
|
|
/// import from there.
|
|
/// <param name="docver" - hook into this doc version></param>
|
|
/// <returns>ROFst: Returns the created rofst object</returns>
|
|
public static ROFst AddRoFst(RODbInfo rdi, DocVersion docver)
|
|
{
|
|
string rofstfilepath = rdi.FolderPath + @"\ro.fst";
|
|
DirectoryInfo di = new DirectoryInfo(rdi.FolderPath);
|
|
|
|
// check if this rofst has been loaded, i.e. dts on file versus dts in db...
|
|
// if so, just make association with docversion.
|
|
ROFst rofst = ROFst.GetByRODbID_DTS(rdi.RODbID, di.LastWriteTime);
|
|
if (rofst != null)
|
|
{
|
|
docver.DocVersionAssociations.Add(rofst);
|
|
docver.Save();
|
|
return rofst;
|
|
}
|
|
|
|
// Next read in the rofst & make the rofst record.
|
|
FileStream fsIn = new FileStream(rofstfilepath, 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);
|
|
fsIn.Close();
|
|
|
|
using (RODb rd = RODb.Get(rdi.RODbID))
|
|
{
|
|
rofst = ROFst.MakeROFst(rd, ab, null, di.LastWriteTime, rdi.UserID);
|
|
// Hook this into the current docversion by replacing the rofstid field in the doc version
|
|
// association object:
|
|
docver.DocVersionAssociations.Add(rofst);
|
|
docver.Save();
|
|
|
|
// Now load any images in... type 8 - integrated graphics ro type
|
|
for (int i = 0; i < rofst.ROFSTLookup.myHdr.myDbs.Length; i++)
|
|
{
|
|
// walk through the rofst 'database' searching for all nodes that are integrated graphics, type 8:
|
|
if (rofst.ROFSTLookup.myHdr.myDbs[i].children != null)
|
|
{
|
|
using (ROFstInfo rfi = ROFstInfo.Get(rofst.ROFstID))
|
|
{
|
|
rfi.MigrateRoFstGraphics(rdi, rofst.ROFSTLookup.myHdr.myDbs[i].children);
|
|
}
|
|
}
|
|
}
|
|
return rofst;
|
|
}
|
|
}
|
|
#endregion
|
|
#region Update Ro Values
|
|
/// <summary>
|
|
/// Updates an ro.fst into a sql database.
|
|
/// </summary>
|
|
/// <param name="rdi" - the Rodb to use as the path for updating the ro.fst, i.e.
|
|
/// import from there.
|
|
/// <param name="dva" - the association record to modify, i.e. make new rofst
|
|
/// the current one.
|
|
/// <param name="docver" - hook into this doc version></param>
|
|
/// <returns>ROFst: Returns the created rofst object</returns>
|
|
public static ROFst UpdateRoFst(RODbInfo rdi, DocVersionAssociation dva, DocVersion docver)
|
|
{
|
|
// file validity checks are done before getting here - just do the import
|
|
// here.
|
|
string rofstfilepath = rdi.FolderPath + @"\ro.fst";
|
|
|
|
DirectoryInfo di = new DirectoryInfo(rdi.FolderPath);
|
|
|
|
// 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);
|
|
|
|
// Next read in the rofst & make the rofst record.
|
|
FileStream fsIn = new FileStream(rofstfilepath, 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);
|
|
fsIn.Close();
|
|
|
|
using (RODb rd = RODb.Get(rdi.RODbID))
|
|
{
|
|
ROFst rofst = ROFst.MakeROFst(rd, ab, null, di.LastWriteTime, rdi.UserID);
|
|
// Hook this into the current docversion by replacing the rofstid field in the doc version
|
|
// association object:
|
|
dva.MyROFst = rofst;
|
|
docver.Save();
|
|
|
|
// Now load any images in... type 8 - integrated graphics ro type
|
|
for (int i = 0; i < rofst.ROFSTLookup.myHdr.myDbs.Length; i++)
|
|
{
|
|
// walk through the rofst 'database' searching for all nodes that are integrated graphics, type 8:
|
|
if (rofst.ROFSTLookup.myHdr.myDbs[i].children != null)
|
|
{
|
|
using (ROFstInfo rfi = ROFstInfo.Get(rofst.ROFstID))
|
|
{
|
|
rfi.MigrateRoFstGraphics(rdi, rofst.ROFSTLookup.myHdr.myDbs[i].children);
|
|
}
|
|
}
|
|
}
|
|
return rofst;
|
|
}
|
|
}
|
|
private static 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(RODbInfo rdi, ROFSTLookup.rochild[] rochild)
|
|
{
|
|
for (int i = 0; i < rochild.Length; i++)
|
|
{
|
|
if (rochild[i].type == 8) this.AddGraphic(rdi, rochild[i].value);
|
|
if (rochild[i].children != null) this.MigrateRoFstGraphics(rdi, rochild[i].children);
|
|
}
|
|
}
|
|
private void AddGraphic(RODbInfo rdi, string p)
|
|
{
|
|
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(rdi.Config);
|
|
fname += string.Format(".{0}", roDbCfg.GetDefaultGraphicExtension());
|
|
}
|
|
|
|
string imgfile = rdi.FolderPath + @"\" + fname;
|
|
|
|
ROImage roImg = null;
|
|
if (File.Exists(imgfile))
|
|
{
|
|
FileInfo fi = new FileInfo(imgfile);
|
|
// if the roimage record exists, don't create a new one...
|
|
using (roImg = ROImage.GetByRODbID_FileName_DTS(rdi.RODbID, imgname,fi.LastWriteTime))
|
|
{
|
|
if (roImg == null)
|
|
{
|
|
FileStream fsIn = new FileStream(imgfile, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
BinaryReader r = new BinaryReader(fsIn);
|
|
byte[] ab = r.ReadBytes((int)fsIn.Length);
|
|
r.Close();
|
|
fsIn.Close();
|
|
using (RODb rodb = RODb.Get(rdi.RODbID))
|
|
{
|
|
roImg = ROImage.MakeROImage(rodb, imgname, ab, null, fi.LastWriteTime, "Migration");
|
|
}
|
|
}
|
|
Figure figure = Figure.GetByROFstID_ImageID(this.ROFstID, roImg.ImageID);
|
|
if (figure != null) return;
|
|
using (ROFst rofst = ROFst.Get(this.ROFstID))
|
|
{
|
|
figure = Figure.MakeFigure(rofst, roImg, null);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
Console.WriteLine(string.Format("{0}", imgfile), "Cannot Find Image File");
|
|
}
|
|
#endregion
|
|
#endregion
|
|
}
|
|
}
|