diff --git a/PROMS/VEPROMS.CSLA.Library/Extension/TransitionExt.cs b/PROMS/VEPROMS.CSLA.Library/Extension/TransitionExt.cs index 83f859a0..d4dc5bab 100644 --- a/PROMS/VEPROMS.CSLA.Library/Extension/TransitionExt.cs +++ b/PROMS/VEPROMS.CSLA.Library/Extension/TransitionExt.cs @@ -282,21 +282,83 @@ namespace VEPROMS.CSLA.Library } #endregion } + public class TransitionBuilder + { + private StringBuilder _Results = new StringBuilder(); + public FormatData _FormatData; + public string _TransFormat; + public E_TransUI _TransUI; + public ItemInfo _FromItem; + public int _TranType; + public ItemInfo _ToItem; + public ItemInfo _RangeItem; + public bool _UsedRangeAncestor; + private string _Token; + public string Token + { + get { return _Token; } + set { _Token = value; } + } + private string _Prefix; + public string Prefix + { + get { return _Prefix; } + set { if(_Prefix==null)_Prefix = value; } + } + public bool HasText + { + get + { + return _Results.Length > 0; + } + } + public StringBuilder Append(string str) + { + return _Results.Append(str); + } + public StringBuilder ReplaceToken(string str) + { + if (str == null || str == "") return _Results; + if (_FormatData.TransData.XchngTranSpForHard && str[0]!=',' && str[0] != ' ') + { + int indx = str.IndexOf(' '); // 16 bit code only processes 1st occurance + if (indx > -1) // Not a while loop in 16 bit. + { // (see \promsnt\lib\edit\gettran.c:TransitionCat) + str.Remove(indx, 1); + str.Insert(indx, @"\u160?"); + } + } + return _Results.Append(str); + } + public override string ToString() + { + return _Results.ToString(); + } + public StringBuilder Remove(int startIndex, int length) + { + return _Results.Remove(startIndex, length); + } + public int Length + { + get { return _Results.Length; } + } + public void AppendPrefix() + { + if (HasText && Prefix != null) + { + _Results.Append(Prefix); + _Prefix = null; + } + } + public string OverridePrefix + { + set { _Prefix = value; } + } + } public static class TransitionText { - private delegate bool TransitionAppendFunction(bool textAdded, TransitionBuilder tb, string token, string nonToken); - private struct TransitionBuilder - { - public StringBuilder _Results; - public FormatData _FormatData; - public string _TransFormat; - public E_TransUI _TransUI; - public ItemInfo _FromItem; - public int _TranType; - public ItemInfo _ToItem; - public ItemInfo _RangeItem; - public bool _UsedRangeAncestor; - } + private delegate bool TransitionAppendFunction(TransitionBuilder tb); + private static Dictionary _AppendMethods; private static void SetupMethods() { @@ -310,8 +372,8 @@ namespace VEPROMS.CSLA.Library _AppendMethods.Add("{.}", AddIncludedStepNumber); _AppendMethods.Add("{Sect Hdr}", AddTranGetSectionHdr); _AppendMethods.Add("{?.Sect Hdr}", AddOptionalTranGetSectionHdr); - _AppendMethods.Add("{Sect Title}", AddTranGetSectionHdr); - _AppendMethods.Add("{?.Sect Title}", AddOptionalTranGetSectionHdr); + _AppendMethods.Add("{Sect Title}", AddTranGetSectionTitle); + _AppendMethods.Add("{?.Sect Title}", AddOptionalTranGetSectionTitle); _AppendMethods.Add("{Sect Num}", AddTranGetSectionNumber); } public static string GetResolvedText(ItemInfo fromInfo, int tranType, ItemInfo toItem, ItemInfo rangeItem) @@ -329,7 +391,6 @@ namespace VEPROMS.CSLA.Library private static TransitionBuilder SetupTransitionBuilder(FormatInfo formatInfo, ItemInfo fromInfo, int tranType, ItemInfo toItem, ItemInfo rangeItem) { TransitionBuilder tb = new TransitionBuilder(); - tb._Results = new StringBuilder(); tb._FormatData = formatInfo.PlantFormat.FormatData; // get the format of the transition string based on this transition's index into the TransData part of // format.... @@ -348,16 +409,15 @@ namespace VEPROMS.CSLA.Library { int startIndex = 0; int index = -1; - string nonToken = null; - bool textAdded = false; + string prefix = null; + bool lastAdded = false; while ((index = tb._TransFormat.IndexOf("{", startIndex)) > -1) { - if (index > startIndex) nonToken = tb._TransFormat.Substring(startIndex, index - startIndex); - if (startIndex == 0 && nonToken != null && nonToken.Length > 0) + if (index > startIndex) prefix = tb._TransFormat.Substring(startIndex, index - startIndex); + if (startIndex == 0 && prefix != null && prefix.Length > 0) { - tb._Results.Append(nonToken); - //textAdded = true; - nonToken = ""; + tb.Append(prefix); + prefix = ""; } int endtokn = tb._TransFormat.IndexOf("}", index); string token = tb._TransFormat.Substring(index, endtokn - index + 1); @@ -367,59 +427,55 @@ namespace VEPROMS.CSLA.Library break; } if (_AppendMethods.ContainsKey(token)) - textAdded = _AppendMethods[token](textAdded, tb, token, nonToken); + { + tb.Token = token; + if (token[1] == '?') // If it's a conditional token, only update the prefix if the prefix is null + tb.Prefix = prefix; + else // If not conditional, force the prefix to be updated + tb.OverridePrefix = prefix; + lastAdded = _AppendMethods[token](tb); + } else - tb._Results.Append("\\"+token.Substring(0,token.Length -1)+"\\}"); + tb.Append("\\" + token.Substring(0, token.Length - 1) + "\\}"); startIndex = endtokn + 1; if (startIndex >= tb._TransFormat.Length) break; } - if (startIndex < tb._TransFormat.Length) Append(tb, tb._TransFormat.Substring(startIndex, tb._TransFormat.Length - startIndex - 1), false); - return (tb._Results.ToString()); - } - private static void Append(TransitionBuilder tb, string str, bool doflags) - { - if (str == null || str == "") return; - if (doflags && tb._FormatData.TransData.XchngTranSpForHard) - { - int indx = str.IndexOf(' '); - if (indx > -1) - { - str.Remove(indx, 1); - str.Insert(indx, @"\u160?"); - } - } - tb._Results.Append(str); + if (startIndex < tb._TransFormat.Length) tb.Append(tb._TransFormat.Substring(startIndex, tb._TransFormat.Length - startIndex - 1)); + return (tb.ToString()); } + // TODO: TStepNoFlag, i.e. include step number in range items. // TODO: TStepNoFlag, i.e. include step number in range items. // TODO: For hlp: LowerCaseTranNumber - lower case substep numbers in transitions - private static bool AddTransitionProcNum(bool textAdded, TransitionBuilder tb, string token, string nonToken) // Coded for HLP + private static bool AddTransitionProcNum(TransitionBuilder tb) // Coded for HLP { string retstr = tb._ToItem.MyProcedure.MyContent.Number; // LATER: start with UnitSpecific procedure number. // LATER: Format Flag TruncateProcNmAfter1stSpace (dropped plants) // LATER: Format Flag HardSpTranProcNumb (active plants) - if (textAdded) Append(tb, nonToken, false); - Append(tb, retstr, true); - if (retstr != null && retstr != "") return true; - return false; + tb.AppendPrefix(); + tb.ReplaceToken(retstr); + return (retstr != null && retstr != ""); } - private static bool AddOptionalTransitionProcNum(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddOptionalTransitionProcNum(TransitionBuilder tb) { // token is passed in for future use, i.e. if text is placed between "?" and "." to // request other processing. // for now the only test is to check if the toitem is in the current procedure. - if (tb._FromItem.MyProcedure.ItemID == tb._ToItem.MyProcedure.ItemID) return textAdded ? true : false; - return AddTransitionProcNum(textAdded, tb, token, nonToken); + if (tb._FromItem.MyProcedure.ItemID == tb._ToItem.MyProcedure.ItemID) return false; // tb.TextAdded; + return AddTransitionProcNum(tb); } - private static bool AddTransitionProcTitle(bool textAdded, TransitionBuilder tb, string token, string nonToken)// Coded for HLP + private static bool AddTransitionProcTitle(TransitionBuilder tb)// Coded for HLP { + tb.AppendPrefix(); + + // if can't find procedure, just put in a question mark. if (tb._ToItem.PreviousID == null && tb._ToItem.ItemPartCount == 0 && tb._ToItem.ItemDocVersionCount == 0) { - Append(tb, "?", true); - return true; + tb.Append("?"); + return true; // (tb.TextAdded = true); } string parenstr = tb._ToItem.MyProcedure.MyContent.Text; StringBuilder lretstr = new StringBuilder(); @@ -433,19 +489,17 @@ namespace VEPROMS.CSLA.Library lretstr.Append(tb._FormatData.TransData.DelimiterForTransitionTitle); } // LATER: if (DoSectionTransitions && GetSTepNO(TSeq1)) TransitionCat(AddCommaStep", Step")); - if (textAdded) Append(tb, nonToken, false); - Append(tb, lretstr.ToString(), true); - if (lretstr.Length != 0) return true; - return false; + tb.ReplaceToken(lretstr.ToString()); + return (lretstr.Length != 0); } - private static bool AddOptionalTransitionProcTitle(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddOptionalTransitionProcTitle(TransitionBuilder tb) { // token is passed in for future use, i.e. if text is placed between "?" and "." to // request other processing. // for now the only test is to check if the toitem is in the current procedure. - if (tb._FromItem.MyProcedure.ItemID == tb._ToItem.MyProcedure.ItemID) return textAdded ? true : false; - return AddTransitionProcTitle(textAdded, tb, token, nonToken); + if (tb._FromItem.MyProcedure.ItemID == tb._ToItem.MyProcedure.ItemID) return false; // tb.TextAdded; + return AddTransitionProcTitle(tb); } private static string Tab(ItemInfo item) { @@ -467,7 +521,6 @@ namespace VEPROMS.CSLA.Library string thisTab = StepInfo.Get(pitem.ItemID).MyTab.CleanText; if (pitem.IsRNOPart) { - //string mytb = sret.Trim(" .)".ToCharArray()); if (thisTab == null || thisTab == "") sret = "RNO." + sret; else @@ -478,7 +531,6 @@ namespace VEPROMS.CSLA.Library } else { - //sret = Tab(item.ActiveParent as ItemInfo) + "." + sret.Trim(" .)".ToCharArray()); //original if (thisTab != null && thisTab != "") { thisTab = thisTab.Trim(" ".ToCharArray()); @@ -502,106 +554,55 @@ namespace VEPROMS.CSLA.Library sret = sret.Trim(" .)".ToCharArray()); return sret; } - private static string RhmTab(ItemInfo item) - { - if (item == null) return ""; - //if (item.ItemID == 2065) Console.WriteLine("here"); - string sret = ""; - switch (item.MyContent.Type / 10000) - { - case 0: //procedure - sret = ProcedureInfo.Get(item.ItemID).MyTab.CleanText; - break; - case 1: // section - sret = SectionInfo.Get(item.ItemID).MyTab.CleanText; - break; - case 2: // step - sret = StepInfo.Get(item.ItemID).MyTab.CleanText; - if (!item.IsHigh) - { - if (item.IsRNOPart) - { - string mytb = sret.Trim(" .)".ToCharArray()); - if (mytb == null || mytb == "") - sret = Tab(item.ActiveParent as ItemInfo) + ".RNO"; - else - sret = Tab(item.ActiveParent as ItemInfo) + ".RNO." + sret.Trim(" .)".ToCharArray()); - } - else - { - //sret = Tab(item.ActiveParent as ItemInfo) + "." + sret.Trim(" .)".ToCharArray()); //original - string tmp1 = sret.Contains(@")") ? ")" : "."; - string tmp2 = sret.Trim(" .)".ToCharArray()); - string srettmp = Tab(item.ActiveParent as ItemInfo); - sret = srettmp + sret.Trim(" ".ToCharArray()); - if (!sret.EndsWith(".") && !sret.EndsWith(")")) sret = sret + "."; - } - } - else - { - sret = sret.Trim(" ".ToCharArray()); - if (!sret.EndsWith(".") && !sret.EndsWith(")")) sret = sret + "."; - } - break; - } - //sret = sret.Trim(" .)".ToCharArray()); // original - return sret; - } - private static bool AddStepNumber(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddStepNumber(TransitionBuilder tb) { // If we're on a step put out the step number. ItemInfo secitm = TranGetSectionItem(tb._ToItem); if ((!((tb._TransUI & E_TransUI.StepAllowNone) == E_TransUI.StepAllowNone)) || tb._ToItem.MyContent.Type > 20000) { - if (textAdded) + if (tb.HasText) { // check for transition that goes to a procedure step section with title of 'Procedure steps' followed // by ', Step'. This should output as 'Procedure step xyz' rather than 'Procedure steps, step xyz' - if (!tb._TransFormat.Contains("{Last Step}") && nonToken.ToUpper().Contains(", STEP") && tb._Results.ToString().ToUpper().EndsWith("PROCEDURE STEPS")) + if (!tb._TransFormat.Contains("{Last Step}") && tb.Prefix.ToUpper().Contains(", STEP") && tb.ToString().ToUpper().EndsWith("PROCEDURE STEPS")) { - tb._Results = tb._Results.Remove(tb._Results.Length-15,15); // remove "procedure steps" - tb._Results = tb._Results.Append("procedure Step"); - nonToken = " "; + tb.Remove(tb.Length - 15, 15); // remove "procedure steps" + tb.Append("procedure Step"); + tb.Prefix = " "; } - if (nonToken!=null)Append(tb, nonToken, false); + tb.AppendPrefix(); } - //Console.WriteLine("NEW - ItemID={0},Ordinal={1}", tb._ToItem.ItemID, tb._ToItem.Ordinal); if (tb._ToItem.PreviousID == null && tb._ToItem.ItemPartCount == 0 && tb._ToItem.ItemDocVersionCount == 0) - Append(tb, "?", true); + tb.Append("?"); else - Append(tb, Tab(tb._ToItem), true); - //Append(tb, tb._ToItem.Ordinal.ToString(), true); - //Append(tb, tb._ToItem.MyTab.CleanText, true); - textAdded = true; + tb.ReplaceToken(Tab(tb._ToItem)); + return true; } - return textAdded; - } - private static bool AddRangeStepNumber(bool textAdded, TransitionBuilder tb, string token, string nonToken) - { - if (textAdded) Append(tb, nonToken, false); - if (tb._RangeItem.PreviousID == null && tb._RangeItem.ItemPartCount == 0 && tb._RangeItem.ItemDocVersionCount == 0) - Append(tb, "?", true); else - Append(tb, Tab(tb._RangeItem), true); - //Append(tb, tb._RangeItem.Ordinal.ToString(), true); - //Append(tb, tb._RangeItem.MyTab.CleanText, true); - textAdded = true; - return textAdded; + return false; } - private static bool AddIncludedStepNumber(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddRangeStepNumber(TransitionBuilder tb) + { + tb.AppendPrefix(); + if (tb._RangeItem.PreviousID == null && tb._RangeItem.ItemPartCount == 0 && tb._RangeItem.ItemDocVersionCount == 0) + tb.Append("?"); + else + tb.ReplaceToken(Tab(tb._RangeItem)); + return true; + } + private static bool AddIncludedStepNumber(TransitionBuilder tb) { Dictionary rangeAncestors = GetAncestors(tb._RangeItem); - if (textAdded) Append(tb, nonToken, false); + tb.AppendPrefix(); bool usedRangeAncestor = false; ItemInfo next = GetNextItem(tb._ToItem, rangeAncestors, ref usedRangeAncestor); while (next.ItemID != tb._RangeItem.ItemID) { - Append(tb, ", " + Tab(next), true); // TODO: Intermediate Range. + tb.ReplaceToken(", " + Tab(next)); // TODO: Intermediate Range. next = GetNextItem(next, rangeAncestors, ref usedRangeAncestor); } - textAdded = true; tb._UsedRangeAncestor = usedRangeAncestor; - return textAdded; + return true; } private static Dictionary GetAncestors(ItemInfo itemInfo) { @@ -622,25 +623,35 @@ namespace VEPROMS.CSLA.Library next = next.ActiveParent as ItemInfo; return next.NextItem; } - private static bool AddOptionalTranGetSectionHdr(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddOptionalTranGetSectionHdr(TransitionBuilder tb) { // first handle 'to' non-step section. Always add it. - if (tb._ToItem.IsSection && !tb._ToItem.IsStepSection)return AddTranGetSectionHdr(textAdded, tb, token, nonToken); + if (tb._ToItem.IsSection && !tb._ToItem.IsStepSection) return AddTranGetSectionHdr(tb); int cntsect = 0; ItemInfo sect = TranGetSectionItem(tb._ToItem); foreach (ItemInfo ii in tb._ToItem.MyProcedure.Sections) if (ii.IsStepSection) cntsect++; // If Procedure going to has only 1 step section, do not add it. - if (cntsect == 1) return textAdded ? true : false; + if (cntsect == 1) return false; // tb.TextAdded; // If procedure going to has more than 1 step section and is NOT same procedure // and going to default section, do not add it. - if (tb._ToItem.MyProcedure.ItemID != tb._FromItem.MyProcedure.ItemID && sect != null && - sect.IsDefaultSection) return textAdded ? true : false; - - return AddTranGetSectionHdr(textAdded, tb, token, nonToken); + if (tb._ToItem.MyProcedure.ItemID != tb._FromItem.MyProcedure.ItemID && sect != null && + sect.IsDefaultSection) return false; // tb.TextAdded; + + // If a 'thru' range, only put out the section if it's to a different section + // that is not the default section. + if (((tb._TransUI & E_TransUI.StepLast) == E_TransUI.StepLast) && ((tb._TransUI & E_TransUI.SectMenuStep) == E_TransUI.SectMenuStep)) + { + ItemInfo fi = tb._FromItem; + while (!fi.IsSection)fi = fi.ActiveParent as ItemInfo; + ItemInfo ti = tb._ToItem; + while (!ti.IsSection) ti = ti.ActiveParent as ItemInfo; + if (fi.ItemID == ti.ItemID || ti.IsDefaultSection) return false; // tb.TextAdded; + } + return AddTranGetSectionHdr(tb); } - private static bool AddTranGetSectionHdr(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddTranGetSectionHdr(TransitionBuilder tb) { // output from 16-bit code looks like section header // if this is a step section & the default section, just return @@ -650,43 +661,40 @@ namespace VEPROMS.CSLA.Library string txt = TranGetSectionTitle(tb,tb._ToItem); if (retstr.Length > 0 && txt.Length > 0) retstr.Append(", "); retstr.Append(txt); - if (textAdded) Append(tb, nonToken, false); - Append(tb, retstr.ToString(), true); - if (retstr.Length != 0) return true; - return false; + tb.AppendPrefix(); + tb.ReplaceToken(retstr.ToString()); + return (retstr.Length != 0); } - private static bool AddOptionalTranGetSectionTitle(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddOptionalTranGetSectionTitle(TransitionBuilder tb) { // token is passed in for future use, i.e. if text is placed between "?" and "." to // request other processing. // for now the only test is to check if the toitem is in the current section. - if (TranGetSectionItem(tb._FromItem).ItemID == TranGetSectionItem(tb._ToItem).ItemID) return textAdded ? true : false; - if (textAdded) Append(tb, nonToken, false); + if (TranGetSectionItem(tb._FromItem).ItemID == TranGetSectionItem(tb._ToItem).ItemID) return false; // tb.TextAdded; + tb.AppendPrefix(); string retstr = TranGetSectionTitle(tb,tb._ToItem); - Append(tb, retstr, true); - if (retstr != null && retstr != "") return true; - return false; + tb.ReplaceToken(retstr); + return (retstr != null && retstr != ""); } - private static bool AddTranGetSectionTitle(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddTranGetSectionTitle(TransitionBuilder tb) { // LATER: Cap1stSectionTitle, CapFirstLetterOnly if (tb._FormatData.TransData.UseSecTitles) { string retstr = TranGetSectionTitle(tb,tb._FromItem); - if (textAdded) Append(tb, nonToken, false); - Append(tb, retstr, true); - if (retstr != null && retstr != "") return true; + tb.AppendPrefix(); + tb.ReplaceToken(retstr); + return (retstr != null && retstr != ""); } return false; } - private static bool AddTranGetSectionNumber(bool textAdded, TransitionBuilder tb, string token, string nonToken) + private static bool AddTranGetSectionNumber(TransitionBuilder tb) { string retstr = TranGetSectionNumber(tb._ToItem); - if (textAdded) Append(tb, nonToken, false); - Append(tb, retstr, true); - if (retstr != null && retstr != "") return true; - return false; + tb.AppendPrefix(); + tb.ReplaceToken(retstr); + return (retstr != null && retstr != ""); } private static string CapFirstLetterOnly(string retstr, int p) {