Inheritance: DataWriter
Exemple #1
0
        private string createDownloadFile(long id, long datasetVersionOrderNo, long dataStructureId, string title, string ext, DataWriter writer, string[] columns = null, bool withUnits = false)
        {
            string addtionalFileNameExt = "";

            if (withUnits)
            {
                addtionalFileNameExt = "_withunits";
            }

            string filename = "data" + addtionalFileNameExt;

            if (ext.Equals(".csv") || ext.Equals(".txt") || ext.Equals(".tsv"))
            {
                AsciiWriter asciiwriter = (AsciiWriter)writer;
                return(asciiwriter.CreateFile(id, datasetVersionOrderNo, dataStructureId, filename, ext));
            }
            else
            if (ext.Equals(".xlsm"))
            {
                ExcelWriter excelwriter = (ExcelWriter)writer;
                excelwriter.VisibleColumns = columns;
                return(excelwriter.CreateFile(id, datasetVersionOrderNo, dataStructureId, filename, ext));
            }
            else
            if (ext.Equals(".xlsx"))
            {
                ExcelWriter excelwriter = (ExcelWriter)writer;
                return(excelwriter.CreateFile(id, datasetVersionOrderNo, dataStructureId, filename, ext));
            }

            return("");
        }
Exemple #2
0
        /// <summary>
        /// create a new file of given extension
        /// ns will be used to seperate from other files created of individual datasets
        /// </summary>
        /// <param name="ns">namespace</param>
        /// <param name="title"></param>
        /// <param name="ext"></param>
        /// <param name="writer"></param>
        /// <returns></returns>
        private string createDownloadFile(string ns, long datastructureId, string title, string ext, DataWriter writer, string[] columns = null)
        {
            ExcelWriter excelwriter = null;

            switch (ext)
            {
            // text based files
            case ".csv":
            case ".txt":
            case ".tsv":
                AsciiWriter asciiwriter = (AsciiWriter)writer;
                return(asciiwriter.CreateFile(ns, title, ext));

            // excel files
            case ".xlsx":
                excelwriter = (ExcelWriter)writer;
                return(excelwriter.CreateFile(ns, datastructureId, title, ext));

            case ".xlsm":
                excelwriter = (ExcelWriter)writer;
                excelwriter.VisibleColumns = columns;
                return(excelwriter.CreateFile(ns, datastructureId, title, ext));

            // no valid extension given
            default:
                return("");
            }
        }
Exemple #3
0
        public string GenerateAsciiFile(long id, string title, string mimeType, string[] visibleColumns)
        {
            string ext  = "";
            string path = "";

            DatasetManager datasetManager = new DatasetManager();

            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);
                AsciiWriter    writer         = new AsciiWriter(TextSeperator.comma);

                // Javad: It is better to have a list of tuple IDs and pass it to the AddDataTuples method.
                // This method is using a special iterator to reduce the number of queries. 18.11.2016

                List <long> datatuples = new List <long>(); //GetFilteredDataTuples(datasetVersion);

                long datastuctureId = datasetVersion.Dataset.DataStructure.Id;

                path = generateDownloadFile(id, datasetVersion.Id, datastuctureId, "data", ext, writer);

                if (visibleColumns != null)
                {
                    writer.VisibleColumns = visibleColumns;
                }

                writer.AddDataTuples(datasetManager, datatuples, path, datastuctureId);

                return(path);
            }
            finally
            {
                datasetManager.Dispose();
            }
        }
Exemple #4
0
        public string GenerateAsciiFile(string ns, DataTable table, string title, string mimeType, long dataStructureId)
        {
            string        ext           = "";
            TextSeperator textSeperator = TextSeperator.semicolon;

            switch (mimeType)
            {
            case "text/csv":
            {
                ext           = ".csv";
                textSeperator = TextSeperator.semicolon;
                break;
            }

            default:
            {
                ext           = ".txt";
                textSeperator = TextSeperator.tab;
                break;
            }
            }

            AsciiWriter writer = new AsciiWriter(textSeperator);

            // write to file
            // if there is already a file, replace it
            string path = generateDownloadFile(ns, dataStructureId, title, ext, writer);

            writer.AddDataTuples(table, path, dataStructureId);

            return(path);
        }
