B2021-045: Search/Load, don’t crash if noncompatible Xml file

This commit is contained in:
Kathy Ruffing 2021-04-20 16:07:17 +00:00
parent 1e85522762
commit 814d26f713

View File

@ -2321,144 +2321,152 @@ namespace Volian.Controls.Library
lbSrchResults.DataSource = null;
lbSrchResults.Items.Clear();
toolTip1.SetToolTip(lbSrchResults, null);
Cursor = Cursors.WaitCursor;
string unitPrefix = "";
if (Mydocversion != null)
unitPrefix = Mydocversion.DocVersionConfig.Unit_ProcedureNumber;
if (unitPrefix.EndsWith("#"))
unitPrefix = unitPrefix.Replace("#", "");
SearchResults = ItemInfoList.GetByContentID(0);
// C2021-006: removed all file type options except xml
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(ofdSearchResults.FileName);
// B2020-107: Add missing attributes/nodes for loading of search results, include procsets & steptypes:
System.Xml.XmlNodeList nlp = xd.SelectNodes("search/procsets/set");
if (nlp != null)
Cursor = Cursors.WaitCursor;
try // B2021-045: don't crash if xml file cannot be loaded.
{
DocVersionInfo tmpdvi = Mydocversion;
DocVersionInfo fromLoadFiledvi = null;
foreach (System.Xml.XmlNode nd in nlp)
SearchResults = ItemInfoList.GetByContentID(0);
// C2021-006: removed all file type options except xml
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(ofdSearchResults.FileName);
// B2020-107: Add missing attributes/nodes for loading of search results, include procsets & steptypes:
System.Xml.XmlNodeList nlp = xd.SelectNodes("search/procsets/set");
if (nlp != null)
{
int folderID = int.Parse(nd.Attributes.GetNamedItem("fid").InnerText);
FolderInfo fi = FolderInfo.Get(folderID);
_MyDocVersion = fi.FolderDocVersions[0];
if (fromLoadFiledvi == null) fromLoadFiledvi = _MyDocVersion;
advTreeProcSetsPreSelect();
}
foreach (System.Xml.XmlNode nd in nlp)
{
int folderID = int.Parse(nd.Attributes.GetNamedItem("fid").InnerText);
FolderInfo fi = FolderInfo.Get(folderID);
Node tnode = advTreeProcSets.FindNodeByDataKey(folderID);
if (tnode != null) tnode.Checked = true;
}
Mydocversion = tmpdvi == null ? fromLoadFiledvi : tmpdvi;
}
AdvTreeStepTypesFillIn();
System.Xml.XmlNodeList nl1 = xd.SelectNodes("search/StepType/type");
if (nl1 != null)
{
foreach (System.Xml.XmlNode nd in nl1)
{
string stptype = nd.Attributes.GetNamedItem("str").InnerText;
Node tnode = advTreeStepTypes.FindNodeByText(stptype);
if (tnode != null)
DocVersionInfo tmpdvi = Mydocversion;
DocVersionInfo fromLoadFiledvi = null;
foreach (System.Xml.XmlNode nd in nlp)
{
tnode.Checked = true;
xpStepTypes.TitleStyle.BackColor1.Color = saveXpStepTypeTitleColor;
lstCheckedStepTypesStr.Add(stptype);
StepData sd = (StepData)tnode.Tag;
lstCheckedStepTypes.Add((int)sd.Index);
int folderID = int.Parse(nd.Attributes.GetNamedItem("fid").InnerText);
FolderInfo fi = FolderInfo.Get(folderID);
_MyDocVersion = fi.FolderDocVersions[0];
if (fromLoadFiledvi == null) fromLoadFiledvi = _MyDocVersion;
advTreeProcSetsPreSelect();
}
}
string typstr = null;
foreach (string s in lstCheckedStepTypesStr) typstr = typstr == null ? s : typstr + ", " + s;
TypesSelected = "Filtered By: " + ((typstr != null) ? typstr : "All Step Types");
ReportTitle = "Step Element Report"; //"Proms - Search by Type: " + typstr;
TypesSelected = "Filtered By: " + typstr;
advTreeStepTypes.RefreshItems();
buildStepTypePannelTitle();
}
cmbResultsStyle.Text = xd.SelectSingleNode("search/results/@style").InnerText;
int sti = int.Parse(xd.SelectSingleNode("search/criteria/@index").InnerText);
switch (sti)
{
case 0: //text or steptypes
foreach (System.Xml.XmlNode nd in nlp)
{
System.Xml.XmlNode xn = xd.SelectSingleNode("search/criteria/@text");
tabSearchTypes.SelectedTab = tabStepTypeSearch;
if (xn != null) // text search
{
cbxTextSearchText.Text = xn.InnerText;
cbxCaseSensitive.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@case").InnerText);
cbxIncROTextSrch.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@ro").InnerText);
cbxByWord.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@byword").InnerText);
}
else // B2020-107: Add in step type search
{
cbxSrchTypeUsage.Checked = true;
}
System.Xml.XmlNodeList nl = xd.SelectNodes("search/results/item");
foreach (System.Xml.XmlNode nd in nl)
{
int itemID = int.Parse(nd.Attributes.GetNamedItem("id").InnerText);
ItemInfo ii = ItemInfo.Get(itemID);
if (ii.SearchAnnotationText == null && ii.ItemAnnotations != null) ii.SearchAnnotationText = AnnotationInfo.Get(ii.ItemAnnotations[0].AnnotationID).SearchText;
SearchResults.AddItemInfo(ii);
}
lbSrchResults.DataSource = SearchResults;
DisplayResults();
break;
int folderID = int.Parse(nd.Attributes.GetNamedItem("fid").InnerText);
FolderInfo fi = FolderInfo.Get(folderID);
Node tnode = advTreeProcSets.FindNodeByDataKey(folderID);
if (tnode != null) tnode.Checked = true;
}
case 1: //annotation
Mydocversion = tmpdvi == null ? fromLoadFiledvi : tmpdvi;
}
AdvTreeStepTypesFillIn();
System.Xml.XmlNodeList nl1 = xd.SelectNodes("search/StepType/type");
if (nl1 != null)
{
foreach (System.Xml.XmlNode nd in nl1)
{
cbxAnnoTypes.Text = xd.SelectSingleNode("search/criteria/@type").InnerText;
cbxTextSearchAnnotation.Text = xd.SelectSingleNode("search/criteria/@text").InnerText;
cbxCaseSensitiveAnnoText.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@case").InnerText);
tabSearchTypes.SelectedTab = tabAnnotationSearch;
cmbResultsStyle.Text = xd.SelectSingleNode("search/results/@style").InnerText;
System.Xml.XmlNodeList nl = xd.SelectNodes("search/results/item");
foreach (System.Xml.XmlNode nd in nl)
string stptype = nd.Attributes.GetNamedItem("str").InnerText;
Node tnode = advTreeStepTypes.FindNodeByText(stptype);
if (tnode != null)
{
int itemID = int.Parse(nd.Attributes.GetNamedItem("id").InnerText);
ItemInfo ii = ItemInfo.Get(itemID);
// B2020-107: The following was needed to set the Step Path result type for annotations
if (ii.SearchAnnotationText == null && ii.ItemAnnotations != null)
tnode.Checked = true;
xpStepTypes.TitleStyle.BackColor1.Color = saveXpStepTypeTitleColor;
lstCheckedStepTypesStr.Add(stptype);
StepData sd = (StepData)tnode.Tag;
lstCheckedStepTypes.Add((int)sd.Index);
}
}
string typstr = null;
foreach (string s in lstCheckedStepTypesStr) typstr = typstr == null ? s : typstr + ", " + s;
TypesSelected = "Filtered By: " + ((typstr != null) ? typstr : "All Step Types");
ReportTitle = "Step Element Report"; //"Proms - Search by Type: " + typstr;
TypesSelected = "Filtered By: " + typstr;
advTreeStepTypes.RefreshItems();
buildStepTypePannelTitle();
}
cmbResultsStyle.Text = xd.SelectSingleNode("search/results/@style").InnerText;
int sti = int.Parse(xd.SelectSingleNode("search/criteria/@index").InnerText);
switch (sti)
{
case 0: //text or steptypes
{
System.Xml.XmlNode xn = xd.SelectSingleNode("search/criteria/@text");
tabSearchTypes.SelectedTab = tabStepTypeSearch;
if (xn != null) // text search
{
ii.SearchAnnotationText = AnnotationInfo.Get(ii.ItemAnnotations[0].AnnotationID).SearchText;
ii.SearchAnnotationID = ii.ItemAnnotations[0].AnnotationID;
cbxTextSearchText.Text = xn.InnerText;
cbxCaseSensitive.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@case").InnerText);
cbxIncROTextSrch.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@ro").InnerText);
cbxByWord.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@byword").InnerText);
}
SearchResults.AddItemInfo(ii);
else // B2020-107: Add in step type search
{
cbxSrchTypeUsage.Checked = true;
}
System.Xml.XmlNodeList nl = xd.SelectNodes("search/results/item");
foreach (System.Xml.XmlNode nd in nl)
{
int itemID = int.Parse(nd.Attributes.GetNamedItem("id").InnerText);
ItemInfo ii = ItemInfo.Get(itemID);
if (ii.SearchAnnotationText == null && ii.ItemAnnotations != null) ii.SearchAnnotationText = AnnotationInfo.Get(ii.ItemAnnotations[0].AnnotationID).SearchText;
SearchResults.AddItemInfo(ii);
}
lbSrchResults.DataSource = SearchResults;
DisplayResults();
break;
}
lbSrchResults.DataSource = SearchResults;
DisplayResults();
break;
}
case 2: //referenced object
case 3: // transitions
{
FlexibleMessageBox.Show(this, "Loading Referenced Objects and Transitions searches is under construction.", "Under Construction", MessageBoxButtons.OK);
break;
}
case 4: // Incoming transitions
{
tabSearchTypes.SelectedTab = tabIncTrans;
cmbResultsStyle.Text = xd.SelectSingleNode("search/results/@style").InnerText;
string tmpi = xd.SelectSingleNode("search/results/@toid").InnerText;
SearchIncTransII = ItemInfo.Get(Convert.ToInt32(tmpi));
System.Xml.XmlNodeList nl = xd.SelectNodes("search/results/item");
foreach (System.Xml.XmlNode nd in nl)
case 1: //annotation
{
int itemID = int.Parse(nd.Attributes.GetNamedItem("id").InnerText);
ItemInfo ii = ItemInfo.Get(itemID);
if (ii.SearchAnnotationText == null && ii.ItemAnnotations != null) ii.SearchAnnotationText = AnnotationInfo.Get(ii.ItemAnnotations[0].AnnotationID).SearchText;
SearchResults.AddItemInfo(ii);
cbxAnnoTypes.Text = xd.SelectSingleNode("search/criteria/@type").InnerText;
cbxTextSearchAnnotation.Text = xd.SelectSingleNode("search/criteria/@text").InnerText;
cbxCaseSensitiveAnnoText.Checked = bool.Parse(xd.SelectSingleNode("search/criteria/@case").InnerText);
tabSearchTypes.SelectedTab = tabAnnotationSearch;
cmbResultsStyle.Text = xd.SelectSingleNode("search/results/@style").InnerText;
System.Xml.XmlNodeList nl = xd.SelectNodes("search/results/item");
foreach (System.Xml.XmlNode nd in nl)
{
int itemID = int.Parse(nd.Attributes.GetNamedItem("id").InnerText);
ItemInfo ii = ItemInfo.Get(itemID);
// B2020-107: The following was needed to set the Step Path result type for annotations
if (ii.SearchAnnotationText == null && ii.ItemAnnotations != null)
{
ii.SearchAnnotationText = AnnotationInfo.Get(ii.ItemAnnotations[0].AnnotationID).SearchText;
ii.SearchAnnotationID = ii.ItemAnnotations[0].AnnotationID;
}
SearchResults.AddItemInfo(ii);
}
lbSrchResults.DataSource = SearchResults;
DisplayResults();
break;
}
lbSrchResultsIncTrans.DataSource = SearchResults;
DisplayResults();
break;
}
case 2: //referenced object
case 3: // transitions
{
FlexibleMessageBox.Show(this, "Loading Referenced Objects and Transitions searches is under construction.", "Under Construction", MessageBoxButtons.OK);
break;
}
case 4: // Incoming transitions
{
tabSearchTypes.SelectedTab = tabIncTrans;
cmbResultsStyle.Text = xd.SelectSingleNode("search/results/@style").InnerText;
string tmpi = xd.SelectSingleNode("search/results/@toid").InnerText;
SearchIncTransII = ItemInfo.Get(Convert.ToInt32(tmpi));
System.Xml.XmlNodeList nl = xd.SelectNodes("search/results/item");
foreach (System.Xml.XmlNode nd in nl)
{
int itemID = int.Parse(nd.Attributes.GetNamedItem("id").InnerText);
ItemInfo ii = ItemInfo.Get(itemID);
if (ii.SearchAnnotationText == null && ii.ItemAnnotations != null) ii.SearchAnnotationText = AnnotationInfo.Get(ii.ItemAnnotations[0].AnnotationID).SearchText;
SearchResults.AddItemInfo(ii);
}
lbSrchResultsIncTrans.DataSource = SearchResults;
DisplayResults();
break;
}
}
}
catch (Exception ex)
{
FlexibleMessageBox.Show("An error occurred while reading the xml file.\r\n\r\nThis can happen when the file selected for the Load is not saved\r\n" +
"from a PROMS Search or if some of the steps no longer exist.", "Search Load Failed", MessageBoxButtons.OK);
}
Cursor = Cursors.Default;
}