199 lines
6.5 KiB
C#
199 lines
6.5 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Saxonite.Net.Core.Web.Base.Procs.Data;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using Saxonite.Net.Core.Web.Admin.Models;
|
|
using System.Collections;
|
|
using Sarand.Net.Standard.Base.Helpers;
|
|
using Sarand.Net.Standard.Proc.Excel;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using System.IO;
|
|
using DocumentFormat.OpenXml.EMMA;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Saxonite.Net.Core.Web.Admin.Controllers;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
|
|
namespace Saxonite.Net.Core.Web.Admin.Procs
|
|
{
|
|
//=======================================================================
|
|
public class SaxOrganisationExporter : SaxProcess_Admin
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
|
|
public enum ExportFormat
|
|
{
|
|
Unknown,
|
|
Excel_Open, /* xlxs */
|
|
}
|
|
|
|
// Process configuration.
|
|
public new class Config : SaxProcess_Admin.Config
|
|
{
|
|
public Config(SaxSessionContext sessionContext, SaxOrganisation.Params prmOrganisation) : base(sessionContext)
|
|
{
|
|
PrmOrganisation = prmOrganisation;
|
|
}
|
|
// Properties.
|
|
public SaxOrganisation.Params PrmOrganisation { get; }
|
|
public ExportFormat Format { get; set; }
|
|
public bool IncludeId { get; set; }
|
|
public ushort MaxRows { get; set; } = 2000;
|
|
}
|
|
|
|
// Processing control data.
|
|
private class ProcessControlData
|
|
{
|
|
public Config Config { get; set; }
|
|
public SaMsSpreadsheet Spreadsheet { get; set; }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private ProcessControlData control { get; } = new ProcessControlData();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaxOrganisationExporter(Config config) : base(config)
|
|
{
|
|
control.Config = config;
|
|
}
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
// Spreadsheet.
|
|
DisposeSpreadsheet();
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
private void DisposeSpreadsheet()
|
|
{
|
|
if (control.Spreadsheet != null)
|
|
control.Spreadsheet.Dispose();
|
|
control.Spreadsheet = null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public override string ProcessDescription => "Organisation Export";
|
|
public byte[] GetDocumentData()
|
|
{
|
|
switch (control.Config.Format)
|
|
{
|
|
case ExportFormat.Excel_Open:
|
|
return control.Spreadsheet.GetDocumentData();
|
|
}
|
|
return null;
|
|
}
|
|
public String GetContentType()
|
|
{
|
|
switch (control.Config.Format)
|
|
{
|
|
case ExportFormat.Excel_Open: return SacsContentTypeHelper.ContentType_Excel_Open;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Run the process.
|
|
protected override bool Run()
|
|
{
|
|
String fn = GetType().Name + ".Run";
|
|
|
|
// Verify configuration.
|
|
if (control.Config == null)
|
|
{
|
|
AddErrorMessage("Configuration must be supplied.");
|
|
SaLogger.Warn(fn + " " + GetErrorMessage());
|
|
return OK;
|
|
}
|
|
if (control.Config.PrmOrganisation == null)
|
|
{
|
|
AddErrorMessage("Organisation params must be supplied.");
|
|
SaLogger.Warn(fn + " " + GetErrorMessage());
|
|
return OK;
|
|
}
|
|
else if (control.Config.Format != ExportFormat.Excel_Open)
|
|
{
|
|
AddErrorMessage("Unsupported export format: " + control.Config.Format.ToString() + ".");
|
|
SaLogger.Warn(fn + " " + GetErrorMessage());
|
|
}
|
|
AddInfoMessage("Export format: " + control.Config.Format.ToString());
|
|
|
|
if (OK) ExportOrganisations();
|
|
|
|
return OK;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
private bool ExportOrganisations()
|
|
{
|
|
if (!OK) return false;
|
|
|
|
DisposeSpreadsheet();
|
|
control.Spreadsheet = new SaMsSpreadsheet_POI();
|
|
control.Spreadsheet.CreateWorkbook();
|
|
control.Spreadsheet.WorksheetName = "Organisations";
|
|
control.Spreadsheet.ApplyDefaultStyle(GetExceldefaultWorkbookStyle());
|
|
|
|
// Columns.
|
|
ColumnDfn colId = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Id);
|
|
colId.Alignment = ColumnAlignment.Center;
|
|
|
|
// Columns to output.
|
|
List<ColumnDfn> columnsExport = new List<ColumnDfn>();
|
|
if (control.Config.IncludeId)
|
|
columnsExport.Add(colId);
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Name));
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.OrganisationCategory_Desc));
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.OrganisationType_Desc));
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Sport_Name));
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Region_Name));
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.User_Count));
|
|
columnsExport.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Member_Count));
|
|
|
|
// Initialise spreadsheet column headings and formats etc
|
|
SaMsExcelColumnHeading_Config configHeading = GetExcelColumnHeadingConfig();
|
|
control.Spreadsheet.SetColumnHeadings(columnsExport, configHeading);
|
|
|
|
// Columns to query - ensure Id is in the list.
|
|
List<ColumnDfn> columnsQuery = new List<ColumnDfn>(columnsExport);
|
|
if (!columnsQuery.Exists(item => item.Id == (int)SaxOrganisation.Column.Id))
|
|
columnsQuery.Add(colId);
|
|
|
|
// Organisation params.
|
|
control.Config.PrmOrganisation.ResultsPaging.PageNo = 1;
|
|
control.Config.PrmOrganisation.ResultsPaging.PageLength = (ushort)Math.Min(ushort.MaxValue, Math.Max(ushort.MinValue, control.Config.MaxRows));
|
|
|
|
// Query Organisations.
|
|
uint dataRowPos = configHeading.FirstDataRowPos - 1;
|
|
using (DbResultSet resultSet = AccountMgr.ExecuteOrganisationQuery(columnsQuery, control.Config.PrmOrganisation))
|
|
{
|
|
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
|
|
{
|
|
dataRowPos++;
|
|
NamedValue.Map values = resultSet.GetRecordValues();
|
|
SysId organisationId = new SysId(values.GetLong(colId.DataName));
|
|
int colIdx = -1;
|
|
foreach (ColumnDfn col in columnsExport)
|
|
{
|
|
colIdx++;
|
|
uint dataColPos = (uint)(configHeading.FirstHeadingColPos + colIdx);
|
|
DataValue value = AccountMgr.GetOrganisationDataValue(col, values);
|
|
if (col.ShowZeroValues || !col.IsNumeric || value.ToNumeric() != 0.0)
|
|
control.Spreadsheet.SetCellValue(dataColPos, dataRowPos, value);
|
|
}
|
|
}
|
|
}
|
|
return OK;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|