Exemple #5
0
        public string GenerateAsciiFile(string ns, DataTable table, string title, string mimeType, long dataStructureId, bool withUnits = false)
        {
            string        ext           = "";
            TextSeperator textSeperator = TextSeperator.semicolon;

            switch (mimeType)
            {
            case "text/csv":
            case "text/comma-separated-values":
            case "application/octet-stream":
                /* of course this is a wrong  mimetype for csv.
                 * but the c# class MimeMapping.GetMimeMapping(ext) currently returns this as a result for .csv.
                 * since we don't use the datatype at the moment,
                 * it will be rebuilt into the case here*/
            {
                ext           = ".csv";
                textSeperator = TextSeperator.semicolon;
                break;
            }

            case "text/tsv":
            case "text/tab-separated-values":
            {
                ext           = ".tsv";
                textSeperator = TextSeperator.tab;
                break;
            }

            default:
            {
                ext           = ".txt";
                textSeperator = TextSeperator.tab;
                break;
            }
            }

            AsciiWriter writer = new AsciiWriter(textSeperator);

            // write to file
            // if there is already a file, replace it
            string path = createDownloadFile(ns, dataStructureId, title, ext, writer);

            string[] units   = null;
            string[] columns = null;
            if (withUnits)
            {
                columns = getColumnNames(table);
                units   = getUnits(dataStructureId, columns);
            }

            writer.AddData(table, path, dataStructureId, units);

            return(path);
        }
Exemple #6
0
        private string generateDownloadFile(long id, long datasetVersionOrderNo, long dataStructureId, string title, string ext, DataWriter writer)
        {
            if (ext.Equals(".csv") || ext.Equals(".txt"))
            {
                AsciiWriter asciiwriter = (AsciiWriter)writer;
                return(asciiwriter.CreateFile(id, datasetVersionOrderNo, dataStructureId, "data", ext));
            }
            else
            if (ext.Equals(".xlsm"))
            {
                ExcelWriter excelwriter = (ExcelWriter)writer;
                return(excelwriter.CreateFile(id, datasetVersionOrderNo, dataStructureId, "data", ext));
            }

            return("");
        }
Exemple #7
0
        /// <summary>
        /// create a new file of given extension
        /// ns will be used to seperate from other files created of individual datasets
        /// </summary>
        /// <param name="ns">namespace</param>
        /// <param name="title"></param>
        /// <param name="ext"></param>
        /// <param name="writer"></param>
        /// <returns></returns>
        private string generateDownloadFile(string ns, long datastructureId, string title, string ext, DataWriter writer)
        {
            switch (ext)
            {
            // text based files
            case ".csv":
            case ".txt":
                AsciiWriter asciiwriter = (AsciiWriter)writer;
                return(asciiwriter.CreateFile(ns, title, ext));

            // excel files
            case ".xlsx":
            case ".xlsm":
                ExcelWriter excelwriter = (ExcelWriter)writer;
                return(excelwriter.CreateFile(ns, datastructureId, title, ext));

            // no valid extension given
            default:
                return("");
            }
        }
