Give option to import RO’s as text when the RO path of the import is different than the target procedure set

Prompts user with options on what to do with the import of a procedure that has a different RO path than the target procedure set.
This commit is contained in:
2016-08-09 12:33:01 +00:00
parent 0804ecc7ee
commit 1bf77667b2
4 changed files with 638 additions and 92 deletions

View File

@@ -10,11 +10,13 @@ using Volian.Base.Library;
using System.Xml;
using System.IO;
using Ionic.Zip;
using System.Text.RegularExpressions;
namespace VEPROMS
{
public partial class dlgExportImport : Form
{
private bool _ConvertROsToTextDuringImport = false;
private bool _ConvertROsAndTransitionsToText = false; // set to true when Approval creates an Export file
private ItemInfo _ExternalTransitionItem = null;
public ItemInfo ExternalTransitionItem
@@ -204,6 +206,13 @@ namespace VEPROMS
}
if (MyDocVersion != null) // import a procedure - a .pxml file
{
// compare the ROPath of MyDocVerion
// if no path set, display Pick RO Folder dialog
// if MyDocVersion ROPath is different than the import file ROPath, then ask user:
// - use MyDocVersion ROPath (warn resulting RO values may be be correct)
// * set oldRODbID to id in import file
// * set newRODbid to MyDocVersion rodbid
// - or convert ROs to text
TurnChangeManagerOff.Execute();
XmlDocument xd = new XmlDocument();
xd.Load(txtImport.Text);
@@ -211,75 +220,107 @@ namespace VEPROMS
string rofolderpath = xd.DocumentElement.Attributes.GetNamedItem("rofolderpath").InnerText;
int rodbid = int.Parse(xd.DocumentElement.Attributes.GetNamedItem("rodbid").InnerText);
int rofstid = int.Parse(xd.DocumentElement.Attributes.GetNamedItem("rofstid").InnerText);
List<string> localROPaths = new List<string>();
RODbInfoList rolist = RODbInfoList.Get();
foreach (RODbInfo dbi in rolist)
if (MyDocVersion.DocVersionAssociationCount > 0)
{
if (dbi.FolderPath == rofolderpath && dbi.RODbID == rodbid)
// use current ROPath
MyRODb = RODb.GetJustRoDb(MyDocVersion.DocVersionAssociations[0].MyROFst.MyRODb.RODbID);
// if the current RO Path and the import file RO Path are not the same
// then ask if we should import using the current workingdraft RO Path, convert the ROs to text, or cancel the import
if (MyRODb.FolderPath != rofolderpath)
{
dlgImpHowToHandleROs dlg = new dlgImpHowToHandleROs();
dlg.ImportedROFolder = rofolderpath;
dlg.WorkingDraftROFolder = MyRODb.FolderPath;
dlg.ShowDialog(this);
if (dlg.CancelImport)
{
this.Cursor = Cursors.Default;
this.btnImport.Enabled = true; // allow user to select a different export file to import
this.btnDoImport.Enabled = true; // allow user to change mind and perform the import
return;
}
_ConvertROsToTextDuringImport = dlg.ConvertROsToText;
}
// this saves the old RO database ID and the current RO database ID so that we can change the RODBIDs in the RO links
newRODbID = MyRODb.RODbID;
oldRODbID = rodbid;
}
else
{
#region NeedToSelectROPath
// MyDocVersion does not have an RO database (ro folder path) assigned to it so we need to select an RO Path
List<string> localROPaths = new List<string>();
RODbInfoList rolist = RODbInfoList.Get();
foreach (RODbInfo dbi in rolist)
{
MyRODb = RODb.GetJustRoDb(rodbid);
oldRODbID = newRODbID = rodbid;
break;
}
if (dbi.FolderPath == rofolderpath && dbi.RODbID == rodbid)
{
MyRODb = RODb.GetJustRoDb(rodbid);
oldRODbID = newRODbID = rodbid;
break;
}
// B2016-175 have the correct (matching) RO Path but the rodbid is different (procedure came from a different SQL database)
// We can use the RO Path but we need to change the rodbid in the RO links of the procedure we are importing
else if (dbi.FolderPath == rofolderpath && dbi.RODbID != rodbid)
{
MyRODb = RODb.GetByFolderPath(rofolderpath);
newRODbID = MyRODb.RODbID;
oldRODbID = rodbid;
break;
}
else
{
DirectoryInfo di = new DirectoryInfo(dbi.FolderPath);
if (di.Exists)
localROPaths.Add(dbi.FolderPath);
}
}
if (MyRODb == null)
{
if (localROPaths.Count == 0)
{
MessageBox.Show("There has been no RO folder defined for this database.\r\n\r\nIn order to import a procedure, you need to assign a RO folder path.\r\n\r\nImport process will terminate.");
this.Cursor = Cursors.Default;
return;
}
else
{
this.Cursor = Cursors.Default;
dlgPickROFolder dlg = new dlgPickROFolder();
dlg.ImportedROFolder = rofolderpath;
dlg.LocalROFolders = localROPaths;
dlg.ShowDialog(this);
if ((dlg.SelectedROFolder ?? string.Empty) != string.Empty) // B2015-216 If the return value is null treat it like an empty string
else if (dbi.FolderPath == rofolderpath && dbi.RODbID != rodbid)
{
MyRODb = RODb.GetByFolderPath(dlg.SelectedROFolder);
RODbInfo myRODBInfo = RODbInfo.Get(MyRODb.RODbID);
oldRODbID = newRODbID = MyRODb.RODbID;
ROFstInfo fstInfo = null;
foreach (ROFstInfo tmp in myRODBInfo.RODbROFsts)
if (fstInfo == null || tmp.ROFstID > fstInfo.ROFstID)
fstInfo = tmp;
using (DocVersion dv = MyDocVersion.Get())
{
using (ROFst fst = fstInfo.Get())
{
dv.DocVersionAssociations.Add(fst);
dv.Save();
}
dv.Reset_DocVersionAssociations();
MyDocVersion.RefreshDocVersionAssociations();
}
MyRODb = RODb.GetByFolderPath(rofolderpath);
newRODbID = MyRODb.RODbID;
oldRODbID = rodbid;
break;
}
else
{
MessageBox.Show("Since you did not pick an existing RO folder defined for this database, the import process will terminate.");
this.Close();// Close the Import Window
return;
DirectoryInfo di = new DirectoryInfo(dbi.FolderPath);
if (di.Exists)
localROPaths.Add(dbi.FolderPath);
}
}
}
if (MyRODb == null)
{
if (localROPaths.Count == 0)
{
MessageBox.Show("There has been no RO folder defined for this database.\r\n\r\nIn order to import a procedure, you need to assign a RO folder path.\r\n\r\nImport process will terminate.");
this.Cursor = Cursors.Default;
return;
}
else
{
this.Cursor = Cursors.Default;
dlgPickROFolder dlg = new dlgPickROFolder();
dlg.ImportedROFolder = rofolderpath;
dlg.LocalROFolders = localROPaths;
dlg.ShowDialog(this);
if ((dlg.SelectedROFolder ?? string.Empty) != string.Empty) // B2015-216 If the return value is null treat it like an empty string
{
MyRODb = RODb.GetByFolderPath(dlg.SelectedROFolder);
RODbInfo myRODBInfo = RODbInfo.Get(MyRODb.RODbID);
oldRODbID = newRODbID = MyRODb.RODbID;
ROFstInfo fstInfo = null;
foreach (ROFstInfo tmp in myRODBInfo.RODbROFsts)
if (fstInfo == null || tmp.ROFstID > fstInfo.ROFstID)
fstInfo = tmp;
using (DocVersion dv = MyDocVersion.Get())
{
using (ROFst fst = fstInfo.Get())
{
dv.DocVersionAssociations.Add(fst);
dv.Save();
}
dv.Reset_DocVersionAssociations();
MyDocVersion.RefreshDocVersionAssociations();
}
}
else
{
MessageBox.Show("Since you did not pick an existing RO folder defined for this database, the import process will terminate.");
this.Close();// Close the Import Window
return;
}
}
}
#endregion
} // end - need to select RO Path
// use resolvedProcNum to determine if procedure is 'unique', i.e. if the procedure number exists
// and user does not overwrite or copy, then the procedure should NOT be imported. Fix for B2016-045
bool resolvedProcNum = true;
@@ -288,7 +329,7 @@ namespace VEPROMS
// procedure numbers that contain a hyphen may have the hyphen represented as the unicode character
// '\u8209?' or the '-' character. If proc number is same except for hyphen representation, they
// should be considered the same procedure (fix for B2016-084)
string hyphenNum = pi.MyContent.Number == null ? "" : pi.MyContent.Number.Replace(@"\u8209?", "-").Replace("\u2011", "-").Replace("\u9586?","_").Replace("\u2572", @"\");
string hyphenNum = pi.MyContent.Number == null ? "" : pi.MyContent.Number.Replace(@"\u8209?", "-").Replace("\u2011", "-").Replace(@"\u9586?",@"\").Replace("\u2572", @"\");
string hyphenImpNum = xd.SelectSingleNode("procedure/content/@number").InnerText;
// bug fix C2015-044 - jsj
// - the cancel button would ignor the user's wishes and proceed with the import (overwriting the existing procedure)
@@ -2966,34 +3007,117 @@ namespace VEPROMS
// content.Save();
// }
//}
private void AddROUsages(Content content, XmlNode xn)
private string GetMyPrefix(string text, int start, int lastIndex)
{
foreach (XmlNode nd in xn.SelectNodes("rousage"))
string defPrefix = text.Substring(start - 3, 3);
if (defPrefix != @"\v ") throw new Exception(string.Format("rtf string {0} does not match expected format", defPrefix));
string txt = text.Substring(lastIndex, start - lastIndex - 3);
int lastSlash = txt.LastIndexOf(@"\");
int lastSpace = txt.LastIndexOf(" ");
int lastCR = txt.LastIndexOf("\r");
if (lastSpace < lastCR)
lastSpace = lastCR;
if (lastSlash <= lastSpace) //this will return "\v "
return defPrefix;
txt = txt.Substring(lastSlash);
if (txt.StartsWith(@"\'")) //this is a hex
return defPrefix;
if (Regex.IsMatch(txt, @"\\u[0-9].*")) //this is unicode
return defPrefix;
return @"\v";
}
private string GetMySuffix(string text, int start)
{
string txt = text.Substring(start);
int firstSlashVeeZero = txt.IndexOf(@"\v0");
if (firstSlashVeeZero == 0 && txt.Length > 3 && txt[3] == ' ') //"\v0 "
return text.Substring(start, 4);
return text.Substring(start, firstSlashVeeZero + 3); //everything upto \v0"
}
private void ConvertImportProcedureROsToText(Content content, XmlNode xn)
{
string newvalue = "";
foreach (XmlNode nd in xn.SelectNodes("rousage"))
{
string rousageid = nd.Attributes.GetNamedItem("rousageid").InnerText;
string roid = nd.Attributes.GetNamedItem("roid").InnerText;
string config = nd.Attributes.GetNamedItem("config").InnerText;
string userid = nd.Attributes.GetNamedItem("userid").InnerText;
DateTime dts = DateTime.Parse(nd.Attributes.GetNamedItem("dts").InnerText);
if (MyDocVersion.DocVersionAssociations == null)
MyDocVersion.RefreshDocVersionAssociations();
ROFSTLookup lookup = MyDocVersion.DocVersionAssociations[0].MyROFst.GetROFSTLookup(MyDocVersion);
string roval = lookup.GetRoValue(roid);
if (roval == "?")
string findLink = @"<START\].*?\[END>";
MatchCollection ms = Regex.Matches(content.Text, findLink);
string lookFor = string.Format(@"^<START\](\\[^v \\]+)*\\v0(\\[^v '?{{}}~\\]+)*( |\\u[0-9]{{1,4}}?|\\'[0-9a-fA-F]{{2}}|\\[{{}}~])(.*?)(\\[^v'?{{}}~ \\]+)*\\v(\\[^v \\]+)* #Link:ReferencedObject:{0} .*?\[END>$", rousageid);
int lastIndex = 0;
string newText = content.Text;
foreach (Match mm in ms)
{
RoUsageInfo roui = RoUsageInfo.Get(int.Parse(rousageid));
}
else
{
RoUsage rou = RoUsage.MakeRoUsage(content, roid, config, dts, userid, MyRODb);
rou.Save();
RoUsageInfo roui = RoUsageInfo.Get(rou.ROUsageID);
string lookFor = string.Format("#Link:ReferencedObject:{0} {1} {2}[END>", rousageid, roid, oldRODbID.ToString());
string replaceWith = string.Format("#Link:ReferencedObject:{0} {1} {2}[END>", rou.ROUsageID.ToString(), roid, newRODbID.ToString());
if (lookFor != replaceWith)
int offset = mm.Index;
Match m = Regex.Match(mm.Value, lookFor, RegexOptions.Singleline);
if (m != null && m.Groups.Count > 1)
{
content.Text = content.Text.Replace(lookFor, replaceWith);
content.FixContentText(roui, roval, 0, MyDocVersion.DocVersionAssociations[0].MyROFst);
string prefix = GetMyPrefix(content.Text, mm.Index, lastIndex);
string suffix = GetMySuffix(content.Text, mm.Index + mm.Length);
int myIndex = m.Groups[4].Index + mm.Index;
int myLength = m.Groups[4].Length;
if (m.Groups[3].Value != " ")
{
myIndex = m.Groups[3].Index + mm.Index;
myLength += m.Groups[3].Length;
}
string gg = newText.Substring(myIndex, myLength);
gg = gg.Replace("{", @"\{").Replace("}", @"\}");
string part1 = newText.Substring(0, mm.Index);
string part2 = gg;
string part3 = newText.Substring(mm.Index + mm.Length);
//modify part1 based on prefix
if (prefix == @"\v ")
part1 = part1.Substring(0, part1.Length - 3);
else
part1 = part1.Substring(0, part1.Length - 3) + " ";
//modify part3 based on suffix
if (suffix == @"\v0 ")
part3 = part3.Substring(4);
else
part3 = suffix.Replace(@"\v0", "") + part3.Substring(suffix.Length);
content.Text = part1 + part2 + part3;
break; // Text has been processed
}
lastIndex = mm.Index + mm.Length;
}
}
content.Save();
}
private void AddROUsages(Content content, XmlNode xn)
{
if (_ConvertROsToTextDuringImport)
ConvertImportProcedureROsToText(content, xn);
else
{
foreach (XmlNode nd in xn.SelectNodes("rousage"))
{
string rousageid = nd.Attributes.GetNamedItem("rousageid").InnerText;
string roid = nd.Attributes.GetNamedItem("roid").InnerText;
string config = nd.Attributes.GetNamedItem("config").InnerText;
string userid = nd.Attributes.GetNamedItem("userid").InnerText;
DateTime dts = DateTime.Parse(nd.Attributes.GetNamedItem("dts").InnerText);
if (MyDocVersion.DocVersionAssociations == null)
MyDocVersion.RefreshDocVersionAssociations();
ROFSTLookup lookup = MyDocVersion.DocVersionAssociations[0].MyROFst.GetROFSTLookup(MyDocVersion);
string roval = lookup.GetRoValue(roid);
if (roval == "?")
{
RoUsageInfo roui = RoUsageInfo.Get(int.Parse(rousageid));
}
else
{
RoUsage rou = RoUsage.MakeRoUsage(content, roid, config, dts, userid, MyRODb);
rou.Save();
RoUsageInfo roui = RoUsageInfo.Get(rou.ROUsageID);
string lookFor = string.Format("#Link:ReferencedObject:{0} {1} {2}[END>", rousageid, roid, oldRODbID.ToString());
string replaceWith = string.Format("#Link:ReferencedObject:{0} {1} {2}[END>", rou.ROUsageID.ToString(), roid, newRODbID.ToString());
if (lookFor != replaceWith)
{
content.Text = content.Text.Replace(lookFor, replaceWith);
content.FixContentText(roui, roval, 0, MyDocVersion.DocVersionAssociations[0].MyROFst);
}
}
}
content.Save();
@@ -3656,26 +3780,73 @@ namespace VEPROMS
XmlNode gn = xd.SelectSingleNode("C1FlexGrid/Control/IsRoTable");
if (gn.InnerText.ToLower() == "true")
{
gn = xd.SelectSingleNode("C1FlexGrid/Control/RODbId");
gn.InnerText = newRODbID.ToString();
if (_ConvertROsToTextDuringImport)
{
gn.InnerText = "False";
}
else
{
gn = xd.SelectSingleNode("C1FlexGrid/Control/RODbId");
gn.InnerText = newRODbID.ToString();
}
data = xd.OuterXml;
}
else
{
XmlNodeList nl = xd.SelectNodes("C1FlexGrid/Cells/Cell/Data");
bool modified = false;
string lookFor = string.Format(" {0}[END", oldRODbID.ToString());
string replaceWith = string.Format(" {0}[END", newRODbID.ToString());
foreach (XmlNode nn in nl)
if (_ConvertROsToTextDuringImport)
{
if (nn.InnerText.Contains("#Link:ReferencedObject:"))
string newvalue = "";
// if it's an ro within a table, need to process into an flex grid to save the grid data:
string findLinkXml = @"&lt;START\].*?\[END&gt;";
//string lookForXml = string.Format(@"^&lt;START\](\\[^v \\]+)*\\v0(\\[^v '?{{}}\\]+)*( |\\u[0-9]{{1,4}}?|\\'[0-9a-fA-F]{{2}}|\\[{{}}~])(.*?)(\\[^v'?{{}} \\]+)*\\v(\\[^v \\]+)* #Link:ReferencedObject:{0} .*?\[END&gt;$", rousageid);
string lookForXml = @"^&lt;START\](\\[^v \\]+)*\\v0(\\[^v '?{{}}\\]+)*( |\\u[0-9]{{1,4}}?|\\'[0-9a-fA-F]{{2}}|\\[{{}}~])(.*?)(\\[^v'?{{}} \\]+)*\\v(\\[^v \\]+)* #Link:ReferencedObject:.*?\[END&gt;$";
MatchCollection msg = Regex.Matches(data, findLinkXml);
int nmsg = msg.Count;
for (int i = nmsg - 1; i >= 0; i--)
{
nn.InnerText = nn.InnerText.Replace(lookFor, replaceWith);
modified = true;
Match mmg = msg[i];
Match mg = Regex.Match(mmg.Value, lookForXml);// Regex.Match(MyGrid.Data, lookForXml);
if (mg != null && mg.Groups.Count > 1)
{
int myIndex = mg.Groups[4].Index + mmg.Index;
int myLength = mg.Groups[4].Length;
if (mg.Groups[3].Value != " ")
{
myIndex = mg.Groups[3].Index + mmg.Index;
myLength += mg.Groups[3].Length;
}
string ss = data.Substring(myIndex, myLength);
if (ss != newvalue)
{
int ii = data.Substring(0, mmg.Index).LastIndexOf(@"\v");
int iil = data.Substring(mmg.Index + mg.Value.Length).IndexOf(@"\v0");
string part1 = data.Substring(0, ii); // length up to \v
string part2 = data.Substring(ii + 2, mmg.Index - (ii + 2));
string part3 = ss;
string part4 = data.Substring(mmg.Index + mg.Value.Length, iil);
string part5 = data.Substring(mmg.Index + (mg.Value.Length + iil + 3));
data = part1 + part2 + part3 + part4 + part5;
}
}
}
}
if (modified)
data = xd.OuterXml;
else
{
string lookFor = string.Format(" {0}[END", oldRODbID.ToString());
string replaceWith = string.Format(" {0}[END", newRODbID.ToString());
foreach (XmlNode nn in nl)
{
if (nn.InnerText.Contains("#Link:ReferencedObject:"))
{
nn.InnerText = nn.InnerText.Replace(lookFor, replaceWith);
modified = true;
}
}
if (modified)
data = xd.OuterXml;
}
}
string config = nd.Attributes.GetNamedItem("config").InnerText;
string userid = nd.Attributes.GetNamedItem("userid").InnerText;