Inheritance: DataWriter
Example #1
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("");
            }
        }
Example #2
0
        private string SaveFileInContentDiscriptor(DatasetVersion datasetVersion)
        {
            try
            {
                //XXX put function like GetStorePathOriginalFile or GetDynamicStorePathOriginalFile
                // the function is available in the abstract class datawriter
                ExcelWriter excelWriter = new ExcelWriter();
                // Move Original File to its permanent location
                String tempPath = TaskManager.Bus[TaskManager.FILEPATH].ToString();
                string originalFileName = TaskManager.Bus[TaskManager.FILENAME].ToString();
                string storePath = excelWriter.GetFullStorePathOriginalFile(datasetVersion.Dataset.Id, datasetVersion.VersionNo, originalFileName);
                string dynamicStorePath = excelWriter.GetDynamicStorePathOriginalFile(datasetVersion.Dataset.Id, datasetVersion.VersionNo, originalFileName);
                string extention = TaskManager.Bus[TaskManager.EXTENTION].ToString();

                Debug.WriteLine("extention : " + extention);

                //Why using the excel writer, isn't any function available in System.IO.File/ Directory, etc. Javad
                FileHelper.MoveFile(tempPath, storePath);

                string mimeType = MimeMapping.GetMimeMapping(originalFileName);

                //Register the original data as a resource of the current dataset version
                ContentDescriptor originalDescriptor = new ContentDescriptor()
                {
                    OrderNo = 1,
                    Name = "unstructuredData",
                    MimeType = mimeType,
                    URI = dynamicStorePath,
                    DatasetVersion = datasetVersion,
                };

                    // add current contentdesciptor to list
                datasetVersion.ContentDescriptors.Add(originalDescriptor);

                return storePath;

            }
            catch(Exception e)
            {
                return "";
            }
        }
Example #3
0
        private string MoveAndSaveOriginalFileInContentDiscriptor(DatasetVersion datasetVersion)
        {
            TaskManager TaskManager = (TaskManager)Session["TaskManager"];

            //dataset id and data structure id are available via datasetVersion properties,why you are passing them via the BUS? Javad
            long datasetId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASET_ID]);
            long dataStructureId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASTRUCTURE_ID]);

            DatasetManager datasetManager = new DatasetManager();

            string title = TaskManager.Bus[TaskManager.DATASET_TITLE].ToString();
            string ext = ".xlsm";// TaskManager.Bus[TaskManager.EXTENTION].ToString();

            ExcelWriter excelWriter = new ExcelWriter();

            // Move Original File to its permanent location
            String tempPath = TaskManager.Bus[TaskManager.FILEPATH].ToString();
            string originalFileName = TaskManager.Bus[TaskManager.FILENAME].ToString();
            string storePath = excelWriter.GetFullStorePathOriginalFile(datasetId, datasetVersion.Id, originalFileName);
            string dynamicStorePath = excelWriter.GetDynamicStorePathOriginalFile(datasetId, datasetVersion.VersionNo, originalFileName);

            //Why using the excel writer, isn't any function available in System.IO.File/ Directory, etc. Javad
            FileHelper.MoveFile(tempPath, storePath);

            //Register the original data as a resource of the current dataset version
            ContentDescriptor originalDescriptor = new ContentDescriptor()
            {
                OrderNo = 1,
                Name = "original",
                MimeType = "application/xlsm",
                URI = dynamicStorePath,
                DatasetVersion = datasetVersion,
            };

            if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(originalDescriptor.Name)) > 0)
            {   // remove the one contentdesciptor
                foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                {
                    if (cd.Name == originalDescriptor.Name)
                    {
                        cd.URI = originalDescriptor.URI;
                    }
                }
            }
            else
            {
                // add current contentdesciptor to list
                datasetVersion.ContentDescriptors.Add(originalDescriptor);
            }

            return storePath;
        }
