From 8ee37324f1353a185fc59e22591360995602f73c Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 26 Oct 2020 19:14:42 +0000 Subject: [PATCH] --- server/AyaNova/AyaNova.csproj | 2 +- .../AyaNova/Controllers/ExportController.cs | 33 ++++++++++++++----- server/AyaNova/biz/WidgetBiz.cs | 31 +++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/server/AyaNova/AyaNova.csproj b/server/AyaNova/AyaNova.csproj index 08d34c58..b5a749e5 100644 --- a/server/AyaNova/AyaNova.csproj +++ b/server/AyaNova/AyaNova.csproj @@ -17,7 +17,7 @@ - + diff --git a/server/AyaNova/Controllers/ExportController.cs b/server/AyaNova/Controllers/ExportController.cs index 7655c8f7..6d9d1d44 100644 --- a/server/AyaNova/Controllers/ExportController.cs +++ b/server/AyaNova/Controllers/ExportController.cs @@ -14,6 +14,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.IO; using System.IO.Compression; +using ChoETL; namespace AyaNova.Api.Controllers { @@ -80,27 +81,31 @@ namespace AyaNova.Api.Controllers log.LogDebug($"Instantiating biz object handler for {dataListSelection.ObjectType}"); var biz = BizObjectFactory.GetBizObject(dataListSelection.ObjectType, ct); log.LogDebug($"Fetching data for {dataListSelection.SelectedRowIds.Length} {dataListSelection.ObjectType} items"); - var TheData = await ((IExportAbleObject)biz).GetExportData(dataListSelection.SelectedRowIds); + // var TheData = await ((IExportAbleObject)biz).GetJSONExportData(dataListSelection.SelectedRowIds); - string baseFileName = FileUtil.StringToSafeFileName($"{dataListSelection.ObjectType.ToString().ToLowerInvariant()}-{FileUtil.GetSafeDateFileName()}"); + string baseFileName = FileUtil.StringToSafeFileName($"{dataListSelection.ObjectType.ToString().ToLowerInvariant()}-{format}-{FileUtil.GetSafeDateFileName()}"); // string outputRandomFileNameNoExtension = StringUtil.ReplaceLastOccurrence(FileUtil.NewRandomFileName, ".", ""); string outputSourceFileName = baseFileName + "." + format; string outputSourceFullPath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, outputSourceFileName); string outputZipFullpath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, baseFileName + ".zip"); - // string outputFullPathSourceFile=outputFullPathNoExtension - // string outputFullpathZipFile=outputFullPathNoExtension + ".zip"; - log.LogDebug($"Calling render export data to file"); + + log.LogDebug($"Calling render export data to file {outputZipFullpath}"); switch (format) { - case "csv": - throw new System.NotImplementedException("CSV export not implemented yet"); + case "csv": + using (var w = new ChoCSVWriter(outputSourceFullPath).WithFirstLineHeader()) + { + var dat = await ((IExportAbleObject)biz).GetExportData(dataListSelection.SelectedRowIds); + w.Write(ToDynamicList(dat)); + } break; case "json": using (StreamWriter file = System.IO.File.CreateText(outputSourceFullPath)) using (JsonTextWriter writer = new JsonTextWriter(file)) { - TheData.WriteTo(writer); + var dat = await ((IExportAbleObject)biz).GetExportData(dataListSelection.SelectedRowIds); + dat.WriteTo(writer); } break; } @@ -118,6 +123,18 @@ namespace AyaNova.Api.Controllers } + public static IList ToDynamicList(JArray data) + { + var dynamicData = new List(); + var expConverter = new Newtonsoft.Json.Converters.ExpandoObjectConverter(); + + foreach (var dataItem in data) + { + dynamic obj = JsonConvert.DeserializeObject(dataItem.ToString(), expConverter); + dynamicData.Add(obj); + } + return dynamicData; + } /// /// Download a rendered Export diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 752a5fab..646406fb 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -321,6 +321,37 @@ namespace AyaNova.Biz return await GetReportData(idList); } + + // public async Task GetObjectArrayExportData(long[] idList) + // { + // //NOTE: Report widget is a superset of biz object widget + // //Biz objects will add and needed linked records here as extra fields with the data included + // //for example instead of a userid only there will be username added to the record + // //so the report designer can just select it as a field, no need to query seperately for it etc + // //REMEMBER: there is a name display format system and it should honour that so that the report + // //displays a user the same as it would display in the UI in an input form, no surprises + + // List wList = new List(); + // while (idList.Any()) + // { + // var batch = idList.Take(IReportAbleObject.REPORT_DATA_BATCH_SIZE); + // idList = idList.Skip(IReportAbleObject.REPORT_DATA_BATCH_SIZE).ToArray(); + // //query for this batch, comes back in db natural order unfortunately + // var batchResults = await ct.Widget.Where(z => batch.Contains(z.Id)).ToArrayAsync(); + // //order the results back into original + // var orderedList = from id in batch join z in batchResults on id equals z.Id select z; + + // wList.AddRange(orderedList); + // // foreach (Widget w in orderedList) + // // { + // // var jo = JObject.FromObject(w); + // // jo["CustomFields"] = JObject.Parse((string)jo["CustomFields"]); + // // ReportData.Add(jo); + // // } + // } + // return wList.ToArray(); + // } + //////////////////////////////////////////////////////////////////////////////////////////////// //JOB / OPERATIONS //