You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
17 KiB

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DOCGEN;
using Model;
using Database;
using Newtonsoft.Json;
using API_NetFramework.Models;
using System.Runtime.Remoting.Messaging;
using System.IO;
using System.Web;
using System.Net.Mime;
using System.Security.Policy;
using System.Text;
using Microsoft.AspNetCore.Http.Features;
using System.Security.Cryptography;
using System.Net.Http.Headers;
using SecuringWebApiUsingApiKey.Middleware;
using DOCGEN.Klassen;
namespace API_NetFramework.Controllers
{
/// <summary>
///
/// </summary>
/// <remarks></remarks>
///
public class ILResponse
{
public int StatusCode;
public int senderror { get; set; } = 0;
public string response { get; set; } = "";
}
public enum uploadtype
{
fast = 1,
slow = 2,
docupload = 3
}
public class ArchivController : ApiController
{
// GET: OnBase
string tokenfunction = "Archiv";
string connectionstring = StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["EDOKAConnectionstring"].ConnectionString, "i%!k!7pab%bNLdA5hE4pkR4XaB%E^jB3d9tHuQ4pbF&BZjF7SB#WBWit5#HrbJiLrLVm");
private void Update_IL_Log(ref ILResponse ilr, string dokumentid)
{
APILogging.Log((HttpRequestMessage)Request, "Start Updaet IL_Log: DokumentID:" + dokumentid, LogLevelType.Debug);
DB dB = new DB(StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["JournalConnectionstring"].ConnectionString, "i%!k!7pab%bNLdA5hE4pkR4XaB%E^jB3d9tHuQ4pbF&BZjF7SB#WBWit5#HrbJiLrLVm"));
string sql = "Insert OnDoc_IL_Log (dokumentid, ilresponse,error, erstellt_am) values ('" + dokumentid + "',";
sql = sql + "'" + ilr.response.ToString() + "',";
if (ilr.senderror != 0) { sql = sql + "1,"; } else { sql = sql + "0,"; }
sql = sql +"getdate() )";
dB.Exec_SQL(sql);
dB = null;
APILogging.Log((HttpRequestMessage)Request, "Ende Update IL_Log: DokumentID:" + dokumentid, LogLevelType.Debug);
}
private void update_dokumentstatus(string dokumentid)
{
DB db = new DB(connectionstring);
db.dokument_abschliessen(dokumentid);
db = null;
}
/// <summary>
/// CheckDocID prüft auf eine vorhandene DokumentID in OnDoc (DokumentID OFFEDK... / Barcode-Klenber-Nr)
/// </summary>
/// <param name="DokumentID"></param>
/// <returns>
/// 200: OK
/// </returns>
/// <remarks></remarks>
[HttpGet]
[Route("API/CheckDocID")]
public IHttpActionResult CheckDocID(string DokumentID)
{
if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
{
return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
}
try
{
APILogging.Log((HttpRequestMessage)Request, "Start Check DokumentID: DokumentID:" + DokumentID, LogLevelType.Debug);
Database.DB db = new Database.DB(connectionstring);
db.Get_Tabledata("Select * from dokument where dokumentid = '" + DokumentID + "'", false, true);
if (db.dsdaten.Tables[0].Rows.Count > 0)
{
return Ok(DokumentID);
}
db.Get_Tabledata("Select barcodenr from barcodeetikette where dokumentid='" + DokumentID + "'", false, true);
if (db.dsdaten.Tables[0].Rows.Count > 0)
{
return Ok(DokumentID);
}
APILogging.Log((HttpRequestMessage)Request, "Ende Check DokumentID: DokumentID:" + DokumentID, LogLevelType.Debug);
return Content(HttpStatusCode.NotFound, DokumentID);
}
catch (Exception e)
{
APILogging.Log((HttpRequestMessage)Request, e.Message, LogLevelType.Error);
return Content(HttpStatusCode.InternalServerError, e.Message);
}
}
/// <summary>
/// Archiviert das Dokument aus OnDoc in OnBase
/// </summary>
/// <param name="DokumentID"></param>
/// <returns>
/// Returncode: 200 (OK)
/// Dokumenthandle aus OnBase
/// </returns>
/// <remarks></remarks>
[HttpPost]
[Route("API/ArchiveDocFromDatabase")]
public IHttpActionResult ArchivDocFromDatabase(string DokumentID)
{
if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
{
return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
}
try
{
string doktypnr = "";
string extension = "";
APILogging.Log((HttpRequestMessage)Request, "Start ArchiveDocFromDatabase: DokumentID:" + DokumentID, LogLevelType.Debug);
DB db = new DB(connectionstring);
db.Get_Tabledata("Select dokumenttypnr, dokumentname from dokument where dokumentid='" + DokumentID+"'", false, true);
extension = System.IO.Path.GetExtension(db.dsdaten.Tables[0].Rows[0]["dokumentname"].ToString());
doktypnr = db.dsdaten.Tables[0].Rows[0][0].ToString();
db.clear_parameter();
db.add_parameter("@dokumentid", DokumentID);
db.Get_Tabledata("[sp_Get_OnDoc_Parameters]", true, false);
OnBaseDocUpload.OnBaseDokument onbasedoc = new OnBaseDocUpload.OnBaseDokument();
DocGet dg = new DocGet(connectionstring);
clsdok dok = new clsdok("", "", "");
string[] officeformat = System.Configuration.ConfigurationManager.AppSettings["ArchivierungOfficeFormat"].ToString().Split(',');
if (officeformat.Contains(doktypnr) || extension.ToUpper()=="PDF")
{
dg.GetDoc(DokumentID);
extension = extension.ToUpper().Replace(".", "");
}
else {
dok = dg.GetDocAsPDF(DokumentID);
extension = "PDF";
}
if (db.dsdaten.Tables[0].Rows[0]["BpNummer"].ToString() == "")
{
onbasedoc.personNummer = db.dsdaten.Tables[0].Rows[0]["PersonNummer"].ToString();
onbasedoc.bpNummer = "";
}
else
{
onbasedoc.bpNummer = db.dsdaten.Tables[0].Rows[0]["BpNummer"].ToString();
onbasedoc.personNummer = "";
}
onbasedoc.dokumentDatum = db.dsdaten.Tables[0].Rows[0]["DokumentDatum"].ToString();
onbasedoc.dokumentTyp = db.dsdaten.Tables[0].Rows[0]["dokumenttyp"].ToString(); ;
onbasedoc.dateiTyp = extension;
var Attribute = new List<Model.OnBaseDocUpload.attribute>();
onbasedoc.attributes = new List<Model.OnBaseDocUpload.attribute>();
foreach (System.Data.DataRow rw in db.dsdaten.Tables[1].Rows)
{
OnBaseDocUpload.attribute na = new OnBaseDocUpload.attribute(rw["fieldname"].ToString(), rw["fieldvalue"].ToString());
onbasedoc.attributes.Add(na);
}
onbasedoc.dokumentDatei = dok.dokument;
db = null;
ILResponse ilr = new ILResponse();
string debugfilename = System.Configuration.ConfigurationManager.AppSettings["JSONDebugPath"];
string SendToOnBase = System.Configuration.ConfigurationManager.AppSettings["SendToOnBase"];
string SendToFile = System.Configuration.ConfigurationManager.AppSettings["SendToFile"];
string debugdir = System.Configuration.ConfigurationManager.AppSettings["DebugDir"];
string jsonstring = Newtonsoft.Json.JsonConvert.SerializeObject(onbasedoc);
IHttpActionResult transferResult = null;
if (SendToOnBase != "Yes")
{
transferResult = Transfer_OnBase(uploadtype.fast, ref jsonstring, ref ilr);
if (SendToFile == "Yes")
{
if (debugfilename != "")
{
debugfilename = debugfilename + DokumentID + ".json";
System.IO.File.WriteAllText(debugfilename, jsonstring);
debugfilename = debugfilename + ".pdf";
Helper.clsFileHelper fh = new Helper.clsFileHelper();
fh.SaveBase64ToFile(onbasedoc.dokumentDatei, debugfilename);
fh = null;
}
}
}
APILogging.Log((HttpRequestMessage)Request, "Ende ArchiveDocFromDatabase: DokumentID:" + DokumentID, LogLevelType.Debug);
//Log nachführen
Update_IL_Log(ref ilr,DokumentID);
if (ilr.senderror == 1)
{
return Content(HttpStatusCode.InternalServerError, ilr.response);
}
else
{
update_dokumentstatus(DokumentID);
return Content(HttpStatusCode.OK, DokumentID + " archiviert");
}
}
catch (Exception e)
{
APILogging.Log((HttpRequestMessage)Request, e.Message, LogLevelType.Error);
return Content(HttpStatusCode.InternalServerError, e.Message);
}
finally
{
}
}
[HttpPost]
[Route("API/ArchivDocFromIRIS")]
public IHttpActionResult ArchivDocFromIRIS(string dokumentid)
{
if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
{
return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
}
try
{
APILogging.Log((HttpRequestMessage)Request, "Start ArchivDocFromIRIS DokumentID: " + dokumentid, LogLevelType.Debug);
if (dokumentid.Substring(0, 9).ToUpper() == "OFFEDK008")
{
return (ArchivDocFromDatabase(dokumentid));
}
else
{
DB db = new DB(connectionstring);
db.clear_parameter();
db.add_parameter("@dokumentid", dokumentid);
db.Get_Tabledata("[OnDoc_IRIS_Archivierung]", true, false);
string returnmessage = db.dsdaten.Tables[0].Rows[0][0].ToString();
switch (db.dsdaten.Tables[0].Rows[0][1].ToString())
{
case "1":
case "2":
case "3":
APILogging.Log((HttpRequestMessage)Request, db.dsdaten.Tables[0].Rows[0].ToString() + dokumentid, LogLevelType.Debug);
return Content(HttpStatusCode.NotFound, returnmessage);
case "0":
return (ArchivDocFromDatabase(dokumentid));
default:
break;
};
}
APILogging.Log((HttpRequestMessage)Request, "Ende ArchivDocFromIRIS DokumentID:" + dokumentid, LogLevelType.Debug);
return Ok(dokumentid);
}
catch (Exception e)
{
APILogging.Log((HttpRequestMessage)Request, e.Message, LogLevelType.Error);
return Content(HttpStatusCode.InternalServerError, e.Message);
}
}
[HttpPost]
[Route("API/ArchivSBDoc")]
public IHttpActionResult ArchivSBDoc(string sbnr, string intid, int partnernr, string dokumentid )
{
if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
{
return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
}
APILogging.Log((HttpRequestMessage)Request, "Start ArchivSBDoc DokumentID: DokumentID:" + dokumentid, LogLevelType.Debug);
string json = "";
if (HttpContext.Current.Request.InputStream.Length > 0)
{
using (var inputStream = new StreamReader(HttpContext.Current.Request.InputStream))
{
json = inputStream.ReadToEnd();
}
}
string debugfilename = System.Configuration.ConfigurationManager.AppSettings["JSONDebugPath"];
string SendToOnBase = System.Configuration.ConfigurationManager.AppSettings["SendToOnBase"];
string SendToFile = System.Configuration.ConfigurationManager.AppSettings["SendToFile"];
string debugdir = System.Configuration.ConfigurationManager.AppSettings["DebugDir"];
string jsonstring = json;
ILResponse ilr = new ILResponse();
//jsonstring = Newtonsoft.Json.JsonConvert.SerializeObject(od);
IHttpActionResult transferResult = null;
if (SendToOnBase != "Yes")
{
transferResult = Transfer_OnBase(uploadtype.slow, ref jsonstring, ref ilr);
if (SendToFile == "Yes")
{
if (debugfilename != "")
{
debugfilename=debugfilename+ sbnr.ToString() + "_" + intid.ToString() + "_" + partnernr.ToString() + ".json";
System.IO.File.WriteAllText(debugfilename, jsonstring);
}
}
}
//Log nachführen
Update_IL_Log(ref ilr, "SB_" + sbnr.ToString() + "_" + intid.ToString());
APILogging.Log((HttpRequestMessage)Request, "Ende ArchivSBDoc DokumentID: DokumentID:" + dokumentid, LogLevelType.Debug);
return transferResult;
//return Content(HttpStatusCode.OK, "");
}
private IHttpActionResult Transfer_OnBase(uploadtype utype, ref string jsonstring, ref ILResponse ilr)
{
//ILResponse ilr = new ILResponse();
string response;
WebRequest request;
APILogging.Log((HttpRequestMessage)Request, "Start Transfer to OnBase", LogLevelType.Debug);
string url ="";
switch (utype){
case uploadtype.fast:
url= System.Configuration.ConfigurationManager.AppSettings["ILFast"];
break;
case uploadtype.slow:
url = System.Configuration.ConfigurationManager.AppSettings["ILSlow"];
break;
case uploadtype.docupload:
url = System.Configuration.ConfigurationManager.AppSettings["ILDocupload"];
break;
}
var data = Encoding.UTF8.GetBytes(jsonstring);
request = WebRequest.Create(url);
request.ContentLength = data.Length;
request.ContentType = "application/json";
request.Method = "POST";
try
{
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
requestStream.Close();
using (Stream responseStream = request.GetResponse().GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
response = reader.ReadToEnd();
}
}
}
ilr.StatusCode = 0;
ilr.senderror = 0;
ilr.response=response;
APILogging.Log((HttpRequestMessage)Request, "Ende Transfer to OnBase", LogLevelType.Debug);
return Content(HttpStatusCode.OK, ilr);
}
catch (Exception ex)
{
ilr.StatusCode = 1;
ilr.senderror = 1;
ilr.response = ex.Message;
return Content(HttpStatusCode.InternalServerError, ilr);
}
}
}
}