Example #1
0
        /// <summary>
        /// Creates a species observation CSV file and stores it on the server in the
        /// Temp/Export/ folder.
        /// This can be used in workflow scenarios.
        ///
        /// </summary>
        /// <returns>JSON data with information about the URL to the generated file.</returns>
        public JsonNetResult SpeciesObservationGridAsCsvStoreFileOnServer()
        {
            JsonModel jsonModel;
            SpeciesObservationGridCalculator resultCalculator = null;
            SpeciesObservationGridResult     result           = null;

            try
            {
                IUserContext currentUser = GetCurrentUser();
                if (currentUser.IsAuthenticated() && !currentUser.IsCurrentRolePrivatePerson())
                {
                    throw new Exception(Resource.ResultWorkflowCurrentRoleIsNotPrivatePerson);
                }

                resultCalculator = new SpeciesObservationGridCalculator(currentUser, SessionHandler.MySettings);
                result           = resultCalculator.GetSpeciesObservationGridResultFromCacheIfAvailableOrElseCalculate();
                string filename = FileExportManager.CreateSpeciesObservationGridAsCsvAndStoreOnServer(currentUser, result);
                string url      = FileSystemManager.ExportFolderUrl + filename;
                jsonModel = JsonModel.CreateFromObject(url);
            }
            catch (Exception ex)
            {
                jsonModel = JsonModel.CreateFailure(ex.Message);
            }

            return(new JsonNetResult(jsonModel));
        }
        /// <summary>
        /// Constructor of an excel xml file with grid statistics on species observation counts.
        /// </summary>
        /// <param name="currentUser">Current user context.</param>
        /// <param name="coordinateSystem">Coordinate system of the transformated coordinates.</param>
        /// <param name="addSettings">True if settings sheet should be included</param>
        /// <param name="addProvenance">True if provenance sheet should be included.</param>
        public GridStatisticsOnSpeciesObservationCountExcelXml(IUserContext currentUser, CoordinateSystemId coordinateSystem, bool addSettings, bool addProvenance)
            : base()
        {
            var resultCalculator = new SpeciesObservationGridCalculator(currentUser, SessionHandler.MySettings);
            var data             = resultCalculator.GetSpeciesObservationGridResultFromCacheIfAvailableOrElseCalculate();

            _xmlBuilder = new StringBuilder();

            //Add file definitions and basic format settings
            _xmlBuilder.AppendLine(base.GetInitialSection());

            //Specify column and row counts
            _xmlBuilder.AppendLine(base.GetColumnInitialSection(7, data.Cells.Count));

            //Specify column widths
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(200));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(90));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(150));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(150));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(170));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(170));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(170));

            //Add row with column headers
            _xmlBuilder.AppendLine(base.GetRowStart());
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine("Id"));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(Resource.GridStatisticsObservationCount));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate X ({0})", data.GridCellCoordinateSystem)));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate Y ({0})", data.GridCellCoordinateSystem)));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate X ({0})", coordinateSystem.ToString())));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate Y ({0})", coordinateSystem.ToString())));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine("Grid cell width (metres)"));
            _xmlBuilder.AppendLine(base.GetRowEnd());

            //Data values
            foreach (SpeciesObservationGridCellResult row in data.Cells)
            {
                _xmlBuilder.AppendLine(base.GetRowStart());
                _xmlBuilder.AppendLine(base.GetDataRowLine("String", row.Identifier));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.ObservationCount.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.OriginalCentreCoordinateX.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.OriginalCentreCoordinateY.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.CentreCoordinateX.ToString(base.GetApprotiateGlobalization())));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.CentreCoordinateY.ToString(base.GetApprotiateGlobalization())));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", data.GridCellSize.ToString()));
                _xmlBuilder.AppendLine(base.GetRowEnd());
            }

            // Add final section of the xml document.
            _xmlBuilder.AppendLine(GetFinalSection(GetAditionalSheets(currentUser, addSettings, addProvenance)));
        }
Example #3
0
        /// <summary>
        /// Creates an excel file.
        /// Writes the content of a list into a worksheet of an excelfile and save the file.
        /// </summary>
        /// <param name="autosizeColumnWidth">
        /// If true, the columns will be autosized.
        /// </param>
        /// <returns>
        /// The <see cref="MemoryStream"/>.
        /// </returns>
        private MemoryStream CreateExcelFile(bool autosizeColumnWidth = false)
        {
            MemoryStream memoryStream = new MemoryStream();

            try
            {
                using (ExcelPackage package = new ExcelPackage(memoryStream))
                {
                    var resultCalculator = new SpeciesObservationGridCalculator(currentUser, SessionHandler.MySettings);
                    var data             = resultCalculator.GetSpeciesObservationGridResultFromCacheIfAvailableOrElseCalculate();

                    // Add a new worksheet to the empty workbook.
                    // The name of the sheet can not be longer than 31 characters.
                    var worksheet = package.Workbook.Worksheets.Add("SLW Data");
                    AddHeaders(worksheet, data);
                    AddContentData(worksheet, data);
                    FormatHeader(worksheet, 1, 7);

                    if (autosizeColumnWidth)
                    {
                        worksheet.Cells.AutoFitColumns(0);
                    }

                    //Add aditional sheets if user has request that
                    AddAditionalSheets(package);

                    package.Save();
                }

                memoryStream.Position = 0;
                return(memoryStream);
            }
            catch (Exception)
            {
                memoryStream.Dispose();

                throw;
            }
        }