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);
            }
        }
Beispiel #2
0
        private static ReactionParticipantViewVM GetParticipantView(ReactionParticipantVM participant)
        {
            try
            {
                var mainViewModel   = (App.Current.MainWindow as MainWindow).DataContext as MainVM;
                var participantView = new ReactionParticipantViewVM();
                participantView.ShortName     = participant.Num.ToString();
                participantView.TooltipText   = participant.Reg + " - " + participant.Name;
                participantView.Name          = participant.Name + "(" + participant.Num + ")";
                participantView.Formula       = participant.Formula;
                participantView.Num           = participant.Num;
                participantView.StageVM       = participant.StageVM;
                participantView.Reg           = participant.Reg;
                participantView.NextImagePath = PLUS_ICON;

                #region Color
                if (participant.ParticipantType == ParticipantType.Product)
                {
                    participantView.BorderBrush = StyleConstants.ProductBrush;
                    participantView.BgColor     = StyleConstants.ProductColor.Name;
                }
                else if (participant.ParticipantType == ParticipantType.Reactant)
                {
                    participantView.BorderBrush = StyleConstants.ReactantBrush;
                    participantView.BgColor     = StyleConstants.ReactantColor.Name;
                }
                else if (participant.ParticipantType == ParticipantType.Agent)
                {
                    participantView.BorderBrush = StyleConstants.AgentBrush;
                }
                else if (participant.ParticipantType == ParticipantType.Catalyst)
                {
                    participantView.BorderBrush = StyleConstants.CatalystBrush;
                }
                else if (participant.ParticipantType == ParticipantType.Solvent)
                {
                    participantView.BorderBrush = StyleConstants.SolventBrush;
                }
                #endregion

                #region Chemical Name
                TanChemicalVM chemicalName = null;
                if (participant.ChemicalType == ChemicalType.S8500 || participant.ChemicalType == ChemicalType.S9000)
                {
                    chemicalName = S.Find(participant.Reg);
                }
                else if (participant.ChemicalType == ChemicalType.NUM || participant.ChemicalType == ChemicalType.S8000)
                {
                    var tanChemical = (from p in mainViewModel.TanVM.TanChemicals where p.NUM == participant.Num select p).FirstOrDefault();
                    if (tanChemical != null)
                    {
                        chemicalName = new Models.TanChemicalVM
                        {
                            RegNumber       = tanChemical.RegNumber,
                            NUM             = tanChemical.NUM,
                            ChemicalType    = tanChemical.ChemicalType,
                            Name            = tanChemical.Name,
                            ImagePath       = tanChemical.ImagePath,
                            MolString       = tanChemical.MolString,
                            AllImagePaths   = tanChemical.Substancepaths.Select(s => s.ImagePath).Distinct().ToList(),
                            StereoChemisrty = tanChemical.ABSSterio
                        };
                    }
                }
                participantView.ChemicalName = chemicalName;
                #endregion
                return(participantView);
            }
            catch (Exception ex)
            {
                Log.This(ex);
                throw;
            }
        }