public static ReactionParticipantVM ValidateWithCVT1(Dictionary <ParticipantType, List <ReactionParticipantVM> > otherstageReactants, List <RsnVM> Rsns, ReactionVM SelectedReaction, TanChemicalVM chemicalName, ParticipantType participanttype)
        {
            var groupedValidations = S.GetGroupedCVTs();

            if (otherstageReactants != null && otherstageReactants.Any())
            {
                foreach (var type in otherstageReactants.Keys)
                {
                    if (groupedValidations.ContainsKey(participanttype) && groupedValidations[participanttype].Select(s => s.ExistingType).Contains(type))
                    {
                        var        cvt          = groupedValidations[participanttype].Where(c => c.ExistingType == type).FirstOrDefault();
                        List <int> StageNumbers = PressureValidations.GetStageDisplayOrdersFromFreetexts(Rsns.ToList(), SelectedReaction, cvt.AssociatedFreeText);
                        if (StageNumbers.Contains(SelectedReaction.SelectedStage.DisplayOrder))
                        {
                            var rsnVM = Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage != null && StageNumbers.Contains(rsn.Stage.DisplayOrder) && rsn.SelectedChemical == null &&
                                                   rsn.FreeText.SafeContainsLower(cvt.AssociatedFreeText)).FirstOrDefault();
                            if (rsnVM != null)
                            {
                                rsnVM.SelectedChemical = chemicalName;
                            }
                            return(null);
                        }
                        else
                        {
                            return(otherstageReactants[type].FirstOrDefault());
                        }
                    }
                    else
                    {
                        return(otherstageReactants.Select(s => s.Value).FirstOrDefault().FirstOrDefault());
                    }
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        public static bool ValidateRsnFreetext(string FreeText, ReactionVM ReactionVM, StageVM StageVM, RsnLevel RsnLevel, out string outMsg)
        {
            string freetextREstring = S.RegularExpressions.Where(re => re.RegulerExpressionFor == ProductTracking.Models.Core.RegulerExpressionFor.FreeText).Select(re => re.Expression).FirstOrDefault();
            Regex  FreetextRE       = new Regex(freetextREstring);
            var    SPLList          = new List <string> {
                "==", "%%", ",,", "((", "))", "{{", "}}", "++", "//", "\\", "::", ";;", "--", "..", "  ", "''", "<<", ">>", "**", "@@", "[[", "]]", ", ,", ").", ".,", " ."
            };

            if (!string.IsNullOrEmpty(FreeText) && SPLList.Where(spl => FreeText.Contains(spl)).FirstOrDefault() != null)
            {
                outMsg = $"Freetext contains invalid repetation of special Characters <SPL Char Start>{SPLList.Where(spl => FreeText.Contains(spl)).FirstOrDefault()}</SPL Char End>.";
                return(false);
            }
            if (!string.IsNullOrEmpty(FreeText) && StageVM != null && RsnLevel == RsnLevel.STAGE)
            {
                string[] list = FreeText.Split(new string[] { "), " }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var freetext in list)
                {
                    string     newText = !freetext.EndsWith(")") ? $"{freetext})" : freetext;
                    List <int> data    = PressureValidations.GetStageDisplayOrdersFromFreetexts(new List <RsnVM> {
                        new ViewModels.RsnVM {
                            Reaction = ReactionVM, Stage = StageVM, FreeText = newText
                        }
                    }, ReactionVM, FreeTextWithOutStageInfo(newText));
                    if (!data.Contains(StageVM.DisplayOrder) && RsnLevel == RsnLevel.STAGE)
                    {
                        outMsg = $"Freetext must contain current stage Number in '{freetext}'";
                        return(false);
                    }
                }
            }
            if (RsnLevel == RsnLevel.REACTION && !string.IsNullOrEmpty(FreeText) && FreeText.Contains(","))
            {
                var list = $" {FreeText}".Split(',');
                if (list.Where(l => !l.StartsWith(" ")).Any())
                {
                    outMsg = "FreeText must seperated by comma and space in Reaction level.";
                    return(false);
                }
            }
            if (RsnLevel == RsnLevel.STAGE && !string.IsNullOrEmpty(FreeText) && FreeText.Contains("),"))
            {
                var list = $" {FreeText}".Split(new string[] { ")," }, StringSplitOptions.RemoveEmptyEntries);
                if (list.Where(l => !l.StartsWith(" ")).Any())
                {
                    outMsg = "FreeText must seperated by comma and space in stage level.";
                    return(false);
                }
            }
            if (!string.IsNullOrEmpty(FreeText) && FreeText.Contains(", ") && FreeText.ToLower().Split(new string[] { RsnLevel == RsnLevel.REACTION ? ", " : "), " }, StringSplitOptions.RemoveEmptyEntries).GroupBy(s => FreeTextWithOutStageInfo(s)).SelectMany(grp => grp.Skip(1)).Count() > 0)
            {
                outMsg = "FreeText Contains duplicates.";
                return(false);
            }

            if (!string.IsNullOrEmpty(FreeText) && FreeText.Contains(", ") && FreeText.ToLower().Split(new string[] { RsnLevel == RsnLevel.REACTION ? ", " : "), " }, StringSplitOptions.RemoveEmptyEntries).Where(s => s.EndsWith(".")).Count() > 0)
            {
                outMsg = "FreeText ending with .(Period)";
                return(false);
            }

            if (RsnLevel == RsnLevel.STAGE && StageVM == null)
            {
                outMsg = "Please Select Stage to Add Stage RSN";
                return(false);
            }

            if (RsnLevel == RsnLevel.REACTION && !String.IsNullOrEmpty(FreeText) && FreeText.Contains("(stage"))
            {
                outMsg = "Reaction Level stage information Not allowed";
                return(false);
            }

            if (!string.IsNullOrEmpty(FreeText) && !FreetextRE.IsMatch(FreeText))
            {
                outMsg = "FreeText Contains special characters.";
                return(false);
            }
            outMsg = string.Empty;
            return(true);
        }
        public static ReactionParticipantVM AlreadyContains(TanChemical selectedChemical, ViewAnalogousVM analogousreactionVM, ReactionVM SelectedReaction, ParticipantType participantType, [Optional] List <ReactionParticipantVM> ReactionParticipant, [Optional] List <RsnVM> Rsn, [Optional] bool BulkValidation)
        {
            try
            {
                var ReactionParticipants = ReactionParticipant == null?analogousreactionVM.ReactionParticipants.OfReaction(SelectedReaction.Id) : ReactionParticipant;

                var Rsns = Rsn == null?analogousreactionVM.Rsns.OfReaction(SelectedReaction.Id, true).ToList() : Rsn;

                var chemicalName = new Models.TanChemicalVM
                {
                    Id        = selectedChemical.Id,
                    Name      = selectedChemical.Name,
                    NUM       = selectedChemical.NUM,
                    RegNumber = selectedChemical.RegNumber
                };
                var groupedValidations = S.GetGroupedCVTs();
                if (ReactionParticipants.Count() > 0)
                {
                    if (participantType == ParticipantType.Product)
                    {
                        var existingProducts = ReactionParticipants.OfReaction(SelectedReaction.Id).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                        if (existingProducts != null && existingProducts.Any())
                        {
                            return(existingProducts.FirstOrDefault());
                        }
                    }

                    else if (SelectedReaction.SelectedStage != null)
                    {
                        #region Reactant Validations
                        if (participantType == ParticipantType.Reactant)
                        {
                            var stageReactants = ReactionParticipants.OfReactionAndStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber).OfExceptTypes(ParticipantType.Solvent);
                            if (stageReactants != null && stageReactants.OfType(ParticipantType.Reactant).Any())
                            {
                                return(stageReactants.FirstOrDefault());
                            }
                            List <int> StageNumbers = new List <int>();
                            if (SelectedReaction.Stages.Count > 1)
                            {
                                StageNumbers = PressureValidations.GetStageDisplayOrdersFromFreetexts(Rsns, SelectedReaction, S.DUAL_ROLE_STRING);
                            }
                            else if (Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage == null && rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING)).Any())
                            {
                                StageNumbers.Add(1);
                            }
                            if (stageReactants != null && stageReactants.OfExceptTypes(ParticipantType.Reactant).Any())
                            {
                                if (!StageNumbers.Contains(SelectedReaction.SelectedStage.DisplayOrder))
                                {
                                    return(stageReactants.OfExceptTypes(ParticipantType.Reactant).FirstOrDefault());
                                }
                                else
                                {
                                    var rsnVM = Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id &&
                                                           rsn.SelectedChemical == null &&
                                                           (rsn.FreeText.SafeContainsLower(S.DUAL_ROLE_STRING) || rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING))).FirstOrDefault();
                                    if (rsnVM != null)
                                    {
                                        rsnVM.SelectedChemical = chemicalName;
                                    }
                                }
                            }

                            var Products = ReactionParticipants.OfReactionOfType(SelectedReaction.Id, ParticipantType.Product).OfNum(selectedChemical.NUM);
                            if (Products != null && Products.Any())
                            {
                                return(Products.FirstOrDefault());
                            }

                            var AmdProducts = ReactionParticipants.OfReactionOfType(SelectedReaction.Id, ParticipantType.Product).Where(ro => ro.Reg == selectedChemical.RegNumber && ro.Num != selectedChemical.NUM);
                            if (AmdProducts != null && AmdProducts.Any())
                            {
                                string       msg          = $"Selected chemical with same Reg and different num already involved in {AmdProducts.FirstOrDefault().ReactionVM.DisplayName} as a Product. Do you want to still add as Reactant?";
                                DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(msg, "Reactions", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                if (dialogResult == DialogResult.No)
                                {
                                    return(AmdProducts.FirstOrDefault());
                                }
                            }

                            var otherstageReactants = ReactionParticipants.OfReactionAndExceptStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id)
                                                      .OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber)
                                                      .OfExceptTypes(ParticipantType.Solvent, ParticipantType.Product)
                                                      .GroupBy(p => p.ParticipantType)
                                                      .ToDictionary(p => p.Key, p => p.ToList());
                            var duplicate = ValidateWithCVT1(otherstageReactants, Rsns, SelectedReaction, chemicalName, ParticipantType.Reactant);
                            if (duplicate != null)
                            {
                                return(duplicate);
                            }
                        }
                        #endregion

                        #region Solvent Validation
                        if (participantType == ParticipantType.Solvent)
                        {
                            var InStageSolvents = ReactionParticipants.OfReactionAndStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id).OfExceptTypes(ParticipantType.Reactant).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                            if (InStageSolvents != null && InStageSolvents.OfType(ParticipantType.Solvent).Any())
                            {
                                return(InStageSolvents.FirstOrDefault());
                            }
                            List <int> StageNumbers = new List <int>();
                            if (SelectedReaction.Stages.Count > 1)
                            {
                                StageNumbers = PressureValidations.GetStageDisplayOrdersFromFreetexts(Rsns, SelectedReaction, S.DUAL_ROLE_STRING);
                            }
                            else if (Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage == null && rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING)).Any())
                            {
                                StageNumbers.Add(1);
                            }
                            if (InStageSolvents != null && InStageSolvents.OfExceptTypes(ParticipantType.Solvent).Any())
                            {
                                if (!StageNumbers.Contains(SelectedReaction.SelectedStage.DisplayOrder))
                                {
                                    return(InStageSolvents.OfExceptTypes(ParticipantType.Solvent).FirstOrDefault());
                                }
                                else
                                {
                                    var rsnVM = Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id &&
                                                           rsn.SelectedChemical == null &&
                                                           (rsn.FreeText.SafeContainsLower(S.DUAL_ROLE_STRING) || rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING))).FirstOrDefault();
                                    if (rsnVM != null)
                                    {
                                        rsnVM.SelectedChemical = chemicalName;
                                    }
                                }
                            }

                            var Products = ReactionParticipants.OfReactionOfType(SelectedReaction.Id, ParticipantType.Product).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                            if (Products != null && Products.Any())
                            {
                                return(Products.FirstOrDefault());
                            }
                            var otherthanProductsAndSolvents = ReactionParticipants.OfReactionAndExceptStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id)
                                                               .OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber)
                                                               .OfExceptTypes(ParticipantType.Solvent, ParticipantType.Reactant, ParticipantType.Product)
                                                               .GroupBy(p => p.ParticipantType)
                                                               .ToDictionary(p => p.Key, p => p.ToList());
                            var duplicate = ValidateWithCVT1(otherthanProductsAndSolvents, Rsns, SelectedReaction, chemicalName, ParticipantType.Solvent);
                            if (duplicate != null)
                            {
                                return(duplicate);
                            }
                        }
                        #endregion

                        #region Catalyst Validations
                        if (participantType == ParticipantType.Catalyst)
                        {
                            var InStageCatalysts = ReactionParticipants.OfReactionAndStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                            if (InStageCatalysts != null && InStageCatalysts.OfType(ParticipantType.Catalyst).Any())
                            {
                                return(InStageCatalysts.FirstOrDefault());
                            }
                            List <int> StageNumbers = new List <int>();
                            if (SelectedReaction.Stages.Count > 1)
                            {
                                StageNumbers = PressureValidations.GetStageDisplayOrdersFromFreetexts(Rsns, SelectedReaction, S.DUAL_ROLE_STRING);
                            }
                            else if (Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage == null && rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING)).Any())
                            {
                                StageNumbers.Add(1);
                            }
                            if (InStageCatalysts != null && InStageCatalysts.OfExceptTypes(ParticipantType.Catalyst).Any())
                            {
                                if (!StageNumbers.Contains(SelectedReaction.SelectedStage.DisplayOrder))
                                {
                                    return(InStageCatalysts.OfExceptTypes(ParticipantType.Catalyst).FirstOrDefault());
                                }
                                else
                                {
                                    var rsnVM = Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id &&
                                                           rsn.SelectedChemical == null &&
                                                           (rsn.FreeText.SafeContainsLower(S.DUAL_ROLE_STRING) || rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING))).FirstOrDefault();
                                    if (rsnVM != null)
                                    {
                                        rsnVM.SelectedChemical = chemicalName;
                                    }
                                }
                            }
                            var Products = ReactionParticipants.OfReactionOfType(SelectedReaction.Id, ParticipantType.Product).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                            if (Products != null && Products.Any())
                            {
                                return(Products.FirstOrDefault());
                            }

                            var otherThanCatalysts = ReactionParticipants.OfReactionAndExceptStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id)
                                                     .OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber)
                                                     .OfExceptTypes(ParticipantType.Catalyst, ParticipantType.Product)
                                                     .GroupBy(p => p.ParticipantType)
                                                     .ToDictionary(p => p.Key, p => p.ToList());
                            var duplicate = ValidateWithCVT1(otherThanCatalysts, Rsns, SelectedReaction, chemicalName, ParticipantType.Catalyst);
                            if (duplicate != null)
                            {
                                return(duplicate);
                            }
                        }
                        #endregion
                        #region Agent Validations
                        if (participantType == ParticipantType.Agent)
                        {
                            var InStageAgents = ReactionParticipants.OfReactionAndStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                            if (InStageAgents != null && InStageAgents.OfType(ParticipantType.Agent).Any())
                            {
                                return(InStageAgents.FirstOrDefault());
                            }
                            List <int> StageNumbers = new List <int>();
                            if (SelectedReaction.Stages.Count > 1)
                            {
                                StageNumbers = PressureValidations.GetStageDisplayOrdersFromFreetexts(Rsns, SelectedReaction, S.DUAL_ROLE_STRING);
                            }
                            else if (Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage == null && rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING)).Any())
                            {
                                StageNumbers.Add(1);
                            }
                            if (InStageAgents != null && InStageAgents.OfExceptTypes(ParticipantType.Agent).Any())
                            {
                                if (!StageNumbers.Contains(SelectedReaction.SelectedStage.DisplayOrder))
                                {
                                    return(InStageAgents.OfExceptTypes(ParticipantType.Agent).FirstOrDefault());
                                }
                                else
                                {
                                    var rsnVM = Rsns.Where(rsn => rsn.Reaction.Id == SelectedReaction.Id &&
                                                           rsn.SelectedChemical == null &&
                                                           (rsn.FreeText.SafeContainsLower(S.DUAL_ROLE_STRING) || rsn.CvtText.SafeEqualsLower(S.DUAL_ROLE_STRING))).FirstOrDefault();
                                    if (rsnVM != null)
                                    {
                                        rsnVM.SelectedChemical = chemicalName;
                                    }
                                }
                            }

                            var Products = ReactionParticipants.OfReactionOfType(SelectedReaction.Id, ParticipantType.Product).OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber);
                            if (Products != null && Products.Any())
                            {
                                return(Products.FirstOrDefault());
                            }

                            var otherStageAgents = ReactionParticipants.OfReactionAndExceptStage(SelectedReaction.Id, SelectedReaction.SelectedStage.Id)
                                                   .OfNumOrReg(selectedChemical.NUM, selectedChemical.RegNumber)
                                                   .OfExceptTypes(ParticipantType.Agent, ParticipantType.Product)
                                                   .GroupBy(p => p.ParticipantType)
                                                   .ToDictionary(p => p.Key, p => p.ToList());

                            var duplicate = ValidateWithCVT1(otherStageAgents, Rsns, SelectedReaction, chemicalName, ParticipantType.Agent);
                            if (duplicate != null)
                            {
                                return(duplicate);
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        return(null);
                    }
                }
                return(null);
            }
            catch (Exception ex)
            {
                Log.This(ex);
                return(null);
            }
        }
