You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using Swift.DAL.SwiftDAL;
  6. namespace Swift.DAL.BL.Helper
  7. {
  8. public class XmlGenerator
  9. {
  10. public string FilePath{get;set;}
  11. public List<FieldName> FieldList { get; set; }
  12. public bool FirstLineIsHeader { get; set; }
  13. public bool CheckFirstLineHeader { get; set; }
  14. public bool UseHearderAsColumn { get; set; }
  15. public string RowSeperator { get; set; }
  16. public string ColSeperator { get; set; }
  17. public bool IgnoreInvalidRow { get; set; }
  18. public int TotalRecords { get; set; }
  19. public int InvalidRecords { get; set; }
  20. public DbResult Dr { get; set; }
  21. private DbResult CheckHeader(string fileName)
  22. {
  23. var res = new DbResult();
  24. res.SetError("0", "Success", "");
  25. var streamReader = new StreamReader(fileName);
  26. if (streamReader.EndOfStream)
  27. {
  28. res.SetError("1", "Invalid file. Please check the data in the file", "");
  29. return res;
  30. }
  31. var firstLine = streamReader.ReadLine();
  32. if(firstLine.Length < 2)
  33. {
  34. res.SetError("1", "Invalid data found. Please check the first line", "");
  35. return res;
  36. }
  37. if(IsNumeric(firstLine.Substring(0,2)))
  38. {
  39. res.SetError("1", "Header should be included. Please include header in the file.", "");
  40. return res;
  41. }
  42. return res;
  43. }
  44. public static Boolean IsNumeric(string stringToTest)
  45. {
  46. int result;
  47. return int.TryParse(stringToTest, out result);
  48. }
  49. public string GenerateXML()
  50. {
  51. var dr = new DbResult();
  52. var errorRecordNumber = "";
  53. if (!UseHearderAsColumn && FieldList.Count==0)
  54. {
  55. if (FirstLineIsHeader)
  56. {
  57. UseHearderAsColumn = true;
  58. }
  59. }
  60. var sb = new StringBuilder();
  61. var contents = "";
  62. if (UseHearderAsColumn && FirstLineIsHeader)
  63. {
  64. contents = ReadFileContent(FilePath, false);
  65. }
  66. else
  67. {
  68. if (CheckFirstLineHeader)
  69. {
  70. var res = CheckHeader(FilePath);
  71. if (res.ErrorCode != "0")
  72. {
  73. Dr = res;
  74. return "";
  75. }
  76. contents = ReadFileContent(FilePath, true);
  77. }
  78. else
  79. {
  80. contents = ReadFileContent(FilePath, false);
  81. }
  82. }
  83. var rowSeperator = new[] { RowSeperator };
  84. var rows = contents.Split(rowSeperator, StringSplitOptions.None);
  85. var colSeperator = new[] { ColSeperator };
  86. int start = UseHearderAsColumn ? 1 : 0;
  87. if (rows.Length > 0 && (UseHearderAsColumn || FieldList.Count==0))
  88. {
  89. var cols = rows[0].Split(colSeperator, StringSplitOptions.None);
  90. if (UseHearderAsColumn)
  91. {
  92. var list = new List<FieldName>();
  93. var i = 0;
  94. foreach (var col in cols)
  95. {
  96. list.Add(new FieldName(i++, col));
  97. }
  98. FieldList = list;
  99. }
  100. else
  101. {
  102. var list = new List<FieldName>();
  103. var i = 0;
  104. foreach (var col in cols)
  105. {
  106. list.Add(new FieldName(i++, "field" + i.ToString()));
  107. }
  108. FieldList = list;
  109. }
  110. }
  111. var end = rows.Length;
  112. TotalRecords = end - start;
  113. var invalidRecords = 0;
  114. sb.Append("<root>");
  115. for (var i = start; i < end; i++)
  116. {
  117. var cols = rows[i].Split(colSeperator, StringSplitOptions.None);
  118. var colLengh = cols.Length;
  119. if (colLengh == FieldList.Count)
  120. {
  121. sb.Append("<row");
  122. foreach (var fld in FieldList)
  123. {
  124. sb.Append(" " + fld.Name.ToLower().Replace(" ", "_") + "=\"" + cols[fld.Index] + "\"");
  125. }
  126. sb.Append(" has_error=\"N\"");
  127. sb.Append("/>");
  128. }
  129. else
  130. {
  131. if (IgnoreInvalidRow)
  132. {
  133. sb.Append("<row");
  134. foreach (var fld in FieldList)
  135. {
  136. if (fld.Index <= colLengh)
  137. {
  138. sb.Append(" " + fld.Name.ToLower().Replace(" ", "_") + "=\"" + cols[fld.Index] + "\"");
  139. }
  140. else
  141. {
  142. sb.Append(" " + fld.Name.ToLower().Replace(" ", "_") + "=\"\"");
  143. }
  144. }
  145. sb.Append(" has_error=\"Y\"");
  146. sb.Append("/>");
  147. errorRecordNumber += ", " + i.ToString();
  148. invalidRecords++;
  149. }
  150. else
  151. {
  152. dr.Msg = "Invalid data found. Please check data at row: " + i.ToString();
  153. dr.ErrorCode = "1";
  154. Dr = dr;
  155. return "";
  156. }
  157. }
  158. }
  159. sb.Append("</root>");
  160. if (errorRecordNumber != "")
  161. {
  162. dr.Msg = "Error records found at row(s) : " + errorRecordNumber;
  163. dr.ErrorCode = "101";
  164. }
  165. else
  166. {
  167. dr.Msg = "xml generated successfully.";
  168. dr.ErrorCode = "0";
  169. }
  170. InvalidRecords = invalidRecords;
  171. Dr = dr;
  172. return sb.ToString();
  173. }
  174. protected string ReadFileContent(string fileName)
  175. {
  176. return ReadFileContent(fileName, false);
  177. }
  178. protected string ReadFileContent(string fileName, bool ignoreFirstLine)
  179. {
  180. var streamReader = new StreamReader(fileName);
  181. if (streamReader.EndOfStream)
  182. return "";
  183. if (ignoreFirstLine)
  184. {
  185. streamReader.ReadLine();
  186. if (streamReader.EndOfStream)
  187. return "";
  188. }
  189. var contents = streamReader.ReadToEnd();
  190. contents = contents.TrimEnd('\r', '\n');
  191. streamReader.Close();
  192. streamReader.Dispose();
  193. return contents;
  194. }
  195. }
  196. public class FieldName
  197. {
  198. public int Index { get; set; }
  199. public string Name { get; set; }
  200. public FieldName(int index, string name)
  201. {
  202. Index = index;
  203. Name = name;
  204. }
  205. }
  206. }