public List <RisultatoDTO> RetrieveResults(string richidext_, ref string errorString, bool?forceUpdating = null) { Stopwatch tw = new Stopwatch(); tw.Start(); log.Info(string.Format("Starting ...")); List <RisultatoDTO> riss = null; try { RichiestaLISDTO rich = bll.GetRichiestaLISByIdExt(richidext_); if (rich == null) { string msg = string.Format("No Rich with ID: {0} found. The operation will be aborted!", richidext_); log.Info(msg); throw new Exception(msg); } rich = null; log.Info("External Request ID " + richidext_); log.Info("Searching for Analysis' related to Request ID Ext " + richidext_ + " ..."); List <AnalisiDTO> anals = bll.GetAnalisisByRichiestaExt(richidext_); log.Info(string.Format("Found {0} Analysis' related to Request ID Ext {1}.", anals != null ? anals.Count : 0, richidext_)); foreach (AnalisiDTO anal in anals) { log.Info("Searching for Results related to Analysis ID " + anal.analidid.Value.ToString() + " ..."); List <RisultatoDTO> anres = bll.GetRisultatiByAnalId(anal.analidid.Value.ToString()); if (anres != null && anres.Count > 0) { log.Info(string.Format("Found {0} Results related to Analysis ID {1}.", anres, anal.analidid.Value.ToString())); //0. Check if updating is set to Forced! if (forceUpdating != null) { //0.1 If it is Updating is true if (forceUpdating.Value) { log.Info("Requested a forced updating of the Raw Results!"); //1. Get Updated Risultati log.Info("Searching for Raw Results related to Request IDExt - Analysis ID " + richidext_ + "-" + anal.analidid.Value.ToString() + " ..."); List <RisultatoDTO> anresUpdt = bll.GetRisultatiByEsamAnalId(richidext_ + "-" + anal.analidid.Value.ToString()); log.Info(string.Format("Found {0} Raw Results related to Request IDExt - Analysis ID : {1}-{2}.", anresUpdt != null ? anresUpdt.Count : 0, richidext_, anal.analidid.Value.ToString())); if (anresUpdt != null && anresUpdt.Count > 0) { //2. Delete Old Risultati int removedRes = bll.DeleteRisultatiByIdAnalisi(anal.analidid.Value.ToString()); log.Info(string.Format("Removed {0} Result items related to AnalId: {1}", removedRes, anal.analidid.Value.ToString())); //3. Write New Risultati List <RisultatoDTO> updts = bll.AddRisultati(anresUpdt); log.Info(string.Format("{0} Raw Results Converted and Written into DB. They are Related to Analysis ID {1}. ANRE ID are '{2}'.", updts != null ? updts.Count : 0, anal.analidid.Value.ToString(), updts != null ? string.Join(", ", updts.Select(p => p.anreidid).ToList().ToArray()) : "")); } log.Info("Forced updating of Results Completed!"); } } //1. Check if Analisi is "Executed" //1.1 If not, Update Analisi to "Executed" log.Info(string.Format("HL7 Status of Analysis with ID {0}, is '{1}'. HL7 Message is '{2}'.", anal.analidid.Value.ToString(), anal.hl7_stato, anal.hl7_msg)); if (anal.hl7_stato != IBLL.HL7StatesAnalisi.Executed) { List <AnalisiDTO> tmp = bll.ChangeHL7StatusAndMessageAnalisis(new List <string>() { anal.analidid.Value.ToString() }, IBLL.HL7StatesAnalisi.Executed, "Risultati Ottenuti"); log.Info(string.Format("HL7 Status of Analysis with ID {0}, has been updated to '{1}'.", anal.analidid.Value.ToString(), tmp != null ? tmp.First().hl7_stato : "--error occurred--")); } //2. Add to Collection if (riss == null) { riss = new List <RisultatoDTO>(); } riss.AddRange(anres); log.Info(string.Format("{0} Results related to Analysis ID {1}, has been added to the Results Collection (actually {2} total items).", anres.Count, anal.analidid.Value.ToString(), riss.Count)); } else { log.Info(string.Format("Found No Results related to Analysis ID {0}.", anal.analidid.Value.ToString())); log.Info("Searching for Raw Results related to Request IDExt - Analysis ID " + richidext_ + "-" + anal.analidid.Value.ToString() + " ..."); List <RisultatoDTO> anresNew = bll.GetRisultatiByEsamAnalId(richidext_ + "-" + anal.analidid.Value.ToString()); log.Info(string.Format("Found {0} Raw Results related to Request IDExt - Analysis ID : {1}-{2}.", anresNew != null ? anresNew.Count : 0, richidext_, anal.analidid.Value.ToString())); if (anresNew != null && anresNew.Count > 0) { //1. Add new Risultato as Executed List <RisultatoDTO> news = bll.AddRisultati(anresNew); log.Info(string.Format("{0} Raw Results Converted and Written into DB. They are Related to Analysis ID {1}. ANRE ID are '{2}'.", news != null ? news.Count : 0, anal.analidid.Value.ToString(), news != null ? string.Join(", ", news.Select(p => p.anreidid).ToList().ToArray()) : "")); //2. Update Analisi to "Executed" log.Info(string.Format("HL7 Status of Analysis with ID {0}, is '{1}'. HL7 Message is '{2}'.", anal.analidid.Value.ToString(), anal.hl7_stato, anal.hl7_msg)); List <AnalisiDTO> tmp = bll.ChangeHL7StatusAndMessageAnalisis(new List <string>() { anal.analidid.Value.ToString() }, IBLL.HL7StatesAnalisi.Executed, "Risultati Ottenuti"); log.Info(string.Format("HL7 Status of Analysis with ID {0}, has been updated to '{1}'.", anal.analidid.Value.ToString(), tmp != null ? tmp.First().hl7_stato : "--error occurred--")); //3. Add to Collection if (news != null && news.Count > 0) { if (riss == null) { riss = new List <RisultatoDTO>(); } riss.AddRange(news); log.Info(string.Format("{0} Results related to Analysis ID {1}, has been added to the Results Collection (actually {2} total items).", news.Count, anal.analidid.Value.ToString(), riss.Count)); } } } } } catch (Exception ex) { string msg = "An Error occured! Exception detected!"; log.Info(msg); log.Error(msg + "\n" + ex.Message); } if (errorString == "") { errorString = null; } tw.Stop(); log.Info(string.Format("Completed! Elapsed time {0}", LibString.TimeSpanToTimeHmsms(tw.Elapsed))); return(riss); }