Exemplo n.º 4
0
        public static bool ValidateAndAddRsn(string FreeText, string CVT, List <RsnVM> Rsns, List <CvtVM> CVTData, ReactionVM ReactionVM, StageVM StageVM, RsnLevel RsnLevel, Regex regex, RSNWindowVM RSNWindowVM, RsnVM EditingRsn = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(FreeText.Trim()) || !string.IsNullOrEmpty(CVT))
                {
                    string freetextREstring = S.RegularExpressions.Where(re => re.RegulerExpressionFor == ProductTracking.Models.Core.RegulerExpressionFor.FreeText).Select(re => re.Expression).FirstOrDefault();
                    Regex  FreetextRE       = new Regex(freetextREstring);
                    var    SPLList          = new List <string> {
                        "==", "%%", ",,", "((", "))", "{{", "}}", "++", "//", "\\", "::", ";;", "--", "..", "  ", "''", "<<", ">>", "**", "@@", "[[", "]]", ", ,", ").", ".,", " ."
                    };
                    if (!string.IsNullOrEmpty(FreeText) && SPLList.Where(spl => FreeText.Contains(spl)).FirstOrDefault() != null)
                    {
                        AppInfoBox.ShowInfoMessage($"Freetext contains invalid repetation of special Characters <SPL Char Start>{SPLList.Where(spl => FreeText.Contains(spl)).FirstOrDefault()}</SPL Char End>.");
                        //MessageBox.Show($"Freetext contains invalid repetation of special Characters {SPLList.Where(spl => FreeText.Contains(spl)).FirstOrDefault()}.", "Reactions", MessageBoxButton.OK, MessageBoxImage.Information);
                        return(false);
                    }
                    if (!string.IsNullOrEmpty(FreeText) && FreeText.Contains("), ") && StageVM != null && RsnLevel == RsnLevel.STAGE)
                    {
                        string[] list = FreeText.Split(new string[] { "), " }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var freetext in list)
                        {
                            string newText = !freetext.EndsWith(")") ? $"{freetext})" : freetext;
                            if (regex.IsMatch(newText))
                            {
                                List <int> data = PressureValidations.GetStageDisplayOrdersFromFreetexts(new List <RsnVM> {
                                    new ViewModels.RsnVM {
                                        Reaction = ReactionVM, Stage = StageVM, FreeText = newText
                                    }
                                }, ReactionVM, FreeTextWithOutStageInfo(newText));
                                if (!data.Contains(StageVM.DisplayOrder) && RsnLevel == RsnLevel.STAGE)
                                {
                                    AppInfoBox.ShowInfoMessage("Comma and space allowed only after stage information");
                                    return(false);
                                }
                            }
                            else
                            {
                                AppInfoBox.ShowInfoMessage($"Invalid freetext '{newText}'");
                                return(false);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(FreeText) && FreeText.Contains(","))
                    {
                        string[] list = FreeText.Split(',');
                        for (int i = 1; i < list.Length; i++)
                        {
                            if (!Regex.IsMatch(list[i], @"^\s|\d"))
                            {
                                AppInfoBox.ShowInfoMessage("invalid freetext. It contains extra characters");
                                return(false);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(FreeText) && FreeText.Contains(", ") && FreeText.ToLower().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).GroupBy(s => FreeTextWithOutStageInfo(s)).SelectMany(grp => grp.Skip(1)).Count() > 0)
                    {
                        AppInfoBox.ShowInfoMessage("FreeText Contains duplicates.");
                        return(false);
                    }

                    if (!string.IsNullOrEmpty(FreeText) && FreeText.Contains(", ") && FreeText.ToLower().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Where(s => s.EndsWith(".")).Count() > 0)
                    {
                        AppInfoBox.ShowInfoMessage("FreeText Contains .(Period)");
                        return(false);
                    }

                    if (RsnLevel == RsnLevel.STAGE && StageVM == null)
                    {
                        AppInfoBox.ShowInfoMessage("Please Select Stage to Add Stage RSN");
                        return(false);
                    }

                    if (RsnLevel == RsnLevel.REACTION && !String.IsNullOrEmpty(FreeText) && FreeText.Contains("(stage"))
                    {
                        AppInfoBox.ShowInfoMessage("Reaction Level stage information Not allowed");
                        return(false);
                    }

                    if (!string.IsNullOrEmpty(FreeText) && !FreetextRE.IsMatch(FreeText))
                    {
                        AppInfoBox.ShowInfoMessage("FreeText Contains special characters.");
                        return(false);
                    }
                    if (EditingRsn != null)
                    {
                        bool OnlyOneFreeTextInReactionLevel = (CVT == String.Empty && RsnLevel == RsnLevel.REACTION && Rsns.Any(r => r.Reaction != null && r.Reaction.Id == ReactionVM.Id && (EditingRsn != null ? r.Id != EditingRsn.Id : true) && r.Stage == null && r.CvtText == String.Empty)) ? false : true;
                        if (OnlyOneFreeTextInReactionLevel)
                        {
                            bool OnlyOneFreeTextInStageLevel = (CVT == String.Empty && RsnLevel == RsnLevel.STAGE && Rsns.Any(r => r.Reaction != null && (EditingRsn != null ? r.Id != EditingRsn.Id : true) && r.Reaction.Id == ReactionVM.Id && r.Stage != null && r.Stage.Id == StageVM.Id && r.CvtText == String.Empty)) ? false : true;
                            if (OnlyOneFreeTextInStageLevel)
                            {
                                if (!String.IsNullOrEmpty(CVT) && Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && (EditingRsn != null ? r.Id != EditingRsn.Id : true) && r.CvtText != string.Empty && r.CvtText == CVT).Count() > 0)
                                {
                                    var SelectedRSNTerm = Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && (EditingRsn != null ? r.Id != EditingRsn.Id : true) && r.CvtText == CVT).FirstOrDefault();
                                    AppInfoBox.ShowInfoMessage("Selected CVT " + (!String.IsNullOrEmpty(CVT) ? CVT : FreeText) + " Already used in " + (SelectedRSNTerm?.Stage != null ? SelectedRSNTerm?.Stage.Name : SelectedRSNTerm.Reaction.Name));
                                    return(false);
                                }
                                if (!string.IsNullOrEmpty(FreeText))
                                {
                                    var splittedFreetexts = FreeText.Split(new String[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Select(c => FreeTextWithOutStageInfo(c));
                                    foreach (var item in splittedFreetexts)
                                    {
                                        if (CVTData.Where(cvt => item.Trim().SafeEqualsLower(cvt.Text.Trim()) && !cvt.Text.Trim().SafeEqualsLower(CVT.Trim())).Count() > 0)
                                        {
                                            AppInfoBox.ShowInfoMessage($"Selected FreeText contains CVT \"{item}\"");
                                            return(false);
                                        }
                                        if (!string.IsNullOrEmpty(item) && Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && (EditingRsn != null ? r.Id != EditingRsn.Id : true) && ((!String.IsNullOrEmpty(r.FreeText) && r.FreeText.Split(new String[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Where(c => FreeTextWithOutStageInfo(c).Trim().SafeEqualsLower(item.Trim())).Count() > 0) || (!string.IsNullOrEmpty(r.CvtText) && item.Trim().SafeContainsLower(r.CvtText.Trim())))).Count() > 0)
                                        {
                                            var SelectedRSNTerm = Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && (EditingRsn != null ? r.Id != EditingRsn.Id : true) && ((!String.IsNullOrEmpty(r.FreeText) && r.FreeText.Split(new String[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Where(c => FreeTextWithOutStageInfo(c).Trim().SafeEqualsLower(item.Trim())).Count() > 0) || (!string.IsNullOrEmpty(r.CvtText) && item.Trim().SafeContainsLower(r.CvtText.Trim())))).FirstOrDefault();
                                            AppInfoBox.ShowInfoMessage("Selected FreeText " + FreeText + " Already used in " + (SelectedRSNTerm?.Stage != null ? SelectedRSNTerm?.Stage.Name : SelectedRSNTerm.Reaction.Name));
                                            return(false);
                                        }
                                    }
                                }

                                if (RsnLevel == RsnLevel.STAGE)
                                {
                                    if (!string.IsNullOrEmpty(CVT) && string.IsNullOrEmpty(FreeText))
                                    {
                                        AppInfoBox.ShowInfoMessage("Stage Level CVT Used, Then Freetext is mandatory..");
                                        return(false);
                                    }
                                    else if (!string.IsNullOrEmpty(FreeText) && regex.IsMatch(FreeText))
                                    {
                                        string outMsg = string.Empty;
                                        if (!ValidateRsnEditArea(FreeText, regex, ReactionVM, StageVM, out outMsg))
                                        {
                                            AppInfoBox.ShowInfoMessage(outMsg);
                                            return(false);
                                        }
                                        return(true);
                                    }
                                    else
                                    {
                                        AppInfoBox.ShowInfoMessage("Stage Level Information missed in Freetext Term... / Ends with some Special characters");
                                        return(false);
                                    }
                                }
                                return(true);
                            }
                            else
                            {
                                AppInfoBox.ShowInfoMessage("Only One Stage Level Free Text Is Allowed With out CVT . .");
                                return(false);
                            }
                        }
                        else
                        {
                            AppInfoBox.ShowInfoMessage("Only One Reaction Level Free Text Is Allowed With out CVT . .");
                            return(false);
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(CVT) || !String.IsNullOrEmpty(FreeText))
                        {
                            bool OnlyOneFreeTextInReactionLevel = (CVT == String.Empty && RsnLevel == RsnLevel.REACTION && Rsns.Any(r => r.Reaction != null && r.Reaction.Id == ReactionVM.Id && r.Stage == null && string.IsNullOrEmpty(r.CvtText))) ? false : true;
                            if (OnlyOneFreeTextInReactionLevel)
                            {
                                bool OnlyOneFreeTextInStageLevel = (CVT == String.Empty && RsnLevel == RsnLevel.STAGE && Rsns.Any(r => r.Reaction != null && r.Reaction.Id == ReactionVM.Id && r.Stage != null && r.Stage.Id == StageVM.Id && string.IsNullOrEmpty(r.CvtText))) ? false : true;
                                if (OnlyOneFreeTextInStageLevel)
                                {
                                    if (!String.IsNullOrEmpty(CVT) && Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && r.CvtText.SafeEqualsLower(CVT)).Count() > 0)
                                    {
                                        var SelectedRSNTerm = Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && r.CvtText.SafeEqualsLower(CVT)).FirstOrDefault();
                                        AppInfoBox.ShowInfoMessage("Selected CVT " + (!String.IsNullOrEmpty(CVT) ? CVT : FreeText) + " Already used in " + (SelectedRSNTerm?.Stage != null ? SelectedRSNTerm?.Stage.Name : SelectedRSNTerm.Reaction.Name));
                                        return(false);
                                    }

                                    if (!string.IsNullOrEmpty(FreeText))
                                    {
                                        var splittedFreetexts = FreeText.Split(new String[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Select(c => FreeTextWithOutStageInfo(c));
                                        foreach (var item in splittedFreetexts)
                                        {
                                            if (CVTData.Where(cvt => item.Trim().SafeEqualsLower(cvt.Text.Trim()) && !cvt.Text.Trim().SafeEqualsLower(CVT.Trim())).Count() > 0)
                                            {
                                                AppInfoBox.ShowInfoMessage($"Selected FreeText contains CVT \"{item}\"");
                                                return(false);
                                            }
                                            if (!string.IsNullOrEmpty(item) && Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && ((!String.IsNullOrEmpty(r.FreeText) && r.FreeText.Split(new String[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Where(c => FreeTextWithOutStageInfo(c).Trim().SafeEqualsLower(item.Trim())).Count() > 0) || (!string.IsNullOrEmpty(r.CvtText) && item.Trim().SafeContainsLower(r.CvtText.Trim())))).Count() > 0)
                                            {
                                                var SelectedRSNTerm = Rsns.Where(r => r.Reaction.Id == ReactionVM.Id && ((!String.IsNullOrEmpty(r.FreeText) && r.FreeText.Split(new String[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Where(c => FreeTextWithOutStageInfo(c).Trim().SafeEqualsLower(item.Trim())).Count() > 0) || (!string.IsNullOrEmpty(r.CvtText) && item.Trim().SafeContainsLower(r.CvtText.Trim())))).FirstOrDefault();
                                                AppInfoBox.ShowInfoMessage($"Selected FreeText '{FreeText}' Already used in {(SelectedRSNTerm?.Stage != null ? SelectedRSNTerm?.Stage.Name : SelectedRSNTerm.Reaction.Name)}");
                                                return(false);
                                            }
                                        }
                                    }
                                    if (RsnLevel == RsnLevel.STAGE)
                                    {
                                        if (!string.IsNullOrEmpty(CVT) && string.IsNullOrEmpty(FreeText))
                                        {
                                            AppInfoBox.ShowInfoMessage("Stage Level CVT Used, Then Freetext is mandatory..");
                                            return(false);
                                        }
                                        else if (!string.IsNullOrEmpty(FreeText) && regex.IsMatch(FreeText))
                                        {
                                            string OutMsg = string.Empty;
                                            if (!ValidateRsnEditArea(FreeText, regex, ReactionVM, StageVM, out OutMsg))
                                            {
                                                AppErrorBox.ShowErrorMessage(OutMsg, String.Empty);
                                                return(false);
                                            }
                                            return(true);
                                        }
                                        else
                                        {
                                            AppInfoBox.ShowInfoMessage("Stage Level Information missed in Freetext Term...");
                                            return(false);
                                        }
                                    }
                                    return(true);
                                }
                                else
                                {
                                    AppInfoBox.ShowInfoMessage("Only One Stage Level Free Text Is Allowed With out CVT . .");
                                    return(false);
                                }
                            }
                            else
                            {
                                AppInfoBox.ShowInfoMessage("Only One Reaction Level Free Text Is Allowed With out CVT . .");
                                return(false);
                            }
                        }
                        return(false);
                    }
                    if (Rsns.Where(rsn => rsn.CvtText.SafeEqualsLower(S.ENZYMIC_CVT)).Count() > 0 && Rsns.Where(rsn => rsn.CvtText.SafeEqualsLower(S.BIOTRANSFORMATION_CVT)).Count() == 0)
                    {
                        var    enzymicRSN    = Rsns.Where(rsn => rsn.CvtText.SafeEqualsLower(S.ENZYMIC_CVT)).FirstOrDefault();
                        string freeTextToAdd = !string.IsNullOrEmpty(enzymicRSN.FreeText) ? StageInfoWithOutFreeText(enzymicRSN.FreeText, regex) : string.Empty;
                        Rsns.Add(new RsnVM
                        {
                            CvtText     = S.BIOTRANSFORMATION_CVT,
                            FreeText    = $"{S.BIOTRANSFORMATION_FREETEXT} {freeTextToAdd}",
                            IsRXN       = true,
                            Stage       = enzymicRSN.Stage != null ? enzymicRSN.Stage : null,
                            Reaction    = ReactionVM,
                            RSNWindowVM = RSNWindowVM,
                            Id          = Guid.NewGuid()
                        });
                    }
                    //ClearEditForm.Execute(this);
                }
                else
                {
                    AppInfoBox.ShowInfoMessage("Either CVT or FreeText mandatory to save RSN");
                }
                return(true);
            }
            catch (Exception ex)
            {
                Log.This(ex);
                AppErrorBox.ShowErrorMessage(ex.Message, ex.ToString());
                return(false);
            }
        }