@@ -10,6 +10,7 @@ using System.Data.SqlClient;
using System.Reflection;
using jdis_import.RESTObjects;
using System.Linq;
using System.Net;
/*
Secret Value up-8Q~y46~JyQZjJlsAJ-zpXpglpmuPIJ1Gx3a2O
@@ -23,6 +24,8 @@ namespace jdis_import
{
class Program
{
private static JSONSetting _settings;
private static string _os, _logDir, _logFile, _successDir, _failureDir, _downloadDir;
private static bool _runAPI = true;
private static RESTService _rservice;
private static List<User> _users;
@@ -192,9 +195,10 @@ namespace jdis_import
workorder.UpdateDate = DateTime.Now;
workorder.UpdateUserID = -1;
string woJson = Serialize.ToJson<WorkOrder>(workorder);
Console.WriteLine("WorkOrder Object");
Console.WriteLine();
Console.WriteLine(woJson);
//Console.WriteLine("WorkOrder Object");
//Console.WriteLine();
//Console.WriteLine(woJson);
JDISLog.WriteToLog(_logFile, string.Format("Creating Work Order: {0}", workorder.Number));
workorder = _rservice.PostRest<WorkOrder>(woJson, "workorders.svc");
if (template == null || template.ID == 0)
{
@@ -203,6 +207,7 @@ namespace jdis_import
}
else
{
JDISLog.WriteToLog(_logFile, String.Format("Creating Inspections for Workorder {0}", workorder.Number));
Dictionary<string, string> tps = new Dictionary<string, string>();
tps.Add("create", string.Format("{0}/{1}/{2}", template.ID, workorder.ID, -1));
Inspection i = _rservice.RestGet<Inspection>(true, "inspections.svc", tps);
@@ -238,6 +243,7 @@ namespace jdis_import
private static void ListsAndDefaults()
{
_rservice = new RESTService();
_rservice.BaseUrl = _settings.Prelub.ActiveService;
_customers = _rservice.RestGet<Customer>("customers.svc");
_makes = _rservice.RestGet<Make>("makes.svc");
_products = _rservice.RestGet<Product>("products.svc");
@@ -260,13 +266,36 @@ namespace jdis_import
_reason.UpdateDate = new DateTime(2013, 10, 1, 9, 23, 7);
_reason.UpdateUserID = -1;
}
static async System.Threading.Tasks.Task IsLocalNetwork()
{
var client = new System.Net.Http.HttpClient();
try
{
var result = await client.GetAsync(_settings.Prelub.Check);
_settings.Prelub.ActiveService = _settings.Prelub.LocalService;
}
catch
{
_settings.Prelub.ActiveService = _settings.Prelub.RemoteService;
}
}
static async System.Threading.Tasks.Task Main(string[] args)
{
_settings = JSONMethods.GetSettings();
await IsLocalNetwork();
if (IsWindows()) _os = "Windows";
else if (IsMacOS()) _os = "Mac";
else if (IsLinux()) _os = "Linux";
SetupDirectories();
JDISLog.Begin(_logFile);
ListsAndDefaults();
Console.WriteLine("Web APIs have loaded.");
//Console.WriteLine("Web APIs have loaded.");
JDISLog.WriteToLog(_logFile, string.Format("Default data loaded..... {0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
_exeDir = Environment.CurrentDirectory;
SetupDirectories();
List<ToDo> list = new List<ToDo>();
var cca = ConfidentialClientApplicationBuilder
.Create(_keys["AppID"])
@@ -277,7 +306,8 @@ namespace jdis_import
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
try
{
Console.WriteLine("Connecting To Exchange.");
//Console.WriteLine("Connecting To Exchange.");
JDISLog.WriteToLog(_logFile, string.Format("Connectiong to Exchange Server....{0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
var authResult = await cca.AcquireTokenForClient(ewsScopes)
.ExecuteAsync();
@@ -299,8 +329,17 @@ namespace jdis_import
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
view.Traversal = ItemTraversal.Shallow;
Console.WriteLine("Line 90: Finding emails.");
//Console.WriteLine("Line 90: Finding emails.");
FindItemsResults<Item> findResults = ewsClient.FindItems(fid, searchFilter, view);
List<EmailMessage> messages = new List<EmailMessage>();
foreach (EmailMessage message in findResults.Items)
{
message.Load();
if (!message.IsRead)
messages.Add(message);
}
JDISLog.WriteToLog(_logFile, string.Format("Found {0} emails", messages.Count));
foreach (EmailMessage msg in findResults.Items)
{
msg.Load();
@@ -315,12 +354,13 @@ namespace jdis_import
{
ToDo todo = new ToDo();
todo.Sender = msg.Sender.Address;
todo.SenderName = msg.Sender.Name;
todo.FileName = attachment.Name;
list.Add(todo);
//Download File
FileAttachment fAttachment = attachment as FileAttachment;
string fileAttachmentPath = Path.Combine(_exe Dir, fAttachment.Name);
string fileAttachmentPath = Path.Combine(_download Dir, fAttachment.Name);
File.WriteAllBytes(fileAttachmentPath, fAttachment.Content);
msg.IsRead = true;
msg.Update(ConflictResolutionMode.AlwaysOverwrite);
@@ -329,7 +369,8 @@ namespace jdis_import
}
}
}
Console.WriteLine("Beginning Import.");
//Console.WriteLine("Beginning Import.");
JDISLog.WriteToLog(_logFile, string.Format("Importing JDIS files....{0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongDateString()));
foreach (ToDo import in list)
{
@@ -340,6 +381,7 @@ namespace jdis_import
foreach (ToDo sendMsg in list)
{
EmailMessage msgToSend = new EmailMessage(ewsClient);
JDISLog.WriteToLog(_logFile, string.Format("Sending Message to: {0}\r\nFor Work Order: {1}\r\nJDIS File: {2}", sendMsg.Sender, sendMsg.WorkOrderNumber, sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1).Trim()));
string body = "JDIS File: {0} Work Order:{1} {2}";
if (sendMsg.Success)
{
@@ -348,7 +390,7 @@ namespace jdis_import
else
{
body = string.Format(body, sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1).Trim(), sendMsg.WorkOrderNumber, string.Format("Had Errors while importing: {0}", sendMsg.Validation));
msgToSend.Attachments.AddFileAttachment(Path.Combine(_exe Dir, sendMsg.FileName));
msgToSend.Attachments.AddFileAttachment(Path.Combine(_download Dir, sendMsg.FileName));
}
string fileNameOnly = sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1);
@@ -357,18 +399,20 @@ namespace jdis_import
msgToSend.CcRecipients.Add(new EmailAddress("mcarman@rpmindustries.com"));
msgToSend.Body = body;
msgToSend.SendAndSaveCopy();
string pathToFile = Path.Combine(_exe Dir, sendMsg.FileName);
if (sendMsg.Success) File.Move(pathToFile, Path.Combine(_exeDir, "Success" , fileNameOnly));
else File.Move(sendMsg.FileName, Path.Combine(_exeDir, "Fail" , fileNameOnly));
string pathToFile = Path.Combine(_download Dir, sendMsg.FileName);
if (sendMsg.Success) File.Move(pathToFile, Path.Combine(_successDir , fileNameOnly));
else File.Move(sendMsg.FileName, Path.Combine(_failureDir , fileNameOnly));
}
}
catch (MsalException ex)
{
Console.WriteLine($"Error acquiring access token: {ex}");
//Console.WriteLine($"Error acquiring access token: {ex}");
JDISLog.WriteToLog(_logFile, $"Error acquiring access token: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
//Console.WriteLine($"Error: {ex}");
JDISLog.WriteToLog(_logFile, $"Error: {ex}");
}
if (System.Diagnostics.Debugger.IsAttached)
@@ -387,15 +431,48 @@ namespace jdis_import
}
private static void SetupDirectories()
{
string path = Path.Combine(_exeDir, "Success");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(_exeDir, "Fail");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(_exeDir, "Log");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
OS os = new OS();
switch (_os)
{
case "Windows":
os = _settings.OSs.Find(x => x.Name == "WINDOWS");
_logDir = os.Log.Replace("/", "\\");
_logFile = Path.Combine(os.Log.Replace("/", "\\"), "jdis.log");
_successDir = os.Success.Replace("/", "\\");
_failureDir = os.Fail.Replace("/", "\\");
_downloadDir = os.Temp.Replace("/", "\\");
break;
case "Mac":
os = _settings.OSs.Find(x => x.Name == "OSX");
_logDir = os.Log;
_logFile = Path.Combine(os.Log, "jdis.log");
_successDir = os.Success;
_failureDir = os.Fail;
_downloadDir = os.Temp;
break;
case "Linux":
os = _settings.OSs.Find(x => x.Name == "LINUX");
_logDir = os.Log;
_logFile = Path.Combine(os.Log, "jdis.log");
_successDir = os.Success;
_failureDir = os.Fail;
_downloadDir = os.Temp;
break;
default:
break;
}
if (!Directory.Exists(_logDir)) Directory.CreateDirectory(_logDir);
if (!Directory.Exists(_failureDir)) Directory.CreateDirectory(_failureDir);
if (!Directory.Exists(_successDir)) Directory.CreateDirectory(_successDir);
//string path = Path.Combine(_exeDir, "Success");
//if (!Directory.Exists(path))
// Directory.CreateDirectory(path);
//path = Path.Combine(_exeDir, "Fail");
//if (!Directory.Exists(path))
// Directory.CreateDirectory(path);
//path = Path.Combine(_exeDir, "Log");
//if (!Directory.Exists(path))
// Directory.CreateDirectory(path);
}
public static void GetInspectionTypes()
{
@@ -407,27 +484,6 @@ namespace jdis_import
InspectionType.Add(t.Name, t.ID);
}
/*
SqlConnection con = new SqlConnection("Data Source=192.168.0.7;Initial Catalog=Connexion_Brandt;User ID=mobilepmuser;Password=data4techs;Encrypt=No;TrustServerCertificate=true;Persist Security Info=True;");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandTimeout = 0;
cmd.Connection = con;
cmd.CommandText = "Select ID, [Name] From InspectionType";
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
InspectionType.Add(row["Name"].ToString(), Convert.ToInt32(row["ID"]));
}
ds.Dispose();
cmd.Dispose();
con.Close();
con.Dispose();
*/
}
private static string frmtString
{
@@ -696,15 +752,15 @@ namespace jdis_import
}
private static void ReadFile(ToDo todo)
{
string file = Path.Combine(_exe Dir, todo.FileName);// todo.FileName;
string file = Path.Combine(_download Dir, todo.FileName);// todo.FileName;
todo.FileName = file;
CustomerImport c = new CustomerImport();
WorkOrderImport wo = new WorkOrderImport();
EquipmentImport eq = new EquipmentImport();
List<PartImport> parts = new List<PartImport>();
Console.WriteLine("Line 306: " + file);
Console.WriteLine("Line 307: " + file);
// Console.WriteLine("Line 306: " + file);
// Console.WriteLine("Line 307: " + file);
string filemove = string.Empty;
string filetxt = string.Empty;
string inspectionLevel = string.Empty;
@@ -1124,6 +1180,14 @@ namespace jdis_import
}
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 class ToDo