Example #1
0
        public void SaveDotSTATFile(ISdmxObjects sdmxObjects, DotStatExportType exportType)
        {
            string ExportFileName;

            ExportFileName = "DotStatExport-" + sdmxObjects.DataStructures.First().Id + "_" + sdmxObjects.DataStructures.First().AgencyId + "_" + sdmxObjects.DataStructures.First().Version;

            List<ISTAT.IO.Utility.FileGeneric> files = new List<ISTAT.IO.Utility.FileGeneric>();

            List<ContactRef> contacs = GetConfigContact();
            List<SecurityDef> securities = GetConfigSecurity();

            DSDExporter _dsdExp = new DSDExporter(sdmxObjects);

            switch (exportType)
            {
                case DotStatExportType.DSD:
                    if (_dsdExp.CreateData(
                        contacs,
                        securities,
                        true, false))
                    {
                        System.Xml.XmlDocument xDoc = _dsdExp.XMLDoc;

                        MemoryStream xmlStream = new MemoryStream();
                        xDoc.Save(xmlStream);

                        xmlStream.Flush();
                        xmlStream.Position = 0;

                        ISTAT.IO.Utility.FileGeneric file = new ISTAT.IO.Utility.FileGeneric();
                        file.filename = ExportFileName + ".xml";
                        file.stream = xmlStream;

                        files.Add(file);
                    }
                    break;
                case DotStatExportType.CODELIST:
                    if (_dsdExp.CreateData(
                        contacs,
                        securities,
                        true, false))
                    {
                        foreach (CodelistExporter _codeExp in _dsdExp.ExporterCodelists)
                        {
                            System.Xml.XmlDocument xDoc_code = _codeExp.XMLDoc;
                            MemoryStream xmlStream_code = new MemoryStream();
                            xDoc_code.Save(xmlStream_code);
                            xmlStream_code.Flush();
                            xmlStream_code.Position = 0;
                            ISTAT.IO.Utility.FileGeneric file_code = new ISTAT.IO.Utility.FileGeneric();
                            file_code.filename = _codeExp.Code.ToString() + ".xml";
                            file_code.stream = xmlStream_code;
                            files.Add(file_code);

                            Stream streamCSV = CSVWriter.CreateStream(_codeExp.DataView);
                            ISTAT.IO.Utility.FileGeneric file_csv = new ISTAT.IO.Utility.FileGeneric();
                            file_csv.filename = _codeExp.DataFilename;
                            file_csv.stream = streamCSV;
                            files.Add(file_csv);
                        }
                    }
                    break;
                case DotStatExportType.ALL:
                    if (_dsdExp.CreateData(
                        contacs,
                        securities,
                        true, false))
                    {
                        System.Xml.XmlDocument xDoc = _dsdExp.XMLDoc;

                        MemoryStream xmlStream = new MemoryStream();
                        xDoc.Save(xmlStream);

                        xmlStream.Flush();
                        xmlStream.Position = 0;

                        ISTAT.IO.Utility.FileGeneric file = new ISTAT.IO.Utility.FileGeneric();
                        file.filename = ExportFileName + ".xml";
                        file.stream = xmlStream;

                        files.Add(file);
                        foreach (CodelistExporter _codeExp in _dsdExp.ExporterCodelists)
                        {
                            System.Xml.XmlDocument xDoc_code = _codeExp.XMLDoc;
                            MemoryStream xmlStream_code = new MemoryStream();
                            xDoc_code.Save(xmlStream_code);
                            xmlStream_code.Flush();
                            xmlStream_code.Position = 0;
                            ISTAT.IO.Utility.FileGeneric file_code = new ISTAT.IO.Utility.FileGeneric();
                            file_code.filename = _codeExp.Code.ToString() + ".xml";
                            file_code.stream = xmlStream_code;
                            files.Add(file_code);

                            Stream streamCSV = CSVWriter.CreateStream(_codeExp.DataView);
                            ISTAT.IO.Utility.FileGeneric file_csv = new ISTAT.IO.Utility.FileGeneric();
                            file_csv.filename = _codeExp.DataFilename;
                            file_csv.stream = streamCSV;
                            files.Add(file_csv);
                        }
                    }
                    break;
            }

            string fileZip = System.Web.HttpContext.Current.Server.MapPath("OutputFiles" + "\\" + ExportFileName + ".zip");

            System.IO.File.Delete(fileZip);
            Ionic.Utils.Zip.ZipFile zip = new Ionic.Utils.Zip.ZipFile(fileZip);
            foreach (ISTAT.IO.Utility.FileGeneric file in files)
                zip.AddFileStream(file.filename, string.Empty, file.stream);
            zip.Save();

            SendAttachment(fileZip, ExportFileName + ".zip");

        }
        public string CreateExport(string agency, string id, string version, List<string> dimensions, List<string> attributes, string type_download)
        {
            // Retrive Logged user
            SDMX_Dataloader.Engine.Client client = null;
            try
            {
                client = HttpContext.Current.Session[UserDef.UserSessionKey] as SDMX_Dataloader.Engine.Client; if (client == null) throw new Exception("Session Expiried");
            }
            catch (Exception ex)
            {
                return JsonMessage.SessionExpired;
            }

            try
            {

                if (!UserDef.UserCan(client.LoggedUser, UserDef.ActionDef.DefaultProc))
                    return JsonMessage.GetError(Resources.Notification.err_action_denied);

                // Get Settings Properties
                ISTAT.EXPORT.Settings.ContactSettingsHandler configContact =
                   (ISTAT.EXPORT.Settings.ContactSettingsHandler)System.Configuration.ConfigurationManager.GetSection(
                       "ExportDotStatSettingsGroup/ContactSection");

                ISTAT.EXPORT.Settings.SecuritySettingsHandler configSecurity =
                   (ISTAT.EXPORT.Settings.SecuritySettingsHandler)System.Configuration.ConfigurationManager.GetSection(
                       "ExportDotStatSettingsGroup/SecuritySection");

                // Get the current user

                SDMXIdentifier sdmxIdentity = new SDMXIdentifier()
                {
                    agencyid = agency,
                    id = id,
                    version = version
                };

                string filenameDSD = sdmxIdentity.ToString();

                Org.Sdmxsource.Sdmx.Util.Objects.Container.SdmxObjectsImpl sdmxObjects = new Org.Sdmxsource.Sdmx.Util.Objects.Container.SdmxObjectsImpl();

                Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.IDataStructureObject dsd = DataSDMX.GetDSD(sdmxIdentity);

                sdmxObjects.AddDataStructure(dsd);

                foreach (Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.IDimension dim in dsd.DimensionList.Dimensions){
                    if (!dim.TimeDimension && dimensions.Contains(dim.Id) && dim.HasCodedRepresentation())
                    {
                        Org.Sdmxsource.Sdmx.Api.Model.Objects.Codelist.ICodelistObject codelist = DataSDMX.GetCodelist(new SDMXIdentifier()
                        {
                            agencyid = dim.Representation.Representation.MaintainableReference.AgencyId,
                            id = dim.Representation.Representation.MaintainableReference.MaintainableId,
                            version = dim.Representation.Representation.MaintainableReference.Version
                        });
                        sdmxObjects.AddCodelist(codelist);
                    }
                }

                foreach (Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.IAttributeObject att in dsd.Attributes){
                    if (attributes.Contains(att.Id) && att.HasCodedRepresentation())
                    {
                        Org.Sdmxsource.Sdmx.Api.Model.Objects.Codelist.ICodelistObject codelist=DataSDMX.GetCodelist(new SDMXIdentifier(){
                            agencyid = att.Representation.Representation.MaintainableReference.AgencyId,
                            id = att.Representation.Representation.MaintainableReference.MaintainableId,
                            version = att.Representation.Representation.MaintainableReference.Version
                        });
                        sdmxObjects.AddCodelist(codelist);
                    }
                }

                List<ISTAT.IO.Utility.FileGeneric> files = new List<ISTAT.IO.Utility.FileGeneric>();

                List<ContactRef> contacs = new List<ContactRef>();
                contacs.Add(new ContactRef()
                {
                    name = configContact.Name,
                    direction = configContact.Direction,
                    email = configContact.Email
                });

                List<SecurityDef> securities = new List<SecurityDef>();
                securities.Add(new SecurityDef()
                {
                    domain = configSecurity.Domain,
                    userGroup = configSecurity.UserGroup,
                });

                DSDExporter _dsdExp = new DSDExporter(sdmxObjects);

                #region Export DSD
                if (type_download == "dsd")
                {

                    if (_dsdExp.CreateData(
                        contacs,
                        securities,
                        true, false))
                    {
                        System.Xml.XmlDocument xDoc = _dsdExp.XMLDoc;

                        MemoryStream xmlStream = new MemoryStream();
                        xDoc.Save(xmlStream);

                        xmlStream.Flush();
                        xmlStream.Position = 0;

                        ISTAT.IO.Utility.FileGeneric file = new ISTAT.IO.Utility.FileGeneric();
                        file.filename = filenameDSD + ".xml";
                        file.stream = xmlStream;

                        files.Add(file);
                    }
                }
                #endregion
                #region Export Codelist
                if (type_download == "codelist")
                {

                    if (_dsdExp.CreateData(
                        contacs,
                        securities,
                        true, false))
                    {
                        foreach (CodelistExporter _codeExp in _dsdExp.ExporterCodelists)
                        {
                            System.Xml.XmlDocument xDoc_code = _codeExp.XMLDoc;
                            MemoryStream xmlStream_code = new MemoryStream();
                            xDoc_code.Save(xmlStream_code);
                            xmlStream_code.Flush();
                            xmlStream_code.Position = 0;
                            ISTAT.IO.Utility.FileGeneric file_code = new ISTAT.IO.Utility.FileGeneric();
                            file_code.filename = _codeExp.Code.ToString() + ".xml";
                            file_code.stream = xmlStream_code;
                            files.Add(file_code);

                            Stream streamCSV = CSVWriter.CreateStream(_codeExp.DataView);
                            ISTAT.IO.Utility.FileGeneric file_csv = new ISTAT.IO.Utility.FileGeneric();
                            file_csv.filename = _codeExp.DataFilename;
                            file_csv.stream = streamCSV;
                            files.Add(file_csv);
                        }
                    }
                }
                #endregion
                #region Export ALL
                if (type_download == "all")
                {

                    if (_dsdExp.CreateData(
                        contacs,
                        securities,
                        true, false))
                    {
                        System.Xml.XmlDocument xDoc = _dsdExp.XMLDoc;

                        MemoryStream xmlStream = new MemoryStream();
                        xDoc.Save(xmlStream);

                        xmlStream.Flush();
                        xmlStream.Position = 0;

                        ISTAT.IO.Utility.FileGeneric file = new ISTAT.IO.Utility.FileGeneric();
                        file.filename = filenameDSD + ".xml";
                        file.stream = xmlStream;

                        files.Add(file);
                        foreach (CodelistExporter _codeExp in _dsdExp.ExporterCodelists)
                        {
                            System.Xml.XmlDocument xDoc_code = _codeExp.XMLDoc;
                            MemoryStream xmlStream_code = new MemoryStream();
                            xDoc_code.Save(xmlStream_code);
                            xmlStream_code.Flush();
                            xmlStream_code.Position = 0;
                            ISTAT.IO.Utility.FileGeneric file_code = new ISTAT.IO.Utility.FileGeneric();
                            file_code.filename = _codeExp.Code.ToString() + ".xml";
                            file_code.stream = xmlStream_code;
                            files.Add(file_code);

                            Stream streamCSV = CSVWriter.CreateStream(_codeExp.DataView);
                            ISTAT.IO.Utility.FileGeneric file_csv = new ISTAT.IO.Utility.FileGeneric();
                            file_csv.filename = _codeExp.DataFilename;
                            file_csv.stream = streamCSV;
                            files.Add(file_csv);
                        }
                    }
                }
                #endregion

                string fileZip = System.Web.HttpContext.Current.Server.MapPath(@"~\Temp\\" + filenameDSD + ".zip");
                System.IO.File.Delete(fileZip);
                Ionic.Utils.Zip.ZipFile zip = new Ionic.Utils.Zip.ZipFile(fileZip);
                foreach (ISTAT.IO.Utility.FileGeneric file in files)
                    zip.AddFileStream(file.filename, string.Empty, file.stream);
                zip.Save();

                client.FileToDownload = fileZip;

                return JsonMessage.EmptyJSON;

            }
            catch (Exception ex)
            {

                Logger.Error(ex.Message);
                return JsonMessage.ErrorOccured;

            }
        }