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;
namespace API_NetFramework.Controllers
{
///
///
///
///
///
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 = ConfigurationManager.ConnectionStrings["EDOKAConnectionstring"].ConnectionString;
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(ConfigurationManager.ConnectionStrings["JournalConnectionstring"].ConnectionString);
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 + "'" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "')";
dB.Exec_SQL(sql);
dB = null;
APILogging.Log((HttpRequestMessage)Request, "Ende Updaet IL_Log: DokumentID:" + dokumentid, LogLevelType.Debug);
}
private void update_dokumentstatus(string dokumentid)
{
DB db = new DB(connectionstring);
db.dokument_abschliessen(dokumentid);
db = null;
}
///
/// Archiviert das Dokument aus OnDoc in OnBase
///
///
///
/// Returncode: 200 (OK)
/// Dokumenthandle aus OnBase
///
///
[HttpGet]
[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
{
APILogging.Log((HttpRequestMessage)Request, "Start ArchiveDocFromDatabase: DokumentID:"+DokumentID, LogLevelType.Debug);
DB db = new DB(connectionstring);
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("", "", "");
dok = dg.GetDocAsPDF(DokumentID);
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 = db.dsdaten.Tables[0].Rows[0]["dateityp"].ToString();
var Attribute = new List();
foreach (System.Data.DataRow rw in db.dsdaten.Tables[1].Rows)
{
var p = new OnBaseDocUpload.attribute()
{
fieldname = rw["fieldname"].ToString(),
fieldvalue = rw["fieldvalue"].ToString()
};
Attribute.Add(p);
}
onbasedoc.attributes = Attribute;
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
{
}
}
//[HttpGet]
//[Route("API/ArchiveDocBase64")]
/////
///// Archiviert das als Base64String übergebene Dokument in OnBase
/////
/////
/////
/////
///// Returncode: 200 (OK)
///// Dokumenthandle aus OnBase
/////
/////
////
//public IHttpActionResult ArchivDocBase64(string DokumentID, string Dokumenttyp)
//{
// if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
// {
// return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
// }
// try
// {
// return Ok();
// }
// catch (Exception e)
// {
// APILogging.Log((HttpRequestMessage)Request, e.Message, LogLevelType.Error);
// return Content(HttpStatusCode.InternalServerError, e.Message);
// }
//}
///
/// CheckDocID prüft auf eine vorhandene DokumentID in OnDoc (DokumentID OFFEDK... / Barcode-Klenber-Nr)
///
///
///
/// 200: OK
///
///
[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);
}
}
[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, 6).ToUpper() == "ONDOC00")
{
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);
}
//return Content(HttpStatusCode.OK, "");
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, "");
}
public 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);
}
}
}
}