Example #4
0
        private string GenerateDownloadFile(DatasetVersion datasetVersion)
        {
            TaskManager TaskManager = (TaskManager)Session["TaskManager"];

            //dataset id and data structure id are available via datasetVersion properties,why you are passing them via the BUS? Javad
            long datasetId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASET_ID]);
            long dataStructureId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASTRUCTURE_ID]);

            DatasetManager datasetManager = new DatasetManager();

            string title = TaskManager.Bus[TaskManager.DATASET_TITLE].ToString();
            string ext = ".xlsm";// TaskManager.Bus[TaskManager.EXTENTION].ToString();

            ExcelWriter excelWriter = new ExcelWriter();

            // create the generated file and determine its location
            string path = excelWriter.CreateFile(datasetId, datasetVersion.VersionNo, dataStructureId, title, ext);
            string dynamicPath = excelWriter.GetDynamicStorePath(datasetId, datasetVersion.VersionNo, title, ext);
            //Register the generated data file as a resource of the current dataset version
            ContentDescriptor generatedDescriptor = new ContentDescriptor()
            {
                OrderNo = 1,
                Name = "generated",
                MimeType = "application/xlsm",
                URI = dynamicPath,
                DatasetVersion = datasetVersion,
            };

            if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(generatedDescriptor.Name)) > 0)
            {   // remove the one contentdesciptor
                foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                {
                    if (cd.Name == generatedDescriptor.Name)
                    {
                        cd.URI = generatedDescriptor.URI;
                    }
                }
            }
            else
            {
                // add current contentdesciptor to list
                datasetVersion.ContentDescriptors.Add(generatedDescriptor);
            }

            // note: the descriptors are not persisted yet, they will be persisted if the caller of this method persists the datasetVersion object.
            return path;
        }
Example #5
0
        private void AddDatatuplesToFile(long datasetId, long dataStructureId, string path)
        {
            DatasetManager datasetManager = new DatasetManager();
            DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(datasetId);
            List<long> tempDataTuplesIds = datasetManager.GetDatasetVersionEffectiveTupleIds(datasetVersion);

            ExcelWriter excelWriter = new ExcelWriter();
            excelWriter.AddDataTuplesToTemplate(datasetManager, tempDataTuplesIds, path, dataStructureId);
        }
