619 lines
25 KiB
C#
619 lines
25 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using PDFGenerator.BusinessLayer;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Data.SqlTypes;
|
|
using System.Diagnostics;
|
|
using iTextSharp.text;
|
|
|
|
|
|
|
|
namespace SavePdf
|
|
{
|
|
class Program
|
|
{
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
string arg1 = "";
|
|
string arg2 = "";
|
|
string retInserimento ="";
|
|
|
|
try
|
|
{
|
|
switch (args.Length)
|
|
{
|
|
case 1:
|
|
arg1 = args[0];
|
|
break;
|
|
case 2:
|
|
arg1 = args[0];
|
|
arg2 = args[1];
|
|
break;
|
|
default:
|
|
arg1 = "0";
|
|
break;
|
|
}
|
|
|
|
|
|
string funzione = "SavePdf";
|
|
DateTime thisDate = DateTime.Now;
|
|
Console.WriteLine("Start Process {0}", thisDate);
|
|
// Lettura dati, da DB, relativi ai report generati e necessari alla scrittura su Report Manager
|
|
List<Report> datiReportToC6 = leggiDatiReportToC6();
|
|
|
|
if (datiReportToC6.Count > 0)
|
|
{
|
|
Console.WriteLine(" Report da processare {0}", datiReportToC6.Count);
|
|
try
|
|
{
|
|
// GESTIONE CONTROLLO INTEGRITA' DI TUTTI REPORT PRODOTTI
|
|
//Console.WriteLine("Totale Report in controllo {0}", datiReportToC6.Count);
|
|
//datiReportToC6 = checkFileCorrotti(datiReportToC6);
|
|
thisDate = DateTime.Now;
|
|
// Lettura dei report in formato PDF e registrazione nel campo img della classe Report
|
|
//datiReportToC6 = leggiFileReport(datiReportToC6);
|
|
//thisDate = DateTime.Now;
|
|
//Console.WriteLine(" file lettura file Process {0}", thisDate);
|
|
//if (datiReportToC6 == null)
|
|
// throw new Exception();
|
|
//////scrittura report in Report Manager
|
|
retInserimento = insertIntoDB(datiReportToC6);
|
|
|
|
if (retInserimento != "")
|
|
throw new Exception();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(retInserimento + ex.Message);
|
|
}
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("nessun report da elaborare");
|
|
}
|
|
|
|
thisDate = DateTime.Now;
|
|
Console.WriteLine("End Process {0}", thisDate);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
Console.WriteLine(" press any key to close the windows");
|
|
Console.ReadLine();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static List<Report> leggiFileReport(List<Report> datiReportToC6)
|
|
{
|
|
List<Report> dati = new List<Report>();
|
|
string dirpathZip = UtilityManager.getAppSetting("PathZip");
|
|
string nomeFile = string.Empty;
|
|
try
|
|
{
|
|
|
|
foreach (Report report in datiReportToC6)
|
|
{
|
|
nomeFile=report.IdReport + "_" + report.NomeFile + ".pdf";
|
|
try
|
|
{
|
|
|
|
using (FileStream fs = new FileStream(dirpathZip + nomeFile, FileMode.Open, FileAccess.Read))
|
|
{
|
|
byte[] pdfDataBinary = new byte[fs.Length];
|
|
fs.Read(pdfDataBinary, 0, System.Convert.ToInt32(fs.Length));
|
|
report.img = pdfDataBinary;
|
|
}
|
|
|
|
dati.Add(report);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dati;
|
|
|
|
}
|
|
catch ( Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static Report leggiSingoloFileReport(Report report)
|
|
{
|
|
List<Report> dati = new List<Report>();
|
|
string dirpathZip = UtilityManager.getAppSetting("PathZip");
|
|
string nomeFile = string.Empty;
|
|
try
|
|
{
|
|
|
|
|
|
nomeFile = report.IdentityReport + "_" + report.NomeFile + ".pdf";
|
|
try
|
|
{
|
|
|
|
using (FileStream fs = new FileStream(dirpathZip + nomeFile, FileMode.Open, FileAccess.Read))
|
|
{
|
|
byte[] pdfDataBinary = new byte[fs.Length];
|
|
fs.Read(pdfDataBinary, 0, System.Convert.ToInt32(fs.Length));
|
|
report.img = pdfDataBinary;
|
|
}
|
|
|
|
//dati.Add(report);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
return report;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
private static List<Report> leggiDatiReportToC6()
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
|
|
string queryLeggiReportFromC6 = UtilityManager.getAppSetting("LeggiReportFromC6");
|
|
Report reportC6 = null;
|
|
List<Report> datiReportC6 = new List<Report>();
|
|
|
|
using (IDataReader drReportC6 = DataAccess.ExecuteDataReaderStoredProcedure(DBProvider.SqlServerStampeC6, queryLeggiReportFromC6, null))
|
|
{
|
|
while (drReportC6.Read())
|
|
{
|
|
reportC6 = new Report();
|
|
reportC6.codiceContratto = drReportC6["codiceContratto"].ToString();
|
|
reportC6.rete = drReportC6["rete"].ToString();
|
|
reportC6.codiceFiscale = drReportC6["codiceFiscale"].ToString();
|
|
reportC6.nomeCliente = drReportC6["nomeCliente"].ToString();
|
|
reportC6.cognomeCliente = drReportC6["cognomeCliente"].ToString();
|
|
reportC6.NomeFile = drReportC6["nomefile"].ToString();
|
|
reportC6.TipoReport = drReportC6["TipoReport"].ToString();
|
|
reportC6.IdReport = Convert.ToInt32(drReportC6["idReport"].ToString());
|
|
switch (reportC6.TipoReport)
|
|
{
|
|
case "D":
|
|
reportC6.descrizioneTipoReport = "Diagnosi";
|
|
reportC6.idReportDiagnosi = reportC6.IdReport;
|
|
break;
|
|
case "M":
|
|
reportC6.descrizioneTipoReport = "Monitoraggio";
|
|
reportC6.idReportMonitoraggio = reportC6.IdReport;
|
|
break;
|
|
default:
|
|
reportC6.descrizioneTipoReport = "";
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
reportC6.codAgente = drReportC6["codAgente"].ToString();
|
|
reportC6.nomeAgente = drReportC6["nomeAgente"].ToString();
|
|
reportC6.cognomeAgente = drReportC6["cognomeAgente"].ToString();
|
|
reportC6.numeroPagine = (int)drReportC6["numeroPagine"];
|
|
reportC6.idZip = (int)drReportC6["id_Fk_Zip"];
|
|
|
|
reportC6.IdentityReport=(Int32) drReportC6["identityreport"];
|
|
if (reportC6.IdReport == reportC6.IdentityReport)
|
|
reportC6.IdReport = 0;
|
|
//reportC6.idReportDiagnosi=(int)drReportC6["idReportDiagnosi"];
|
|
//reportC6.idReportMonitoraggio=(int)drReportC6["idReportMonitoraggio"];
|
|
datiReportC6.Add(reportC6);
|
|
}
|
|
|
|
}
|
|
return datiReportC6;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static List<Report> checkFileCorrotti(List<Report> datiReportToC6)
|
|
{
|
|
List<Report> dati = new List<Report>();
|
|
string dirpathZip = UtilityManager.getAppSetting("PathZip");
|
|
string nomeFile = string.Empty;
|
|
try
|
|
{
|
|
//dirpathZip = "\\10.14.152.11\\e$\\ReportisticaTrimestrale\\PdftoDb\\";
|
|
|
|
//Cosa bruttissima Main alle 5.00 AM sembra unchecked paradiso
|
|
if (File.Exists("Log\\log.txt"))
|
|
File.Delete("Log\\log.txt");
|
|
|
|
using (StreamWriter w = File.AppendText( "Log\\log.txt"))
|
|
{
|
|
|
|
DateTime thisDate = DateTime.Now;
|
|
w.WriteLine("Start Process {0} per {1} report ", thisDate,datiReportToC6.Count);
|
|
foreach (Report report in datiReportToC6)
|
|
{
|
|
nomeFile = report.IdReport + "_" + report.NomeFile + ".pdf";
|
|
//nomeFile = dirpathZip + "1398_306281CO102_20150309.pdf";
|
|
//nomeFile = dirpathZip + report.IdReport + "_" + report.NomeFile + ".pdf";
|
|
//nomeFile = "21480_084023CO206_20150419.pdf";
|
|
try
|
|
{
|
|
|
|
iTextSharp.text.pdf.PdfReader r = new iTextSharp.text.pdf.PdfReader(dirpathZip + nomeFile);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("File Corrotto {0} in Zip {1}", nomeFile, report.idZip);
|
|
w.WriteLine("File Corrotto {0} in Zip {1}", nomeFile, report.idZip);
|
|
}
|
|
|
|
}
|
|
|
|
thisDate = DateTime.Now;
|
|
w.WriteLine("End Process {0}", thisDate);
|
|
}
|
|
|
|
|
|
|
|
|
|
/* test FileINFo
|
|
DirectoryInfo dirInfo = new DirectoryInfo(dirpathZip);
|
|
foreach (FileInfo fi in dirInfo.GetFiles("*.pdf"))
|
|
{
|
|
int endIndex = fi.Name.IndexOf(".pdf") ;
|
|
string nomefile =fi.Name.Substring(0,endIndex);
|
|
//Console.WriteLine(codiceContratto);
|
|
// Fc ToDo
|
|
//Step 1
|
|
//Datatable datiReportC6=leggiDatiReportC6(nomiFile);
|
|
//Step 3
|
|
//retIdReportSei=InserisciReportSei(datiReportC6);
|
|
//Step 3
|
|
//aggiornaReportC6(reIdReportSei);
|
|
|
|
|
|
}
|
|
*/
|
|
|
|
|
|
return dati;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string insertIntoDB(List<Report> datiReport)
|
|
{
|
|
|
|
string errorMsg = "";
|
|
try
|
|
{
|
|
string saveToDisk = UtilityManager.getAppSetting("SavePDFtoDISK");
|
|
switch (saveToDisk)
|
|
{
|
|
case "1":
|
|
return "";
|
|
case "2":
|
|
return "";
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
string queryInserimento = UtilityManager.getAppSetting("insertReportToRM");
|
|
string queryAggiornaIdReport = UtilityManager.getAppSetting("aggiornaIdReportC6");
|
|
int retIdReport = 0;
|
|
|
|
errorMsg = "";
|
|
using (StreamWriter w = File.AppendText("Log\\logInsertReportManager.txt"))
|
|
{
|
|
w.WriteLine("***************************************************************************************");
|
|
w.WriteLine("Start scrittura Report {0}",DateTime.Now);
|
|
w.WriteLine(" Report Letti da Scrivere su Report Manager {0}", datiReport.Count);
|
|
int contReportScritti = 0;
|
|
string nomefile;
|
|
string dirpathZip = UtilityManager.getAppSetting("PathZip");
|
|
Report reportIMG = new Report();
|
|
foreach (Report report in datiReport)
|
|
{
|
|
|
|
reportIMG=null;
|
|
reportIMG=leggiSingoloFileReport(report);
|
|
|
|
if (reportIMG != null)
|
|
{
|
|
try
|
|
{
|
|
#region scritturaDB
|
|
List<Parametro> listaParametri = new List<Parametro>();
|
|
|
|
Parametro CodiceContratto = new Parametro();
|
|
CodiceContratto.ParameterName = "CodiceContratto";
|
|
CodiceContratto.DbType = DbType.String;
|
|
CodiceContratto.Value = reportIMG.codiceContratto;
|
|
listaParametri.Add(CodiceContratto);
|
|
|
|
Parametro Rete = new Parametro();
|
|
Rete.ParameterName = "Rete";
|
|
Rete.DbType = DbType.String;
|
|
Rete.Value = reportIMG.rete;
|
|
listaParametri.Add(Rete);
|
|
|
|
Parametro idCliente = new Parametro();
|
|
idCliente.ParameterName = "CodiceFiscale";
|
|
idCliente.DbType = DbType.String;
|
|
idCliente.Value = reportIMG.codiceFiscale;
|
|
listaParametri.Add(idCliente);
|
|
|
|
Parametro NomeCliente = new Parametro();
|
|
NomeCliente.ParameterName = "NomeCliente";
|
|
NomeCliente.DbType = DbType.String;
|
|
NomeCliente.Value = reportIMG.nomeCliente;
|
|
listaParametri.Add(NomeCliente);
|
|
|
|
Parametro CognomeCliente = new Parametro();
|
|
CognomeCliente.ParameterName = "CognomeCliente";
|
|
CognomeCliente.DbType = DbType.String;
|
|
CognomeCliente.Value = reportIMG.cognomeCliente;
|
|
listaParametri.Add(CognomeCliente);
|
|
|
|
Parametro utente = new Parametro();
|
|
utente.ParameterName = "TipoReport";
|
|
utente.DbType = DbType.String;
|
|
utente.Value = reportIMG.descrizioneTipoReport;
|
|
listaParametri.Add(utente);
|
|
|
|
|
|
Parametro codReport = new Parametro();
|
|
codReport.ParameterName = "codReport";
|
|
codReport.DbType = DbType.Int32;
|
|
codReport.Value = reportIMG.TipoReport == "D" ? 5 : 4;
|
|
listaParametri.Add(codReport);
|
|
|
|
|
|
Parametro img = new Parametro();
|
|
img.ParameterName = "img";
|
|
img.DbType = DbType.Binary;
|
|
img.Value = reportIMG.img;
|
|
listaParametri.Add(img);
|
|
|
|
Parametro CodicePB = new Parametro();
|
|
CodicePB.ParameterName = "CodicePB";
|
|
CodicePB.DbType = DbType.String;
|
|
CodicePB.Value = reportIMG.codAgente;
|
|
listaParametri.Add(CodicePB);
|
|
|
|
Parametro NomePB = new Parametro();
|
|
NomePB.ParameterName = "NomePB";
|
|
NomePB.DbType = DbType.String;
|
|
NomePB.Value = reportIMG.nomeAgente;
|
|
listaParametri.Add(NomePB);
|
|
|
|
Parametro CognomePB = new Parametro();
|
|
CognomePB.ParameterName = "CognomePB";
|
|
CognomePB.DbType = DbType.String;
|
|
CognomePB.Value = reportIMG.cognomeAgente;
|
|
listaParametri.Add(CognomePB);
|
|
|
|
Parametro NumeroPagine = new Parametro();
|
|
NumeroPagine.ParameterName = "NumeroPagine";
|
|
NumeroPagine.DbType = DbType.Int32;
|
|
NumeroPagine.Value = reportIMG.numeroPagine;
|
|
listaParametri.Add(NumeroPagine);
|
|
|
|
|
|
Parametro Identificativo = new Parametro();
|
|
Identificativo.ParameterName = "Identificativo";
|
|
Identificativo.DbType = DbType.Int32;
|
|
// In Fase di Inserimento Nuovo Report ID =0
|
|
// In Fase di Aggiornamento report esistente ID <> 0
|
|
// ToDo Gestione Ristampa Report
|
|
Identificativo.Value = reportIMG.IdReport;
|
|
listaParametri.Add(Identificativo);
|
|
|
|
// La Sp di inserimento riport in Report Manager restituisci un identiti IdReport che va ad aggiornare il temporaneo
|
|
// idReport su Gestione_Pdf_Ftp
|
|
errorMsg = "Scrittura su Report Manager : ";
|
|
retIdReport = Convert.ToInt32(DataAccess.ExecuteScalarStoredProcedure(DBProvider.SqlServer, queryInserimento, listaParametri));
|
|
|
|
if (retIdReport > 0)
|
|
{
|
|
|
|
List<Parametro> Parametri = new List<Parametro>();
|
|
|
|
Parametro idReportC6 = new Parametro();
|
|
idReportC6.ParameterName = "IdReportC6";
|
|
idReportC6.DbType = DbType.Int32;
|
|
idReportC6.Value = reportIMG.IdReport;
|
|
Parametri.Add(idReportC6);
|
|
|
|
Parametro IdReportManager = new Parametro();
|
|
IdReportManager.ParameterName = "IdReportManager";
|
|
IdReportManager.DbType = DbType.Int32;
|
|
IdReportManager.Value = retIdReport;
|
|
Parametri.Add(IdReportManager);
|
|
|
|
errorMsg = "Aggiornamento IdReport su ReportC6 : ";
|
|
DataAccess.ExecuteScalarStoredProcedure(DBProvider.SqlServerStampeC6, queryAggiornaIdReport, Parametri);
|
|
}
|
|
#endregion
|
|
contReportScritti++;
|
|
if (contReportScritti % 1000 == 0)
|
|
{
|
|
Console.WriteLine(" Report Scritti su Report Manager {0} - {1}", contReportScritti,DateTime.Now);
|
|
w.WriteLine(" Report Scritti su Report Manager {0} - {1}", contReportScritti,DateTime.Now);
|
|
}
|
|
else if (contReportScritti == datiReport.Count)
|
|
{
|
|
w.WriteLine("Totale Report Scritti Report Manager {0} ", contReportScritti);
|
|
}
|
|
|
|
|
|
}
|
|
catch
|
|
{
|
|
w.WriteLine("Errore Scrittura Report {0} Cliente {1} Rete{2}", reportIMG.IdReport, reportIMG.codiceFiscale, reportIMG.rete);
|
|
}
|
|
}
|
|
}
|
|
w.WriteLine("End Process {0}", DateTime.Now);
|
|
w.WriteLine("***************************************************************************************");
|
|
}
|
|
return "";
|
|
}
|
|
|
|
catch ( DataBaseException dbEx)
|
|
{
|
|
// ToDo
|
|
// gestione errore
|
|
errorMsg = errorMsg + getDataBaseException(dbEx);
|
|
return errorMsg;
|
|
}
|
|
catch ( Exception ex)
|
|
{
|
|
|
|
errorMsg = errorMsg + ex.Message;
|
|
return errorMsg;
|
|
}
|
|
|
|
}
|
|
private static string getDataBaseException(DataBaseException ecc)
|
|
{
|
|
const string aCapo = "#/n#";
|
|
string ritorno = "";
|
|
ritorno += aCapo;
|
|
ritorno += " Informazioni Comando SQL:" + aCapo;
|
|
ritorno += " sql file name: " + ecc.SqlFileName + aCapo;
|
|
ritorno += " connection string: " + ecc.ConnectionStringWithoutCredentials + aCapo;
|
|
//ritorno += " command text: " + ecc.CommandText + aCapo;
|
|
//controllo se ci sono i parametri
|
|
if (ecc.Parameters != null)
|
|
{
|
|
foreach (Parametro parametro in ecc.Parameters)
|
|
{
|
|
ritorno += aCapo;
|
|
ritorno += " parametro: " + parametro.ParameterName + aCapo;
|
|
ritorno += " dimensione: " + parametro.Size.ToString() + aCapo;
|
|
ritorno += " direzione: " + parametro.Direction.ToString() + aCapo;
|
|
ritorno += " tipo: " + parametro.DbType.ToString() + aCapo;
|
|
|
|
if (parametro.Value == null)
|
|
ritorno += " valore: null" + aCapo;
|
|
else
|
|
{
|
|
string valoreParametro = "";
|
|
if (parametro.Size > 500)
|
|
{
|
|
valoreParametro = " valore: null(sono visualizzati solo i primi 500 bytes): " + parametro.Value.ToString().Substring(0, 500);
|
|
}
|
|
else
|
|
{
|
|
valoreParametro = " valore: " + parametro.Value.ToString();
|
|
}
|
|
ritorno += valoreParametro + aCapo;
|
|
}
|
|
}
|
|
}
|
|
ritorno += aCapo;
|
|
ritorno += " tipo: DataBaseException" + aCapo;
|
|
ritorno += " descrizione: " + ecc.Eccezione.Message + aCapo;
|
|
ritorno += " sorgente: " + ecc.Eccezione.Source + aCapo;
|
|
ritorno += " traccia: " + FormattaErrore(ecc.StackTrace) + aCapo;
|
|
|
|
return ritorno;
|
|
|
|
}
|
|
private static string FormattaErrore(string errore)
|
|
{
|
|
const string aCapo = "#/n#";
|
|
const string spazio = " ";
|
|
string separatore1 = " at ";
|
|
string separatore2 = " in ";
|
|
string separatore3 = ":line ";
|
|
string erroreInterno = errore.Replace(separatore1, "@");
|
|
string ritorno = aCapo;
|
|
|
|
|
|
//nome metodo e tutto il resto
|
|
string[] messaggioErrore = erroreInterno.Split('@');
|
|
string messaggioSenzaSpazi;
|
|
string[] messaggioInterno;
|
|
|
|
foreach (string messaggio in messaggioErrore)
|
|
{
|
|
messaggioSenzaSpazi = messaggio.Trim();
|
|
|
|
if (messaggioSenzaSpazi != "")
|
|
{
|
|
messaggioSenzaSpazi = messaggioSenzaSpazi.Replace(separatore2, "@");
|
|
messaggioInterno = messaggioSenzaSpazi.Split('@');
|
|
if (messaggioInterno.GetUpperBound(0) == 1)
|
|
{
|
|
ritorno += spazio + "metodo: " + messaggioInterno[0] + aCapo;
|
|
string fileLineaSingolo = messaggioInterno[1].Replace(separatore3, "@");
|
|
string[] fileLinea = fileLineaSingolo.Split('@');
|
|
ritorno += spazio + "percorsoFile: " + fileLinea[0] + aCapo;
|
|
ritorno += spazio + "linea: " + fileLinea[1] + aCapo;
|
|
ritorno += aCapo;
|
|
}
|
|
else
|
|
{
|
|
ritorno += spazio + "metodo: " + messaggioInterno[0] + aCapo;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|