diff --git a/WEBSERVICES/ZUGFeRDRESTService/Config.cs b/WEBSERVICES/ZUGFeRDRESTService/Config.cs new file mode 100644 index 00000000..599cc0d6 --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/Config.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ZUGFeRDRESTService +{ + public class Config + { + public string Name { get; set; } + public string LogPath { get; set; } + + public string MSSQLConnectionString { get; set; } + } +} diff --git a/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs b/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs new file mode 100644 index 00000000..91ff5730 --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs @@ -0,0 +1,126 @@ +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using DigitalData.Modules.Interfaces; +using DigitalData.Modules.Logging; + +namespace ZUGFeRDRESTService.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValidationController : ControllerBase + { + public static string RESPONSE_OK = "OK"; + public static string RESPONSE_ERROR = "ERROR"; + + private readonly ZUGFeRDInterface _zugferd; + private readonly IDatabase _database; + + private readonly DigitalData.Modules.Logging.LogConfig _logConfig; + private readonly DigitalData.Modules.Logging.Logger _logger; + + private readonly PropertyValues _props; + private readonly Dictionary _propertyMap; + + public class ValidationResponse + { + public string status; + public string message; + public List errors; + + public ValidationResponse() + { + status = RESPONSE_OK; + message = String.Empty; + errors = new List(); + } + } + + public ValidationController(ILogging logging, IDatabase database) + { + _logConfig = logging.LogConfig; + _logger = _logConfig.GetLogger(); + + _logger.Debug("Validation Controller initializing"); + + _database = database; + var oGDPictureKey = database.GetGDPictureKey(); + var oPropertyMap = database.GetPropertyMap(); + + _propertyMap = oPropertyMap; + + _zugferd = new ZUGFeRDInterface(_logConfig, oGDPictureKey); + _props = new PropertyValues(_logConfig); + + _logger.Debug("Validation Controller initialized!"); + } + + /// + /// POST: api/Validation + /// + /// This parameter's name needs to correspond to the html form's file-input name + [HttpPost] + public async Task Post(IFormFile file, string user_id) + { + string oFilePath; + string oFileName; + + oFilePath = Path.Combine(Path.GetTempPath(), file.FileName); + oFileName = file.FileName; + + using FileStream oStream = new FileStream(oFilePath, FileMode.Create); + await file.CopyToAsync(oStream); + + CrossIndustryDocumentType oDocument; + + try + { + oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFilePath); + } + catch (Exception ex) + { + _logger.Error(ex); + return new ValidationResponse() + { + status = RESPONSE_ERROR, + message = "The uploaded file is not a valid ZUGFeRD Invoice!" + }; + } + + try + { + PropertyValues.CheckPropertyValuesResult oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID"); + + if (oResult.MissingProperties.Count > 0) + { + return new ValidationResponse() + { + status = RESPONSE_ERROR, + message = "The uploaded file is a valid ZUGFeRD Invoice but missing some properties!", + errors = oResult.MissingProperties + }; + } + } + catch (Exception ex) + { + _logger.Error(ex); + return new ValidationResponse() + { + status = RESPONSE_ERROR, + message = "The uploaded file is not a valid ZUGFeRD Invoice!" + }; + } + + return new ValidationResponse() + { + status = RESPONSE_OK, + message = "The uploaded files is a valid ZUGFeRD Invoice!" + }; + } + } +} diff --git a/WEBSERVICES/ZUGFeRDRESTService/Controllers/ZugferdValidationController.cs b/WEBSERVICES/ZUGFeRDRESTService/Controllers/ZugferdValidationController.cs deleted file mode 100644 index f8d462c3..00000000 --- a/WEBSERVICES/ZUGFeRDRESTService/Controllers/ZugferdValidationController.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace ZUGFeRDRESTService.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class ZugferdValidationController : ControllerBase - { - public static string RESPONSE_OK = "OK"; - public static string RESPONSE_ERROR = "ERROR"; - - private readonly IZugferdValidationDataService _dataService; - - public class ZugferdValidationResponse - { - public string status; - public string message; - public List errors; - - - - public ZugferdValidationResponse() - { - status = RESPONSE_OK; - message = String.Empty; - errors = new List(); - } - } - - public ZugferdValidationController(IZugferdValidationDataService dataService) - { - _dataService = dataService; - } - - /// - /// POST: api/ZugferdValidation - /// - /// This parameter's name needs to correspond to the html form's file-input name - [HttpPost] - public async Task Post(List files) - { - var oFilePaths = new List(); - var oFileNames = new List(); - - if (files.Count == 0) { - return new ZugferdValidationResponse() - { - status = RESPONSE_ERROR, - message = "No File received!" - }; - } - - foreach (var formFile in files) - { - var oFilePath = Path.GetTempFileName(); - oFilePaths.Add(oFilePath); - oFileNames.Add(formFile.FileName); - - using (var oStream = new FileStream(oFilePath, FileMode.Create)) - { - await formFile.CopyToAsync(oStream); - } - } - - var oResponse = new ZugferdValidationResponse - { - message = "You uploaded the following file: " + oFileNames.First() - }; - - return oResponse; - } - } -} diff --git a/WEBSERVICES/ZUGFeRDRESTService/Database.cs b/WEBSERVICES/ZUGFeRDRESTService/Database.cs new file mode 100644 index 00000000..c223321c --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/Database.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using DigitalData.Modules.Database; +using DigitalData.Modules.Interfaces; +using Microsoft.Extensions.Configuration; + +namespace ZUGFeRDRESTService +{ + public class Database: IDatabase + { + private string _gdPictureKey = null; + private Dictionary _propertyMap = null; + + private const string QUERY_GET_GDPICTURE_KEY = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'"; + private const string QUERY_GET_PROPERTY_MAP = "SELECT * FROM TBEDM_XML_ITEMS WHERE SPECIFICATION = '{0}' AND ACTIVE = True ORDER BY XML_PATH"; + + public MSSQLServer MSSQL { get; set; } + public Firebird Firebird { get; set; } + + public Database(ILogging Logging, IConfiguration Config) + { + var LogConfig = Logging.LogConfig; + var AppConfig = Config.GetSection("Config"); + var FBConfig = AppConfig.GetSection("Firebird"); + + MSSQL = new MSSQLServer(LogConfig, AppConfig["MSSQLConnectionString"]); + Firebird = new Firebird(LogConfig, FBConfig["Datasource"], FBConfig["Database"], FBConfig["Username"], FBConfig["Password"]); + } + + public string GetGDPictureKey() + { + if (_gdPictureKey == null) + _gdPictureKey = MSSQL.GetScalarValue(QUERY_GET_GDPICTURE_KEY).ToString(); + + return _gdPictureKey; + } + + public Dictionary GetPropertyMap() + { + if (_propertyMap == null) + { + _propertyMap = new Dictionary(); + var oDatatable = Firebird.GetDatatable(string.Format(QUERY_GET_PROPERTY_MAP, "DEFAULT")); + + foreach (DataRow oRow in oDatatable.Rows) + { + _propertyMap.Add(oRow["XML_PATH"].ToString(), new XmlItemProperty() + { + Description = oRow["DESCRIPTION"].ToString(), + TableName = oRow["TABLE_NAME"].ToString(), + GroupScope = oRow["GROUP_SCOPE"].ToString(), + IsRequired = (bool)oRow["IS_REQUIRED"], + IsGrouped = (bool)oRow["IS_GROUPED"] + }); + } + } + + return _propertyMap; + } + } +} diff --git a/WEBSERVICES/ZUGFeRDRESTService/IDatabase.cs b/WEBSERVICES/ZUGFeRDRESTService/IDatabase.cs new file mode 100644 index 00000000..0cce0b8b --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/IDatabase.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using DigitalData.Modules.Database; +using DigitalData.Modules.Interfaces; + +namespace ZUGFeRDRESTService +{ + public interface IDatabase + { + public MSSQLServer MSSQL { get; set; } + public Firebird Firebird { get; set; } + + public string GetGDPictureKey(); + public Dictionary GetPropertyMap(); + } +} diff --git a/WEBSERVICES/ZUGFeRDRESTService/ILogging.cs b/WEBSERVICES/ZUGFeRDRESTService/ILogging.cs new file mode 100644 index 00000000..5c31118c --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/ILogging.cs @@ -0,0 +1,9 @@ +using DigitalData.Modules.Logging; + +namespace ZUGFeRDRESTService +{ + public interface ILogging + { + public LogConfig LogConfig { get; set; } + } +} \ No newline at end of file diff --git a/WEBSERVICES/ZUGFeRDRESTService/IZugferdValidationDataService.cs b/WEBSERVICES/ZUGFeRDRESTService/IZugferdValidationDataService.cs deleted file mode 100644 index 181a091a..00000000 --- a/WEBSERVICES/ZUGFeRDRESTService/IZugferdValidationDataService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace ZUGFeRDRESTService -{ - public interface IZugferdValidationDataService - { - } -} diff --git a/WEBSERVICES/ZUGFeRDRESTService/Logger.cs b/WEBSERVICES/ZUGFeRDRESTService/Logger.cs new file mode 100644 index 00000000..0f42a78f --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/Logger.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ZUGFeRDRESTService +{ + public sealed class Logger + { + private static readonly Lazy lazy = new Lazy(() => new Logger()); + + public static Logger Instance { get { return lazy.Value; } } + + private Logger() + { + + } + } +} + diff --git a/WEBSERVICES/ZUGFeRDRESTService/Logging.cs b/WEBSERVICES/ZUGFeRDRESTService/Logging.cs new file mode 100644 index 00000000..6e6a6281 --- /dev/null +++ b/WEBSERVICES/ZUGFeRDRESTService/Logging.cs @@ -0,0 +1,15 @@ +using DigitalData.Modules.Logging; + +namespace ZUGFeRDRESTService +{ + internal class Logging : ILogging + { + public LogConfig LogConfig { get; set; } + + public Logging(string LogPath, bool Debug) + { + LogConfig = new LogConfig(LogConfig.PathType.CustomPath, LogPath, null, "Digital Data", "ZUGFeRD-REST-API"); + LogConfig.Debug = Debug; + } + } +} \ No newline at end of file diff --git a/WEBSERVICES/ZUGFeRDRESTService/Pages/Index.cshtml b/WEBSERVICES/ZUGFeRDRESTService/Pages/Index.cshtml index b5f02066..d941e9ef 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/Pages/Index.cshtml +++ b/WEBSERVICES/ZUGFeRDRESTService/Pages/Index.cshtml @@ -3,14 +3,39 @@ ViewData["Title"] = "UploadTest"; } -

UploadTest

+

ZUGFeRD Validation

-
+

- PDF Datei: - + + +

+ +

+ +

+ + diff --git a/WEBSERVICES/ZUGFeRDRESTService/Program.cs b/WEBSERVICES/ZUGFeRDRESTService/Program.cs index 5a4e487d..007ffa36 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/Program.cs +++ b/WEBSERVICES/ZUGFeRDRESTService/Program.cs @@ -6,6 +6,9 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using NLog.Web; +using DigitalData.Modules.Logging; +using System.Reflection; namespace ZUGFeRDRESTService { diff --git a/WEBSERVICES/ZUGFeRDRESTService/Properties/launchSettings.json b/WEBSERVICES/ZUGFeRDRESTService/Properties/launchSettings.json index 06475a99..3e1562b7 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/Properties/launchSettings.json +++ b/WEBSERVICES/ZUGFeRDRESTService/Properties/launchSettings.json @@ -12,7 +12,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "api/zugferdvalidation", + "launchUrl": "", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/WEBSERVICES/ZUGFeRDRESTService/Startup.cs b/WEBSERVICES/ZUGFeRDRESTService/Startup.cs index 22e70f10..0a880841 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/Startup.cs +++ b/WEBSERVICES/ZUGFeRDRESTService/Startup.cs @@ -18,16 +18,29 @@ namespace ZUGFeRDRESTService public Startup(IConfiguration configuration) { Configuration = configuration; + AppConfig = configuration.GetSection("Config"); } public IConfiguration Configuration { get; } + private IConfigurationSection AppConfig { get; } + // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + string oLogPath = AppConfig["LogPath"]; + bool.TryParse(AppConfig["LogDebug"], out bool oLogDebug); + services.AddControllers().AddNewtonsoftJson(); services.AddRazorPages(); - services.AddTransient(); + services.Configure(Configuration.GetSection("Config")); + services.AddSingleton(sp => new Logging(oLogPath, oLogDebug)); + services.AddSingleton((Func)(sp => { + var logging = sp.GetService(); + var config = sp.GetService(); + + return new Database(logging, config); + })); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj b/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj index a7b78335..6450a6f4 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj +++ b/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj @@ -1,12 +1,22 @@ - + netcoreapp3.1 + + + + + + + + + + diff --git a/WEBSERVICES/ZUGFeRDRESTService/ZugferdValidationDataService.cs b/WEBSERVICES/ZUGFeRDRESTService/ZugferdValidationDataService.cs deleted file mode 100644 index 045f481f..00000000 --- a/WEBSERVICES/ZUGFeRDRESTService/ZugferdValidationDataService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace ZUGFeRDRESTService -{ - public class ZugferdValidationDataService: IZugferdValidationDataService - { - } -} diff --git a/WEBSERVICES/ZUGFeRDRESTService/appsettings.json b/WEBSERVICES/ZUGFeRDRESTService/appsettings.json index d9d9a9bf..82a76a87 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/appsettings.json +++ b/WEBSERVICES/ZUGFeRDRESTService/appsettings.json @@ -6,5 +6,17 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Config": { + "LogPath": "E:\\ZUGFeRDRESTService", + "LogDebug": true, + "Name": "ZUGFeRD REST API", + "MSSQLConnectionString": "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM_TEST;User Id=sa;Password=dd;", + "Firebird": { + "Datasource": "172.24.12.41", + "Database": "172.24.12.41:E:\\DB\\Firebird\\Databases\\EDMI_TEMPLATE\\EDMI_MASTER.FDB", + "Username": "SYSDBA", + "Password": "dd" + } + } }