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.

204 lines
7.5 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
namespace OnDoc_ArchivierungBatch
{
internal class Program
{
private static string connectionstring { get; set; } = "";
private static DataSet dsdaten = new DataSet();
private static DataTable SP_Parameters = new DataTable();
private static int total = 0;
private static int ok = 0;
private static int error = 0;
private static int docerror = 0;
static void Main(string[] args)
{
write_log("Start");
run();
write_log("Total Dokumente:" + total.ToString());
write_log("Total OK: "+ok.ToString());
write_log("Total Fehler: "+ error.ToString());
write_log("Ende");
}
private static void write_log(string entry)
{
File.AppendAllText(Properties.Settings.Default.logfile, DateTime.Now.ToString() + " - " + entry + Environment.NewLine);
}
private static void archivedoc(string documentid)
{
write_log(documentid);
string URL = Properties.Settings.Default.RESTURI + "api/ArchiveDocFromDatabase?dokumentid=" + documentid;
HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Headers["Authorization"] = "Bearer " + Properties.Settings.Default.APIKey;
try
{
using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseContent = reader.ReadToEnd();
if (responseContent.Contains("Hinweismeldung:"))
{
write_log("Die Archivierung des Dokumentes " + documentid + " wurde nicht durchgeführt:" + Environment.NewLine + responseContent);
error = error + 1;
docerror = 1;
return;
}
ok = ok + 1;
}
else
{
write_log("Die Archivierung des Dokumentes " + documentid + " wurde abgebrochen." + Environment.NewLine + "Fehler: " + response.StatusDescription);
error = error + 1;
docerror = 1;
}
}
}
catch (Exception ex)
{
write_log("Die Archivierung des Dokumentes " + documentid + " konnte nicht durchgeführt werden." + Environment.NewLine + "Fehler: " + ex.Message);
error = error + 1;
docerror = 1;
}
}
public static void Exec_SQL(string sql)
{
SqlConnection sqlconnect = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlconnect;
sqlconnect.ConnectionString = Properties.Settings.Default.Connectionstring;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = sql;
sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();
}
private static void run()
{
connectionstring = Properties.Settings.Default.Connectionstring;
Get_Tabledata("Select dokumentid from dokument where automatischearchivierung=1 and dbo.fnkt_check_archived(dokumentid)=0 and dokumentid not in (select dokumentid from dokinbearbeitung where dokumentid=dokument.dokumentid) ",false,true);
foreach (System.Data.DataRow dr in dsdaten.Tables[0].Rows)
{
docerror = 0;
archivedoc(dr[0].ToString());
if (docerror == 0)
{
Exec_SQL("Update dokument set automatischarchiviertam=getdate() where dokumentid='" + dr[0].ToString() + "'");
}
}
}
private static DataTable Get_Tabledata(string Tablename, bool StoredProc = false, bool is_SQL_String = false, DataTable sp_params = null)
{
try
{
if (sp_params == null && SP_Parameters.Rows.Count > 0)
{
sp_params = SP_Parameters.Copy();
}
SqlConnection sqlconnect = new SqlConnection();
DataSet ds = new DataSet();
ds.Tables.Clear();
dsdaten.Tables.Clear();
sqlconnect.ConnectionString = connectionstring;
SqlDataAdapter da = new SqlDataAdapter("", sqlconnect);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = sqlconnect;
if (StoredProc == true)
{
sqlcmd.CommandType = CommandType.StoredProcedure;
if (Tablename.IndexOf("@@Mandantnr@@") > 0)
Tablename = Tablename.Replace("@@Mandantnr@@", "");
sqlcmd.CommandText = Tablename;
try
{
foreach (DataRow r in sp_params.Rows)
{
sqlcmd.Parameters.Add(r["Paramname"].ToString(), SqlDbType.VarChar);
sqlcmd.Parameters[sqlcmd.Parameters.Count - 1].Value = r["Paramvalue"].ToString();
}
}
catch { };
}
else
{
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = "Select * from " + Tablename;
}
if (is_SQL_String == true)
sqlcmd.CommandText = Tablename;
da.SelectCommand = sqlcmd;
sqlconnect.Open();
da.Fill(dsdaten, "Daten1");
sqlconnect.Close();
return dsdaten.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
private static void sendmail(string empfaenger, string betreff, string message)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add(empfaenger);
mail.From = new MailAddress("OnDoc@tkb.ch");
mail.Subject = Properties.Settings.Default.prefix_betreff + betreff;
mail.IsBodyHtml = true;
mail.Body = message;
SmtpServer.Host = "smtp.tgcorp.ch";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
write_log("Mailversand an " + empfaenger + " nicht erfolgt" );
}
catch (Exception ex)
{
write_log("Mailversand an " + empfaenger + " nicht erfolgt");
}
mail = null;
SmtpServer = null;
}
}
}