Example #1
0
        public MirthResponseDTO SubmitNewRequest(string richid, ref string errorString)
        {
            Stopwatch tw = new Stopwatch();

            tw.Start();

            log.Info(string.Format("Starting ..."));

            MirthResponseDTO data = null;

            if (errorString == null)
            {
                errorString = "";
            }

            try
            {
                // 0. Check if richid is a numeric Value
                int richid_int = 0;
                if (!int.TryParse(richid, out richid_int))
                {
                    string msg = string.Format("ID of the riquest is not an integer string. {0} is not a valid ID for this context!", richid);
                    errorString = msg;
                    log.Info(msg);
                    log.Error(msg);
                    throw new Exception(msg);
                }

                // 1. Check if ESAM and ANAL exist
                RichiestaLISDTO   chkEsam  = bll.GetRichiestaLISById(richid);
                List <AnalisiDTO> chkAnals = bll.GetAnalisisByRichiesta(richid);
                if (chkEsam == null || chkAnals == null || (chkAnals != null && chkAnals.Count == 0))
                {
                    string msg = "Error! No Esam or Anal records found referring to EsamID " + richid + "! A request must be Scheduled first!";
                    errorString = msg;
                    log.Info(msg);
                    log.Error(msg);
                    return(null);
                }

                // 2. Check if PRES exists
                if (!chkEsam.esameven.HasValue)
                {
                    string msg = "Error! Found an EsamEven NULL referring to EsamID " + richid + "!";
                    errorString = msg;
                    log.Info(msg);
                    log.Error(msg);
                    return(null);
                }
                PrestazioneDTO chkPres = bll.GetPrestazioneByEvento(chkEsam.esameven.Value.ToString());
                if (chkPres == null)
                {
                    string msg = "Error! No Pres record found referring to EvenID " + chkEsam.esameven.Value.ToString() + "! A request must be Scheduled first!";
                    errorString = msg;
                    log.Info(msg);
                    log.Error(msg);
                    return(null);
                }
                string presidid = chkPres.presidid.Value.ToString();

                // 3. Settare Stato a "SEDNING"
                int res = bll.ChangeHL7StatusAndMessageAll(richid, presidid, IBLL.HL7StatesRichiestaLIS.Sending, "");

                // 4. Invio a Mirth
                string hl7orl = bll.SendMirthRequest(richid);
                if (hl7orl == null)
                {
                    string msg = "Mirth Returned an Error!";
                    errorString = msg;
                    // 3.e1 Cambiare stato in errato
                    int err = bll.ChangeHL7StatusAndMessageAll(richid, presidid, IBLL.HL7StatesRichiestaLIS.Errored, msg);
                    // 3.e2 Restituire null
                    return(null);
                }
                // 4.1 Settare a SENT
                int snt = bll.ChangeHL7StatusAndMessageAll(richid, presidid, IBLL.HL7StatesRichiestaLIS.Sent, "");

                // 5. Estrarre i dati dalla risposta di Mirth
                log.Info("Mirth Data Response Extraction ...");
                data = bll.ORLParser(hl7orl);
                if (data == null)
                {
                    string emsg = "Mirth Data Response Extraction failed!";
                    if (errorString == "")
                    {
                        errorString = emsg;
                    }
                    else
                    {
                        errorString += "\n\r" + emsg;
                    }
                    log.Info(emsg);
                    log.Error(emsg);
                }
                else
                {
                    log.Info("Mirth Data Response Successfully extracted!");
                }

                // 6. Settare Stato a seconda della risposta
                string status = IBLL.HL7StatesRichiestaLIS.Sent;
                if (data.ACKCode != "AA")
                {
                    status = IBLL.HL7StatesRichiestaLIS.Errored;
                }
                else
                {
                    if (data.Labes != null)
                    {
                        status = IBLL.HL7StatesRichiestaLIS.Labelled;
                    }
                    else
                    {
                        string msg = "An Error Occurred! No Lables Retrieved By the Remote LAB!";
                        errorString = msg;
                        log.Info(msg);
                        log.Error(msg);
                        return(null);
                    }
                }
                RichiestaLISDTO RichUpdt = bll.ChangeHL7StatusAndMessageRichiestaLIS(richid, status, data.ACKDesc);
                PrestazioneDTO  PresUpdt = bll.ChangeHL7StatusAndMessagePrestazione(presidid, status, data.ACKDesc);

                List <ORCStatus> orcs = data.ORCStatus;
                if (orcs != null)
                {
                    foreach (ORCStatus orc in orcs)
                    {
                        string            desc      = orc.Description;
                        string            stat      = orc.Status;
                        string            analid    = orc.AnalID;
                        List <AnalisiDTO> AnalUpdts = bll.ChangeHL7StatusAndMessageAnalisis(new List <string>()
                        {
                            analid
                        }, stat, desc);
                    }
                }

                // 7. Scrivere Labels nel DB
                if (data.Labes != null)
                {
                    data.Labes.ForEach(p => p.labeesam = richid_int);
                    List <LabelDTO> stored = bll.StoreLabels(data.Labes);
                    if (stored == null)
                    {
                        string msg = "An Error Occurred! Labels successfully retrieved by the remote LAB, but they haven't been sotred into the local DB! The EsamIDID is " + richid;
                        errorString = msg;
                        log.Info(msg);
                        log.Error(msg);
                    }
                }
            }
            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)));

            // 8. Restituire il DTO
            return(data);
        }