public static Dictionary<string, List<string>> GetView(System.IO.Stream stream, TranscodeTime transcode_time = null, int maxResult = 0)
        {
            Dictionary<string, List<string>> view = new Dictionary<string, List<string>>();

            using (System.IO.StreamReader _stream = new System.IO.StreamReader(stream))
            {
                ReadableDataLocationFactory fact = new ReadableDataLocationFactory();
                IReadableDataLocation rdl = fact.GetReadableDataLocation(_stream.BaseStream);

                try
                {
                    ISdmxObjectRetrievalManager retrievalManager = DataSDMX.GetManagerImmutable();

                    Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager dataReader = new Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager();
                    Org.Sdmxsource.Sdmx.Api.Engine.IDataReaderEngine dataEngine = dataReader.GetDataReaderEngine(rdl, retrievalManager);

                    if (dataEngine != null)
                    {
                        dataEngine.Reset();

                        #region Data Parsing

                        while (dataEngine.MoveNextDataset())
                        {
                            IDataStructureObject dsd = dataEngine.DataStructure;

                            List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>> groupAttributeDef = new List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>>();
                            Dictionary<string, string> groupAttrTemplate = new Dictionary<string, string>();

                            while (dataEngine.MoveNextKeyable())
                            {

                                IKeyable currentKey = dataEngine.CurrentKey;

                                if (currentKey.GroupName != null)
                                {
                                    KeyValuePair<IList<IKeyValue>, IList<IKeyValue>> groupAttrAssignment =
                                        new KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>(currentKey.Key, currentKey.Attributes);
                                    groupAttributeDef.Add(groupAttrAssignment);
                                    foreach (var attr in currentKey.Attributes)
                                    {
                                        string c = attr.Concept.Trim();
                                        if (!view.Keys.Contains(c))
                                        {
                                            view.Add(c, new List<string>());
                                            groupAttrTemplate[c] = null;
                                        }
                                    }
                                }

                                while (dataEngine.MoveNextObservation())
                                {
                                    IObservation obs = dataEngine.CurrentObservation;
                                    var freq = TranscodeTime.TypePeriod.A.ToString();

                                    #region add column for key of attributes at level series
                                    foreach (IKeyValue keyValue in currentKey.Attributes)
                                    {
                                        if (view.Keys.Contains<string>(keyValue.Concept))
                                        {
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(keyValue.Concept, new List<string>());
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                    }
                                    #endregion
                                    #region add column for key of dim
                                    foreach (IKeyValue keyValue in currentKey.Key)
                                    {
                                        if (keyValue.Concept == dsd.FrequencyDimension.Id)
                                            freq = keyValue.Code.Trim();

                                        if (view.Keys.Contains<string>(keyValue.Concept))
                                        {
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(keyValue.Concept, new List<string>());
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                    }
                                    #endregion
                                    #region add column for key of attributes to level observation
                                    foreach (IKeyValue keyValue in obs.Attributes)
                                    {
                                        if (view.Keys.Contains<string>(keyValue.Concept))
                                        {
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(keyValue.Concept, new List<string>());
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                    }
                                    #endregion
                                    #region add columns for key of attributes at level group
                                    Dictionary<string, string> groupAttrValues = new Dictionary<string, string>(groupAttrTemplate);
                                    foreach (var attributeDef in groupAttributeDef)
                                    {
                                        bool setAttribute = true;
                                        foreach (var condition in attributeDef.Key)
                                        {
                                            var  viewItem = view[condition.Concept] as List<string>;
                                            if (viewItem == null ||
                                                viewItem.Count == 0 ||
                                                viewItem.Last() != condition.Code)
                                            {
                                                setAttribute = false;
                                                break;
                                            }
                                        }
                                        if (setAttribute)
                                        {
                                            foreach (var attribute in attributeDef.Value)
                                            {
                                                groupAttrValues[attribute.Concept] = attribute.Code;
                                            }
                                        }

                                    }
                                    foreach (var item in groupAttrValues)
                                        ((List<string>)view[item.Key]).Add(item.Value);

                                    #endregion
                                    #region add column Cross Sectional Value
                                    if (obs.CrossSection)
                                        if (view.Keys.Contains<string>(obs.CrossSectionalValue.Concept))
                                        {
                                            ((List<string>)view[obs.CrossSectionalValue.Concept]).Add(obs.CrossSectionalValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(obs.CrossSectionalValue.Concept, new List<string>());
                                            ((List<string>)view[obs.CrossSectionalValue.Concept]).Add(obs.CrossSectionalValue.Code.Trim());
                                        }
                                    #endregion

                                    #region Apply transcoding

                                    string obsTime = obs.ObsTime;

                                    bool useTranscode = true;
                                    //if (view.Keys.Contains<string>("FREQ"))
                                    if (freq == TranscodeTime.TypePeriod.A.ToString()) useTranscode = false;

                                    if (useTranscode)
                                    {
                                        int sepIndex = obsTime.IndexOf('-');
                                        string period = string.Empty;
                                        string period_change = obsTime[sepIndex].ToString();
                                        int nextChar;

                                        if (!int.TryParse(obsTime[sepIndex + 1].ToString(), out nextChar))
                                        {
                                            period = obsTime[sepIndex + 1].ToString();
                                            period_change += period;
                                        }
                                        else period = TranscodeTime.TypePeriod.M.ToString();

                                        if (view.Keys.Contains<string>("FREQ")) period = freq;

                                        TranscodeTime _transcode_time = new TranscodeTime();
                                        _transcode_time.stopChar = period_change;
                                        _transcode_time.periodChar = (TranscodeTime.TypePeriod)Enum.Parse(typeof(TranscodeTime.TypePeriod), period);

                                        obsTime = _transcode_time.TransCodific(obsTime);
                                    }

                                    #endregion

                                    #region add column TIME_PERIOD

                                    if (view.Keys.Contains<string>("TIME_PERIOD"))
                                    {
                                        ((List<string>)view["TIME_PERIOD"]).Add(obsTime);
                                    }
                                    else
                                    {
                                        view.Add("TIME_PERIOD", new List<string>());
                                        ((List<string>)view["TIME_PERIOD"]).Add(obsTime);
                                    }
                                    #endregion
                                    #region add column OBS_VALUE
                                    if (view.Keys.Contains<string>("OBS_VALUE"))
                                    {
                                        ((List<string>)view["OBS_VALUE"]).Add(obs.ObservationValue.Trim());
                                    }
                                    else
                                    {
                                        view.Add("OBS_VALUE", new List<string>());
                                        ((List<string>)view["OBS_VALUE"]).Add(obs.ObservationValue.Trim());
                                    }
                                    #endregion

                                    if (maxResult > 0
                                        && ((List<string>)view["OBS_VALUE"]).Count >= maxResult)
                                        return view;

                                }

                            }
                            break;
                        }
                        #endregion

                    }
                }
                catch (Exception ex)
                {
                    //Console.Write(ex.Message);
                }

                //_stream.Close();
                rdl.Close();

            }

            //return first dataset
            return (view.Count > 0) ? view : null;
        }
        public static List<string> WriteSDMXtoCSV(System.IO.Stream sdmxFile, System.IO.StreamWriter csvOutput, TranscodeTime transcode_time = null, int maxResult = 0)
        {
            //Dictionary<string, List<string>> view = new Dictionary<string, List<string>>();
            char separator = ';';
            List<string> currentRow;
            List<string> header = new List<string>();
            bool writeHeader = true;

            using (System.IO.StreamReader _stream = new System.IO.StreamReader(sdmxFile))
            {
                using (csvOutput)
                {

                    ReadableDataLocationFactory fact = new ReadableDataLocationFactory();
                    IReadableDataLocation rdl = fact.GetReadableDataLocation(_stream.BaseStream);

                    try
                    {
                        ISdmxObjectRetrievalManager retrievalManager = DataSDMX.GetManagerImmutable();

                        Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager dataReader = new Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager();
                        Org.Sdmxsource.Sdmx.Api.Engine.IDataReaderEngine dataEngine = dataReader.GetDataReaderEngine(rdl, retrievalManager);

                        if (dataEngine != null)
                        {
                            dataEngine.Reset();

                            #region Data Parsing

                            while (dataEngine.MoveNextDataset())
                            {
                                IDataStructureObject dsd = dataEngine.DataStructure;

                                List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>> groupAttributeDef = new List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>>();

                                Dictionary<string, int> headerIndx = new Dictionary<string, int>();
                                int i = 0;
                                foreach (var dimension in dsd.GetDimensions())
                                {
                                    header.Add(dimension.Id);
                                    headerIndx[dimension.Id] = i;
                                    i++;
                                }
                                foreach (var attribute in dsd.Attributes)
                                {
                                    header.Add(attribute.Id);
                                    headerIndx[attribute.Id] = i;
                                    i++;
                                }
                                header.Add("OBS_VALUE");
                                headerIndx["OBS_VALUE"] = i;

                                while (dataEngine.MoveNextKeyable())
                                {

                                    IKeyable currentKey = dataEngine.CurrentKey;

                                    if (currentKey.GroupName != null)
                                    {
                                        KeyValuePair<IList<IKeyValue>, IList<IKeyValue>> groupAttrAssignment =
                                            new KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>(currentKey.Key, currentKey.Attributes);
                                        groupAttributeDef.Add(groupAttrAssignment);
                                    }

                                    while (dataEngine.MoveNextObservation())
                                    {
                                        currentRow = new List<string>();
                                        var currentRowArray = new string[header.Count];
                                        for (i = 0; i < currentRowArray.Length; i++) currentRowArray[i] = "";

                                        IObservation obs = dataEngine.CurrentObservation;
                                        var freq = TranscodeTime.TypePeriod.A.ToString();

                                        #region add column for key of attributes at level series
                                        foreach (IKeyValue keyValue in currentKey.Attributes)
                                        {
                                            currentRowArray[headerIndx[keyValue.Concept]] = keyValue.Code;

                                        }
                                        #endregion
                                        #region add column for key of dim
                                        foreach (IKeyValue keyValue in currentKey.Key)
                                        {
                                            currentRowArray[headerIndx[keyValue.Concept]] = keyValue.Code;

                                            if (keyValue.Concept == dsd.FrequencyDimension.Id)
                                                freq = keyValue.Code.Trim();
                                        }
                                        #endregion
                                        #region add column for key of attributes to level observation
                                        foreach (IKeyValue keyValue in obs.Attributes)
                                        {
                                            currentRowArray[headerIndx[keyValue.Concept]] = keyValue.Code;
                                        }
                                        #endregion
                                        #region add columns for key of attributes at level group
                                        foreach (var attributeDef in groupAttributeDef)
                                        {
                                            bool setAttribute = true;
                                            foreach (var condition in attributeDef.Key)
                                            {
                                                if (currentRowArray[headerIndx[condition.Concept]] != condition.Code)
                                                {
                                                    setAttribute = false;
                                                    break;
                                                }
                                            }
                                            if (setAttribute)
                                            {
                                                foreach (var attribute in attributeDef.Value)
                                                {
                                                    currentRowArray[headerIndx[attribute.Concept]] = attribute.Code;
                                                }
                                            }

                                        }

                                        #endregion
                                        #region add column Cross Sectional Value
                                        if (obs.CrossSection)
                                        {
                                            currentRowArray[headerIndx[obs.CrossSectionalValue.Concept]] = obs.CrossSectionalValue.Code.Trim();
                                        }
                                        #endregion

                                        #region Apply transcoding

                                        string obsTime = obs.ObsTime;

                                        bool useTranscode = true;
                                        if (obs.ObsTimeFormat.FrequencyCode == TranscodeTime.TypePeriod.A.ToString())
                                            useTranscode = false;

                                        if (useTranscode)
                                        {
                                            int sepIndex = obsTime.IndexOf('-');
                                            string period = string.Empty;
                                            string period_change = obsTime[sepIndex].ToString();
                                            int nextChar;

                                            if (!int.TryParse(obsTime[sepIndex + 1].ToString(), out nextChar))
                                            {
                                                period = obsTime[sepIndex + 1].ToString();
                                                period_change += period;
                                            }
                                            else period = TranscodeTime.TypePeriod.M.ToString();

                                            if (dsd.HasFrequencyDimension() && currentRowArray[headerIndx[dsd.FrequencyDimension.Id]] != null)
                                                period = currentRowArray[headerIndx[dsd.FrequencyDimension.Id]];

                                            TranscodeTime _transcode_time = new TranscodeTime();
                                            _transcode_time.stopChar = period_change;
                                            _transcode_time.periodChar = (TranscodeTime.TypePeriod)Enum.Parse(typeof(TranscodeTime.TypePeriod), period);

                                            obsTime = _transcode_time.TransCodific(obsTime);
                                        }

                                        #endregion

                                        #region add column TIME_PERIOD

                                        currentRowArray[headerIndx["TIME_PERIOD"]] = obsTime;

                                        #endregion
                                        #region add column OBS_VALUE

                                        if (obs.ObservationValue.Trim() != "NaN")
                                            currentRowArray[headerIndx["OBS_VALUE"]] = obs.ObservationValue.Trim();

                                        #endregion

                                        /*
                                        if (maxResult > 0
                                            && ((List<string>)view["OBS_VALUE"]).Count >= maxResult)
                                            return view;
                                        */

                                        if (writeHeader)
                                        {
                                            i = 0;
                                            foreach (var concept in header)
                                            {
                                                if (i > 0) csvOutput.Write(separator);
                                                csvOutput.Write(concept);
                                                i++;
                                            }
                                            csvOutput.Write(Environment.NewLine);
                                            writeHeader = false;
                                        }
                                        int j = 0;
                                        foreach (var item in currentRowArray)
                                        {
                                            if (j > 0) csvOutput.Write(separator);
                                            csvOutput.Write(item);
                                            j++;
                                        }
                                        csvOutput.Write(Environment.NewLine);
                                    }

                                }
                                break;
                            }
                            #endregion

                        }
                    }
                    catch (Exception ex)
                    {
                        //Console.Write(ex.Message);
                        throw ex;
                    }
                    finally
                    {
                        rdl.Close();
                    }
                }
            }

            //returns the list of concepts
            return header;
        }
        public static IList<ArtefactImportStatus> SubmitStructure(System.IO.Stream stream)
        {
            System.IO.StreamReader _stream = new System.IO.StreamReader(stream);
            ReadableDataLocationFactory fact = new ReadableDataLocationFactory();
            IReadableDataLocation rdl = fact.GetReadableDataLocation(_stream.BaseStream);

            IList<ArtefactImportStatus> artefactImportStatuses = new List<ArtefactImportStatus>();

            try
            {
                StructureParsingManager spm = new StructureParsingManager();
                IStructureWorkspace workspace = spm.ParseStructures(rdl);
                ISdmxObjects sdmxObjects = workspace.GetStructureObjects(false);

                _stream.Close();
                rdl.Close();

                return SubmitStructure(sdmxObjects);
            }
            catch (Exception ex)
            {
                _stream.Close();
                rdl.Close();
                throw ex;
            }
        }
        /// <summary>
        ///     Merges the specified file information.
        /// </summary>
        /// <param name="stream">The file information.</param>
        /// <param name="sdmxObjects">The SDMX objects.</param>
        private static void Merge(Stream stream, ISdmxObjects sdmxObjects)
        {
            IStructureWorkspace structureWorkspace;
            StructureParsingManager parsingManager = new StructureParsingManager();
            IReadableDataLocationFactory dataLocationFactory = new ReadableDataLocationFactory();
            using (var readableDataLocation = dataLocationFactory.GetReadableDataLocation(stream))
            {
                structureWorkspace = parsingManager.ParseStructures(readableDataLocation);
            }

            sdmxObjects.Merge(structureWorkspace.GetStructureObjects(false));
        }
Example #5
0
        public static IList<ArtefactImportStatus> SubmitStructure(System.IO.Stream stream)
        {
            System.IO.StreamReader _stream = new System.IO.StreamReader(stream);
            ReadableDataLocationFactory fact = new ReadableDataLocationFactory();
            IReadableDataLocation rdl = fact.GetReadableDataLocation(_stream.BaseStream);

            IList<ArtefactImportStatus> artefactImportStatuses = new List<ArtefactImportStatus>();

            try
            {
                StructureParsingManager spm = new StructureParsingManager();
                IStructureWorkspace workspace = spm.ParseStructures(rdl);
                ISdmxObjects sdmxObjects = workspace.GetStructureObjects(false);

                //sdmxObjects = RemoveNotFinalDSD(sdmxObjects);

                var manager = new MappingStoreManager(SQLConnString_DB, artefactImportStatuses);
                manager.SaveStructures(sdmxObjects);

                _stream.Close();
                rdl.Close();

                foreach (ArtefactImportStatus datastructure in artefactImportStatuses)
                {
                    if (datastructure.ImportMessage.StructureReference.MaintainableStructureEnumType == Org.Sdmxsource.Sdmx.Api.Constants.SdmxStructureEnumType.Dsd)
                    {

                        Delete_Dataflow((int)datastructure.PrimaryKeyValue + 1);
                        /*
               DeleteStructure(new SDMXIdentifier() {
                   id = datastructure.ImportMessage.StructureReference.MaintainableId,
                   agencyid = datastructure.ImportMessage.StructureReference.AgencyId,
                   version = datastructure.ImportMessage.StructureReference.Version,
               }, Org.Sdmxsource.Sdmx.Api.Constants.SdmxStructureEnumType.Dataflow);   */
                    }
                }

                return artefactImportStatuses;
            }
            catch (Exception ex)
            {
                _stream.Close();
                rdl.Close();
                throw ex;
            }
        }
        public RestService()
        {
            // 1.a To support additional data formats pass additional IDataWriterFactory implementations to the DataWriterManager constructor  
            IDataWriterFactory dataWriterFactory = new DataWriterFactory();
            IDataWriterManager dataWriterManager = new DataWriterManager(dataWriterFactory/*, add your factories here) */);

            // 1.b To support additional data formats pass additional IStructureWriterFactory implementations to the DataWriterManager constructor  
            IStructureWriterManager structureWriterManager = new StructureWriterManager(new SdmxStructureWriterFactory() /*, add your factories here */);

            // a map between the HTTP Accept header value and the IDataFormat
            this._acceptToDataFormats = new Dictionary<string, IDataFormat>(StringComparer.Ordinal) {
                                                { "application/vnd.sdmx.genericdata+xml", this._genericV21Format },
                                                { "application/xml", this._genericV21Format },
                                                { "application/vnd.sdmx.structurespecificdata+xml", this._structureSpecificFormat },
                                                /* 2. add new data formats here */ };

            // a map between the HTTP Accept header value and the IStructureFormat
            this._acceptToStructureFormats = new Dictionary<string, IStructureFormat>(StringComparer.Ordinal)
                                                 {
                                                     { "application/vnd.sdmx.structure+xml", this._structureFormatv21 },
                                                     { "application/xml", this._structureFormatv21 }
                                                    /* 2. add new structure formats here */ };
            
            // load the structures
            ISdmxObjects objects;
            IReadableDataLocationFactory dataLocationFactory = new ReadableDataLocationFactory();
            IStructureParsingManager structureParsingManager = new StructureParsingManager();

            using (IReadableDataLocation structureLocation = dataLocationFactory.GetReadableDataLocation(HttpContext.Current.Server.MapPath("~/App_Data/structures.xml")))
            {
                IStructureWorkspace structureWorkspace = structureParsingManager.ParseStructures(structureLocation);
                objects = structureWorkspace.GetStructureObjects(false);
            }

            ISdmxObjectRetrievalManager retrievalManager = new InMemoryRetrievalManager(objects);

            _dataQueryManager = new RestDataQueryManager(new SampleDataRetrieval(), dataWriterManager, retrievalManager);
            _restStructureQueryManager = new RestStructureQueryManager(structureWriterManager, retrievalManager);
        }