135 lines
5.1 KiB
C#
135 lines
5.1 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace BeWoDatabaseTerminator.Utils
|
|
{
|
|
public static class FileUtils
|
|
{
|
|
// Wird nach dem Laden der Stati aufgerufen. Enthält also den neuen Status.
|
|
public static void GenerateNewConfigFile(string pathToConfigFile, string connectionString, string tenant, bool isDeactivated = false)
|
|
{
|
|
var config = pathToConfigFile;
|
|
|
|
if(isDeactivated)
|
|
{
|
|
config += ".deactivated";
|
|
}
|
|
|
|
var configText =
|
|
"<?xml version=\"1.0\"?>\r\n" +
|
|
"<configuration>\r\n\t" +
|
|
"<configSections>\r\n\t\t" +
|
|
"<section name=\"hibernate-configuration\" type=\"NHibernate.Cfg.ConfigurationSectionHandler, NHibernate\" />\r\n\t" +
|
|
"</configSections>\r\n\t" +
|
|
"<hibernate-configuration xmlns=\"urn:nhibernate-configuration-2.2\">\r\n\t\t" +
|
|
"<session-factory>\r\n\t\t\t" +
|
|
"<property name=\"connection.provider\">\r\n\t\t\t\t" +
|
|
"NHibernate.Connection.DriverConnectionProvider\r\n\t\t\t" +
|
|
"</property>\r\n\t\t\t" +
|
|
"<property name=\"connection.driver_class\">\r\n\t\t\t\t" +
|
|
"NHibernate.Driver.MySqlDataDriver\r\n\t\t\t" +
|
|
"</property>\r\n\t\t\t" +
|
|
"<property name=\"connection.connection_string\">\r\n\t\t\t\t" +
|
|
$"{connectionString};Initial Catalog={tenant}\r\n\t\t\t" +
|
|
"</property>\r\n\t\t\t" +
|
|
"<property name=\"dialect\">\r\n\t\t\t\t" +
|
|
"NHibernate.Dialect.MySQLDialect\r\n\t\t\t" +
|
|
"</property>\r\n\t\t\t" +
|
|
"<property name=\"proxyfactory.factory_class\">\r\n\t\t\t\t" +
|
|
"NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle\r\n\t\t\t" +
|
|
"</property>\r\n\t\t\t" +
|
|
"<property name=\"hbm2ddl.keywords\">none</property>\r\n\t\t\t" +
|
|
"<property name=\"show_sql\">true</property>\r\n\t\t\t" +
|
|
"<property name=\"format_sql\">true</property>\r\n\t\t\t" +
|
|
"<mapping assembly=\"BeWo.Data\" />\r\n\t\t" +
|
|
"</session-factory>\r\n\t" +
|
|
"</hibernate-configuration>\r\n" +
|
|
"</configuration>";
|
|
|
|
File.WriteAllText(config, configText);
|
|
}
|
|
|
|
public static void GenerateNewDBAdminFile(string pathToFile, string tenant, string port, string username, string password, bool isDeactivated = false)
|
|
{
|
|
//
|
|
//Mittelpunkt GbR
|
|
//cb@ownsoft.de
|
|
//localhost
|
|
//2206
|
|
//5564670353
|
|
//BeWoUser
|
|
//BeWoPl@ner!
|
|
|
|
var config = pathToFile;
|
|
|
|
if (isDeactivated)
|
|
{
|
|
config += ".deactivated";
|
|
}
|
|
|
|
var text =
|
|
$"{tenant}\r\n" +
|
|
"cb@ownsoft.de\r\n" +
|
|
"localhost\r\n" +
|
|
$"{port}\r\n" +
|
|
$"{tenant}\r\n" +
|
|
$"{username}\r\n" +
|
|
$"{password}\r\n";
|
|
|
|
File.WriteAllText(pathToFile, text);
|
|
}
|
|
|
|
public static void WriteLog(string message, string logFilePath)
|
|
{
|
|
if(string.IsNullOrEmpty(logFilePath) || !Directory.Exists(logFilePath))
|
|
{
|
|
throw new ArgumentException("Der Pfad zum Log-Verzeichnis ist ungültig!");
|
|
}
|
|
|
|
var lineToLog = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: {message}";
|
|
|
|
Console.WriteLine(lineToLog);
|
|
|
|
if(!Directory.Exists(logFilePath))
|
|
{
|
|
Directory.CreateDirectory(logFilePath);
|
|
}
|
|
|
|
using(var streamWriter = new StreamWriter(Path.Combine(logFilePath, $"Check_{DateTime.Today:yyyy-MM-dd}.log"), true))
|
|
{
|
|
streamWriter.WriteLine(lineToLog);
|
|
}
|
|
}
|
|
|
|
public static void DeleteOldLogs(string logFilePath)
|
|
{
|
|
if(string.IsNullOrEmpty(logFilePath) || !Directory.Exists(logFilePath))
|
|
{
|
|
throw new ArgumentException("Der Pfad zum Log-Verzeichnis ist ungültig!");
|
|
}
|
|
|
|
var files = Directory.EnumerateFiles(logFilePath);
|
|
|
|
foreach(var filePath in files)
|
|
{
|
|
var fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
|
|
var length = fileName.Length;
|
|
|
|
if(length > 10)
|
|
{
|
|
var datePart = fileName.Substring(length - 10);
|
|
|
|
if(DateTime.TryParse(datePart, out var date))
|
|
{
|
|
if(DateTime.Today > date.AddDays(14))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|