public void TestGetAllCategorisations(string name)
        {
            var mutableStructureSearchManager = GetStructureSearchManager(name);
            var catRef = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Categorisation));
            var detailLevel = StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.ReferencedStubs);
            var referenceLevel = StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.All);

            var structureQuery = new RESTStructureQueryCore(detailLevel, referenceLevel, null, catRef, false);
            var mutableObjects = mutableStructureSearchManager.GetMaintainables(structureQuery);
            Assert.IsTrue(mutableObjects.Dataflows.Count > 0);
            Assert.IsTrue(mutableObjects.DataStructures.Count > 0);
            Assert.IsTrue(mutableObjects.CategorySchemes.Count > 0);
            Assert.IsTrue(mutableObjects.Categorisations.Count > 0);
            Assert.IsTrue(mutableObjects.Codelists.Count > 0);
            Assert.IsTrue(mutableObjects.ConceptSchemes.Count > 0);
            Assert.IsTrue(mutableObjects.HierarchicalCodelists.Count == 0);
        }
        /// <summary>
        /// Gets the maintainables.
        /// </summary>
        /// <param name="structureQuery">The structure query.</param>
        /// <returns>The <see cref="IMutableObjects"/>.</returns>
        public IMutableObjects GetMaintainables(IComplexStructureQuery structureQuery)
        {
            // In this sample we convert the SDMX v2.1 SOAP request to REST.
            var maintainableRefObject = structureQuery.StructureReference.GetMaintainableRefObject();
            var structureReference = new StructureReferenceImpl(maintainableRefObject, structureQuery.StructureReference.ReferencedStructureType);
            StructureQueryDetail structureQueryDetail = Convert(structureQuery.StructureQueryMetadata.StructureQueryDetail, structureQuery.StructureQueryMetadata.ReferencesQueryDetail);

            StructureReferenceDetail reference = structureQuery.StructureQueryMetadata.StructureReferenceDetail;

            SdmxStructureType specificStructureReference = null;
            if (structureQuery.StructureQueryMetadata.ReferenceSpecificStructures != null)
            {
                specificStructureReference = structureQuery.StructureQueryMetadata.ReferenceSpecificStructures.FirstOrDefault();
            }

            bool returnLatest = structureQuery.StructureReference.VersionReference.IsReturnLatest.IsTrue;
            IRestStructureQuery restStructureQuery = new RESTStructureQueryCore(structureQueryDetail, reference, specificStructureReference, structureReference, returnLatest);
            return this._mutableStructureSearchManager.GetMaintainables(restStructureQuery);
        }
        public void TestGetOneCategorisations(string name)
        {
            var mutableStructureSearchManager = GetStructureSearchManager(name);
            var detailLevel = StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full);
            var referenceLevel = StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.All);

            IStructureReference catRef = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Categorisation));
            var categorisation = mutableStructureSearchManager.RetrieveStructures(new[] { catRef }, false, false).Categorisations.First();

            var referenceForOne = new StructureReferenceImpl(categorisation.AgencyId, categorisation.Id,categorisation.Version, SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Categorisation));
            var structureQuery = new RESTStructureQueryCore(detailLevel, referenceLevel, null, referenceForOne , false);
            var mutableObjects = mutableStructureSearchManager.GetMaintainables(structureQuery);
            Assert.IsTrue(mutableObjects.Dataflows.Count > 0);
            Assert.IsTrue(mutableObjects.DataStructures.Count > 0);
            Assert.IsTrue(mutableObjects.CategorySchemes.Count > 0);
            Assert.IsTrue(mutableObjects.Categorisations.Count > 0);
            Assert.IsTrue(mutableObjects.Codelists.Count > 0);
            Assert.IsTrue(mutableObjects.ConceptSchemes.Count > 0);
            Assert.IsTrue(mutableObjects.HierarchicalCodelists.Count == 0);
        }
        private static IRestStructureQuery BuildRestQueryBean(string structure, string agencyId, string resourceId, string version, NameValueCollection queryParameters)
        {
            var queryString = new string[4];
            queryString[0] = structure;
            queryString[1] = agencyId;
            queryString[2] = resourceId;
            queryString[3] = version;

            IDictionary<string, string> paramsDict = new Dictionary<string, string>();

            IRestStructureQuery query;
            try
            {
                query = new RESTStructureQueryCore(queryString, paramsDict);
            }

            catch (Exception e)
            {

                throw new WebFaultException<string>(e.Message, HttpStatusCode.BadRequest);
            }

            return query;
        }
        public void TestGetMaintainables(
            SdmxStructureEnumType structureEnumType, 
            StructureQueryDetailEnumType detail, 
            StructureReferenceDetailEnumType refDetailEnum, 
            SdmxStructureEnumType specificStructure)
        {
            var mutableSearchManager = this.GetMutableSearchManager(this._connectionString);
            var refDetail = StructureReferenceDetail.GetFromEnum(refDetailEnum);

            var sdmxStructureType = SdmxStructureType.GetFromEnum(specificStructure == SdmxStructureEnumType.HierarchicalCodelist && structureEnumType == SdmxStructureEnumType.CodeList ? SdmxStructureEnumType.HierarchicalCodelist : SdmxStructureEnumType.Categorisation);
            IStructureReference reference = new StructureReferenceImpl(new MaintainableRefObjectImpl(), sdmxStructureType);

            SdmxStructureType structureType = SdmxStructureType.GetFromEnum(structureEnumType);
            IRestStructureQuery query = new RESTStructureQueryCore(
                StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full),
                StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Specific),
                structureType,
                reference,
                false);
            var allObjects = mutableSearchManager.GetMaintainables(query).GetMaintainables(structureType);
            var referenceLevel = StructureReferenceDetail.GetFromEnum(refDetailEnum);

            _log.DebugFormat(CultureInfo.InvariantCulture, "Structure type: {0}, Detail: {1}, Reference : {2}", structureType, detail, refDetail);
            foreach (var maintainableMutableObject in allObjects)
            {
                if ("MA".Equals(maintainableMutableObject.AgencyId))
                {
                    continue;
                }

                var structureReference = _fromMutable.Build(maintainableMutableObject);
                _log.DebugFormat(CultureInfo.InvariantCulture, "Checking maintainable : {0}", structureReference);
                var mockQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(detail), referenceLevel, SdmxStructureType.GetFromEnum(specificStructure), structureReference, false);
                switch (refDetail.EnumType)
                {
                    case StructureReferenceDetailEnumType.Null:
                        break;
                    case StructureReferenceDetailEnumType.None:
                        {
                            var mutableObject = GetMutables(mutableSearchManager, mockQuery, maintainableMutableObject);
                            Assert.IsTrue(mutableObject.GetMaintainables(structureType).All(o => o.StructureType.EnumType == structureType.EnumType));
                        }

                        break;
                    case StructureReferenceDetailEnumType.Parents:
                        {
                            var mutableObject = GetMutables(mutableSearchManager, mockQuery, maintainableMutableObject);
                            ValidateParents(mutableObject, structureType, detail != StructureQueryDetailEnumType.Full, structureReference);
                        }

                        break;
                    case StructureReferenceDetailEnumType.ParentsSiblings:
                        {
                            var mutableObject = GetMutables(mutableSearchManager, mockQuery, maintainableMutableObject);
                            ValidateParentsAndSiblings(mutableObject, structureType, detail != StructureQueryDetailEnumType.Full);
                        }

                        break;
                    case StructureReferenceDetailEnumType.Children:
                        {
                            var mutableObject = GetMutables(mutableSearchManager, mockQuery, maintainableMutableObject);
                            ValidateChildren(mutableObject, structureType, detail != StructureQueryDetailEnumType.Full);
                        }

                        break;
                    case StructureReferenceDetailEnumType.Descendants:
                        {
                            var mutableObject = GetMutables(mutableSearchManager, mockQuery, maintainableMutableObject);
                            ValidateDescendants(mutableObject, structureType, detail != StructureQueryDetailEnumType.Full);
                        }

                        break;
                    case StructureReferenceDetailEnumType.All:
                        {
                            var mutableObject = GetMutables(mutableSearchManager, mockQuery, maintainableMutableObject);
                            ValidateAll(mutableObject, structureType, detail != StructureQueryDetailEnumType.Full);
                        }

                        break;
                    case StructureReferenceDetailEnumType.Specific:
                        {
                            if (specificStructure != SdmxStructureEnumType.Categorisation || detail == StructureQueryDetailEnumType.Full)
                            {
                                var specificStructureReference = SdmxStructureType.GetFromEnum(specificStructure);
                                var specificQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(detail), referenceLevel, specificStructureReference, structureReference, false);
                                var mutablesWithSpecific = GetMutables(mutableSearchManager, specificQuery, maintainableMutableObject);
                                ValidateSpecific(mutablesWithSpecific, structureType, detail != StructureQueryDetailEnumType.Full, specificStructureReference, structureReference);
                            }
                        }

                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }
        /// <summary>
        /// Retrieves all available dataflows.
        /// </summary>
        private IComplexStructureQuery RetrieveDataflows()
        {
            var dataflowRefBean = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dataflow));
            IRestStructureQuery structureQueryDataflow = new RESTStructureQueryCore(dataflowRefBean);

            IBuilder<IComplexStructureQuery, IRestStructureQuery> transformerDataFlow = new StructureQuery2ComplexQueryBuilder();

            IComplexStructureQuery complexStructureQueryDataflow = transformerDataFlow.Build(structureQueryDataflow);

            IList<SdmxStructureType> specificObjects = new List<SdmxStructureType>();
            specificObjects.Add(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dsd));

            IComplexStructureQueryMetadata complexStructureQueryMetadataWithDsd =
                    new ComplexStructureQueryMetadataCore(false,
                    ComplexStructureQueryDetail.GetFromEnum(ComplexStructureQueryDetailEnumType.Full),
                    ComplexMaintainableQueryDetail.GetFromEnum(ComplexMaintainableQueryDetailEnumType.Full),
                    StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Specific),
                    specificObjects);

            IComplexStructureQuery complexStructureQueryTempDataflow = new ComplexStructureQueryCore(
                    complexStructureQueryDataflow.StructureReference, complexStructureQueryMetadataWithDsd);

            return complexStructureQueryTempDataflow;
        }
        /// <summary>
        /// Retrieves all available categorisations and category schemes.
        /// </summary>
        private IComplexStructureQuery RetrieveCategorySchemesAndCategorisations()
        {
            var catSch = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CategoryScheme));
            IRestStructureQuery structureQueryCategoryScheme = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full), StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Parents), null, catSch, false);
            IBuilder<IComplexStructureQuery, IRestStructureQuery> transformerCategoryScheme = new StructureQuery2ComplexQueryBuilder();

            IComplexStructureQuery complexStructureQueryCategoryScheme = transformerCategoryScheme.Build(structureQueryCategoryScheme);

            return complexStructureQueryCategoryScheme;
        }
        public ISdmxObjects GetStructure(string DataflowId, string DatafloAgency, string DatafloVersion, bool resolseRef = false)
        {
            Logger.InfoFormat(
                    CultureInfo.InvariantCulture,
                    Resources.InfoGettingStructureFormat3,
                    DatafloAgency,
                    DataflowId,
                    DatafloVersion);
            ISdmxObjects structure = new SdmxObjectsImpl();

            #region GetDataflow
            //Get dataflows
            ISdmxObjects responseDataflows = new SdmxObjectsImpl();
            IComplexStructureQuery complexStructureQueryDataflow = RetrieveDataflow(DataflowId, DatafloAgency, DatafloVersion);
            responseDataflows = this.SendQueryStructureRequest(complexStructureQueryDataflow, SDMXWSFunctionV21.GetDataflow);
            //Remove from structure (ISdmxObjects) the DSDS built with SDMX v2.0
            var structureSdmxV20DSD = responseDataflows.DataStructures.Where(o => o.Annotations.Any(a => a.FromAnnotation() == CustomAnnotationType.SDMXv20Only)).ToArray();
            foreach (var sdmxV20Only in structureSdmxV20DSD)
            {
                responseDataflows.RemoveDataStructure(sdmxV20Only);
            }

            // DSDS with annotation
            var sdmxV20onlyReferences = structureSdmxV20DSD.Select(o => o.AsReference).ToArray();

            // Add the DSDS built with Sdmx V2.0
            ISdmxObjects responseDSD = new SdmxObjectsImpl();
            if (sdmxV20onlyReferences.Length > 0)
            {
                responseDSD = _nsiClientWs.SendQueryStructureRequest(sdmxV20onlyReferences, false);
                responseDataflows.Merge(responseDSD);
            }
            if (responseDataflows.Dataflows == null || responseDataflows.Dataflows.Count == 0)
                throw new Exception("Dataflow not found");
            #endregion

            try
            {
                IDataflowObject dataflow = responseDataflows.Dataflows.First();
                ISdmxObjects responseConceptScheme = new SdmxObjectsImpl();
                ISdmxObjects response = new SdmxObjectsImpl();

                IDataStructureObject dsd = NsiClientHelper.GetDsdFromDataflow(dataflow, responseDataflows.DataStructures);
                structure.AddDataStructure(dsd);

                NsiClientValidation.CheckifStructureComplete(structure, dataflow);
                IEnumerable<IStructureReference> conceptRefs = NsiClientHelper.BuildConceptSchemeRequest(structure.DataStructures.First());
                foreach (var structureReference in conceptRefs)
                {
                    IRestStructureQuery structureQueryConceptScheme = new RESTStructureQueryCore(structureReference);
                    IBuilder<IComplexStructureQuery, IRestStructureQuery> transformerCategoryScheme = new StructureQuery2ComplexQueryBuilder();
                    IComplexStructureQuery complexStructureQueryConceptScheme = transformerCategoryScheme.Build(structureQueryConceptScheme);
                    responseConceptScheme = this.SendQueryStructureRequest(complexStructureQueryConceptScheme, SDMXWSFunctionV21.GetConceptScheme);
                    response.Merge(responseConceptScheme);
                }
                structure.Merge(responseDataflows);
                structure.Merge(response);

                NsiClientValidation.CheckConcepts(structure);
                Logger.Info(Resources.InfoSuccess);
            }
            catch (NsiClientException e)
            {
                Logger.Error(Resources.ExceptionGettingStructure);
                Logger.Error(e.Message, e);
                throw;
            }
            catch (Exception e)
            {
                Logger.Error(Resources.ExceptionGettingStructure);
                Logger.Error(e.Message, e);
                throw new NsiClientException(Resources.ExceptionGettingStructure, e);
            }

            return structure;
        }
 /// <summary>
 /// Retrieve the structures referenced by <paramref name="queries"/> and populate the
 ///     <paramref name="mutableObjects"/>
 /// </summary>
 /// <param name="retrievalManager">
 ///     The retrieval manager.
 /// </param>
 /// <param name="mutableObjects">
 ///     The mutable objects.
 /// </param>
 /// <param name="queries">
 ///     The queries.
 /// </param>
 /// <param name="returnLatest">
 ///     The return Latest.
 /// </param>
 /// <param name="returnStub">
 ///     The return Stub.
 /// </param>
 /// <param name="allowedDataflows">
 ///     The allowed Dataflows.
 /// </param>
 /// <param name="crossReferenceMutableRetrievalManager">
 ///     The cross reference mutable retrieval manager.
 /// </param>
 protected virtual void PopulateMutables(IAuthAdvancedSdmxMutableObjectRetrievalManager retrievalManager, IMutableObjects mutableObjects, IList<IStructureReference> queries, bool returnLatest, StructureQueryDetailEnumType returnStub, IList<IMaintainableRefObject> allowedDataflows, IAuthCrossReferenceMutableRetrievalManager crossReferenceMutableRetrievalManager)
 {
     //// changes here might also apply to AuthAdvancedMutableStructureSearchManagerBase 
     for (int i = 0; i < queries.Count; i++)
     {
         var structureReference = queries[i];
         IRestStructureQuery restStructureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(returnStub), StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.None), null, structureReference, returnLatest);
         var complexStructureQuery = _complexQueryBuilder.Build(restStructureQuery);
         base.PopulateMutables(retrievalManager, mutableObjects, complexStructureQuery, allowedDataflows, crossReferenceMutableRetrievalManager);
     }
 }
        /// <summary>
        /// Retrieves all available categorisations and category schemes.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string RetrieveCategorySchemesAndCategorisations()
        {
            var catSch = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CategoryScheme));
            IStructureQueryFormat<string> structureQueryFormat = new RestQueryFormat();
            IRestStructureQuery structureQueryCategoryScheme = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full), StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Parents), null, catSch, false);
            IStructureQueryFactory factory = new RestStructureQueryFactory();

            IStructureQueryBuilderManager structureQueryBuilderManager = new StructureQueryBuilderManager(factory);
            string request = structureQueryBuilderManager.BuildStructureQuery(structureQueryCategoryScheme, structureQueryFormat);

            return request;
        }
        public void TestRestStructureQueryIsMaintainable(string agency, string id, string version, StructureQueryDetailEnumType structureQueryDetail, StructureReferenceDetailEnumType structureReferenceDetail)
        {

            var dataflowRef = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dataflow))
            {
                MaintainableId = id,
                AgencyId = agency,
                Version = version,
            };

            bool returnLatest = version == null;
            IRestStructureQuery structureQuery;
            
            Assert.Throws<SdmxSemmanticException>(() => structureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(structureQueryDetail), StructureReferenceDetail.GetFromEnum(structureReferenceDetail), SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Code), dataflowRef, returnLatest));
            Assert.Throws<SdmxSemmanticException>(() => structureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(structureQueryDetail), StructureReferenceDetail.GetFromEnum(structureReferenceDetail), SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Category), dataflowRef, returnLatest));
            Assert.Throws<SdmxSemmanticException>(() => structureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(structureQueryDetail), StructureReferenceDetail.GetFromEnum(structureReferenceDetail), SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dimension), dataflowRef, returnLatest));

        }
        public void TestGetOneCatScheme(string name)
        {
            var mutableStructureSearchManager = GetStructureSearchManager(name);
            var detailLevel = StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full);
            var referenceLevel = StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.All);

            IStructureReference getAllReference = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CategoryScheme));
            var objectToSearch = mutableStructureSearchManager.RetrieveStructures(new[] { getAllReference }, false, false).CategorySchemes.First();

            var referenceForOne = new StructureReferenceImpl(objectToSearch.AgencyId, objectToSearch.Id, objectToSearch.Version, SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CategoryScheme));
            var structureQuery = new RESTStructureQueryCore(detailLevel, referenceLevel, null, referenceForOne, false);
            var mutableObjects = mutableStructureSearchManager.GetMaintainables(structureQuery);
            Assert.IsNotEmpty(mutableObjects.Dataflows, referenceForOne.ToString());
            Assert.IsEmpty(mutableObjects.DataStructures, referenceForOne.ToString());
            Assert.IsNotEmpty(mutableObjects.CategorySchemes, referenceForOne.ToString());
            Assert.IsNotEmpty(mutableObjects.Categorisations, referenceForOne.ToString());
            Assert.IsEmpty(mutableObjects.Codelists, referenceForOne.ToString());
            Assert.IsEmpty(mutableObjects.ConceptSchemes, referenceForOne.ToString());
            Assert.IsTrue(mutableObjects.HierarchicalCodelists.Count == 0, referenceForOne.ToString());
        }
        public void TestEmulateNsiClientCallsRestSpecific(string name, string dataflowId)
        {
            ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[name];
            var mutableSearchManager = _mutableStructureSearchManagerFactory.GetStructureSearchManager(css, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));
            var mutableSearchManagerV20 = _mutableStructureSearchManagerFactory.GetStructureSearchManager(css, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));
            Assert.IsNotNull(mutableSearchManager);

            // get dataflows & categorisations
            IStructureReference dataflowReference = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dataflow));
            IStructureReference categorySchemeReference = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CategoryScheme));
            var dataflowAndDsdRef = new RESTStructureQueryCore(
                StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full),
                StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Children),
                null,
                dataflowReference,
                true);
            IMutableObjects mutableObjects = mutableSearchManager.GetMaintainables(dataflowAndDsdRef);
            IMutableObjects mutableObjectsCat =
                mutableSearchManager.GetMaintainables(
                    new RESTStructureQueryCore(
                        StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full),
                        StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Parents),
                        null,
                        categorySchemeReference,
                        false));

            Assert.IsNotNull(mutableObjects);
            Assert.IsNotEmpty(mutableObjects.Dataflows);
            Assert.IsTrue(mutableObjects.Dataflows.All(o => o.ExternalReference == null || !o.ExternalReference.IsTrue));
            Assert.IsNotEmpty(mutableObjects.DataStructures);
            Assert.IsNotEmpty(mutableObjectsCat.CategorySchemes);
            Assert.IsNotEmpty(mutableObjectsCat.Categorisations);

            // build lookup for SDMX v2.0 only DSD.
            var sdmxv20Only =
                mutableObjects.DataStructures.Where(o => o.Annotations.Any(a => a.FromAnnotation() == CustomAnnotationType.SDMXv20Only))
                    .ToDictionary(o => _fromMutableBuilder.Build(o).MaintainableReference);

            // for each dataflow get a dsd
            var dataflow = mutableObjects.Dataflows.First(o => o.Id.Equals(dataflowId));
            // request count
            RequestCount(dataflow, mutableSearchManagerV20);

            // get dsd
            IDataStructureMutableObject firstDsd;
            if (sdmxv20Only.TryGetValue(dataflow.DataStructureRef.MaintainableReference, out firstDsd))
            {
                Func<IStructureReference, IMutableObjects> searchManager = reference => mutableSearchManagerV20.RetrieveStructures(new[] { reference }, false, false);
                Func<IDataStructureMutableObject, IMutableObjects> conceptFunc = dsd =>
                    {
                        var structureReferences =
                            _crossReferenceSetBuilder.Build(dsd)
                                .Select(
                                    reference =>
                                    reference.TargetReference.IsMaintainable ? reference : new StructureReferenceImpl(reference.MaintainableReference, reference.MaintainableStructureEnumType))
                                .ToArray();
                        return mutableSearchManagerV20.RetrieveStructures(structureReferences, false, false);
                    };

                firstDsd = RequestDsd(dataflow, searchManager);

                // get concept schems
                RequestConceptScheme(firstDsd, conceptFunc);
            }
            else
            {
                firstDsd = mutableObjects.DataStructures.First(o => _fromMutableBuilder.Build(o).Equals(dataflow.DataStructureRef));
            }

            // get codelists
            var criteria = RequestPartialCodelist(dataflow, firstDsd, mutableSearchManagerV20);
            RequestCount(dataflow, mutableSearchManagerV20, criteria);
        }
        private static IRestStructureQuery BuildRestQueryBean(string structure, string agencyId, string resourceId, string version, System.Collections.Specialized.NameValueCollection queryParameters)
        {
            var queryString = new string[4];
            queryString[0] = structure;
            queryString[1] = agencyId;
            queryString[2] = resourceId;
            queryString[3] = version;

            IDictionary<string, string> paramsDict = new Dictionary<string, string>();
            var enumQ = queryParameters.GetEnumerator();
            while (enumQ.MoveNext())
            {
                var queryName = enumQ.Current.ToString();
                var queryValue = queryParameters[queryName];
                if (paramsDict.ContainsKey(queryName))
                {

                    throw new WebFaultException(HttpStatusCode.BadRequest);
                }

                paramsDict.Add(queryName, queryValue);
            }



            IRestStructureQuery query;

            try
            {
                query = new RESTStructureQueryCore(queryString, paramsDict);
            }

            catch (Exception e)
            {

                throw new WebFaultException<string>(e.Message, HttpStatusCode.BadRequest);
            }

            return query;
        }
        /// <summary>
        /// Builds the rest query bean.
        /// </summary>
        /// <param name="structure">The structure.</param>
        /// <param name="agencyId">The agency id.</param>
        /// <param name="resourceId">The resource id.</param>
        /// <param name="version">The version.</param>
        /// <param name="queryParameters">The query parameters.</param>
        /// <returns>
        /// The <see cref="IRestStructureQuery" />.
        /// </returns>
        /// <exception cref="WebFaultException{String}">An exception is thrown</exception>
        private static IRestStructureQuery BuildRestQueryBean(string structure, string agencyId, string resourceId, string version, NameValueCollection queryParameters)
        {
            var queryString = new string[4];
            queryString[0] = structure;
            queryString[1] = agencyId;
            queryString[2] = resourceId;
            queryString[3] = version;

            var paramsDict = HeaderUtils.GetQueryStringAsDict(queryParameters);

            IRestStructureQuery query;

            try
            {
                query = new RESTStructureQueryCore(queryString, paramsDict);
            }
            catch (SdmxException e)
            {
                Logger.Error(e.Message, e);
                throw _builder.Build(e);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message, e);
                throw new WebFaultException<string>(e.Message, HttpStatusCode.BadRequest);
            }

            return query;
        }
        /// <summary>
        /// Gets a bean with data about the key family for specified dataflow.
        /// </summary>
        /// <param name="dataflow">
        /// The dataflow
        /// </param>
        /// <param name="dataStructures">
        /// The data Structures.
        /// </param>
        /// <returns>
        /// a <c>StructureBean</c> instance with requested data; the result is never <c>null</c> or  incomplete, instead an exception is throwed away if something goes wrong and not all required data is successfully retrieved
        /// </returns>
        /// <remarks>
        /// The resulted bean will contain exactly one key family, but also will include any concepts and codelists referenced by the key family.
        /// </remarks>
        public ISdmxObjects GetStructure(IDataflowObject dataflow, ISet<IDataStructureObject> dataStructures)
        {
            Logger.InfoFormat(
                    CultureInfo.InvariantCulture,
                    Resources.InfoGettingStructureFormat3,
                    dataflow.AgencyId,
                    dataflow.Id,
                    dataflow.Version);
            ISdmxObjects structure = new SdmxObjectsImpl();

            try
            {
                ISdmxObjects responseConceptScheme = new SdmxObjectsImpl();
                ISdmxObjects response = new SdmxObjectsImpl();

                IStructureQueryFormat<string> structureQueryFormat = new RestQueryFormat();
                IStructureQueryFactory factory = new RestStructureQueryFactory();
                IStructureQueryBuilderManager structureQueryBuilderManager = new StructureQueryBuilderManager(factory);

                IDataStructureObject dsd = NsiClientHelper.GetDsdFromDataflow(dataflow, dataStructures);
                structure.AddDataStructure(dsd);

                NsiClientValidation.CheckifStructureComplete(structure, dataflow);
                IEnumerable<IStructureReference> conceptRefs = NsiClientHelper.BuildConceptSchemeRequest(structure.DataStructures.First());
                foreach (var structureReference in conceptRefs)
                {
                    IRestStructureQuery structureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full), StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.None), null, structureReference, false);
                    string request = structureQueryBuilderManager.BuildStructureQuery(structureQuery, structureQueryFormat);
                    responseConceptScheme = this.SendQueryStructureRequest(request);
                    response.Merge(responseConceptScheme);
                }

                structure.Merge(response);

                NsiClientValidation.CheckConcepts(structure);
                Logger.Info(Resources.InfoSuccess);
            }
            catch (NsiClientException e)
            {
                Logger.Error(Resources.ExceptionGettingStructure);
                Logger.Error(e.Message, e);
                throw;
            }
            catch (Exception e)
            {
                Logger.Error(Resources.ExceptionGettingStructure);
                Logger.Error(e.Message, e);
                throw new NsiClientException(Resources.ExceptionGettingStructure, e);
            }

            return structure;
        }
 public void TestRestStructureQueryRoundTrip(string restQuery)
 {
     var restStructureQueryCore = new RESTStructureQueryCore(restQuery);
     Assert.NotNull(restStructureQueryCore.StructureReference);
     var manager = new StructureQueryBuilderManager(new RestStructureQueryFactory());
     var buildStructureQuery = manager.BuildStructureQuery(restStructureQueryCore, new RestQueryFormat());
     Assert.NotNull(buildStructureQuery);
     Assert.IsTrue(buildStructureQuery.StartsWith(restQuery));
 }
        /// <summary>
        /// Retrieves all available dataflows.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string RetrieveDataflows()
        {
            var dataflowRefBean = new StructureReferenceImpl(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dataflow));

            IRestStructureQuery structureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(StructureQueryDetailEnumType.Full), StructureReferenceDetail.GetFromEnum(StructureReferenceDetailEnumType.Specific), SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dsd), dataflowRefBean, true);
            IStructureQueryFactory factory = new RestStructureQueryFactory();
            IStructureQueryBuilderManager structureQueryBuilderManager = new StructureQueryBuilderManager(factory);
            IStructureQueryFormat<string> structureQueryFormat = new RestQueryFormat();
            string request = structureQueryBuilderManager.BuildStructureQuery(structureQuery, structureQueryFormat);

            return request;
        }
 private string  GetStructureQueryFormat(IStructureReference dataflowRef, string version,StructureQueryDetailEnumType structureQueryDetail, StructureReferenceDetailEnumType structureReference )
 {
     IStructureQueryFormat<string> structureQueryFormat = new RestQueryFormat();
     IStructureQueryFactory factory = new RestStructureQueryFactory();
     IStructureQueryBuilderManager structureQueryBuilderManager = new StructureQueryBuilderManager(factory);
     bool returnLatest = version == null;
     SdmxStructureType specificStructureReference = structureReference == StructureReferenceDetailEnumType.Specific? SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CodeList) : null;
     IRestStructureQuery structureQuery = new RESTStructureQueryCore(StructureQueryDetail.GetFromEnum(structureQueryDetail), StructureReferenceDetail.GetFromEnum(structureReference), specificStructureReference, dataflowRef, returnLatest);
     string request = structureQueryBuilderManager.BuildStructureQuery(structureQuery, structureQueryFormat);
     return request;
 }
        /// <summary>
        /// Gets a bean with data about the key family for specified dataflow.
        /// </summary>
        /// <param name="dataflow">
        /// The dataflow
        /// </param>
        /// <returns>
        /// a <c>StructureBean</c> instance with requested data; the result is never <c>null</c> or  incomplete, instead an exception is throwed away if something goes wrong and not all required data is successfully retrieved
        /// </returns>
        /// <remarks>
        /// The resulted bean will contain exactly one key family, but also will include any concepts and codelists referenced by the key family.
        /// </remarks>
        public ISdmxObjects GetStructure(IDataflowObject dataflow, ISet<IDataStructureObject> dataStructures, bool resolseRef = false)
        {
            Logger.InfoFormat(
                    CultureInfo.InvariantCulture,
                    Resources.InfoGettingStructureFormat3,
                    dataflow.AgencyId,
                    dataflow.Id,
                    dataflow.Version);
            ISdmxObjects structure = new SdmxObjectsImpl();

            try
            {
                ISdmxObjects responseConceptScheme = new SdmxObjectsImpl();
                ISdmxObjects response = new SdmxObjectsImpl();

                IDataStructureObject dsd = NsiClientHelper.GetDsdFromDataflow(dataflow, dataStructures);
                structure.AddDataStructure(dsd);

                NsiClientValidation.CheckifStructureComplete(structure, dataflow);
                IEnumerable<IStructureReference> conceptRefs = NsiClientHelper.BuildConceptSchemeRequest(structure.DataStructures.First());
                foreach (var structureReference in conceptRefs)
                {
                    IRestStructureQuery structureQueryConceptScheme = new RESTStructureQueryCore(structureReference);
                    IBuilder<IComplexStructureQuery, IRestStructureQuery> transformerCategoryScheme = new StructureQuery2ComplexQueryBuilder();
                    IComplexStructureQuery complexStructureQueryConceptScheme = transformerCategoryScheme.Build(structureQueryConceptScheme);
                    responseConceptScheme = this.SendQueryStructureRequest(complexStructureQueryConceptScheme, SDMXWSFunctionV21.GetConceptScheme);
                    response.Merge(responseConceptScheme);
                }
                structure.Merge(response);

                NsiClientValidation.CheckConcepts(structure);
                Logger.Info(Resources.InfoSuccess);
            }
            catch (NsiClientException e)
            {
                Logger.Error(Resources.ExceptionGettingStructure);
                Logger.Error(e.Message, e);
                throw;
            }
            catch (Exception e)
            {
                Logger.Error(Resources.ExceptionGettingStructure);
                Logger.Error(e.Message, e);
                throw new NsiClientException(Resources.ExceptionGettingStructure, e);
            }

            return structure;
        }
        public void ProcessRequest(HttpContext context) {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string restUrl = Regex.Replace(request.Url.PathAndQuery, @"^\/RestService.ashx", string.Empty);
            if (restUrl.StartsWith("/data/"))
            {
                IRestDataQuery query = new RESTDataQueryCore(restUrl);

                // get the first known Accept Header value and IDataFormat from the request
                KeyValuePair<string, IDataFormat> dataFormat = GetDataFormat(request);

                // for simplicity use the first known accept header
                response.ContentType = dataFormat.Key;
                response.AddHeader("Content-Disposition", new ContentDisposition { FileName = "data.xml", Inline = true }.ToString());
                this._dataQueryManager.ExecuteQuery(query, dataFormat.Value, response.OutputStream);

            }
            else
            {
                IRestStructureQuery query = new RESTStructureQueryCore(restUrl);
                KeyValuePair<string, IStructureFormat> format = GetStructureFormat(request);

                // for simplicity use the first known accept header
                response.ContentType = format.Key;
                response.AddHeader("Content-Disposition", new ContentDisposition { FileName = "structure.xml", Inline = true }.ToString());
                this._restStructureQueryManager.GetStructures(query, response.OutputStream, format.Value);
            }

            response.End();
        }