|
- using System;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using System.IO;
-
- namespace importer
- {
- public class Global
- {
- private static Settings _settings;
- private static string _storageDir;
- public static string StorageDir { get { return _storageDir; } set { _storageDir = value; } }
- public static Settings Settings { get { return _settings; } set { _settings = value; } }
- public static string ServiceUrl
- {
- get
- {
- if (Program.Args[0] == "brandt")
- return JDISImport.ServiceUrl;
- else return string.Empty;
- }
- }
- public Global() { }
- public Global(string[] settingsFile)
- {
- if (IsWindows())
- {
- Global.StorageDir = string.Format("C:/{0}/store/", settingsFile[0]).Replace("/", "\\");
- }
- else if (IsMacOS())
- {
- Global.StorageDir = string.Format("/Users/{0}/{1}/store/", settingsFile[1], settingsFile[0]);
- }
- else if (IsLinux())
- {
- Global.StorageDir = string.Format("/home/{0}/{1}/store/", settingsFile[1], settingsFile[0]);
- }
- else
- {
- throw new Exception("OS cannot be detected.");
- }
- Global.Settings = new Settings();
- string path = Path.Combine(Global.StorageDir, string.Format("{0}.json", settingsFile[0]));
-
- string text = File.ReadAllText(path);
- Global.Settings = JsonConvert.DeserializeObject<Settings>(text);
-
- }
-
- public static bool IsWindows() =>
- System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
-
- public static bool IsMacOS() =>
- System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
-
- public static bool IsLinux() =>
- System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
- public static DateTime DateFormatter(string d, bool hastime = true)
- {
- string month, day, year, time;
-
- DateTime dt = new DateTime();
- if (d.IndexOf("JAN") != -1)
- {
- month = "01";
- d = d.Replace("JAN", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("FEB") != -1)
- {
- month = "02";
- d = d.Replace("FEB", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("MARCH") != -1)
- {
- month = "03";
- d = d.Replace("MARCH", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("MAR") != -1)
- {
- month = "03";
- d = d.Replace("MAR", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("APRIL") != -1)
- {
- month = "04";
- d = d.Replace("APRIL", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("APR") != -1)
- {
- month = "04";
- d = d.Replace("APR", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
-
- else if (d.IndexOf("MAY") != -1)
- {
- month = "05";
- d = d.Replace("MAY", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("JUNE") != -1)
- {
- month = "06";
- d = d.Replace("JUNE", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("JUN") != -1)
- {
- month = "06";
- d = d.Replace("JUN", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("JULY") != -1)
- {
- month = "07";
- d = d.Replace("JULY", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("JUL") != -1)
- {
- month = "07";
- d = d.Replace("JUL", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("AUG") != -1)
- {
- month = "08";
- d = d.Replace("AUG", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("SEPT") != -1)
- {
- month = "09";
- d = d.Replace("SEPT", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("SEP") != -1)
- {
- month = "09";
- d = d.Replace("SEP", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("OCT") != -1)
- {
- month = "10";
- d = d.Replace("OCT", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("0CT") != -1)
- {
- month = "10";
- d = d.Replace("0CT", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else if (d.IndexOf("NOV") != -1)
- {
- month = "11";
- d = d.Replace("NOV", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- else
- {
- month = "12";
- d = d.Replace("DEC", "|");
- day = d.Substring(0, d.IndexOf("|"));
- year = d.Substring(d.IndexOf("|") + 1, 4);
- if (hastime)
- time = d.Substring(d.IndexOf(":") - 3 + 2);
- else time = "00:00:00.000";
-
- string date = string.Format("{0}-{1}-{2} {3}", year, month, day, time);
- dt = DateTime.Parse(date);
- }
- return dt;
- }
- }
- public static class Deserialize
- {
- public static T FromJson<T>(string json)
- {
- //Console.WriteLine(json);
- return JsonConvert.DeserializeObject<T>(json, Converter.Settings);
-
- }
- }
- public static class Serialize
- {
- public static string ToJson<T>(this T self)
- {
- return JsonConvert.SerializeObject(self, Converter.Settings);
- }
- }
- internal static class Converter
- {
- public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
- {
- MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
- DateParseHandling = DateParseHandling.None,
- DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
- };
- }
- [JsonObject(MemberSerialization.OptIn)]
- public class Settings
- {
-
- [JsonProperty(PropertyName = "Remote")]
- public bool Remote { get; set; }
- [JsonProperty(PropertyName = "LogFile")]
- public string LogFile { get; set; }
- [JsonProperty(PropertyName = "MailProperties")]
- public MailProperties MailProperties { get; set; }
- [JsonProperty(PropertyName = "Prelub")]
- public Prelub Prelub { get; set; }
- [JsonProperty(PropertyName = "Customer")]
- public string Customer { get; set; }
- }
- [JsonObject(MemberSerialization.OptIn)]
- public class MailProperties
- {
- [JsonProperty(PropertyName = "AppID")]
- public string AppID { get; set; }
- [JsonProperty(PropertyName = "TenentID")]
- public string TenentID { get; set; }
- [JsonProperty(PropertyName = "ObjectId")]
- public string ObjectId { get; set; }
- [JsonProperty(PropertyName = "SecretID")]
- public string SecretID { get; set; }
- [JsonProperty(PropertyName = "SecretV")]
- public string SecretV { get; set; }
- [JsonProperty(PropertyName = "Mailbox")]
- public string Mailbox { get; set; }
- [JsonProperty(PropertyName = "Url")]
- public string Url { get; set; }
- [JsonProperty(PropertyName = "Password")]
- public string Password { get; set; }
- [JsonProperty(PropertyName = "Username")]
- public string Username { get; set; }
- [JsonProperty(PropertyName = "Scope")]
- public string Scope { get; set; }
- [JsonProperty(PropertyName = "Filter")]
- public string Filter { get; set; }
- }
- [JsonObject(MemberSerialization.OptIn)]
- public class Prelub
- {
- [JsonProperty(PropertyName = "LocalService")]
- public string LocalService { get; set; }
- [JsonProperty(PropertyName = "RemoteService")]
- public string RemoteService { get; set; }
- }
- }
- /*
- {
- "MailProperties": {
- "Username": "mcarman@prelub.com",
- "Password": "Gr@nt@cc3$$",
- "Mailbox": 4,
- "Url": "https://outlook.office365.com/ews/Exchange.asmx",
- "Filter": "Brandt Import, brandt import, Brandt import, brandt Import, BRANDT IMPORT",
- "AppID": "489776b1-ee79-4b14-bc44-9f6bf47332db",
- "TenentID": "1fd06c96-d3a4-45e9-9ed7-bcecb394d277",
- "ObjectId": "c7647074-edb6-4e8e-8a01-a1ed924fdae9",
- "SecretID": "cba04a6c-a233-4646-909b-b50921edbe1c",
- "SecretV": "up-8Q~y46~JyQZjJlsAJ-zpXpglpmuPIJ1Gx3a2O"
- },
- "Remote": false,
- "LogFile": "jdis.log",
- "Prelub": {
- "LocalService": "http://servicesbrandt.rpmindustries.com:8081",
- "RemoteService": "https://servicesbrandt.rpmindustries.com"
- }
- }
- */
|