IImporter.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace InABox.Core
  6. {
  7. public delegate void ImportNotificationEvent(object sender, string message);
  8. public delegate void ImportLogEvent(object sender, string message);
  9. public delegate bool ImportPreProcessEvent(object sender, Dictionary<string, string> values);
  10. public delegate bool ImportPostProcessEvent(object sender, object entity, Dictionary<string, string> values);
  11. public delegate void ImportSaveEvent(object sender, object entity);
  12. public delegate CoreTable ImportLoadEvent(object sender, Type type, String[] fields, String ID);
  13. public interface IImporter
  14. {
  15. string[] Properties { get; }
  16. /// <summary>
  17. /// Represents the fields in the header of the imported file, and therefore what fields <see cref="Mappings"/> can map from.
  18. /// These should be populated by <see cref="ReadHeader"/>.
  19. /// </summary>
  20. string[] Fields { get; }
  21. bool HasHeader { get; set; }
  22. int HeaderRow { get; set; }
  23. /// <summary>
  24. /// While <see cref="Fields"/> represents the fields that this importer recognises, <see cref="Mappings"/> represents how the properties of
  25. /// the imported object are populated, so while there may be a one to one mapping from <see cref="Fields"/>, this does not have to be the case.
  26. /// </summary>
  27. /// <remarks>
  28. /// These mappings must be initialised to get any data from <see cref="Import"/>.
  29. /// </remarks>
  30. List<ImportMapping> Mappings { get; }
  31. IEnumerable<string> Log { get; }
  32. event ImportNotificationEvent OnNotify;
  33. event ImportLogEvent OnError;
  34. event ImportLogEvent OnLog;
  35. event ImportPreProcessEvent BeforeProcess;
  36. event ImportPostProcessEvent AfterProcess;
  37. event ImportSaveEvent OnSave;
  38. event ImportLoadEvent OnLoad;
  39. bool Open(Stream stream);
  40. void Close();
  41. /// <summary>
  42. /// Import the data from the stream specified when <see cref="Open(Stream)"/> was called, using <see cref="OnSave"/> to for each line.
  43. /// </summary>
  44. /// <remarks>
  45. /// This method <i>must</i> be called after <see cref="Open(Stream)"/> and <see cref="ReadHeader"/>.
  46. ///
  47. /// One should also have set <see cref="OnSave"/> and <see cref="OnLoad"/>.
  48. /// </remarks>
  49. /// <returns>The number of items imported.</returns>
  50. int Import();
  51. /// <summary>
  52. /// Move to the next line in the import.
  53. /// </summary>
  54. /// <remarks>
  55. /// While not all importers will need to do anything with this method, it <i>must</i> be called before <see cref="ReadLine"/>.
  56. /// </remarks>
  57. /// <returns><see langword="true"/> if there is still data to be read.</returns>
  58. bool MoveNext();
  59. /// <summary>
  60. /// Read the header of the file, populating <see cref="Fields"/>.
  61. /// </summary>
  62. /// <remarks>
  63. /// This is called regardless of whether <see cref="HasHeader"/> is <see langword="true"/>.
  64. /// </remarks>
  65. /// <returns><see langword="true"/> if the header was read successfully.</returns>
  66. bool ReadHeader();
  67. /// <summary>
  68. /// Read the values of a line.
  69. /// </summary>
  70. /// <remarks>
  71. /// The keys of the results are those present in <see cref="Fields"/>.
  72. /// </remarks>
  73. /// <returns>
  74. /// A dictionary of values, where the keys are given by <see cref="Fields"/>.
  75. /// </returns>
  76. Dictionary<string, string> ReadLine();
  77. }
  78. }