Exemple #8
0
        public string GenerateAsciiFile(long id, long versionId, string mimeType, bool withUnits)
        {
            DatasetManager       datasetManager          = new DatasetManager();
            DataStructureManager datasetStructureManager = new DataStructureManager();

            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(versionId);
                int            versionNr      = datasetManager.GetDatasetVersionNr(datasetVersion);

                string        contentDescriptorTitle = "";
                string        ext           = "";
                string        nameExt       = "";
                TextSeperator textSeperator = TextSeperator.semicolon;

                if (withUnits)
                {
                    nameExt = "_withunits";
                }

                switch (mimeType)
                {
                case "text/csv":
                case "text/comma-separated-values":
                case "application/octet-stream":
                    /* of course this is a wrong  mimetype for csv.
                     * but the c# class MimeMapping.GetMimeMapping(ext) currently returns this as a result for .csv.
                     * since we don't use the datatype at the moment,
                     * it will be rebuilt into the case here*/
                {
                    contentDescriptorTitle = "generatedCSV" + nameExt;
                    ext           = ".csv";
                    textSeperator = TextSeperator.semicolon;
                    break;
                }

                case "text/tsv":
                case "text/tab-separated-values":
                {
                    contentDescriptorTitle = "generatedTSV" + nameExt;
                    ext           = ".tsv";
                    textSeperator = TextSeperator.tab;
                    break;
                }

                default:
                {
                    contentDescriptorTitle = "generatedTXT" + nameExt;
                    ext           = ".txt";
                    textSeperator = TextSeperator.tab;
                    break;
                }
                }

                AsciiWriter writer = new AsciiWriter(textSeperator);

                string path = "";

                //ascii allready exist
                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(contentDescriptorTitle) &&
                                                            p.URI.Contains(datasetVersion.Id.ToString())) > 0 &&
                    !withUnits)
                {
                    #region FileStream exist

                    ContentDescriptor contentdescriptor = datasetVersion.ContentDescriptors.Where(p => p.Name.Equals(contentDescriptorTitle)).FirstOrDefault();
                    path = Path.Combine(AppConfiguration.DataPath, contentdescriptor.URI);

                    if (FileHelper.FileExist(path))
                    {
                        return(path);
                    }

                    #endregion FileStream exist
                }

                // not exist, needs to generated - get data first as datatable
                DataTable data = getData(id, versionId);

                long datastuctureId = datasetVersion.Dataset.DataStructure.Id;

                path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, null, withUnits);

                storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, withUnits);

                //add units if want
                string[] units = null;
                if (withUnits)
                {
                    units = getUnits(datastuctureId, null);
                }

                writer.AddData(data, path, datastuctureId, units);

                return(path);
            }
            finally
            {
                datasetManager.Dispose();
                datasetStructureManager.Dispose();
            }
        }
Exemple #9
0
        public string GenerateAsciiFile(long id, long versionId, string title, string mimeType)
        {
            DatasetManager datasetManager = new DatasetManager();

            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(versionId);

                string contentDescriptorTitle = "";
                string ext = "";

                TextSeperator textSeperator = TextSeperator.semicolon;

                switch (mimeType)
                {
                case "text/csv":
                {
                    contentDescriptorTitle = "generatedCSV";
                    ext           = ".csv";
                    textSeperator = TextSeperator.semicolon;
                    break;
                }

                default:
                {
                    contentDescriptorTitle = "generatedTXT";
                    ext           = ".txt";
                    textSeperator = TextSeperator.tab;
                    break;
                }
                }

                AsciiWriter writer = new AsciiWriter(textSeperator);

                string path = "";

                //ascii allready exist
                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(contentDescriptorTitle) && p.URI.Contains(datasetVersion.Id.ToString())) > 0)
                {
                    #region FileStream exist

                    ContentDescriptor contentdescriptor = datasetVersion.ContentDescriptors.Where(p => p.Name.Equals(contentDescriptorTitle)).FirstOrDefault();
                    path = Path.Combine(AppConfiguration.DataPath, contentdescriptor.URI);

                    if (FileHelper.FileExist(path))
                    {
                        return(path);
                    }
                    else
                    {
                        List <long> datatupleIds   = datasetManager.GetDatasetVersionEffectiveTupleIds(datasetVersion);
                        long        datastuctureId = datasetVersion.Dataset.DataStructure.Id;

                        path = generateDownloadFile(id, datasetVersion.Id, datastuctureId, "Data", ext, writer);

                        storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext);

                        writer.AddDataTuples(datasetManager, datatupleIds, path, datastuctureId);

                        return(path);
                    }

                    #endregion
                }
                // not exist needs to generated
                else
                {
                    #region FileStream not exist

                    List <long> datatupleIds   = datasetManager.GetDatasetVersionEffectiveTupleIds(datasetVersion);
                    long        datastuctureId = datasetVersion.Dataset.DataStructure.Id;

                    path = generateDownloadFile(id, datasetVersion.Id, datastuctureId, "data", ext, writer);

                    storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext);

                    writer.AddDataTuples(datasetManager, datatupleIds, path, datastuctureId);

                    return(path);

                    #endregion
                }
            }
            finally
            {
                datasetManager.Dispose();
            }
        }