Esempio n. 1
0
        public string ScheduleNewRequest(EventoDTO even, PrestazioneDTO pres, RichiestaLISDTO esam, List <AnalisiDTO> anals, ref string errorString)
        {
            Stopwatch tw = new Stopwatch();

            tw.Start();

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

            string hl7_stato = IBLL.HL7StatesRichiestaLIS.Idle;
            string evenIdStr = null;
            string esamIdStr = null;
            string presIdStr = null;

            EventoDTO         evenInserted  = null;
            PrestazioneDTO    presInserted  = null;
            RichiestaLISDTO   esamInserted  = null;
            List <AnalisiDTO> analsInserted = null;

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

            try
            {
                if (even == null || esam == null || anals == null || (anals != null && anals.Count == 0))
                {
                    throw new Exception("Error! Request a null or void insertion of EVEN and/or ESAM and/or ANAL.");
                }

                // Validation Even!!!!
                if (!bll.ValidateEven(even, ref errorString))
                {
                    string msg = "Validation Even Failure! Check the error string for figuring out the issue!";
                    log.Info(msg + "\r\n" + errorString);
                    log.Error(msg + "\r\n" + errorString);
                    throw new Exception(msg);
                }

                // Check if Epis exists!!!!
                log.Info(string.Format("Checking EPIS '{0}' exists ...", even.evenepis));
                EpisodioDTO epis = bll.GetEpisodioById(even.evenepis.ToString());
                if (epis == null)
                {
                    string msg = string.Format("EPIS Checking Failure! EPIS '{0}' not Found!", even.evenepis);
                    log.Info(msg);
                    log.Error(msg);
                    throw new Exception(msg);
                }
                epis = null;
                log.Info(string.Format("Checking EPIS '{0}' succeded!", even.evenepis));

                // Create Even
                log.Info(string.Format("EVEN Insertion ..."));
                even.evenidid = null;
                evenInserted  = bll.AddEvento(even);
                if (evenInserted == null)
                {
                    throw new Exception("Error during EVEN writing into the DB.");
                }
                log.Info(string.Format("EVEN Inserted. Got {0} EVENIDID!", evenInserted.evenidid));

                evenIdStr = evenInserted.evenidid.ToString();

                pres.preseven = evenInserted.evenidid;
                log.Info(string.Format("PRES foreign key PRESEVEN updated!"));
                // Validation Pres!!!!
                if (!bll.ValidatePres(pres, ref errorString))
                {
                    string msg = "Validation Pres Failure! Check the error string for figuring out the issue!";
                    log.Info(msg + "\r\n" + errorString);
                    log.Error(msg + "\r\n" + errorString);
                    throw new Exception(msg);
                }

                pres.hl7_stato = hl7_stato;

                // Create Pres
                log.Info(string.Format("PRES Insertion ..."));
                presInserted = bll.AddPrestazione(pres);
                if (presInserted == null)
                {
                    throw new Exception("Error during PRES writing into the DB.");
                }
                log.Info(string.Format("PRES Inserted. Got {0} PRESIDID!", presInserted.presidid));

                presIdStr = presInserted.presidid.ToString();

                esam.esameven = evenInserted.evenidid;
                // Validation Esam!!!!
                if (!bll.ValidateEsam(esam, ref errorString))
                {
                    string msg = "Validation Esam Failure! Check the error string for figuring out the issue!";
                    log.Info(msg + "\r\n" + errorString);
                    log.Error(msg + "\r\n" + errorString);
                    throw new Exception(msg);
                }

                log.Info(string.Format("ESAM foreign key ESAMEVEN updated!"));
                esam.hl7_stato = hl7_stato;

                // Create Esam
                log.Info(string.Format("ESAM Insertion ..."));
                esamInserted = bll.AddRichiestaLIS(esam);
                if (esamInserted == null)
                {
                    throw new Exception("Error during ESAM writing into the DB.");
                }
                log.Info(string.Format("ESAM Inserted. Got {0} ESAMIDID!", esamInserted.esamidid));

                esamIdStr = esamInserted.esamidid.ToString();

                anals.ForEach(p => { p.analesam = int.Parse(esamIdStr); p.hl7_stato = hl7_stato; });

                // Validation Anals!!!!
                if (!bll.ValidateAnals(anals, ref errorString))
                {
                    string msg = "Validation Anals Failure! Check the error string for figuring out the issue!";
                    log.Info(msg + "\r\n" + errorString);
                    log.Error(msg + "\r\n" + errorString);
                    throw new Exception(msg);
                }

                // Create Anals
                log.Info(string.Format("Insertion of {0} ANAL requested. Processing ...", anals.Count));
                analsInserted = bll.AddAnalisis(anals);
                if ((analsInserted == null) || (analsInserted != null && analsInserted.Count != anals.Count))
                {
                    throw new Exception("Error during ANALs writing into the DB.");
                }
                log.Info(string.Format("Inserted {0} ANAL successfully!", analsInserted.Count));

                // Log Total Number Of Records Inserted! 1 for Esam, 1 for PRES and n for ANAL(s)
                log.Info(string.Format("Inserted {0} records successfully!", analsInserted.Count + 1 + 1));
            }
            catch (Exception ex)
            {
                string msg = "An Error occured! Exception detected!";
                log.Info(msg);
                log.Error(msg + "\n" + ex.Message);

                if (errorString == "")
                {
                    errorString = msg + "\r\n" + ex.Message;
                }
                else
                {
                    errorString += "\r\n" + msg + "\r\n" + ex.Message;
                }

                int evenRB  = 0;
                int presRB  = 0;
                int esamRB  = 0;
                int analsRB = 0;

                log.Info(string.Format("Rolling Back of the Insertions due an error occured ..."));
                // Rolling Back
                if (evenIdStr != null)
                {
                    evenRB = bll.DeleteEventoById(evenIdStr);
                    log.Info(string.Format("Rolled Back {0} EVEN record. EVENIDID was {1}!", evenRB, evenIdStr));
                }
                if (presIdStr != null)
                {
                    presRB = bll.DeletePrestazioneById(presIdStr);
                    log.Info(string.Format("Rolled Back {0} PRES record. PRESIDID was {1}!", presRB, presIdStr));
                }
                if (esamIdStr != null)
                {
                    esamRB = bll.DeleteRichiestaLISById(esamIdStr);
                    log.Info(string.Format("Rolled Back {0} ESAM record. ESAMIDID was {1}!", esamRB, esamIdStr));
                    analsRB = bll.DeleteAnalisiByRichiesta(esamIdStr);
                    log.Info(string.Format("Rolled Back {0} ANAL records. ANALESAM was {1}!", analsRB, esamIdStr));
                }

                log.Info(string.Format("Rolled Back {0} records of {1} requested!", evenRB + presRB + esamRB + analsRB, anals.Count + 1 + 1 + 1));
                esamIdStr = null;
            }

            tw.Stop();
            log.Info(string.Format("Completed! Elapsed time {0}", LibString.TimeSpanToTimeHmsms(tw.Elapsed)));

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

            return(esamIdStr);
        }