private ResultMatrixViewModel GetResultMatrix(string operationNumber, string pageChart = null)
        {
            ResultMatrixViewModel model = null;

            try
            {
                SetViewBagPermissionAndCheckAny(operationNumber, ActionEnum.FWIndicatorWritePermission);

                var response = _resultMatrixService.GetResultMatrix(operationNumber);
                SetViewBagErrorMessageInvalidResponse(response);
                model = response.ResultMatrix;
                if (pageChart != null)
                {
                    var fieldAccessList = _securityModelRepository.GetFieldBehaviorByPermissions(
                        IDBContext.Current.Operation,
                        pageChart,
                        IDBContext.Current.Permissions,
                        0,
                        0).ToList();
                    response.ResultMatrix.FieldAccessList = fieldAccessList;
                    response.ResultMatrix.Components.ForEach(o => o.Objectives.ForEach(a =>
                                                                                       a.FieldAccessList = fieldAccessList));
                }

                SetViewBag(operationNumber, model);
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
                ViewBag.IsValid      = false;
            }

            return(model);
        }
        private void SetViewBag(string operationNumber, ResultMatrixViewModel resultMatrix)
        {
            var outcomeResponse = _linkPredefinedIndicatorService.GetLinkIndicatorModelForCSResultMatrix(false, "LinkIndicator");

            ViewBag.LinkIndicatorModel = outcomeResponse.Filter;

            var expiredObjectiveResponse = _resultMatrixService.GetExpiredObjectives(operationNumber);

            ViewBag.AssociatedObjectiveList = expiredObjectiveResponse.ExpiredObjectives;

            ViewBag.CRFIndicators = new List <SelectListItem>();
            if (resultMatrix != null)
            {
                var allOutcomes           = resultMatrix.Components.SelectMany(x => x.Objectives).SelectMany(y => y.ExpectedOutcomeIndicators);
                var allCRFIndicatorIdUsed = allOutcomes.SelectMany(x => x.LinkedIndicators).Distinct();
                var crfIndicatorResponse  = _resultMatrixService.GetCRFIndicatorsById(allCRFIndicatorIdUsed);
                ViewBag.CRFIndicators = crfIndicatorResponse.CRFIndicators;
            }

            ViewBag.TCType = _enumMappingService.GetMappingCode(CSOperationTypeEnum.TechnicalCooperation);
            ViewBag.SGType = _enumMappingService.GetMappingCode(CSOperationTypeEnum.LoanOperation);
        }
        public static ResultMatrixViewModel UpdateResultMatrixViewModel(this ResultMatrixViewModel model, ClientFieldData[] formData)
        {
            var outcomeIdNew   = 0;
            var indicatorIdNew = 0;

            model.Components.Clear();

            var fieldsGroupedForComponent = formData.Where(x => !string.IsNullOrWhiteSpace(x.Id)).GroupBy(x => x.Id);

            foreach (var fieldsForComponent in fieldsGroupedForComponent)
            {
                var componentId = fieldsForComponent.Key;
                if (componentId.Contains("new-"))
                {
                    componentId = "0";
                }

                var newComponent = new ComponentViewModel()
                {
                    OperationId = model.OperationId,
                    ComponentId = componentId.ConvertToInt(),
                };
                model.Components.Add(newComponent);

                var field = fieldsForComponent.First(x => x.Name == "Component-OrderNumber");
                if (field != null)
                {
                    newComponent.OrderNumber = field.ConvertToInt();
                }

                field = fieldsForComponent.First(x => x.Name == "PriorityArea");
                newComponent.PriorityArea = field.ConvertToString();

                var fieldsGroupedForObjective = fieldsForComponent
                                                .Where(x => x.ExtraData.ContainsKey("data-persist-objectiveid"))
                                                .GroupBy(x => x.ExtraData.Single(y => y.Key == "data-persist-objectiveid"));
                foreach (var fieldsForObjective in fieldsGroupedForObjective)
                {
                    var objectiveId = fieldsForObjective.Key.Value;
                    if (objectiveId.Contains("new-"))
                    {
                        objectiveId = "0";
                    }

                    var newObjective = new ObjectiveViewModel()
                    {
                        ComponentId = newComponent.ComponentId,
                        ObjectiveId = objectiveId.ConvertToInt()
                    };
                    newComponent.Objectives.Add(newObjective);

                    field = fieldsForObjective.First(x => x.Name == "Objective-OrderNumber");
                    newObjective.OrderNumber = field.ConvertToInt();

                    field = fieldsForObjective.First(x => x.Name == "GovernmentPriority");
                    newObjective.GovernmentPriority = field.ConvertToString();

                    field = fieldsForObjective.First(x => x.Name == "countryStrategicObjective");
                    newObjective.CountryStrategicObjective = field.ConvertToString();

                    field = fieldsForObjective.First(x => x.Name == "countryStrategicObjectiveES");
                    newObjective.CountryStrategicObjectiveES = field.ConvertToString();

                    field = fieldsForObjective.FirstOrDefault(x => x.Name == "countryStrategicObjectivePT");
                    if (field != null)
                    {
                        newObjective.CountryStrategicObjectivePT = field.ConvertToString();
                    }

                    field = fieldsForObjective.FirstOrDefault(x => x.Name == "countryStrategicObjectiveFR");
                    if (field != null)
                    {
                        newObjective.CountryStrategicObjectiveFR = field.ConvertToString();
                    }

                    field = fieldsForObjective.FirstOrDefault(x => x.Name == "AssociatedExpiredCSObjective");
                    newObjective.AssociatedExpiredCSObjective = field.ConvertToNullableInt();

                    var fieldsGroupedForOutcome = fieldsForObjective
                                                  .Where(x => x.ExtraData.ContainsKey("data-persist-outcomeid"))
                                                  .GroupBy(x => x.ExtraData.Single(y => y.Key == "data-persist-outcomeid"));
                    foreach (var fieldsForOutcome in fieldsGroupedForOutcome)
                    {
                        var outcomeId = fieldsForOutcome.Key.Value;
                        if (outcomeId.Contains("new-"))
                        {
                            outcomeIdNew--;
                            outcomeId = outcomeIdNew.ToString();
                        }

                        field = fieldsForOutcome.First(x => x.Name == "ExpectedOutcome");
                        var ecpectedOutcome = field.ConvertToString();

                        var fieldsGroupedForIndicator = fieldsForOutcome
                                                        .Where(x => x.ExtraData.ContainsKey("data-persist-indicatorid"))
                                                        .GroupBy(x => x.ExtraData.Single(y => y.Key == "data-persist-indicatorid"));
                        foreach (var fieldsForIndicator in fieldsGroupedForIndicator)
                        {
                            var indicatorId = fieldsForIndicator.Key.Value;
                            if (indicatorId.Contains("new-"))
                            {
                                indicatorIdNew--;
                                indicatorId = indicatorIdNew.ToString();
                            }

                            var newOutcome = new ExpectedOutcomeIndicatorViewModel()
                            {
                                ComponentId       = newComponent.ComponentId,
                                ObjectiveId       = newObjective.ObjectiveId,
                                ExpectedOutcomeId = outcomeId.ConvertToInt(),
                                IndicatorId       = indicatorId.ConvertToInt()
                            };

                            newObjective.ExpectedOutcomeIndicators.Add(newOutcome);

                            newOutcome.ExpectedOutcome = ecpectedOutcome;

                            field = fieldsForIndicator.First(x => x.Name == "Indicator");
                            newOutcome.Indicator = field.ConvertToString();

                            field = fieldsForIndicator.First(x => x.Name == "UnitOfMeasure");
                            newOutcome.UnitOfMeasure = field.ConvertToString();

                            field = fieldsForIndicator.First(x => x.Name == "Baseline");
                            newOutcome.Baseline = field.ConvertToString();

                            field = fieldsForIndicator.First(x => x.Name == "BaselineYear");
                            newOutcome.BaselineYear = field.ConvertToString();

                            field             = fieldsForIndicator.First(x => x.Name == "Source");
                            newOutcome.Source = field.ConvertToString();

                            var linkedIndicators = fieldsForIndicator.Where(x => x.Name == "LinkedIndicator");

                            foreach (var linedField in linkedIndicators)
                            {
                                newOutcome.LinkedIndicators.Add(linedField.ConvertToInt());
                            }
                        }
                    }
                }
            }

            return(model);
        }