public int AddDrugToDB(string delivery, string criteriaType, string criteria, int?termID)
        {
            int _rowsAffected = 0;

            if (termID == null)
            {
                _cmd = new SqlCommand($"insert into [dbo].[ADFeedSelectionCriteriaLookup] values ('{delivery}','{criteriaType}','{criteria}',null,{1},GETDATE(),GETDATE())", _conn);
            }
            else
            {
                _cmd = new SqlCommand($"insert into [dbo].[ADFeedSelectionCriteriaLookup] values ('{delivery}','{criteriaType}','{criteria}',{termID},{1},GETDATE(),GETDATE())", _conn);
            }

            try
            {
                _conn.Open();
                _rowsAffected = _cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while adding drug to database.");
                throw;
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while adding drug to database.");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_rowsAffected);
        }
        public int CheckIsAvailableActive(string delivery, string criteriaType, string criteria, int?termID)
        {
            int _rowCount = 0;

            _cmd = new SqlCommand($"select Criteria FROM [dbo].[ADFeedSelectionCriteriaLookup] where Delivery = '{delivery}' and CriteriaType = '{criteriaType}' and Criteria = '{criteria}' and IsActive = 1 ", _conn);
            try
            {
                SqlDataAdapter dAdapter = new SqlDataAdapter(_cmd);
                DataTable      dTable   = new DataTable();
                dAdapter.Fill(dTable);
                if (dTable.Rows.Count > 0)
                {
                    _rowCount = 1;
                }
                else
                {
                    _rowCount = 0;
                }
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while checking the Available and Active");
                throw;
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while checking the Available and Active");
                throw;
            }
            finally
            {
                _cmd.Dispose();
            }
            return(_rowCount);
        }
        public void AuditLogger(string Delivery, string CriteriaType, string Criteria, string ActionType)
        {
            SqlCommand _logCmd = null;
            int?       _termID = 0;

            try
            {
                _termID = GetTermID(Delivery, CriteriaType, Criteria);

                int _rowsAffected = 0;

                if (_termID == 0)
                {
                    _logCmd = new SqlCommand($"insert into [dbo].[ADFeedSelectionCriteriaHistory] values('{Delivery}','{CriteriaType}','{Criteria}',null,'{ActionType}',GETDATE(),'{Environment.UserName}')", _conn);
                }
                else
                {
                    _logCmd = new SqlCommand($"insert into [dbo].[ADFeedSelectionCriteriaHistory] values('{Delivery}','{CriteriaType}','{Criteria}',{_termID},'{ActionType}',GETDATE(),'{Environment.UserName}')", _conn);
                }

                _conn.Open();
                _rowsAffected = _logCmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while logging the events.");
                throw;
            }
            finally
            {
                _conn.Close();
                _logCmd.Dispose();
            }
        }
        public void DeleteDrug(FormCollection form)
        {
            string Criteria     = form["criteria"].Trim();
            string CriteriaType = form["criteriaType"];
            string Delivery     = form["client"];

            TempData["Client"]       = Delivery;
            TempData["CriteriaType"] = CriteriaType;
            bool isAvailableNonActive = false;

            try
            {
                isAvailableNonActive = CheckIsAvailableNonActive(Delivery, CriteriaType, Criteria);
                if (isAvailableNonActive)

                {
                    Response.Write("<script>window.alert(\'Drug is Present but it is Non-Active\');window.location='DeleteDrugView';</script>");
                }
                else
                {
                    DeleteDrugFromDB(Delivery, CriteriaType, Criteria);
                }
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='DeleteDrugView'</script>");
            }
        }
        public List <SelectListItem> PopulateCriteriaType(string ClientName)
        {
            List <SelectListItem> _criteriaType = new List <SelectListItem>();

            _cmd = new SqlCommand($"select Distinct CriteriaType from [dbo].[ADFeedSelectionCriteriaLookup] where Delivery = '{ClientName}'", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    _criteriaType.Add(new SelectListItem {
                        Text = reader[0].ToString()
                    });
                }
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while Populating the Criteria Type List.");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_criteriaType);
        }
        public int DeleteDrugFromDB(string Delivery, string CriteriaType, string Criteria)
        {
            int _rowsAffected = 0;

            _cmd = new SqlCommand($"update [dbo].[ADFeedSelectionCriteriaLookup] set IsActive = 0 , ModificationDate = GETDATE() where Delivery = '{Delivery}' and CriteriaType = '{CriteriaType}' and Criteria = '{Criteria}'", _conn);
            try
            {
                _conn.Open();
                _rowsAffected = _cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while Deleting the Drug");
                throw;
            }
            catch (Exception e)
            {
                UserFriendlyMessage.setMessage("Exception while Deleting the Drug");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_rowsAffected);
        }
        public bool ValidateLeadTerm(string criteria)
        {
            bool _flag = false;

            _cmd = new SqlCommand($"select LeadTerm FROM [dbo].[THSTerm] where LeadTerm  = '{criteria}'", _conn);
            try
            {
                SqlDataAdapter dAdapter = new SqlDataAdapter(_cmd);
                DataTable      dTable   = new DataTable();
                dAdapter.Fill(dTable);
                if (dTable.Rows.Count > 0)
                {
                    _flag = true;
                }
                else
                {
                    _flag = false;
                }
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while Validating the Criteria");
                throw;
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while Validating the Criteria");
                throw;
            }
            finally
            {
                _cmd.Dispose();
            }
            return(_flag);
        }
        public List <string> GetAutoCriteria(string criteria, string delivery, string criteriaType)
        {
            List <string> _criteriaList = new List <string>();

            _cmd = new SqlCommand($"SELECT distinct Criteria FROM [dbo].[ADFeedSelectionCriteriaLookup] WHERE Delivery = '{delivery}' and  CriteriaType = '{criteriaType}' and Criteria LIKE '%{criteria}%' and IsActive = 1", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    _criteriaList.Add(reader[0].ToString());
                }
            }
            catch (Exception e)
            {
                UserFriendlyMessage.setMessage("Exception while fetching the Criteria List");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_criteriaList);
        }
        public List <SelectListItem> PopulateClients()
        {
            List <SelectListItem> _clients = new List <SelectListItem>();

            _cmd = new SqlCommand("select Distinct Delivery from [dbo].[ADFeedSelectionCriteriaLookup] where Delivery not in ('nl.novartis','nl.novartis_nonicsr') and IsActive = 1", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    _clients.Add(new SelectListItem {
                        Text = reader[0].ToString()
                    });
                }
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while Populating the Clients List.");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_clients);
        }
        public List <string> GetAutoTHSTerm(string criteria, string delivery, string criteriaType)
        {
            List <string> _leadTermList = new List <string>();

            _cmd = new SqlCommand($"select leadterm from [dbo].[THSTerm] where LeadTerm like '%{criteria}%' and IsApproved = 'Y'", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    _leadTermList.Add(reader[0].ToString());
                }
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while fetching the THSTerm List");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_leadTermList);
        }
        public bool CheckIsAvailableNonActive(string delivery, string criteriaType, string criteria)
        {
            bool _flag = false;

            _cmd = new SqlCommand($"select Criteria FROM [dbo].[ADFeedSelectionCriteriaLookup] where Criteria  = '{criteria}' and CriteriaType = '{criteriaType}' and Delivery = '{delivery}' and IsActive = 0", _conn);
            try
            {
                SqlDataAdapter dAdapter = new SqlDataAdapter(_cmd);
                DataTable      dTable   = new DataTable();
                dAdapter.Fill(dTable);
                if (dTable.Rows.Count > 0)
                {
                    _flag = true;
                }
                else
                {
                    _flag = false;
                }
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while checking the Available and Non-Active");
                throw;
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while checking the Available and Non-Active");
                throw;
            }
            finally
            {
                _cmd.Dispose();
            }
            return(_flag);
        }
        public ArrayList GetDataFromThesTerm(string criteria)
        {
            ArrayList data = new ArrayList();

            _cmd = new SqlCommand($"select LeadTerm, TermID from [dbo].[THSTerm] where LeadTerm = '{criteria}'", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    data.Add(reader[0].ToString());
                    data.Add(reader[1]);
                }
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while fetching the data from THSTerm");
                throw;
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while fetching the data from THSTerm");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(data);
        }
        public List <DrugDetails> GetDrugList(string ClientName, string CriteriaType)
        {
            List <DrugDetails> _ddList = new List <DrugDetails>();

            _cmd = new SqlCommand($"select * from [dbo].[ADFeedSelectionCriteriaLookup] where Delivery = '{ClientName}' and CriteriaType = '{CriteriaType}'", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    DrugDetails dd = new DrugDetails();
                    dd.Delivery     = reader[0].ToString();
                    dd.CriteriaType = reader[1].ToString();
                    dd.Criteria     = reader[2].ToString();
                    dd.IsActive     = Convert.ToInt32(reader[4]);
                    if (reader[3].Equals(System.DBNull.Value))
                    {
                        dd.TermID = null;
                    }
                    else
                    {
                        dd.TermID = Convert.ToInt32(reader[3]);
                    }
                    if (reader[5].Equals(System.DBNull.Value))
                    {
                        dd.ModificationDate = null;
                    }
                    else
                    {
                        dd.ModificationDate = Convert.ToDateTime(reader[5]);
                    }
                    if (reader[6].Equals(System.DBNull.Value))
                    {
                        dd.CreationDate = null;
                    }
                    else
                    {
                        dd.CreationDate = Convert.ToDateTime(reader[6]);
                    }
                    _ddList.Add(dd);
                }
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while fetching the Drug List");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_ddList);
        }
        public JsonResult GetDrugList(string ClientName, string CriteriaType)
        {
            List <DrugDetails> _ddList = new List <DrugDetails>();

            try
            {
                _ddList = _drugAmendmentConnectionService.GetDrugList(ClientName, CriteriaType);
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='Dashboard';</script>");
            }
            return(Json(_ddList, JsonRequestBehavior.AllowGet));
        }
        public JsonResult PopulateCriteriaType(string ClientName)
        {
            List <SelectListItem> _criteriaType = new List <SelectListItem>();

            try
            {
                _criteriaType = _drugAmendmentConnectionService.PopulateCriteriaType(ClientName);
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='Dashboard';</script>");
            }
            return(Json(_criteriaType, JsonRequestBehavior.AllowGet));
        }
        public JsonResult GetAutoCriteria(string criteria, string delivery, string criteriaType)
        {
            List <string> _criteriaList = new List <string>();

            try
            {
                _criteriaList = _drugAmendmentConnectionService.GetAutoCriteria(criteria, delivery, criteriaType);
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='DeleteDrugView'</script>");
            }
            return(Json(_criteriaList, JsonRequestBehavior.AllowGet));
        }
        private void AuditLogger(string Delivery, string CriteriaType, string Criteria, string ActionType)
        {
            TempData["Client"]       = Delivery;
            TempData["CriteriaType"] = CriteriaType;


            try
            {
                _drugAmendmentConnectionService.AuditLogger(Delivery, CriteriaType, Criteria, ActionType);
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView';</script>");
            }
        }
        public List <ExportToExcel> GetActiveDrugList(string Delivery, string CriteriaType)
        {
            List <ExportToExcel> _ddList = new List <ExportToExcel>();

            _cmd = new SqlCommand($"select Delivery,CriteriaType,Criteria,ModificationDate,CreationDate from [dbo].[ADFeedSelectionCriteriaLookup] where Delivery = '{Delivery}' and CriteriaType = '{CriteriaType}' and IsActive = 1", _conn);
            try
            {
                _conn.Open();
                SqlDataReader reader = _cmd.ExecuteReader();
                while (reader.Read())
                {
                    ExportToExcel dd = new ExportToExcel();
                    dd.Delivery     = reader[0].ToString();
                    dd.CriteriaType = reader[1].ToString();
                    dd.Criteria     = reader[2].ToString();
                    if (reader[3].Equals(System.DBNull.Value))
                    {
                        dd.ModificationDate = "Not Available";
                    }
                    else
                    {
                        dd.ModificationDate = reader[3].ToString();
                    }
                    if (reader[4].Equals(System.DBNull.Value))
                    {
                        dd.CreationDate = "Not Available";
                    }
                    else
                    {
                        dd.CreationDate = reader[4].ToString();
                    }
                    _ddList.Add(dd);
                }
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while Populating the Active Drug List.");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_ddList);
        }
 private void DeleteDrugFromDB(string Delivery, string CriteriaType, string Criteria)
 {
     try
     {
         if (_drugAmendmentConnectionService.DeleteDrugFromDB(Delivery, CriteriaType, Criteria) > 0)
         {
             AuditLogger(Delivery, CriteriaType, Criteria, "InActive");
             Response.Write("<script>window.alert(\'The drug have been successfully deleted.\');window.location='DeleteDrugView';</script>");
         }
         else
         {
             Response.Write("<script>window.alert(\'This drug does not exist.\');window.location='DeleteDrugView';</script>");
         }
     }
     catch (Exception)
     {
         Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='DeleteDrugView'</script>");
     }
 }
 private void DeleteDrugFromDB(string Delivery, string CriteriaType, string Criteria)
 {
     try
     {
         if (_drugAmendmentConnectionService.DeleteDrugFromDB(Delivery, CriteriaType, Criteria) > 0)
         {
             AuditLogger(Delivery, CriteriaType, Criteria, "NonActive");
             Response.Write("<script>window.alert(\'Drug Deleted Successfully...! Set IsActive to Zero\');window.location='DeleteDrugView';</script>");
         }
         else
         {
             Response.Write("<script>window.alert(\'Drug is Not Available in Database to Delete...!\');window.location='DeleteDrugView';</script>");
         }
     }
     catch (Exception)
     {
         Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='DeleteDrugView'</script>");
     }
 }
        public string ExportUsingDatatable(string Delivery, string CriteriaType)
        {
            string pathDownload = "";

            _cmd = new SqlCommand($"select Delivery,CriteriaType,Criteria,TermID,ModificationDate,CreationDate from [dbo].[ADFeedSelectionCriteriaLookup] where Delivery = '{Delivery}' and CriteriaType = '{CriteriaType}' and IsActive = 1", _conn);
            try
            {
                SqlDataAdapter dAdapter = new SqlDataAdapter(_cmd);
                DataTable      dTable   = new DataTable();
                dAdapter.Fill(dTable);
                string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                pathDownload = Path.Combine(pathUser, "Downloads");

                String       ServerPath = System.Web.HttpContext.Current.Server.MapPath("Logs");
                StreamWriter sw         = new StreamWriter(ServerPath + @"\LogFile.log", false);
                sw.Write(pathDownload);
                sw.Flush();
                sw.Close();


                string FileName = Delivery.Substring(Delivery.IndexOf('.') + 1);
                FileName  = char.ToUpper(FileName[0]) + FileName.Substring(1);
                FileName += "_" + CriteriaType + "-" + DateTime.Now.ToString("dd-mm-yyyy-HH-mm-ss-tt") + ".xls";
                ExportToExcelSheet(dTable, pathDownload + @"\" + FileName);
            }
            catch (SqlException)
            {
                UserFriendlyMessage.setMessage("SQL Exception while Exporting to Excel Format");
                throw;
            }
            catch (Exception e)
            {
                UserFriendlyMessage.setMessage("Exception while Exporting to Excel Format");
                throw;
            }
            finally
            {
                _cmd.Dispose();
            }
            return(pathDownload);
        }
        private void CheckIsAvailableActive(string delivery, string criteriaType, string criteria, int?termID)
        {
            TempData["Client"]       = delivery;
            TempData["CriteriaType"] = criteriaType;


            try
            {
                if (_drugAmendmentConnectionService.CheckIsAvailableActive(delivery, criteriaType, criteria, termID) > 0)
                {
                    Response.Write("<script>window.alert(\'This drug is already Present & Active in the DB...!');window.location='AddDrugView'</script>");
                }
                else
                {
                    AddDrugToDB(delivery, criteriaType, criteria, termID);
                }
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
            }
        }
        private void UpdateToActive(string delivery, string criteriaType, string criteria)
        {
            TempData["Client"]       = delivery;
            TempData["CriteriaType"] = criteriaType;

            try
            {
                if (_drugAmendmentConnectionService.UpdateToActive(delivery, criteriaType, criteria) > 0)
                {
                    AuditLogger(delivery, criteriaType, criteria, "Active");
                    Response.Write("<script>window.alert(\'Drug Updated to Active Successfully\');window.location='AddDrugView';</script>");
                }
                else
                {
                    Response.Write("<script>window.alert(\'Drug Not Updated...! May be this drug is not present in DB...!\');window.location='AddDrugView';</script>");
                }
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView';</script>");
            }
        }
        private void AddDrugToDB(string delivery, string criteriaType, string criteria, int?termID)
        {
            TempData["Client"]       = delivery;
            TempData["CriteriaType"] = criteriaType;

            try
            {
                if (_drugAmendmentConnectionService.AddDrugToDB(delivery, criteriaType, criteria, termID) > 0)
                {
                    AuditLogger(delivery, criteriaType, criteria, "Add");
                    Response.Write("<script>window.alert(\'Drug Added Successfully\');window.location='AddDrugView';</script>");
                }
                else
                {
                    Response.Write("<script>window.alert(\'Drug is not added.Something went wrong.\');window.location='AddDrugView';</script>");
                }
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView';</script>");
            }
        }
        public int UpdateToActive(string delivery, string criteriaType, string criteria)
        {
            int _rowsAffected = 0;

            _cmd = new SqlCommand($"update [dbo].[ADFeedSelectionCriteriaLookup] set IsActive = 1, ModificationDate = GETDATE() where Delivery = '{delivery}' and CriteriaType = '{criteriaType}' and Criteria = '{criteria}'", _conn);
            try
            {
                _conn.Open();
                _rowsAffected = _cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                UserFriendlyMessage.setMessage("Exception while Updating the existing drug to Active");
                throw;
            }
            finally
            {
                _conn.Close();
                _cmd.Dispose();
            }
            return(_rowsAffected);
        }
        public JsonResult ExportUsingDatatable(string Delivery, string CriteriaType)
        {
            string _exportMsg = "";

            try
            {
                _exportMsg = _drugAmendmentConnectionService.ExportUsingDatatable(Delivery, CriteriaType);
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='Dashboard'</script>");
            }
            if (_exportMsg == "")
            {
                _exportMsg = "Error";
                return(Json(_exportMsg, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(_exportMsg, JsonRequestBehavior.AllowGet));
            }
        }
        public void AddDrug(FormCollection form)
        {
            string criteriaFromUser   = form["criteria"].Trim();
            string delivery           = form["client"];
            string criteriaType       = form["criteriaType"];
            string hiddenMappedClient = form["hiddenMappedClient"];

            TempData["Client"]             = delivery;
            TempData["CriteriaType"]       = criteriaType;
            TempData["hiddenMappedClient"] = hiddenMappedClient;
            try
            {
                if (criteriaType == "BrandName")
                {
                    if (string.IsNullOrWhiteSpace(criteriaFromUser))
                    {
                        Response.Write("<script>window.alert(\'Blank drug name is not allowed.\');window.location='AddDrugView'</script>");
                    }
                    bool isAvailableNonActive = false;
                    try
                    {
                        isAvailableNonActive = CheckIsAvailableNonActive(delivery, criteriaType, criteriaFromUser);
                    }
                    catch (Exception)
                    {
                        Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                    }

                    if (isAvailableNonActive)
                    {
                        UpdateToActive(delivery, criteriaType, criteriaFromUser);
                    }
                    else
                    {
                        CheckIsAvailableActive(delivery, criteriaType, criteriaFromUser, null);
                    }
                }
                else
                {
                    bool isValidLeadTerm = false;

                    try
                    {
                        isValidLeadTerm = ValidateLeadTerm(criteriaFromUser);
                    }
                    catch (Exception)
                    {
                        Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                    }

                    if (isValidLeadTerm)
                    {
                        ArrayList ThesData = new ArrayList();
                        try
                        {
                            ThesData = GetDataFromThesTerm(criteriaFromUser);
                        }
                        catch (Exception)
                        {
                            Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                        }

                        string criteria             = ThesData[0].ToString();
                        int?   termID               = Convert.ToInt32(ThesData[1]);
                        bool   isAvailableNonActive = false;
                        try
                        {
                            isAvailableNonActive = CheckIsAvailableNonActive(delivery, criteriaType, criteria);
                        }
                        catch (Exception)
                        {
                            Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                        }

                        if (isAvailableNonActive)
                        {
                            UpdateToActive(delivery, criteriaType, criteria);
                        }
                        else
                        {
                            CheckIsAvailableActive(delivery, criteriaType, criteria, termID);
                        }
                    }
                    else
                    {
                        TempData["Client"]             = delivery;
                        TempData["CriteriaType"]       = criteriaType;
                        TempData["hiddenMappedClient"] = hiddenMappedClient;
                        Response.Write("<script>window.alert(\'This drug is either not approved or not present in the thesaurus.\');window.location='AddDrugView'</script>");
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
            }
        }
        public void AddDrug(FormCollection form)
        {
            string criteriaFromUser = form["criteria"].Trim();
            string delivery         = form["client"];
            string criteriaType     = form["criteriaType"];

            TempData["Client"]       = delivery;
            TempData["CriteriaType"] = criteriaType;
            try
            {
                if (criteriaType == "BrandName")
                {
                    bool isAvailableNonActive = false;
                    try
                    {
                        isAvailableNonActive = CheckIsAvailableNonActive(delivery, criteriaType, criteriaFromUser);
                    }
                    catch (Exception)
                    {
                        Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                    }

                    if (isAvailableNonActive)
                    {
                        UpdateToActive(delivery, criteriaType, criteriaFromUser);
                    }
                    else
                    {
                        CheckIsAvailableActive(delivery, criteriaType, criteriaFromUser, null);
                    }
                }
                else
                {
                    bool isValidLeadTerm = false;

                    try
                    {
                        isValidLeadTerm = ValidateLeadTerm(criteriaFromUser);
                    }
                    catch (Exception)
                    {
                        Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                    }

                    if (isValidLeadTerm)
                    {
                        ArrayList ThesData = new ArrayList();
                        try
                        {
                            ThesData = GetDataFromThesTerm(criteriaFromUser);
                        }
                        catch (Exception)
                        {
                            Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                        }

                        string criteria             = ThesData[0].ToString();
                        int?   termID               = Convert.ToInt32(ThesData[1]);
                        bool   isAvailableNonActive = false;
                        try
                        {
                            isAvailableNonActive = CheckIsAvailableNonActive(delivery, criteriaType, criteria);
                        }
                        catch (Exception)
                        {
                            Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
                        }

                        if (isAvailableNonActive)
                        {
                            UpdateToActive(delivery, criteriaType, criteria);
                        }
                        else
                        {
                            CheckIsAvailableActive(delivery, criteriaType, criteria, termID);
                        }
                    }
                    else
                    {
                        TempData["Client"]       = delivery;
                        TempData["CriteriaType"] = criteriaType;
                        Response.Write("<script>window.alert(\'This drug is not a valid drug...! Please insert a valid one...!\');window.location='AddDrugView'</script>");
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<script>window.alert(\'" + UserFriendlyMessage.getMessage() + "\');window.location='AddDrugView'</script>");
            }
        }