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.

396 lines
15 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. using System.IO;
  5. namespace importer
  6. {
  7. public class Global
  8. {
  9. private static Settings _settings;
  10. private static string _storageDir;
  11. public static string StorageDir { get { return _storageDir; } set { _storageDir = value; } }
  12. public static Settings Settings { get { return _settings; } set { _settings = value; } }
  13. public static string ServiceUrl
  14. {
  15. get
  16. {
  17. if (Program.Args[0] == "brandt")
  18. return JDISImport.ServiceUrl;
  19. else return string.Empty;
  20. }
  21. }
  22. public Global() { }
  23. public Global(string[] settingsFile)
  24. {
  25. if (IsWindows())
  26. {
  27. Global.StorageDir = string.Format("C:/{0}/store/", settingsFile[0]).Replace("/", "\\");
  28. }
  29. else if (IsMacOS())
  30. {
  31. Global.StorageDir = string.Format("/Users/{0}/{1}/store/", settingsFile[1], settingsFile[0]);
  32. }
  33. else if (IsLinux())
  34. {
  35. Global.StorageDir = string.Format("/home/{0}/{1}/store/", settingsFile[1], settingsFile[0]);
  36. }
  37. else
  38. {
  39. throw new Exception("OS cannot be detected.");
  40. }
  41. Global.Settings = new Settings();
  42. string path = Path.Combine(Global.StorageDir, string.Format("{0}.json", settingsFile[0]));
  43. string text = File.ReadAllText(path);
  44. Global.Settings = JsonConvert.DeserializeObject<Settings>(text);
  45. }
  46. public static bool IsWindows() =>
  47. System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
  48. public static bool IsMacOS() =>
  49. System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
  50. public static bool IsLinux() =>
  51. System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
  52. public static DateTime DateFormatter(string d, bool hastime = true)
  53. {
  54. string month, day, year, time;
  55. DateTime dt = new DateTime();
  56. if (d.IndexOf("JAN") != -1)
  57. {
  58. month = "01";
  59. d = d.Replace("JAN", "|");
  60. day = d.Substring(0, d.IndexOf("|"));
  61. year = d.Substring(d.IndexOf("|") + 1, 4);
  62. if (hastime)
  63. time = d.Substring(d.IndexOf(":") - 3 + 2);
  64. else time = "00:00:00.000";
  65. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  66. dt = DateTime.Parse(date);
  67. }
  68. else if (d.IndexOf("FEB") != -1)
  69. {
  70. month = "02";
  71. d = d.Replace("FEB", "|");
  72. day = d.Substring(0, d.IndexOf("|"));
  73. year = d.Substring(d.IndexOf("|") + 1, 4);
  74. if (hastime)
  75. time = d.Substring(d.IndexOf(":") - 3 + 2);
  76. else time = "00:00:00.000";
  77. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  78. dt = DateTime.Parse(date);
  79. }
  80. else if (d.IndexOf("MARCH") != -1)
  81. {
  82. month = "03";
  83. d = d.Replace("MARCH", "|");
  84. day = d.Substring(0, d.IndexOf("|"));
  85. year = d.Substring(d.IndexOf("|") + 1, 4);
  86. if (hastime)
  87. time = d.Substring(d.IndexOf(":") - 3 + 2);
  88. else time = "00:00:00.000";
  89. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  90. dt = DateTime.Parse(date);
  91. }
  92. else if (d.IndexOf("MAR") != -1)
  93. {
  94. month = "03";
  95. d = d.Replace("MAR", "|");
  96. day = d.Substring(0, d.IndexOf("|"));
  97. year = d.Substring(d.IndexOf("|") + 1, 4);
  98. if (hastime)
  99. time = d.Substring(d.IndexOf(":") - 3 + 2);
  100. else time = "00:00:00.000";
  101. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  102. dt = DateTime.Parse(date);
  103. }
  104. else if (d.IndexOf("APRIL") != -1)
  105. {
  106. month = "04";
  107. d = d.Replace("APRIL", "|");
  108. day = d.Substring(0, d.IndexOf("|"));
  109. year = d.Substring(d.IndexOf("|") + 1, 4);
  110. if (hastime)
  111. time = d.Substring(d.IndexOf(":") - 3 + 2);
  112. else time = "00:00:00.000";
  113. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  114. dt = DateTime.Parse(date);
  115. }
  116. else if (d.IndexOf("APR") != -1)
  117. {
  118. month = "04";
  119. d = d.Replace("APR", "|");
  120. day = d.Substring(0, d.IndexOf("|"));
  121. year = d.Substring(d.IndexOf("|") + 1, 4);
  122. if (hastime)
  123. time = d.Substring(d.IndexOf(":") - 3 + 2);
  124. else time = "00:00:00.000";
  125. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  126. dt = DateTime.Parse(date);
  127. }
  128. else if (d.IndexOf("MAY") != -1)
  129. {
  130. month = "05";
  131. d = d.Replace("MAY", "|");
  132. day = d.Substring(0, d.IndexOf("|"));
  133. year = d.Substring(d.IndexOf("|") + 1, 4);
  134. if (hastime)
  135. time = d.Substring(d.IndexOf(":") - 3 + 2);
  136. else time = "00:00:00.000";
  137. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  138. dt = DateTime.Parse(date);
  139. }
  140. else if (d.IndexOf("JUNE") != -1)
  141. {
  142. month = "06";
  143. d = d.Replace("JUNE", "|");
  144. day = d.Substring(0, d.IndexOf("|"));
  145. year = d.Substring(d.IndexOf("|") + 1, 4);
  146. if (hastime)
  147. time = d.Substring(d.IndexOf(":") - 3 + 2);
  148. else time = "00:00:00.000";
  149. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  150. dt = DateTime.Parse(date);
  151. }
  152. else if (d.IndexOf("JUN") != -1)
  153. {
  154. month = "06";
  155. d = d.Replace("JUN", "|");
  156. day = d.Substring(0, d.IndexOf("|"));
  157. year = d.Substring(d.IndexOf("|") + 1, 4);
  158. if (hastime)
  159. time = d.Substring(d.IndexOf(":") - 3 + 2);
  160. else time = "00:00:00.000";
  161. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  162. dt = DateTime.Parse(date);
  163. }
  164. else if (d.IndexOf("JULY") != -1)
  165. {
  166. month = "07";
  167. d = d.Replace("JULY", "|");
  168. day = d.Substring(0, d.IndexOf("|"));
  169. year = d.Substring(d.IndexOf("|") + 1, 4);
  170. if (hastime)
  171. time = d.Substring(d.IndexOf(":") - 3 + 2);
  172. else time = "00:00:00.000";
  173. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  174. dt = DateTime.Parse(date);
  175. }
  176. else if (d.IndexOf("JUL") != -1)
  177. {
  178. month = "07";
  179. d = d.Replace("JUL", "|");
  180. day = d.Substring(0, d.IndexOf("|"));
  181. year = d.Substring(d.IndexOf("|") + 1, 4);
  182. if (hastime)
  183. time = d.Substring(d.IndexOf(":") - 3 + 2);
  184. else time = "00:00:00.000";
  185. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  186. dt = DateTime.Parse(date);
  187. }
  188. else if (d.IndexOf("AUG") != -1)
  189. {
  190. month = "08";
  191. d = d.Replace("AUG", "|");
  192. day = d.Substring(0, d.IndexOf("|"));
  193. year = d.Substring(d.IndexOf("|") + 1, 4);
  194. if (hastime)
  195. time = d.Substring(d.IndexOf(":") - 3 + 2);
  196. else time = "00:00:00.000";
  197. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  198. dt = DateTime.Parse(date);
  199. }
  200. else if (d.IndexOf("SEPT") != -1)
  201. {
  202. month = "09";
  203. d = d.Replace("SEPT", "|");
  204. day = d.Substring(0, d.IndexOf("|"));
  205. year = d.Substring(d.IndexOf("|") + 1, 4);
  206. if (hastime)
  207. time = d.Substring(d.IndexOf(":") - 3 + 2);
  208. else time = "00:00:00.000";
  209. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  210. dt = DateTime.Parse(date);
  211. }
  212. else if (d.IndexOf("SEP") != -1)
  213. {
  214. month = "09";
  215. d = d.Replace("SEP", "|");
  216. day = d.Substring(0, d.IndexOf("|"));
  217. year = d.Substring(d.IndexOf("|") + 1, 4);
  218. if (hastime)
  219. time = d.Substring(d.IndexOf(":") - 3 + 2);
  220. else time = "00:00:00.000";
  221. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  222. dt = DateTime.Parse(date);
  223. }
  224. else if (d.IndexOf("OCT") != -1)
  225. {
  226. month = "10";
  227. d = d.Replace("OCT", "|");
  228. day = d.Substring(0, d.IndexOf("|"));
  229. year = d.Substring(d.IndexOf("|") + 1, 4);
  230. if (hastime)
  231. time = d.Substring(d.IndexOf(":") - 3 + 2);
  232. else time = "00:00:00.000";
  233. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  234. dt = DateTime.Parse(date);
  235. }
  236. else if (d.IndexOf("0CT") != -1)
  237. {
  238. month = "10";
  239. d = d.Replace("0CT", "|");
  240. day = d.Substring(0, d.IndexOf("|"));
  241. year = d.Substring(d.IndexOf("|") + 1, 4);
  242. if (hastime)
  243. time = d.Substring(d.IndexOf(":") - 3 + 2);
  244. else time = "00:00:00.000";
  245. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  246. dt = DateTime.Parse(date);
  247. }
  248. else if (d.IndexOf("NOV") != -1)
  249. {
  250. month = "11";
  251. d = d.Replace("NOV", "|");
  252. day = d.Substring(0, d.IndexOf("|"));
  253. year = d.Substring(d.IndexOf("|") + 1, 4);
  254. if (hastime)
  255. time = d.Substring(d.IndexOf(":") - 3 + 2);
  256. else time = "00:00:00.000";
  257. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  258. dt = DateTime.Parse(date);
  259. }
  260. else
  261. {
  262. month = "12";
  263. d = d.Replace("DEC", "|");
  264. day = d.Substring(0, d.IndexOf("|"));
  265. year = d.Substring(d.IndexOf("|") + 1, 4);
  266. if (hastime)
  267. time = d.Substring(d.IndexOf(":") - 3 + 2);
  268. else time = "00:00:00.000";
  269. string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
  270. dt = DateTime.Parse(date);
  271. }
  272. return dt;
  273. }
  274. }
  275. public static class Deserialize
  276. {
  277. public static T FromJson<T>(string json)
  278. {
  279. //Console.WriteLine(json);
  280. return JsonConvert.DeserializeObject<T>(json, Converter.Settings);
  281. }
  282. }
  283. public static class Serialize
  284. {
  285. public static string ToJson<T>(this T self)
  286. {
  287. return JsonConvert.SerializeObject(self, Converter.Settings);
  288. }
  289. }
  290. internal static class Converter
  291. {
  292. public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
  293. {
  294. MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
  295. DateParseHandling = DateParseHandling.None,
  296. DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
  297. };
  298. }
  299. [JsonObject(MemberSerialization.OptIn)]
  300. public class Settings
  301. {
  302. [JsonProperty(PropertyName = "Remote")]
  303. public bool Remote { get; set; }
  304. [JsonProperty(PropertyName = "LogFile")]
  305. public string LogFile { get; set; }
  306. [JsonProperty(PropertyName = "MailProperties")]
  307. public MailProperties MailProperties { get; set; }
  308. [JsonProperty(PropertyName = "Prelub")]
  309. public Prelub Prelub { get; set; }
  310. [JsonProperty(PropertyName = "Customer")]
  311. public string Customer { get; set; }
  312. }
  313. [JsonObject(MemberSerialization.OptIn)]
  314. public class MailProperties
  315. {
  316. [JsonProperty(PropertyName = "AppID")]
  317. public string AppID { get; set; }
  318. [JsonProperty(PropertyName = "TenentID")]
  319. public string TenentID { get; set; }
  320. [JsonProperty(PropertyName = "ObjectId")]
  321. public string ObjectId { get; set; }
  322. [JsonProperty(PropertyName = "SecretID")]
  323. public string SecretID { get; set; }
  324. [JsonProperty(PropertyName = "SecretV")]
  325. public string SecretV { get; set; }
  326. [JsonProperty(PropertyName = "Mailbox")]
  327. public string Mailbox { get; set; }
  328. [JsonProperty(PropertyName = "Url")]
  329. public string Url { get; set; }
  330. [JsonProperty(PropertyName = "Password")]
  331. public string Password { get; set; }
  332. [JsonProperty(PropertyName = "Username")]
  333. public string Username { get; set; }
  334. [JsonProperty(PropertyName = "Scope")]
  335. public string Scope { get; set; }
  336. [JsonProperty(PropertyName = "Filter")]
  337. public string Filter { get; set; }
  338. }
  339. [JsonObject(MemberSerialization.OptIn)]
  340. public class Prelub
  341. {
  342. [JsonProperty(PropertyName = "LocalService")]
  343. public string LocalService { get; set; }
  344. [JsonProperty(PropertyName = "RemoteService")]
  345. public string RemoteService { get; set; }
  346. }
  347. }
  348. /*
  349. {
  350. "MailProperties": {
  351. "Username": "mcarman@prelub.com",
  352. "Password": "Gr@nt@cc3$$",
  353. "Mailbox": 4,
  354. "Url": "https://outlook.office365.com/ews/Exchange.asmx",
  355. "Filter": "Brandt Import, brandt import, Brandt import, brandt Import, BRANDT IMPORT",
  356. "AppID": "489776b1-ee79-4b14-bc44-9f6bf47332db",
  357. "TenentID": "1fd06c96-d3a4-45e9-9ed7-bcecb394d277",
  358. "ObjectId": "c7647074-edb6-4e8e-8a01-a1ed924fdae9",
  359. "SecretID": "cba04a6c-a233-4646-909b-b50921edbe1c",
  360. "SecretV": "up-8Q~y46~JyQZjJlsAJ-zpXpglpmuPIJ1Gx3a2O"
  361. },
  362. "Remote": false,
  363. "LogFile": "jdis.log",
  364. "Prelub": {
  365. "LocalService": "http://servicesbrandt.rpmindustries.com:8081",
  366. "RemoteService": "https://servicesbrandt.rpmindustries.com"
  367. }
  368. }
  369. */