|
|
|
@@ -1,6 +1,7 @@ |
|
|
|
using System.Collections.Generic;
|
|
|
|
using System;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Identity.Client;
|
|
|
|
using Microsoft.Exchange.WebServices.Data;
|
|
|
|
@@ -22,6 +23,10 @@ namespace jdis_import |
|
|
|
{
|
|
|
|
class Program
|
|
|
|
{
|
|
|
|
private static string _success;
|
|
|
|
private static string _fail;
|
|
|
|
|
|
|
|
private static string _logdir;
|
|
|
|
private static List<Customer> _customers;
|
|
|
|
private static List<Make> _makes;
|
|
|
|
private static List<Product> _products;
|
|
|
|
@@ -43,137 +48,163 @@ namespace jdis_import |
|
|
|
{ "TenentID", "1fd06c96-d3a4-45e9-9ed7-bcecb394d277" },
|
|
|
|
{ "AppID", "489776b1-ee79-4b14-bc44-9f6bf47332db" } };
|
|
|
|
public static Dictionary<string, int> InspectionType;
|
|
|
|
static async System.Threading.Tasks.Task Main(string[] args)
|
|
|
|
|
|
|
|
static int _minute = DateTime.Now.Minute;
|
|
|
|
static int _second = DateTime.Now.Second;
|
|
|
|
|
|
|
|
static public async void Tick(Object stateInfo)
|
|
|
|
{
|
|
|
|
RESTService rservice = new RESTService();
|
|
|
|
_customers = rservice.RestGet<Customer>("customers.svc");
|
|
|
|
_makes = rservice.RestGet<Make>("makes.svc");
|
|
|
|
_products = rservice.RestGet<Product>("products.svc");
|
|
|
|
_models = rservice.RestGet<Model>("models.svc");
|
|
|
|
_inspectionTypes = rservice.RestGet<InspectionType>("inspectionTypes.svc");
|
|
|
|
_templates = rservice.RestGet<Template>("templates.svc");
|
|
|
|
|
|
|
|
Console.WriteLine("Web APIs have loaded.");
|
|
|
|
_exeDir = Environment.CurrentDirectory;
|
|
|
|
SetupDirectories();
|
|
|
|
List<ToDo> list = new List<ToDo>();
|
|
|
|
var cca = ConfidentialClientApplicationBuilder
|
|
|
|
// .Create("1dff6bdb-7009-4d2a-ae0f-9f333a4f182b")
|
|
|
|
// .WithClientSecret("VOD8Q~7BI9u4ySU0saesCG87TaEtKSCya5vw6as5")
|
|
|
|
// .WithTenantId("1fd06c96-d3a4-45e9-9ed7-bcecb394d277")
|
|
|
|
.Create(_keys["AppID"])
|
|
|
|
.WithClientSecret(_keys["SecretV"])
|
|
|
|
.WithTenantId(_keys["TenentID"])
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
|
|
|
|
try
|
|
|
|
_minute = DateTime.Now.Minute;
|
|
|
|
_second = DateTime.Now.Second;
|
|
|
|
int modmin10 = _minute % 10;
|
|
|
|
if(_second == 0 && modmin10 == 0)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Connecting To Exchange.");
|
|
|
|
var authResult = await cca.AcquireTokenForClient(ewsScopes)
|
|
|
|
.ExecuteAsync();
|
|
|
|
|
|
|
|
// Configure the ExchangeService with the access token
|
|
|
|
var ewsClient = new ExchangeService();
|
|
|
|
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
|
|
|
|
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
|
|
|
|
ewsClient.ImpersonatedUserId =
|
|
|
|
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "cmobile@prelub.com");
|
|
|
|
Mailbox mb = new Mailbox("connexionmobile@rpmindustries.org");
|
|
|
|
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
|
|
|
|
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
|
|
|
|
string[] filters = "Brandt Import;brandt import;Brandt import;brandt Import".Split(';');//"Brandt Import;BRANDT IMPORT;brandt import;TEST Brandt".Split(';');
|
|
|
|
foreach (string s in filters)
|
|
|
|
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, s));
|
|
|
|
|
|
|
|
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
|
|
|
|
ItemView view = new ItemView(100);
|
|
|
|
|
|
|
|
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.");
|
|
|
|
FindItemsResults<Item> findResults = ewsClient.FindItems(fid, searchFilter, view);
|
|
|
|
foreach (EmailMessage msg in findResults.Items)
|
|
|
|
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss"));
|
|
|
|
|
|
|
|
string path = Path.Combine(Environment.CurrentDirectory, "Settings.json");
|
|
|
|
JSetting settings = await JsonFileReader.ReadAsync<JSetting>(path);
|
|
|
|
RESTService rservice = new RESTService();
|
|
|
|
_customers = rservice.RestGet<Customer>("customers.svc");
|
|
|
|
_makes = rservice.RestGet<Make>("makes.svc");
|
|
|
|
_products = rservice.RestGet<Product>("products.svc");
|
|
|
|
_models = rservice.RestGet<Model>("models.svc");
|
|
|
|
_inspectionTypes = rservice.RestGet<InspectionType>("inspectionTypes.svc");
|
|
|
|
_templates = rservice.RestGet<Template>("templates.svc");
|
|
|
|
|
|
|
|
Console.WriteLine("Web APIs have loaded.");
|
|
|
|
_exeDir = Environment.CurrentDirectory;
|
|
|
|
SetupDirectories();
|
|
|
|
List<ToDo> list = new List<ToDo>();
|
|
|
|
var cca = ConfidentialClientApplicationBuilder
|
|
|
|
// .Create("1dff6bdb-7009-4d2a-ae0f-9f333a4f182b")
|
|
|
|
// .WithClientSecret("VOD8Q~7BI9u4ySU0saesCG87TaEtKSCya5vw6as5")
|
|
|
|
// .WithTenantId("1fd06c96-d3a4-45e9-9ed7-bcecb394d277")
|
|
|
|
.Create(settings.appid)
|
|
|
|
.WithClientSecret(settings.secretv)
|
|
|
|
.WithTenantId(settings.tenentid)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
|
|
|
|
try
|
|
|
|
{
|
|
|
|
msg.Load();
|
|
|
|
if (!msg.IsRead)
|
|
|
|
Console.WriteLine("Connecting To Exchange.");
|
|
|
|
var authResult = await cca.AcquireTokenForClient(ewsScopes)
|
|
|
|
.ExecuteAsync();
|
|
|
|
|
|
|
|
// Configure the ExchangeService with the access token
|
|
|
|
var ewsClient = new ExchangeService();
|
|
|
|
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
|
|
|
|
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
|
|
|
|
ewsClient.ImpersonatedUserId =
|
|
|
|
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "cmobile@prelub.com");
|
|
|
|
Mailbox mb = new Mailbox("connexionmobile@rpmindustries.org");
|
|
|
|
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
|
|
|
|
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
|
|
|
|
string[] filters = "Brandt Import;brandt import;Brandt import;brandt Import;BRANDT IMPORT".Split(';');//"Brandt Import;BRANDT IMPORT;brandt import;TEST Brandt".Split(';');
|
|
|
|
foreach (string s in filters)
|
|
|
|
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, s));
|
|
|
|
|
|
|
|
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
|
|
|
|
ItemView view = new ItemView(100);
|
|
|
|
|
|
|
|
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.");
|
|
|
|
FindItemsResults<Item> findResults = ewsClient.FindItems(fid, searchFilter, view);
|
|
|
|
foreach (EmailMessage msg in findResults.Items)
|
|
|
|
{
|
|
|
|
if (msg.HasAttachments)
|
|
|
|
msg.Load();
|
|
|
|
if (!msg.IsRead)
|
|
|
|
{
|
|
|
|
foreach (Attachment attachment in msg.Attachments)
|
|
|
|
if (msg.HasAttachments)
|
|
|
|
{
|
|
|
|
attachment.Load();
|
|
|
|
if (attachment is FileAttachment)
|
|
|
|
foreach (Attachment attachment in msg.Attachments)
|
|
|
|
{
|
|
|
|
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(_exeDir, fAttachment.Name);
|
|
|
|
File.WriteAllBytes(fileAttachmentPath, fAttachment.Content);
|
|
|
|
msg.IsRead = true;
|
|
|
|
msg.Update(ConflictResolutionMode.AlwaysOverwrite);
|
|
|
|
attachment.Load();
|
|
|
|
if (attachment is FileAttachment)
|
|
|
|
{
|
|
|
|
ToDo todo = new ToDo();
|
|
|
|
todo.Sender = msg.Sender.Address;
|
|
|
|
todo.SenderName = msg.Sender.Name;
|
|
|
|
if(attachment.Name.Contains("/"))
|
|
|
|
todo.FileName = attachment.Name.Substring(attachment.Name.LastIndexOf("/")+1);
|
|
|
|
else
|
|
|
|
todo.FileName = attachment.Name;
|
|
|
|
list.Add(todo);
|
|
|
|
//Download File
|
|
|
|
FileAttachment fAttachment = attachment as FileAttachment;
|
|
|
|
string fileAttachmentPath = Path.Combine(_exeDir, fAttachment.Name);
|
|
|
|
File.WriteAllBytes(fileAttachmentPath, fAttachment.Content);
|
|
|
|
msg.IsRead = true;
|
|
|
|
msg.Update(ConflictResolutionMode.AlwaysOverwrite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Console.WriteLine("Beginning Import.");
|
|
|
|
foreach (ToDo import in list)
|
|
|
|
{
|
|
|
|
//System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
|
|
|
|
|
|
|
|
//Start Importing
|
|
|
|
BeginImport(import);
|
|
|
|
GetInspectionTypes();
|
|
|
|
ReadFile(import);
|
|
|
|
}
|
|
|
|
foreach (ToDo sendMsg in list)
|
|
|
|
{
|
|
|
|
EmailMessage msgToSend = new EmailMessage(ewsClient);
|
|
|
|
string body = "JDIS File: {0} Work Order:{1} {2}";
|
|
|
|
if (sendMsg.Success)
|
|
|
|
Console.WriteLine("Beginning Import.");
|
|
|
|
foreach (ToDo import in list)
|
|
|
|
{
|
|
|
|
body = string.Format(body, sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1).Trim(), sendMsg.WorkOrder, "Was Imported Successfully");
|
|
|
|
//System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
|
|
|
|
|
|
|
|
//Start Importing
|
|
|
|
BeginImport(import);
|
|
|
|
GetInspectionTypes();
|
|
|
|
ReadFile(import);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
foreach (ToDo sendMsg in list)
|
|
|
|
{
|
|
|
|
body = string.Format(body, sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1).Trim(), sendMsg.WorkOrder, string.Format("Had Errors while importing: {0}", sendMsg.Validation));
|
|
|
|
msgToSend.Attachments.AddFileAttachment(Path.Combine(_exeDir, sendMsg.FileName));
|
|
|
|
EmailMessage msgToSend = new EmailMessage(ewsClient);
|
|
|
|
string body = "JDIS File: {0} Work Order:{1} {2}";
|
|
|
|
if (sendMsg.Success)
|
|
|
|
{
|
|
|
|
body = string.Format(body, sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1).Trim(), sendMsg.WorkOrder, "Was Imported Successfully");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body = string.Format(body, sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1).Trim(), sendMsg.WorkOrder, string.Format("Had Errors while importing: {0}", sendMsg.Validation));
|
|
|
|
msgToSend.Attachments.AddFileAttachment(Path.Combine(_exeDir, sendMsg.FileName));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
msgToSend.Subject = sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1);
|
|
|
|
msgToSend.ToRecipients.Add(new EmailAddress(sendMsg.Sender));
|
|
|
|
msgToSend.CcRecipients.Add(new EmailAddress("mcarman@rpmindustries.com"));
|
|
|
|
msgToSend.Body = body;
|
|
|
|
msgToSend.Send();
|
|
|
|
//msgToSend.SendAndSaveCopy();
|
|
|
|
string pathToFile = Path.Combine(_exeDir, sendMsg.FileName);
|
|
|
|
if (sendMsg.Success) File.Move(pathToFile, Path.Combine(_exeDir, "Success", sendMsg.FileName));
|
|
|
|
else File.Move(sendMsg.FileName, Path.Combine(_exeDir, "Fail", sendMsg.FileName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (MsalException ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Error acquiring access token: {ex}");
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Error: {ex}");
|
|
|
|
}
|
|
|
|
|
|
|
|
msgToSend.Subject = sendMsg.FileName.Substring(sendMsg.FileName.LastIndexOf("/") + 1);
|
|
|
|
msgToSend.ToRecipients.Add(new EmailAddress(sendMsg.Sender));
|
|
|
|
msgToSend.CcRecipients.Add(new EmailAddress("mcarman@rpmindustries.com"));
|
|
|
|
msgToSend.Body = body;
|
|
|
|
msgToSend.SendAndSaveCopy();
|
|
|
|
string pathToFile = Path.Combine(_exeDir, sendMsg.FileName);
|
|
|
|
if (sendMsg.Success) File.Move(pathToFile, Path.Combine(_exeDir, "Success", sendMsg.FileName));
|
|
|
|
else File.Move(sendMsg.FileName, Path.Combine(_exeDir, "Fail", sendMsg.FileName));
|
|
|
|
if (System.Diagnostics.Debugger.IsAttached)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Hit any key to exit...");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (MsalException ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Error acquiring access token: {ex}");
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Error: {ex}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (System.Diagnostics.Debugger.IsAttached)
|
|
|
|
}
|
|
|
|
static async System.Threading.Tasks.Task Main(string[] args)
|
|
|
|
{
|
|
|
|
TimerCallback callback = new TimerCallback(Tick);
|
|
|
|
Timer timer = new Timer(callback, null, 0, 1000);
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Hit any key to exit...");
|
|
|
|
Console.ReadKey();
|
|
|
|
Thread.Sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void BeginImport(ToDo todo)
|
|
|
|
@@ -182,15 +213,15 @@ 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);
|
|
|
|
_success = Path.Combine(_exeDir, "Success");
|
|
|
|
_fail = Path.Combine(_exeDir, "Fail");
|
|
|
|
_logdir = Path.Combine(_exeDir, "Log");
|
|
|
|
if (!Directory.Exists(_success))
|
|
|
|
Directory.CreateDirectory(_success);
|
|
|
|
if (!Directory.Exists(_fail))
|
|
|
|
Directory.CreateDirectory(_fail);
|
|
|
|
if (!Directory.Exists(_logdir))
|
|
|
|
Directory.CreateDirectory(_logdir);
|
|
|
|
}
|
|
|
|
public static void GetInspectionTypes()
|
|
|
|
{
|
|
|
|
@@ -483,14 +514,15 @@ namespace jdis_import |
|
|
|
private static void ReadFile(ToDo todo)
|
|
|
|
{
|
|
|
|
string file = Path.Combine(_exeDir, 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 494: " + file);
|
|
|
|
Console.WriteLine("Line 495: " + file);
|
|
|
|
string filemove = string.Empty;
|
|
|
|
string filetxt = string.Empty;
|
|
|
|
string inspectionLevel = string.Empty;
|
|
|
|
@@ -731,7 +763,8 @@ namespace jdis_import |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filemove = Path.Combine("Success", file);// string.Format("{0}\\{1}\\{2}", _currentDir, _settings["SUCCESSDIR"], file);
|
|
|
|
filemove = Path.Combine(_exeDir, "Success");
|
|
|
|
filemove = Path.Combine(filemove, todo.FileName.Substring(todo.FileName.LastIndexOf("/")+1));// Path.Combine("Success", file);// string.Format("{0}\\{1}\\{2}", _currentDir, _settings["SUCCESSDIR"], file);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SqlConnection conn = new SqlConnection(_connection);
|
|
|
|
@@ -776,14 +809,16 @@ namespace jdis_import |
|
|
|
string logmsg = string.Format("Date: {0} Error: {1}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"), exSql.Message);
|
|
|
|
WriteToLog(logmsg);
|
|
|
|
todo.Validation = exSql.Message + " " + _workOrderNumber;
|
|
|
|
Console.WriteLine("Line 781: " + filemove);
|
|
|
|
filemove = Path.Combine("Fail", todo.FileName);
|
|
|
|
//File.Move(todo.FileName, filemove);
|
|
|
|
//MailSvc.SendResponse(msg, _workOrderNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
Console.WriteLine("Line 584: " + filemove);
|
|
|
|
Console.WriteLine("Line 787: " + filemove);
|
|
|
|
if (File.Exists(filemove)) File.Delete(filemove);
|
|
|
|
//File.Move(file, filemove);
|
|
|
|
File.Move(file, filemove);
|
|
|
|
todo.FileName = filemove;
|
|
|
|
if (_log)
|
|
|
|
{
|
|
|
|
string successmsg = string.Format("Success Date: {0} File Name: {1} Work Order #: {2}{3}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"), file, _workOrderNumber, "********************************");
|
|
|
|
@@ -862,8 +897,8 @@ namespace jdis_import |
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Console.WriteLine("Line 634: " + file);
|
|
|
|
Console.WriteLine("Line 635: " + filemove);
|
|
|
|
Console.WriteLine("Line 868: " + file);
|
|
|
|
Console.WriteLine("Line 869: " + filemove);
|
|
|
|
//File.Move(file, filemove);
|
|
|
|
}
|
|
|
|
catch (Exception moveExcept)
|
|
|
|
|