SourceCode/PROMS/DataLoader/TransitionFixer.cs
2012-11-15 16:58:57 +00:00

260 lines
8.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Volian.Controls.Library;
using VEPROMS.CSLA.Library;
using System.Text.RegularExpressions;
using Volian.Base.Library;
using System.Xml;
using System.IO;
namespace DataLoader
{
public delegate void TransitionFixerEvent(object sender, TransitionFixerEventArgs args);
public class TransitionFixerEventArgs
{
private string _MyStatus;
public string MyStatus
{
get { return _MyStatus; }
set { _MyStatus = value; }
}
public TransitionFixerEventArgs(string myStatus)
{
_MyStatus = myStatus;
}
}
class TransitionFixer
{
public event TransitionFixerEvent StatusChanged;
private void OnStatusChanged(object sender, TransitionFixerEventArgs args)
{
if (StatusChanged != null)
StatusChanged(sender, args);
}
private string _Status;
public string Status
{
get { return _Status; }
set
{
_Status = value;
OnStatusChanged(this, new TransitionFixerEventArgs(_Status));
}
}
private int _ErrorCount = 0;
public int ErrorCount
{
get { return _ErrorCount; }
set { _ErrorCount = value; }
}
private StepRTB _MyStepRTB;
public StepRTB MyStepRTB
{
get { return _MyStepRTB; }
}
private string _LogPath;
public TransitionFixer(StepRTB myStepRTB,string logpath)
{
_MyStepRTB = myStepRTB;
_LogPath = logpath;
}
public TimeSpan Process(bool checkRTF)
{
DateTime tstart = DateTime.Now;
ProcessTransitions(checkRTF);
return DateTime.Now - tstart;
}
private void ProcessTransitions(bool checkRTF)
{
Status = "Getting List...";
// Loop through all Items and check before and after text
ItemInfoList myListFrom = ItemInfoList.GetListTranFrom();
ItemInfoList myListTo = ItemInfoList.GetListTranTo();
ConversionRTBProblems myProblems = new ConversionRTBProblems();
int i = 0;
foreach (ItemInfo item in myListFrom)
{
Status = string.Format("Processing {0} of {1} steps", ++i, myListFrom.Count);
//MyStepRTB.ViewRTB = false;
string originalText = item.MyContent.Text;
string updatedText = item.MyContent.Text;
if (item.MyContent.MyGrid != null)
{
originalText = item.MyContent.MyGrid.Data;
updatedText = (item.MyContent.MyGrid.Data.Replace("&lt;START]", "<START]")).Replace("[END&gt;", "[END>");
}
// Exclude items that are not connected (Dummy steps for invalid transition destinations)
if (item.ItemDocVersionCount != 0 || item.MyPrevious != null || item.MyParent != null)
{
if (item.MyContent.ContentTransitionCount > 0)
{
//updatedText = Volian.Controls.Library.DisplayText.StaticRemoveRtfStyles(updatedText, item);
foreach (TransitionInfo tran in item.MyContent.ContentTransitions)
{
try
{
if (item.MyContent.MyGrid != null)
updatedText = FixTableTransitionText(updatedText, tran, item.MyContent.Get());
else
updatedText = FixTransitionText(updatedText, tran);
}
catch (Exception ex)
{
Console.WriteLine("{0} {1}", ex.GetType().Name, ex.Message);
}
// Added for transitions to un-numbered steps
if (tran.NewTransToUnNumberedItem) item.NewTransToUnNumberedItem = true;
}
}
if (updatedText.EndsWith(" ")) updatedText = updatedText.Substring(0, updatedText.Length - 1);
if (item.MyContent.MyGrid != null)
{
using (Item itm = item.Get())
{
updatedText = (updatedText.Replace("<START]", "&lt;START]")).Replace("[END>", "[END&gt;");
string sstring = AdjustSizeAndGetSearchString(updatedText, itm);
itm.MyContent.Text = sstring;
itm.Save();
}
}
else
{
using (Content c = item.MyContent.Get())
{
c.Text = updatedText;
c.Save();
}
}
// Added for transitions to un-numbered steps
if (item.NewTransToUnNumberedItem)
{
using (Item itm = item.Get())
{
ItemAnnotation ia = itm.ItemAnnotations.Add(VerificationRequiredType);
ia.SearchText = "Transition to un-numbered step";
ia.UserID = "Migration";
itm.Save();
}
}
if (checkRTF)
{
MyStepRTB.MyItemInfo = item;
// Force Save - This will put change bars on everything
if (MyStepRTB.Text.Contains("(Resolved Transition Text)") != false) MyStepRTB.OrigDisplayText.Save(MyStepRTB);
string afterText = item.MyContent.Text;
// aftertext is 'newrtf'
if (afterText != updatedText)
myProblems.RTBProblems.Add(item.ItemID, item.MyContent.ContentID, originalText, updatedText, MyStepRTB.Rtf, afterText, item.Path);
}
}
}
if (checkRTF)
{
Status = "Saving problems";
string logFile = _LogPath + @"\RTBProblems.xml";
ErrorCount = myProblems.RTBProblems.Count;
myProblems.Save(logFile);
if (ErrorCount > 0)
{
System.Windows.Forms.DialogResult answer = System.Windows.Forms.MessageBox.Show(
string.Format("{0} Differences found in Transition Text\r\nResults in {1}\r\n\r\nOpen Log File?",
ErrorCount, logFile), "Transitions Different", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question);
if(answer == System.Windows.Forms.DialogResult.Yes)
System.Diagnostics.Process.Start(logFile);
}
}
Status = "Done comparing";
}
// Added for transitions to un-numbered steps
private AnnotationType _VerificationRequiredType; // Using this to flag table to grid conversions
public AnnotationType VerificationRequiredType
{
get
{
if (_VerificationRequiredType == null)
_VerificationRequiredType = AnnotationType.GetByName("Verification Required");
return _VerificationRequiredType;
}
}
public string FixTransitionText(string Text, TransitionInfo tran)
{
string lookFor = string.Format(@"<START\]\\v0 ([^#]*?)\\v #Link:Transition[^:]*?:{0} {1}( [0-9]*){2}\[END>", tran.TranType, tran.TransitionID, "{1,2}");
string transText = tran.ResolvePathTo();
//Console.WriteLine(">>>>> FixTransitionText");
//Console.WriteLine("Text = {0}", Text);
//Console.WriteLine("lookFor = {0}", lookFor);
//Console.WriteLine("TransText = {0}", transText);
Match m = Regex.Match(Text, lookFor);
if (m != null && m.Groups.Count > 1)
{
System.Text.RegularExpressions.Group g = m.Groups[1];
if (g.ToString() != transText)
Text = Text.Substring(0, g.Index) + transText + Text.Substring(g.Index + g.Length);
}
else
Console.WriteLine("Transition not Found");
return Text;
}
public string FixTableTransitionText(string Text, TransitionInfo tran, Content content)
{
string lookFor = string.Format(@"<START\]\\cf1\\v0 ([^#]*?)\\cf0\\v #Link:Transition[^:]*?:{0} {1}( [0-9]*){2}\[END>", tran.TranType, tran.TransitionID, "{1,2}");
string transText = tran.ResolvePathTo();
//Console.WriteLine(">>>>> FixTransitionText");
//Console.WriteLine("Text = {0}", Text);
//Console.WriteLine("lookFor = {0}", lookFor);
//Console.WriteLine("TransText = {0}", transText);
Match m = Regex.Match(Text, lookFor);
if (m != null && m.Groups.Count > 1)
{
System.Text.RegularExpressions.Group g = m.Groups[1];
if (g.ToString() != transText)
Text = Text.Substring(0, g.Index) + transText + Text.Substring(g.Index + g.Length);
}
else
Console.WriteLine("Transition not Found");
//VlnFlexGrid grd = new VlnFlexGrid(1, 1);
//XmlDocument xd = new XmlDocument();
//xd.LoadXml(Text);
//grd.ReadXml(xd);
//grd.FixTableCellsHeightWidth(); // resize the column width/height
//using (StringWriter sw = new StringWriter())
//{
// grd.WriteXml(sw);
// //Console.WriteLine(sw.GetStringBuilder().ToString());
// content.MyGrid.Data = sw.GetStringBuilder().ToString();
// sw.Close();
//}
return Text;
}
private string AdjustSizeAndGetSearchString(string strXML, Item itm)
{
string rstring = "";
VlnFlexGrid grd = new VlnFlexGrid(1, 1);
XmlDocument xd = new XmlDocument();
xd.LoadXml(strXML);
grd.ReadXml(xd);
//using (StringReader sr = new StringReader(strXML))
//{
// grd.ReadXml(sr);
// sr.Close();
//}
grd.FixTableCellsHeightWidth(); // resize the column width/height
rstring = grd.GetSearchableText();
using (StringWriter sw = new StringWriter())
{
grd.WriteXml(sw);
itm.MyContent.MyGrid.Data = sw.GetStringBuilder().ToString();
sw.Close();
}
return rstring;
}
}
}