Пример #1
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);
        }
Пример #2
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();
            }
        }