CSVPosterSettings.cs 2.3 KB

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