diff --git a/JSONParser/Logger/Logger.cs b/JSONParser/Logger/Logger.cs index e3212c8..f19e323 100644 --- a/JSONParser/Logger/Logger.cs +++ b/JSONParser/Logger/Logger.cs @@ -1,4 +1,7 @@ -namespace JSONParser.Logger +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace JSONParser.Logger { public enum LogLevel { @@ -9,13 +12,13 @@ public class Logger { - private readonly string _name; - private readonly string _logDirPath; + //private readonly string _name; + //private readonly string _logDirPath; public Logger(string name, string logDirPath = "log\\") { _name = name; - _logDirPath = logDirPath; + _directoryPath = logDirPath; if (!Directory.Exists(logDirPath)) { @@ -28,10 +31,29 @@ //} } + private string _name; + + public string Name + { + get { return _name; } + set { _name = value; } + } + + + private string _directoryPath; + + public string DirectoryPath + { + get { return _directoryPath; } + set { _directoryPath = value; } + } + public void Log(LogLevel level, string message) { + var bytes = Encoding.UTF8.GetBytes(message); + var str = Encoding.UTF8.GetString(bytes); var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - var logMessage = $"[{timestamp}] [{level}] [{_name}] {message}"; + var logMessage = $"[{timestamp}] [{level}] [{_name}] {str}"; // Write to console (optional) WriteToConsole(level, logMessage); @@ -65,7 +87,7 @@ private void WriteToFile(string message) { var today = DateTime.Now.ToString("yyyy-MM-dd"); - var filePath = $"{_logDirPath}\\{today}_{_name}.txt"; + var filePath = $"{_directoryPath}\\{today}_{_name}.txt"; if (!File.Exists(filePath)) { using (File.Create(filePath)) { } @@ -73,7 +95,7 @@ try { // Append the log message to the file - using (StreamWriter writer = new StreamWriter(filePath, true)) + using (StreamWriter writer = new(filePath, true, Encoding.UTF8)) { writer.WriteLine(message); } diff --git a/JSONParser/Program.cs b/JSONParser/Program.cs index 4ee13a1..5723590 100644 --- a/JSONParser/Program.cs +++ b/JSONParser/Program.cs @@ -31,7 +31,7 @@ public static class Program //using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole()); //ILogger logger = factory.CreateLogger("HRM_MQ"); - var logger = new Logger("HRM_MQ_Test", @"C:\log"); + var logger = new Logger("HRM_MQ", @"C:\log"); var wrapper = new QDocWrapper(odataClient, logger); diff --git a/JSONParser/QDocWrapper/QDocWrapper.cs b/JSONParser/QDocWrapper/QDocWrapper.cs index 98cd109..907640d 100644 --- a/JSONParser/QDocWrapper/QDocWrapper.cs +++ b/JSONParser/QDocWrapper/QDocWrapper.cs @@ -7,10 +7,12 @@ namespace JSONParser.QDocWrapper { ODataClient _client; Logger.Logger _logger; + Logger.Logger _errorLogger; public QDocWrapper(ODataClient client, Logger.Logger logger) { _client = client; _logger = logger; + _errorLogger = new Logger.Logger($"{logger.Name}_Error", logger.DirectoryPath); } public async Task CreateEmployee(Employee employee) @@ -30,6 +32,7 @@ namespace JSONParser.QDocWrapper if ((employee.Person == null || employee.Department == null) || employee.Status == false && emp == null) { _logger.Warn($"{employee.Type} - {employee.Sid} - Skip"); + _errorLogger.Warn(employee.Serialize()); return 0; } @@ -79,16 +82,19 @@ namespace JSONParser.QDocWrapper employee.JobTitle = new JobTitle { DirectumId = (long)empJobTitle["Id"] }; } - if (employee.Login != null) + if (employee.Login != null && emp != null) { dynamic currLogin = emp["Login"]; - string loginName = currLogin["LoginName"]; var id = await employee.Login.GetIdBySid(_client); - if (id != 0 && !loginName.Equals(employee.Login.LoginName)) + if (currLogin != null) { - long currId = currLogin["Id"]; - employee.Login.CloseLogin(_client, currId); - employee.Login.OpenLogin(_client, id); + string loginName = currLogin["LoginName"]; + if (id != 0 && !loginName.Equals(employee.Login.LoginName)) + { + long currId = currLogin["Id"]; + employee.Login.CloseLogin(_client, currId); + employee.Login.OpenLogin(_client, id); + } } if (id == 0) { @@ -104,10 +110,15 @@ namespace JSONParser.QDocWrapper employee.Login = new Login { DirectumId = (long)empLogin["Id"] }; } } + else if (employee.Login != null && emp == null) + { + employee.Login.DirectumId = await employee.Login.GetIdBySid(_client); + } if (employee.Person.DirectumId == 0 || employee.Department.DirectumId == 0 || employee.JobTitle.DirectumId == 0) { _logger.Info($"{employee.Type} - {employee.Sid} - Not Valid"); + _errorLogger.Info(employee.Serialize()); return result; } @@ -188,6 +199,7 @@ namespace JSONParser.QDocWrapper if (department.BusinessUnit.DirectumId == 0) { _logger.Info($"{department.Type} - {department.Sid} - Not Valid"); + _errorLogger.Info(department.Serialize()); return result; } diff --git a/JSONParser/Structure/BusinessUnit.cs b/JSONParser/Structure/BusinessUnit.cs index 3d39e5d..613c652 100644 --- a/JSONParser/Structure/BusinessUnit.cs +++ b/JSONParser/Structure/BusinessUnit.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.ConstrainedExecution; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace JSONParser.Structure @@ -25,6 +26,11 @@ namespace JSONParser.Structure return Name; } + public override string Serialize() + { + return JsonSerializer.Serialize(this); + } + public async override Task Create(ODataClient client) { var newBusinessUnit = new diff --git a/JSONParser/Structure/Department.cs b/JSONParser/Structure/Department.cs index 6ffccb1..4a723dd 100644 --- a/JSONParser/Structure/Department.cs +++ b/JSONParser/Structure/Department.cs @@ -1,5 +1,6 @@  using Simple.OData.Client; +using System.Text.Json; namespace JSONParser.Structure { @@ -16,6 +17,11 @@ namespace JSONParser.Structure return Name; } + public override string Serialize() + { + return JsonSerializer.Serialize(this); + } + public override async Task Create(ODataClient client) { var newDept = new diff --git a/JSONParser/Structure/Employee.cs b/JSONParser/Structure/Employee.cs index 5dc4a20..72918a5 100644 --- a/JSONParser/Structure/Employee.cs +++ b/JSONParser/Structure/Employee.cs @@ -1,4 +1,5 @@ -using Simple.OData.Client; +using Newtonsoft.Json; +using Simple.OData.Client; namespace JSONParser.Structure { @@ -12,6 +13,11 @@ namespace JSONParser.Structure public string JobTitleName { get; set; } public bool Status { get; set; } + public override string Serialize() + { + return JsonConvert.SerializeObject(this); + } + public override string ToString() { if (Person != null) diff --git a/JSONParser/Structure/Entity.cs b/JSONParser/Structure/Entity.cs index 47bce0a..86cbe4f 100644 --- a/JSONParser/Structure/Entity.cs +++ b/JSONParser/Structure/Entity.cs @@ -2,7 +2,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace JSONParser.Structure @@ -13,6 +15,11 @@ namespace JSONParser.Structure public string Sid { get; set; } public long? DirectumId { get; set; } + public virtual string Serialize() + { + return JsonSerializer.Serialize(this); + } + public virtual Task Create(ODataClient client) { throw new NotImplementedException(); diff --git a/JSONParser/Structure/JobTitle.cs b/JSONParser/Structure/JobTitle.cs index c1a7ab5..81c5008 100644 --- a/JSONParser/Structure/JobTitle.cs +++ b/JSONParser/Structure/JobTitle.cs @@ -1,5 +1,6 @@  using Simple.OData.Client; +using System.Text.Json; namespace JSONParser.Structure { @@ -13,6 +14,11 @@ namespace JSONParser.Structure return Name; } + public override string Serialize() + { + return JsonSerializer.Serialize(this); + } + public override async Task Create(ODataClient client) { var newJobTitle = new diff --git a/JSONParser/Structure/Login.cs b/JSONParser/Structure/Login.cs index f2e6642..99d1c69 100644 --- a/JSONParser/Structure/Login.cs +++ b/JSONParser/Structure/Login.cs @@ -1,5 +1,6 @@ using Simple.OData.Client; using System.Dynamic; +using System.Text.Json; namespace JSONParser.Structure { @@ -14,6 +15,11 @@ namespace JSONParser.Structure return LoginName; } + public override string Serialize() + { + return JsonSerializer.Serialize(this); + } + public override async Task Create(ODataClient client) { var newPerson = new diff --git a/JSONParser/Structure/Person.cs b/JSONParser/Structure/Person.cs index 11b14d7..de961f2 100644 --- a/JSONParser/Structure/Person.cs +++ b/JSONParser/Structure/Person.cs @@ -1,6 +1,7 @@  using Simple.OData.Client; using System; +using System.Text.Json; namespace JSONParser.Structure { @@ -20,6 +21,11 @@ namespace JSONParser.Structure return fullname; } + public override string Serialize() + { + return JsonSerializer.Serialize(this); + } + public override async Task Create(ODataClient client) { var newPerson = new diff --git a/JSONParser/config.json b/JSONParser/config.json index aad5e29..b7f95f4 100644 --- a/JSONParser/config.json +++ b/JSONParser/config.json @@ -4,11 +4,14 @@ "UserName": "erp", "Password": "Z!1;Q5#GE4v", "VirtualHost": "erp", - "ChannelName": "hrm_test" + "ChannelName": "hrm" + //"ChannelName": "hrm_test" }, "QDocSettings": { - "IntegrationServiceUrl": "https://astsrvqtest.solidcore-resources.com/Integration/odata/", + //"IntegrationServiceUrl": "https://astsrvqtest.solidcore-resources.com/Integration/odata/", + "IntegrationServiceUrl": "https://qdoc.solidcore-resources.com/Integration/odata/", "Login": "Administrator", - "Password": "D3cTXol8Se" + //"Password": "D3cTXol8Se" + "Password": "MQVuEw9avO" } } \ No newline at end of file