CSVPosterSettings.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using InABox.Core;
  2. using Inflector;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Poster.CSV
  9. {
  10. public class CSVPosterSettings : PosterSettings
  11. {
  12. [FileNameEditor("CSV Files (*.csv)|*.csv", RequireExisting = false)]
  13. public string DefaultOutputFile { get; set; }
  14. public override string DefaultScript(Type TPostable)
  15. {
  16. var tName = TPostable.Name;
  17. var decapital = tName[0..1].ToLower() + tName[1..];
  18. var ns = TPostable.Namespace;
  19. return @"
  20. using " + ns + @";
  21. using InABox.Poster.CSV;
  22. using InABox.Core;
  23. using System.Collections.Generic;
  24. public class Module
  25. {
  26. // Output Results for CSV
  27. public ICSVExport<" + tName + @"> Results { get; set; }
  28. // Customise 'model' before loading data. Return false if the post needs to be cancelled.
  29. public bool BeforePost(IDataModel<" + tName + @"> model)
  30. {
  31. return true;
  32. }
  33. public bool Process(IDataModel<" + tName + @"> model)
  34. {
  35. // Create new export object. You can use any object as your map,
  36. // but for simple cases just use " + tName + @".
  37. var export = new CSVExport<" + $"{tName}, {tName}" + @">();
  38. // Define the mapping from the fields of " + tName + @" to columns in the CSV file.
  39. export.DefineMapping(new()
  40. {
  41. new(""ID"", " + decapital + @" => " + decapital + @".ID),
  42. new(""Column1"", " + decapital + @" => " + decapital + @".Column1),
  43. new(""Column2"", " + decapital + @" => " + decapital + @".Column2),
  44. new(""Column3"", " + decapital + @" => " + decapital + @".Column3),
  45. // etc.
  46. });
  47. foreach(var " + decapital + @" in model.GetTable<" + tName + @">().ToObjects<" + tName + @">())
  48. {
  49. // Do processing
  50. // Add item to export
  51. export.Add(" + $"{decapital}, {decapital}" + @");
  52. }
  53. Results = export; // Set result
  54. return true; // return true for success.
  55. }
  56. // Perform any post-processing. All the items of type '" + tName + @" will be saved after this function is called.
  57. public void AfterPost(IDataModel<" + tName + @"> model)
  58. {
  59. }
  60. }";
  61. }
  62. }
  63. }