using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using SteamWare;
using Microsoft.Reporting.WinForms;
using System.Drawing.Imaging;
using System.Drawing.Printing;
///
/// Classe che si occupa di stampare report da reportViewer via printer remota
///
public class reportPrinter
{
#region area codice da non modificare
private int m_currentPageIndex;
private IList m_streams;
///
/// ciclo da fornire al renderizzatore dei report, per salvare 1 immagine da ogni pagina del report
///
///
///
///
///
///
///
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new FileStream(SteamWare.IO.fileMover.getFilePath(@".\temp\" + name + "." + fileNameExtension), FileMode.Create);
m_streams.Add(stream);
return stream;
}
///
/// Handler per PrintPageEvents
///
///
///
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
///
/// funzione di stampa...
///
private void Print(string printerName)
{
//const string printerName = "Microsoft Office Document Image Writer";
//const string printerName = "Brother HL-2170W series";
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = printerName;
if (!printDoc.PrinterSettings.IsValid)
{
logger.lg.scriviLog(String.Format("Can't find printer \"{0}\".", printerName), tipoLog.ERROR);
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
///
/// Export del report come EMF (Enhanced Metafile) file.
///
///
private void Export(LocalReport report, string deviceInfo)
{
Warning[] warnings;
m_streams = new List();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams) stream.Position = 0;
}
///
/// dispone l'applicazione e rilascia le risorse
///
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
#endregion
#region area codice da modificare
///
/// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
///
/// report ammessi: ElencoMacchine / RichiestaIntervento
/// oggetto che contiene data inizio e data fine dell'analisi richiesta per il report
/// tabella dati
private DataTable caricaDati(reportRichiesto tipoReport, string idxObj)
{
DataTable tab = new DataTable();
switch (tipoReport)
{
case reportRichiesto.ElencoMacchine:
tab = (DataTable)TA_app.obj.taElencoImpMacch.GetData();
break;
case reportRichiesto.RichiestaIntervento:
tab = (DataTable)TA_app.obj.taInterventiMtz.getByIdx(Convert.ToInt32(idxObj));
break;
default:
break;
}
return tab;
}
///
/// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
///
/// report ammessi: ElencoInterventi / MTBF_MTTR
/// oggetto che contiene data inizio e data fine dell'analisi richiesta per il report
/// tabella dati
private DataTable caricaDati(reportRichiesto tipoReport, intervalloDate periodoAnalizzato, string username)
{
DataTable tab = new DataTable();
switch (tipoReport)
{
case reportRichiesto.ElencoInterventi:
tab = (DataTable)TA_app.obj.taElencoIntFilt.getByIntervDateUsernameFilt(periodoAnalizzato.inizio, periodoAnalizzato.fine, username);
break;
case reportRichiesto.MTBF_MTTR:
tab = (DataTable)TA_app.obj.taElenco_MTBF_MTTR.GetData(periodoAnalizzato.inizio, periodoAnalizzato.fine, username);
break;
default:
break;
}
return tab;
}
///
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante
///
/// report ammessi: ElencoMacchine / RichiestaIntervento
/// nome completo stampante (rispetto al server)
/// numIntMtz (codice numero richiesta)
public void printReport(reportRichiesto tipoReport, string printerName, string parametro)
{
LocalReport report = new LocalReport();
devInfoParam deviceInfo = new devInfoParam("EMF", "21cm", "29.7cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
switch (tipoReport)
{
case reportRichiesto.ElencoMacchine:
report.ReportPath = string.Format(@".\{0}\MacchineReparti.rdlc", memLayer.ML.CRS("ReportBasePath"));
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, parametro)));
deviceInfo = new devInfoParam("EMF", "21cm", "29.7cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
break;
case reportRichiesto.RichiestaIntervento:
report.ReportPath = string.Format(@".\{0}\RichiestaIntervento.rdlc", memLayer.ML.CRS("ReportBasePath"));
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, parametro)));
deviceInfo = new devInfoParam("EMF", "29.7cm", "21cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
break;
default:
break;
}
doEmfPrint(printerName, report, deviceInfo.xmlParam);
}
///
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante
///
/// report ammessi: ElencoInterventi / MTBF_MTTR
/// nome completo stampante (rispetto al server)
/// oggetto che contiene data inizio e data fine dell'analisi richiesta per il report
public void printReport(reportRichiesto tipoReport, string printerName, intervalloDate periodoAnalizzato, string username)
{
LocalReport report = new LocalReport();
devInfoParam deviceInfo = new devInfoParam("EMF", "21cm", "29.7cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
switch (tipoReport)
{
case reportRichiesto.ElencoInterventi:
report.ReportPath = string.Format(@".\{0}\ReportInterventi.rdlc", memLayer.ML.CRS("ReportBasePath"));
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, periodoAnalizzato, username)));
deviceInfo = new devInfoParam("EMF", "29.7cm", "21cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
break;
case reportRichiesto.MTBF_MTTR:
report.ReportPath = string.Format(@".\{0}\Report_MTBF_MTTR.rdlc", memLayer.ML.CRS("ReportBasePath"));
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, periodoAnalizzato, username)));
deviceInfo = new devInfoParam("EMF", "29.7cm", "21cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
// calcolo le ore totali equivalenti...
ReportParameter[] OreTotali = { new ReportParameter("OreTotali", WebGimUtils.obj.oreEquivalenti(periodoAnalizzato).ToString()) };
report.SetParameters(OreTotali);
break;
default:
break;
}
doEmfPrint(printerName, report, deviceInfo.xmlParam);
}
///
/// esegue print vero e proprio
///
///
///
///
private void doEmfPrint(string printerName, LocalReport report, string deviceInfo)
{
// export in EMF
Export(report, deviceInfo);
m_currentPageIndex = 0;
// stampo
Print(printerName);
// do dispose?
Dispose();
}
///
/// oggetto protected
///
///
protected reportPrinter()
{
}
///
/// singleton pubblico
///
public static reportPrinter obj = new reportPrinter();
#endregion
}
///
/// tipologia di report ammessi alla stampa...
///
public enum reportRichiesto
{
ElencoInterventi,
ElencoMacchine,
MTBF_MTTR,
RichiestaIntervento
}