Example #1
0
        /// <summary>
        /// The main entry point for writing codebook files.
        /// </summary>
        /// <param name="path">The path to location file should be written.</param>
        /// <returns>ExportResult</returns>
        private int WriteCodebookFile(string path)
        {
            var error = ExportResult.NoError;

            using (TextWriter writer = new StreamWriter(path))
            {
                using (var csvWriter = new CsvWriter(writer))
                {
                    _csvWriter = csvWriter;

                    // write headers for file
                    _csvWriter.WriteField("Question number");
                    _csvWriter.WriteField("Variable label");
                    _csvWriter.WriteField("Question type");
                    _csvWriter.WriteField("Question text");
                    _csvWriter.WriteField("Value label");
                    _csvWriter.WriteField("Value");
                    _csvWriter.NextRecord();

                    // write codebook entries into file
                    if ((error = WriteCodebookEntries(_schemeTree)) != ExportResult.NoError)
                    {
                        return(error);
                    }
                }
            }

            // set successful export result code and export file path
            if (error == ExportResult.NoError)
            {
                _result = new ExportResult(path);
            }

            return(error);
        }
Example #2
0
        // Export the project codebook, which servers as a guide to how all answer are coded
        // in numeric and text based exports
        public async Task <ExportResult> ExportProjectCodebook(long projectId)
        {
            _result    = new ExportResult();
            _projectId = projectId;
            _project   = await _unitOfWork.Projects.GetProjectWithScheme(p => p.Id == projectId);

            if (_project == null)
            {
                return(new ExportResult(ExportResult.ProjectNotFound));
            }

            _schemeTree = _unitOfWork.Schemes.GetSchemeQuestionsAsTree(_project.Scheme);
            if (_schemeTree == null || _schemeTree.Count == 0)
            {
                return(new ExportResult(ExportResult.SchemeNotFound));
            }

            // make sure we have a valid filename and replaces spaces with dashes
            var projectName = _project.Name;

            projectName = projectName.Replace(" ", "-");
            if (projectName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
            {
                projectName = "project-" + _project.Id.ToString();
            }

            // do file stuff
            var projDataDirInfo = GetProjectExportDirectoryInfo(projectName);
            var filePath        = Path.Combine(projDataDirInfo.FullName, projectName + "-stat-export.csv");
            var result          = WriteCodebookFile(filePath);

            return(_result);
        }
Example #3
0
        private async Task <int> WriteExportFile(string path)
        {
            var error = ExportResult.NoError;

            using (TextWriter writer = new StreamWriter(path, false, new UTF8Encoding(true)))
            {
                await writer.WriteAsync('\uFEFF');

                using (var csvWriter = new CsvWriter(writer))
                {
                    _csvWriter = csvWriter;
                    if ((error = WriteExportHeaders()) != ExportResult.NoError)
                    {
                        return(error);
                    }
                    if ((error = await WriteJurisdictionAnswers()) != ExportResult.NoError)
                    {
                        return(error);
                    }
                }
            }

            // set successful export result code and export file path
            if (error == ExportResult.NoError)
            {
                _result = new ExportResult(path);
            }

            return(error);
        }
Example #4
0
        /// <summary>
        /// The entry point of the export service.
        /// </summary>
        /// <param name="projectId">The project ID of the project to be exported.</param>
        /// <param name="type">The type of export desired.</param>
        /// <param name="userId">optional userid of the coder data to be exported</param>
        /// <returns>An ExportResult object</returns>
        public async Task <ExportResult> ExportProjectData(long projectId, string type, long userId = -1)
        {
            _result    = new ExportResult();
            _projectId = projectId;
            _userId    = userId;
            _validated = _userId == -1;
            _project   = await _unitOfWork.Projects.GetProjectWithScheme(p => p.Id == projectId);

            if (!_validated)
            {
                _codedUser = await _unitOfWork.Users.SingleAsync(u => u.Id == _userId);
            }
            if (_project == null)
            {
                return(new ExportResult(ExportResult.ProjectNotFound));
            }
            _schemeTree = _unitOfWork.Schemes.GetSchemeQuestionsAsTree(_project.Scheme);
            if (_schemeTree == null || _schemeTree.Count == 0)
            {
                return(new ExportResult(ExportResult.SchemeNotFound));
            }
            if (type.Equals("text"))
            {
                _codedTextExport = true;
            }
            else if (type.Equals("codebook"))
            {
                _codebookExport = true;
            }
            // make sure we have a valid filename and replaces spaces with dashes
            var projectName = _validated ? _project.Name: _project.Name + "-" + _codedUser.GetFullName();

            projectName = projectName.Replace(" ", "-");
            if (projectName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
            {
                projectName = "project-" + _project.Id.ToString();
            }

            // create paths for saving file on server
            var filePath        = "";
            var projDataDirInfo = GetProjectExportDirectoryInfo(projectName);

            // check for codebook export
            if (_codebookExport)
            {
                filePath = Path.Combine(projDataDirInfo.FullName, projectName + "-codebook-export.csv");
                var result = WriteCodebookFile(filePath);
            }
            else
            {
                // check for a text export, otherwise do numeric
                if (_codedTextExport)
                {
                    filePath = Path.Combine(projDataDirInfo.FullName, projectName + "-text-export.csv");
                }
                else
                {
                    filePath = Path.Combine(projDataDirInfo.FullName, projectName + "-numeric-export.csv");
                }
                var result = await WriteExportFile(filePath);
            }

            return(_result);
        }