Example #6
0
        //public string GenerateExcelFile(long id, string title, bool createAsTemplate, DataTable data = null, bool withUnits = false)
        //{
        //    string mimeType = "";
        //    string ext = ".xlsx";
        //    string contentDescriptorTitle = "";

        //    if (createAsTemplate)
        //    {
        //        ext = ".xlsm";
        //        contentDescriptorTitle = "generated";
        //    }
        //    else
        //    {
        //        ext = ".xlsx";
        //        if (withUnits) contentDescriptorTitle = "generatedExcelWithUnits";
        //        else contentDescriptorTitle = "generatedExcel";
        //    }

        //    mimeType = MimeMapping.GetMimeMapping(ext);

        //    DatasetManager datasetManager = new DatasetManager();

        //    try
        //    {
        //        DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);
        //        ExcelWriter writer = new ExcelWriter(createAsTemplate);

        //        string path = "";

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

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

        //            long version = datasetVersion.Id;
        //            long versionNrGeneratedFile =
        //                Convert.ToInt64(contentdescriptor.URI.Split('\\').Last().Split('_')[1]);

        //            // check if FileStream exist
        //            if (FileHelper.FileExist(path) && version == versionNrGeneratedFile)
        //            {
        //                return path;
        //            }

        //            #endregion FileStream exist
        //        }

        //        // not exist needs to generated

        //        #region FileStream not exist

        //        if (data == null)
        //        {
        //            DatasetManager dm = new DatasetManager();
        //            data = dm.GetLatestDatasetVersionTuples(id);
        //            data.Strip();
        //        }

        //        long datastuctureId = datasetVersion.Dataset.DataStructure.Id;
        //        int versionNr = datasetManager.GetDatasetVersionNr(datasetVersion);
        //        if (createAsTemplate)
        //        {
        //            string[] columnNames = (from dc in data.Columns.Cast<DataColumn>()
        //                                    select dc.Caption).ToArray();

        //            path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, columnNames);
        //            storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, false);
        //            writer.AddData(data.Rows, path, datastuctureId);
        //        }
        //        else
        //        {
        //            path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, null, withUnits);

        //            // the default data is without units, so store the path of the file if it was generated
        //            storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, withUnits);

        //            string[] units = null;
        //            if (withUnits) units = getUnits(datastuctureId, null);

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

        //        return path;

        //        #endregion FileStream not exist
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        datasetManager.Dispose();
        //    }
        //}

        /// <summary>
        /// version id = 0 == latest version
        /// </summary>
        /// <param name="id"></param>
        /// <param name="versionId"></param>
        /// <param name="createAsTemplate"></param>
        /// <param name="data"></param>
        /// <param name="withUnits"></param>
        /// <returns></returns>
        public string GenerateExcelFile(long id, long versionId, bool createAsTemplate, DataTable data = null, bool withUnits = false)
        {
            string mimeType = "";
            string ext      = ".xlsx";
            string contentDescriptorTitle = "";

            if (createAsTemplate)
            {
                ext = ".xlsm";
                contentDescriptorTitle = "generated";
            }
            else
            {
                ext = ".xlsx";
                if (withUnits)
                {
                    contentDescriptorTitle = "generatedExcelWithUnits";
                }
                else
                {
                    contentDescriptorTitle = "generatedExcel";
                }
            }

            mimeType = MimeMapping.GetMimeMapping(ext);

            DatasetManager datasetManager = new DatasetManager();

            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);
                ExcelWriter    writer         = new ExcelWriter(createAsTemplate);

                string path = "";

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

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

                    long version = datasetVersion.Id;
                    long versionNrGeneratedFile =
                        Convert.ToInt64(contentdescriptor.URI.Split('\\').Last().Split('_')[1]);

                    // check if FileStream exist
                    if (FileHelper.FileExist(path) && version == versionNrGeneratedFile)
                    {
                        return(path);
                    }

                    #endregion FileStream exist
                }

                // not exist needs to generated

                #region FileStream not exist

                if (data == null)
                {
                    data = getData(id, versionId);
                }

                long datastuctureId = datasetVersion.Dataset.DataStructure.Id;
                int  versionNr      = datasetManager.GetDatasetVersionNr(datasetVersion);
                if (createAsTemplate)
                {
                    string[] columnNames = (from dc in data.Columns.Cast <DataColumn>()
                                            select dc.Caption).ToArray();

                    path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, columnNames);
                    storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, false);
                    writer.AddData(data.Rows, path, datastuctureId);
                }
                else
                {
                    path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, null, withUnits);

                    // the default data is without units, so store the path of the file if it was generated
                    storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, withUnits);

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

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

                return(path);

                #endregion FileStream not exist
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                datasetManager.Dispose();
            }
        }
Example #7
0
        public string GenerateExcelFile(long id, string title)
        {
            string mimeType = "application / xlsm";
            string contentDescriptorTitle = "generated";
            string ext = ".xlsm";


            DatasetManager datasetManager = new DatasetManager();


            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);
                ExcelWriter    writer         = new ExcelWriter();

                string path = "";

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

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

                    long version = datasetVersion.Id;
                    long versionNrGeneratedFile =
                        Convert.ToInt64(contentdescriptor.URI.Split('\\').Last().Split('_')[1]);

                    // check if FileStream exist
                    if (FileHelper.FileExist(path) && version == versionNrGeneratedFile)
                    {
                        return(path);
                    }

                    // if not generate
                    else
                    {
                        List <long> datatupleIds =
                            datasetManager.GetDatasetVersionEffectiveTupleIds(datasetVersion);
                        long datastuctureId = datasetVersion.Dataset.DataStructure.Id;
                        path = generateDownloadFile(id, datasetVersion.Id, datastuctureId, title, ext,
                                                    writer);

                        storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext);
                        writer.AddDataTuplesToTemplate(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.AddDataTuplesToTemplate(datasetManager, datatupleIds, path, datastuctureId);

                    return(path);

                    #endregion
                }

                return("");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                datasetManager.Dispose();
            }
        }