ZUGFeRDRESTService: WIP Prepare validation of buyer order reference

This commit is contained in:
Jonathan Jenne 2022-11-10 16:19:25 +01:00
parent cb2372e1e7
commit 923df866ed
4 changed files with 139 additions and 55 deletions

View File

@ -1,15 +1,20 @@
using System; namespace ZUGFeRDRESTService
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ZUGFeRDRESTService
{ {
public class Config public class Config
{ {
public string Name { get; set; } public string Name { get; set; }
public string LogPath { get; set; } public string LogPath { get; set; }
public string MSSQLConnectionString { get; set; } public string MSSQLConnectionString { get; set; }
public FirebirdConfig Firebird { get; set; }
} }
public class FirebirdConfig
{
public string Datasource { get; set; }
public string Database { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
} }

View File

@ -1,24 +1,24 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using DigitalData.Modules.Interfaces; using DigitalData.Modules.Interfaces;
using DigitalData.Modules.Logging;
using static DigitalData.Modules.Interfaces.Exceptions; using static DigitalData.Modules.Interfaces.Exceptions;
using static DigitalData.Modules.Interfaces.ZUGFeRDInterface; using static DigitalData.Modules.Interfaces.ZUGFeRDInterface;
using static DigitalData.Modules.Interfaces.PropertyValues;
using System.Data.SqlClient;
using DigitalData.Modules.Database;
namespace ZUGFeRDRESTService.Controllers namespace ZUGFeRDRESTService.Controllers
{ {
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
public class ValidationController : ControllerBase public class ValidationController : ControllerBase
{ {
public static string RESPONSE_OK = "OK"; public const string RESPONSE_OK = "OK";
public static string RESPONSE_ERROR = "ERROR"; public const string RESPONSE_ERROR = "ERROR";
private readonly ZUGFeRDInterface _zugferd; private readonly ZUGFeRDInterface _zugferd;
private readonly IDatabase _database; private readonly IDatabase _database;
@ -29,34 +29,6 @@ namespace ZUGFeRDRESTService.Controllers
private readonly PropertyValues _props; private readonly PropertyValues _props;
private readonly Dictionary<String, XmlItemProperty> _propertyMap; private readonly Dictionary<String, XmlItemProperty> _propertyMap;
public class ValidationResponse
{
public string status;
public string message;
public List<string> errors;
public ValidationResponse()
{
status = RESPONSE_OK;
message = string.Empty;
errors = new List<string>();
}
public ValidationResponse(string Status, string Message)
{
status = Status;
message = Message;
errors = new List<string>();
}
public ValidationResponse(string Status, string Message, List<String> Errors)
{
status = Status;
message = Message;
errors = Errors;
}
}
public ValidationController(ILogging logging, IDatabase database) public ValidationController(ILogging logging, IDatabase database)
{ {
_logConfig = logging.LogConfig; _logConfig = logging.LogConfig;
@ -81,15 +53,15 @@ namespace ZUGFeRDRESTService.Controllers
/// </summary> /// </summary>
/// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param> /// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param>
[HttpPost] [HttpPost]
public async Task<ValidationResponse> Post(IFormFile file, string user_id) public ValidationResponse Post(IFormFile file, string user_id)
{ {
_logger.Debug("Start processing request to ValidationController"); _logger.Debug("Start processing request to ValidationController");
object oDocument; object oDocument;
PropertyValues.CheckPropertyValuesResult oResult = new PropertyValues.CheckPropertyValuesResult(); PropertyValues.CheckPropertyValuesResult oResult = new PropertyValues.CheckPropertyValuesResult();
try try
{ {
using Stream oStream = file.OpenReadStream(); using Stream oStream = file.OpenReadStream();
{ {
_logger.Debug("Extracting ZUGFeRD Data from file [{0}]", file.FileName); _logger.Debug("Extracting ZUGFeRD Data from file [{0}]", file.FileName);
@ -101,19 +73,19 @@ namespace ZUGFeRDRESTService.Controllers
oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID"); oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
var oRequiredProperties = _propertyMap. var oRequiredProperties = _propertyMap.
Where(prop => {return prop.Value.IsRequired == true;}). Where(prop => { return prop.Value.IsRequired == true; }).
Select(prop => { return prop.Value.Description; }) Select(prop => { return prop.Value.Description; })
.ToList(); .ToList();
_logger.Debug("Found [{0}] required properties", oRequiredProperties.Count); _logger.Debug("Found [{0}] required properties", oRequiredProperties.Count);
_logger.Debug(String.Join(",", oRequiredProperties.ToArray())); _logger.Debug(string.Join(",", oRequiredProperties.ToArray()));
_logger.Debug("Result of checking against the database: {0} valid properties, {1} missing properties", _logger.Debug("Result of checking against the database: {0} valid properties, {1} missing properties",
oResult.ValidProperties.Count, oResult.MissingProperties.Count); oResult.ValidProperties.Count, oResult.MissingProperties.Count);
if (oResult.MissingProperties.Count > 0) if (oResult.MissingProperties.Count > 0)
{ {
throw new ZUGFeRDExecption(ZUGFeRDInterface.ErrorType.MissingProperties, throw new ZUGFeRDExecption(ErrorType.MissingProperties,
"Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten."); "Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten.");
} }
@ -129,7 +101,7 @@ namespace ZUGFeRDRESTService.Controllers
}; };
} }
catch (ZUGFeRDExecption ex) catch (ZUGFeRDExecption ex)
{ {
_logger.Error(ex); _logger.Error(ex);
// Determine which message should be sent in the response // Determine which message should be sent in the response
@ -172,7 +144,64 @@ namespace ZUGFeRDRESTService.Controllers
status = RESPONSE_ERROR, status = RESPONSE_ERROR,
message = oMessage message = oMessage
}; };
} }
} }
private Tuple<bool, string> ValidateBuyerOrderReference(Dictionary<string, XmlItemProperty> pPropertyMap, List<ValidProperty> pProperties)
{
foreach (var oItem in pProperties)
{
var oResult = InsertPropertyMap(oItem);
if (oResult == false)
{
_logger.Warn("Error while inserting the Property [{0}] into the Database!", oItem.Description);
}
}
// TODO: Execute the Procedure.
// _database.MSSQL.ExecuteNonQuery("");
// TODO: Return a Status value which will be returned to the caller.
return new Tuple<bool, string>(true, "");
}
public bool InsertPropertyMap(ValidProperty pProperty)
{
try
{
var oCreator = "ZUGFeRD REST Service";
var oDomain = "test.wisag.de";
var oMessageId = $"{Guid.NewGuid()}@{oDomain}";
var oSql = $"INSERT INTO {pProperty.TableName} " +
"(REFERENCE_GUID, ITEM_DESCRIPTION, ITEM_VALUE, CREATEDWHO, SPEC_NAME, GROUP_COUNTER, IS_REQUIRED) VALUES " +
"(@REFERENCE_GUID, @ITEM_DESCRIPTION, @ITEM_VALUE, @CREATEDWHO, @SPEC_NAME, @GROUP_COUNTER, @IS_REQUIRED)";
var oParams = new SqlParameter[]
{
new SqlParameter("@REFERENCE_GUID", oMessageId),
new SqlParameter("@ITEM_DESCRIPTION", pProperty.Description),
new SqlParameter("@ITEM_VALUE", pProperty.Value),
new SqlParameter("@CREATEDWHO", oCreator),
new SqlParameter("@GROUP_COUNTER", pProperty.GroupCounter),
new SqlParameter("@SPEC_NAME", pProperty.TableColumn),
new SqlParameter("@IS_REQUIRED", pProperty.ISRequired)
};
var oCommand = new SqlCommand(oSql);
oCommand.Parameters.AddRange(oParams);
return _database.MSSQL.ExecuteNonQuery(oCommand);
}
catch (Exception ex)
{
_logger.Error(ex);
return false;
}
}
} }
} }

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
namespace ZUGFeRDRESTService.Controllers
{
public class ValidationResponse
{
public string status;
public string message;
public List<string> errors;
public ValidationResponse()
{
status = ValidationController.RESPONSE_OK;
message = string.Empty;
errors = new List<string>();
}
public ValidationResponse(string Status, string Message)
{
status = Status;
message = Message;
errors = new List<string>();
}
public ValidationResponse(string Status, string Message, List<String> Errors)
{
status = Status;
message = Message;
errors = Errors;
}
}
}

View File

@ -1,18 +1,21 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.SqlClient;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DigitalData.Modules.Database; using DigitalData.Modules.Database;
using DigitalData.Modules.Interfaces; using DigitalData.Modules.Interfaces;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace ZUGFeRDRESTService namespace ZUGFeRDRESTService
{ {
public class Database: IDatabase public class Database: IDatabase
{ {
private string _gdPictureKey = null; private string _gdPictureKey = null;
private Dictionary<String, XmlItemProperty> _propertyMap = null; private Dictionary<string, XmlItemProperty> _propertyMap = null;
private const string QUERY_GET_GDPICTURE_KEY = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'"; 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"; private const string QUERY_GET_PROPERTY_MAP = "SELECT * FROM TBEDM_XML_ITEMS WHERE SPECIFICATION = '{0}' AND ACTIVE = True ORDER BY XML_PATH";
@ -20,15 +23,28 @@ namespace ZUGFeRDRESTService
public MSSQLServer MSSQL { get; set; } public MSSQLServer MSSQL { get; set; }
public Firebird Firebird { get; set; } public Firebird Firebird { get; set; }
public Database(ILogging Logging, IConfiguration Config) public Database(ILogging Logging, IConfiguration Config, IOptions<Config> Options)
{ {
var LogConfig = Logging.LogConfig; var LogConfig = Logging.LogConfig;
var Logger = Logging.LogConfig.GetLogger(); var Logger = Logging.LogConfig.GetLogger();
var AppConfig = Config.GetSection("Config"); var FirebirdConfig = Options.Value.Firebird;
var FBConfig = AppConfig.GetSection("Firebird");
MSSQL = new MSSQLServer(LogConfig, AppConfig["MSSQLConnectionString"]); // var AppConfig = Config.GetSection("Config");
Firebird = new Firebird(LogConfig, FBConfig["Datasource"], FBConfig["Database"], FBConfig["Username"], FBConfig["Password"]); // var FBConfig = AppConfig.GetSection("Firebird");
Logger.Debug("Establishing MSSQL Database connection..");
MSSQL = new MSSQLServer(LogConfig, Options.Value.MSSQLConnectionString);
Logger.Debug("Establishing Firebird Database connection..");
Firebird = new Firebird(LogConfig,
FirebirdConfig.Datasource,
FirebirdConfig.Database,
FirebirdConfig.Username,
FirebirdConfig.Password);
// MSSQL = new MSSQLServer(LogConfig, AppConfig["MSSQLConnectionString"]);
// Firebird = new Firebird(LogConfig, FBConfig["Datasource"], FBConfig["Database"], FBConfig["Username"], FBConfig["Password"]);
Logger.Debug("MSSQL Connection: [{0}]", MSSQL.CurrentConnectionString); Logger.Debug("MSSQL Connection: [{0}]", MSSQL.CurrentConnectionString);
Logger.Debug("Firebird Connection: [{0}]", Firebird.ConnectionString); Logger.Debug("Firebird Connection: [{0}]", Firebird.ConnectionString);