using Newtonsoft.Json; using SteamWare; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace MagData { public class MagDataLayer { #region oggetti public // reports public DS_ReportTableAdapters.PrintJobQueueTableAdapter taPJQ; public DS_ReportTableAdapters.stp_prt_CartellinoFinitiOdetteTableAdapter taCFOdette; public DS_ReportTableAdapters.stp_prt_CartellinoPedaneTableAdapter taCPed; #endregion #region init classe public MagDataLayer() { // inizializzaizone classe initTA(); setupConnString(); } private void setupConnString() { string connString = memLayer.ML.CRS("NKC_WFConnectionString"); // reports taPJQ.Connection.ConnectionString = connString; taCFOdette.Connection.ConnectionString = connString; taCPed.Connection.ConnectionString = connString; } private void initTA() { // reports taPJQ = new DS_ReportTableAdapters.PrintJobQueueTableAdapter(); taCFOdette = new DS_ReportTableAdapters.stp_prt_CartellinoFinitiOdetteTableAdapter(); taCPed = new DS_ReportTableAdapters.stp_prt_CartellinoPedaneTableAdapter(); } /// /// oggetto static/singleton per fare chiamate sul datalayer /// public static MagDataLayer man = new MagDataLayer(); #endregion #region helpers e utils public static string redQueueConf = "MP-CTRACK:MAG:queueConf"; /// /// Restituisce un array JSon x le conf delle code di stampa leggendo dal file /// /// lista oggetto Json in formato SteamwareSDK.queueConf public List queueConfJson { get { List answ = new List(); string dirPath = HttpContext.Current.Server.MapPath("~/Reports/"); string nomeFile = "queueConf.json"; bool fileExist = fileMover.obj.fileExist(dirPath, nomeFile); if (fileExist) { string rawData = File.ReadAllText($"{dirPath}\\{nomeFile}"); if (!string.IsNullOrEmpty(rawData)) { try { answ = JsonConvert.DeserializeObject>(rawData); } catch (Exception exc) { Log.Instance.Error(exc, "Eccezione in decodifica file conf printQueue"); } } } return answ; } } /// /// Cache redis della conf delle code /// /// lista oggetto Json in formato SteamwareSDK.queueConf public List queueConfRedis { get { List answ = new List(); string redKey = memLayer.ML.redHash(redQueueConf); string rawData = memLayer.ML.getRSV(redKey); if (!string.IsNullOrEmpty(rawData)) { try { answ = JsonConvert.DeserializeObject>(rawData); } catch (Exception exc) { Log.Instance.Error(exc, "Eccezione in recupero dati queueConf da redis"); } } // controllo se vuoto rileggo da file if (answ.Count == 0) { answ = queueConfJson; // a quesot punto salvo in Redis queueConfRedis = queueConfJson; } return answ; } set { string redKey = memLayer.ML.redHash(redQueueConf); string rawData = ""; // salvo inc ache per 1 h... memLayer.ML.setRSV(redKey, rawData, 3600); } } /// /// effettua la stampa di un documento /// /// Codice UNIVOCO del documento /// stampante specifica (da postazione o std da web.config, a cura dell'utente /// IP del chiamante /// public bool stampaDoc(string keyParam, string printQueue, string clientIp) { bool answ = false; // controllo se esista UDC if (checkDoc(printQueue, keyParam)) { answ = reportPrinter.obj.stampaReport(keyParam, printQueue); if (answ) { logger.lg.scriviLog($" | {clientIp} | salvata richiesta stampa su coda {printQueue} | key {keyParam}", tipoLog.INFO); } else { logger.lg.scriviLog($"ERRORE | {clientIp} | richiesta stampa su coda {printQueue} | key {keyParam}", tipoLog.ERROR); } } else { logger.lg.scriviLog($"Impossibile | {clientIp} | richiesta stampa su coda {printQueue} | key {keyParam}", tipoLog.ERROR); } return answ; } /// /// Verifica esistenza record da NOME CODA stampa + chiave... /// /// /// /// private bool checkDoc(string printQueue, string keyParam) { bool answ = false; reportRichiesto tipoReport = reportRichiesto.ND; //cerco in setup queueConf var coda = queueConfRedis.Find(a => a.name == printQueue); if (coda != null) { try { tipoReport = (reportRichiesto)Enum.Parse(typeof(reportRichiesto), coda.template.Replace(".rdlc","")); } catch { } } // ricerco! switch (tipoReport) { case reportRichiesto.CartellinoFinitiOdette: var tabFiniti = taCFOdette.GetData(keyParam); answ = tabFiniti.Count > 0; break; case reportRichiesto.CartellinoPedane: var tabPedane = taCPed.GetData(keyParam); answ = tabPedane.Count > 0; break; default: break; } return answ; } #endregion } }