public SpendingByOrganizationTypeByOrganizationViewData(TenantAttribute tenantAttribute, List <OrganizationType> organizationTypes, List <ProjectFirmaModels.Models.ProjectFundingSourceExpenditure> projectFundingSourceExpenditures, List <TaxonomyTier> taxonomyTiers)
        {
            OrganizationTypes = organizationTypes;
            ProjectFundingSourceExpenditures = projectFundingSourceExpenditures;
            TaxonomyTiers           = taxonomyTiers;
            TaxonomyTierDisplayName = TaxonomyTierHelpers.GetFieldDefinitionForTaxonomyLevel(tenantAttribute.TaxonomyLevel).GetFieldDefinitionLabel();

            ProjectFundingSourceExpendituresByTaxonomyTierID = ProjectFundingSourceExpenditures.GroupBy(x => {
                switch (tenantAttribute.TaxonomyLevel.ToEnum)
                {
                case TaxonomyLevelEnum.Trunk:
                    return(x.Project.GetTaxonomyLeaf().TaxonomyBranch.TaxonomyTrunkID);

                case TaxonomyLevelEnum.Branch:
                    return(x.Project.GetTaxonomyLeaf().TaxonomyBranchID);

                case TaxonomyLevelEnum.Leaf:
                    return(x.Project.GetTaxonomyLeaf().TaxonomyLeafID);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            })
                                                               .ToDictionary(x => x.Key, x => x.ToList() as IEnumerable <ProjectFirmaModels.Models.ProjectFundingSourceExpenditure>);
        }
        public void UpdateModel(ProjectFirmaModels.Models.Organization organization,
                                ICollection <MatchmakerOrganizationTaxonomyTrunk> allMatchMakerOrganizationTaxonomyTrunks,
                                ICollection <MatchmakerOrganizationTaxonomyBranch> allMatchMakerOrganizationTaxonomyBranches,
                                ICollection <MatchmakerOrganizationTaxonomyLeaf> allMatchMakerOrganizationTaxonomyLeafs
                                )
        {
            // determine which taxonomy level each key in TaxonomyCompoundKeys is
            var updatedTaxonomyTrunkIDs  = new List <int>();
            var updatedTaxonomyBranchIDs = new List <int>();
            var updatedTaxonomyLeafIDs   = new List <int>();

            if (TaxonomyCompoundKeys == null)
            {
                TaxonomyCompoundKeys = new List <string>();
            }
            foreach (var compoundKey in TaxonomyCompoundKeys)
            {
                var levelAndID = TaxonomyTierHelpers.GetTaxonomyLevelAndIDFromComboTreeNodeKey(compoundKey);
                switch (levelAndID.Item1)
                {
                case TaxonomyLevelEnum.Trunk:
                    updatedTaxonomyTrunkIDs.Add(levelAndID.Item2);
                    break;

                case TaxonomyLevelEnum.Branch:
                    updatedTaxonomyBranchIDs.Add(levelAndID.Item2);
                    break;

                case TaxonomyLevelEnum.Leaf:
                    updatedTaxonomyLeafIDs.Add(levelAndID.Item2);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (MultiTenantHelpers.IsTaxonomyLevelTrunk())
            {
                // merge trunk
                var taxonomyTrunksUpdated = updatedTaxonomyTrunkIDs.Select(x => new MatchmakerOrganizationTaxonomyTrunk(organization.OrganizationID, x, true)).ToList();
                organization.MatchmakerOrganizationTaxonomyTrunks.Merge(taxonomyTrunksUpdated,
                                                                        allMatchMakerOrganizationTaxonomyTrunks,
                                                                        (x, y) => x.OrganizationID == y.OrganizationID && x.TaxonomyTrunkID == y.TaxonomyTrunkID,
                                                                        HttpRequestStorage.DatabaseEntities);
            }

            if (MultiTenantHelpers.IsTaxonomyLevelBranch() || MultiTenantHelpers.IsTaxonomyLevelTrunk())
            {
                // merge branch
                var taxonomyBranchesUpdated = updatedTaxonomyBranchIDs.Select(x => new MatchmakerOrganizationTaxonomyBranch(organization.OrganizationID, x, true)).ToList();
                organization.MatchmakerOrganizationTaxonomyBranches.Merge(taxonomyBranchesUpdated,
                                                                          allMatchMakerOrganizationTaxonomyBranches,
                                                                          (x, y) => x.OrganizationID == y.OrganizationID && x.TaxonomyBranchID == y.TaxonomyBranchID,
                                                                          HttpRequestStorage.DatabaseEntities);
            }

            // merge leaf
            var taxonomyLeafsUpdated = updatedTaxonomyLeafIDs.Select(x => new MatchmakerOrganizationTaxonomyLeaf(organization.OrganizationID, x, true)).ToList();

            organization.MatchmakerOrganizationTaxonomyLeafs.Merge(taxonomyLeafsUpdated,
                                                                   allMatchMakerOrganizationTaxonomyLeafs,
                                                                   (x, y) => x.OrganizationID == y.OrganizationID && x.TaxonomyLeafID == y.TaxonomyLeafID,
                                                                   HttpRequestStorage.DatabaseEntities);
        }