/// <summary>
        /// The resolve references internal.
        /// </summary>
        /// <param name="beans">
        /// The beans.
        /// </param>
        /// <param name="retrievalManager">
        /// The retrieval manager.
        /// </param>
        /// <param name="populateMissingMap">
        /// The populate missing map.
        /// </param>
        /// <returns>
        /// The <see cref="IDictionary"/>.
        /// </returns>
        /// <exception cref="ReferenceException">
        /// </exception>
        private IDictionary<IIdentifiableObject, ISet<IIdentifiableObject>> ResolveReferencesInternal(
            ISdmxObjects beans, 
            ISdmxRetrievalManager retrievalManager, 
            IDictionary<IIdentifiableObject, ISet<ICrossReference>> populateMissingMap)
        {
            this._structureRetrievalManager = retrievalManager;

            /* foreach */
            foreach (IAgency currentAgency in beans.Agencies)
            {
                this._agencies.Add(currentAgency.FullId, currentAgency);
            }

            // Add all the top level beans to the maintainables list
            beans.GetAllMaintinables().AddAll(this._maintianables);

            // LOOP THROUGH ALL THE BEANS AND RESOLVE ALL THE REFERENCES
            if (this._resolveAgencies)
            {
                /* foreach */
                foreach (IMaintainableObject currentBean in beans.GetAllMaintinables())
                {
                    try
                    {
                        this.ResoveAgency(currentBean);
                    }
                    catch (ReferenceException e)
                    {
                        throw new ReferenceException(
                            e, 
                            ExceptionCode.ReferenceError, 
                            SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Agency), 
                            currentBean.StructureType, 
                            currentBean.ToString());
                    }
                }
            }

            ISet<IMaintainableObject> loopSet = new HashSet<IMaintainableObject>();
            this._maintianables.AddAll(loopSet);

            /* foreach */
            foreach (IMaintainableObject currentMaintainable in loopSet)
            {
                this._log.Debug("Resolving References For : " + currentMaintainable.Urn);
                ISet<ICrossReference> crossReferences0 = currentMaintainable.CrossReferences;
                this._log.Debug("Number of References : " + crossReferences0.Count);
                int i = 0;

                /* foreach */
                foreach (ICrossReference crossReference in crossReferences0)
                {
                    i++;
                    if (this._log.IsDebugEnabled)
                    {
                        this._log.Debug(
                            "Resolving Reference " + i + ": " + crossReference + " - referenced from -"
                            + crossReference.ReferencedFrom.StructureType);
                    }

                    try
                    {
                        this.StoreRef(
                            crossReference.ReferencedFrom, this.ResolveCrossReference(crossReference, retrievalManager));
                    }
                    catch (ReferenceException e1)
                    {
                        HandleMissingReference(e1, populateMissingMap);
                    }
                }
            }

            return this._crossReferences;
        }
        /// <summary>
        /// For the included <paramref name="sdmxObjects"/>, returns a map of agency URN to maintainable Bean that references the agency
        /// </summary>
        /// <param name="sdmxObjects">
        /// The included <c>SDMX</c> objects
        /// </param>
        /// <param name="retrievalManager">
        /// The <see cref="ISdmxRetrievalManager"/>
        /// </param>
        /// <returns>
        /// The included <paramref name="sdmxObjects"/>, returns a map of agency URN to maintainable Bean that references the agency
        /// </returns>
        public virtual IDictionary<string, ISet<IMaintainableObject>> GetMissingAgencies(
            ISdmxObjects sdmxObjects, ISdmxRetrievalManager retrievalManager)
        {
            ISet<string> agencyIds = new HashSet<string>();

            /* foreach */
            var agencies = sdmxObjects.Agencies;
            foreach (IAgency acy in agencies)
            {
                agencyIds.Add(acy.FullId);
            }

            IDictionary<string, ISet<IMaintainableObject>> returnMap =
                new Dictionary<string, ISet<IMaintainableObject>>();

            /* foreach */
            var maintainableObjects = sdmxObjects.GetAllMaintinables();
            foreach (IMaintainableObject currentMaint in maintainableObjects)
            {
                string referencedAgencyId = currentMaint.AgencyId;
                if (!agencyIds.Contains(referencedAgencyId))
                {
                    if (retrievalManager != null)
                    {
                        try
                        {
                            IAgency acy0 = retrievalManager.GetAgency(referencedAgencyId);
                            if (acy0 != null)
                            {
                                agencyIds.Add(acy0.FullId);
                                continue;
                            }
                        }
                        catch (Exception th)
                        {
                            Console.Error.WriteLine(th.StackTrace);
                        }
                    }

                    ISet<IMaintainableObject> maintainables;
                    if (!returnMap.TryGetValue(referencedAgencyId, out maintainables))
                    {
                        maintainables = new HashSet<IMaintainableObject>();
                        returnMap.Add(referencedAgencyId, maintainables);
                    }

                    maintainables.Add(currentMaint);
                }
            }

            return returnMap;
        }