CSVPosterSettings.cs 2.2 KB

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