2025-04-15 12:10:19 +02:00

301 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Collections.Specialized;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.IO;
using System.Data.SqlClient;
namespace RestorePDF
{
class Program
{
static int Main(string[] args)
{
string ambiente = "SVILUPPO";
string tngServer = "";
string tngJobset = "";
string tngJob = "";
// Metodo utilizzato per la sostituzione di report su DB ReporManager
// solo ed esclusivamente in caso di anomalia segnalata da Ticktes
//insertRecuperoPDF();
//return 1;
if (args.Length == 3)
{
ambiente = UtilityManager.getAppSetting("ambiente");
tngServer = args[0];
tngJobset = args[1];
tngJob = args[2];
}
return MainOld(ambiente, tngServer, tngJobset, tngJob);
}
static int MainOld(string ambiente,string tngServer,string tngJobset,string tngJob)
{
//string ambiente = ambiente;
string cawTo_Bin = UtilityManager.getAppSetting("cawTo_Bin");
//string tngServer = args[0];
//string tngJobset = args[1];
//string tngJob = args[2];
// MZ: Ask Valerio about the function name to be called from TNG
string funzione = UtilityManager.getAppSetting("TNGFunction");
string fac_I_100 = UtilityManager.getAppSetting("fac_I_100");
string fac_T_100 = UtilityManager.getAppSetting("fac_T_100");
string fac_E_100 = UtilityManager.getAppSetting("fac_E_100");
string argomentiCawToInizio = "-n " + tngServer + " " + fac_I_100 + " " + tngJobset + " - " + tngJob + " - Funzione " + funzione + " INIZIATA";
string argomentiCawToTerminato = "-n " + tngServer + " " + fac_T_100 + " " + tngJobset + " - " + tngJob + " - Funzione " + funzione + " terminata con SUCCESSO";
string argomentiCawToErrore = "-n " + tngServer + " " + fac_E_100 + " " + tngJobset + " - " + tngJob + " - Funzione "; // +funzione + " terminata con ERRORE";
try
{
//MZ: check with Valerio if it is ok. (it's ok! remove the comment in release)
//notify TNG that the sw started after TNG's trigger
StartTng(ambiente, cawTo_Bin, argomentiCawToInizio);
Start();
DeleteFilesFromFileSystem();
//V notify TNG that the sw finished its computation
StartTng(ambiente, cawTo_Bin, argomentiCawToTerminato);
return 0;
}
catch (DataBaseException ex)
{
try
{
funzione = "ScriviErroreNelDB";
UtilityManager.scriviErroreNelDB(-292929, "Errore nel Gestore PDF: " + UtilityManager.getDataBaseException(ex), -292929, "Restore PDF", "Restore PDF");
funzione = "Restore PDF";
}
catch (Exception exc) {
funzione += "-ERRORE-" + exc.Message + "-ERRORE-";
}
argomentiCawToErrore += funzione + " terminata con ERRORE";
//V needed in order to notify tng the error
StartTng(ambiente, cawTo_Bin, argomentiCawToErrore);
return 1;
}
catch (Exception ex)
{
try
{
funzione = "ScriviErroreNelDB";
UtilityManager.scriviErroreNelDB(-292929, "Errore nel Gestore PDF: " + UtilityManager.getException(ex), -292929, "Restore PDF", "Restore PDF");
funzione = "GESTORE PDF";
}catch (Exception exc){
funzione += "-ERRORE-" + exc.Message + "-ERRORE-";
}
argomentiCawToErrore += funzione + " terminata con ERRORE";
StartTng(ambiente, cawTo_Bin, argomentiCawToErrore);
return 1;
}
}
private static void Start()
{
string path = UtilityManager.getAppSetting("RestoreSource_Folder"); ;
string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
FileStream fs;
if (f.IndexOf(".pdf") > 0)
{
fs = new FileStream(f, FileMode.OpenOrCreate, FileAccess.Read);
byte[] pdfData = new byte[fs.Length];
fs.Read(pdfData, 0, System.Convert.ToInt32(fs.Length));
// MyData object is holding the pdf file bytes.
// Insert the file into the DB.
//i file sono memorizzati con l'id che hanno nella tabella dei metadati es 145.pdf
string f2 = f.Replace(path + "\\", "");
int idMeta = Convert.ToInt32(f2.Replace(".pdf", ""));
try
{
InsertIntoDB(pdfData,idMeta);
AggiornaEvasione(idMeta);
}
catch (DataBaseException ex)
{
Console.WriteLine(ex.Message);
UtilityManager.scriviErroreNelDB(-292929, "Errore nel RestorePDF: " + UtilityManager.getDataBaseException(ex), -292929, "Restore PDF", "Restore PDF");
string message = ex.Message;
//message += "Cliente: " + _dataThread.CodiceFiscale + "; PB_Rete: " + _dataThread.Rete + _dataThread.Agente;
Exception newThrownexception = new Exception(message);
throw newThrownexception;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
UtilityManager.scriviErroreNelDB(-292929, "Errore nel RestorePDF: " + UtilityManager.getException(ex), -292929, "Restore PDF", "Restore PDF");
string message = ex.Message;
//message += "Cliente: " + _dataThread.CodiceFiscale + "; PB_Rete: " + _dataThread.Rete + _dataThread.Agente;
Exception newThrownexception = new Exception(message);
throw newThrownexception;
}
finally
{
fs.Close();
}
}
}
}
private static void AggiornaEvasione(int idMeta)
{
DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerStampeC6, "UPDATE_EVASIONE", new List<Parametro>
{
new Parametro
{
ParameterName = "idMetadato",
DbType = DbType.Int32,
Value = idMeta
}
});
}
private static void InsertIntoDB(byte[] pdfData,int idMeta)
{
List<Parametro> listaParametri = new List<Parametro>();
Parametro idMe = new Parametro();
idMe.ParameterName = "idMetadato";
idMe.DbType = DbType.Int32;
idMe.Value = idMeta;
listaParametri.Add(idMe);
Parametro img = new Parametro();
img.ParameterName = "img";
img.DbType = DbType.Binary;
img.Value = pdfData;
listaParametri.Add(img);
// MZ: I used "C6_S_WritePDFintoDB_Periodico" temporary
DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServer, "HIST_INSERTPDFRESTORATO", listaParametri);
}
private static void StartTng(string ambiente, string cawTo_Bin, string argomentiCaw)
{
if (!ambiente.Equals("SVILUPPO"))
{
Process processTng = new Process();
try
{
processTng = Process.Start(cawTo_Bin, argomentiCaw);
while (!processTng.HasExited)
;
}
catch (Exception ex)
{
throw new Exception("cawTo_Bin: Non riesco ad eseguire " + cawTo_Bin + " sulla macchina " + System.Environment.MachineName + ":" + ex.Message);
}
finally
{
processTng.Close();
processTng.Dispose();
}
}
}
private static void DeleteFilesFromFileSystem()
{
string path = UtilityManager.getAppSetting("RestoreSource_Folder"); ;
string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
//FileStream fs;
if (f.IndexOf(".pdf") > 0)
{
File.Delete(f);
//fs = new FileStream(f, FileMode.OpenOrCreate, FileAccess.Read);
}
}
}
private static void insertRecuperoPDF()
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "Data Source=10.14.152.12\\I1;Database=SEIReport;User ID=SEIReport;Password=SEIReport;Trusted_Connection=false;";
try
{
//string path = UtilityManager.getAppSetting("RestoreSource_Folder"); ;
//string[] files = Directory.GetFiles(path);
int idMeta;
string[] files = Directory.GetFiles("C:\\recuperoPDF");
//apro connessione una sola volta
sqlconn.Open();
foreach (string f in files)
{
FileStream fs;
if (f.IndexOf(".pdf") > 0)
{
fs = new FileStream(f, FileMode.OpenOrCreate, FileAccess.Read);
byte[] pdfData = new byte[fs.Length];
fs.Read(pdfData, 0, System.Convert.ToInt32(fs.Length));
// MyData object is holding the pdf file bytes.
// Insert the file into the DB.
//i file sono memorizzati con l'id che hanno nella tabella dei metadati es 145.pdf
string f2 = f.Replace("C:\\recuperoPDF\\", "");
idMeta = Convert.ToInt32(f2.Replace(".pdf", ""));
SqlCommand cmd = new SqlCommand("UPDATE [SEIReport].[dbo].[C6ReportFisico] SET [Image] = @ImageData WHERE identificativo = " + idMeta, sqlconn);
SqlParameter par = cmd.Parameters.Add("@ImageData", SqlDbType.Image);
par.Value = pdfData;
cmd.ExecuteNonQuery();
}
}
sqlconn.Close();
}
catch (Exception ex)
{
Console.WriteLine("\nErrore restore ---", ex.Message);
string message = ex.Message;
sqlconn.Close();
}
}
}
}