//method to change data center of components list selected
        public void ChangingDataCenter(List <int> mListOfCheckBoxes, int pstrCompanyId, string pstrDataCenterSelected)
        {
            WinMonitorEntityModelContext contextObj = new WinMonitorEntityModelContext();
            DBCompany companyObj = new DBCompany();

            companyObj = (from company in contextObj.DBCompanies
                          where company.DBCompanyId == pstrCompanyId
                          select company).FirstOrDefault();
            foreach (int componentId in mListOfCheckBoxes)
            {
                DBComponent_With_Status componentObj = new DBComponent_With_Status();
                componentObj = (from component in contextObj.DBComponent_With_Status
                                where component.DBCSId == componentId
                                select component).FirstOrDefault();
                componentObj.DBCenterName = pstrDataCenterSelected;
                contextObj.SaveChanges();
                List <DBSubscription> subscriptionObjList = new List <DBSubscription>();
                subscriptionObjList = (from subObj in contextObj.DBSubscriptions
                                       where subObj.DBCompanyId == pstrCompanyId
                                       select subObj).ToList();
                DBEmailPage emailPageObj = new DBEmailPage();
                emailPageObj = (from emailPage in contextObj.DBEmailPages
                                where emailPage.DBEmailPageId == 1
                                select emailPage).FirstOrDefault();

                foreach (DBSubscription subscriptionObj in subscriptionObjList)
                {
                    PerformSubscription performSubscriptionObj = new PerformSubscription();
                    WebClient           clientObj = new WebClient();
                    int    endIndex, startIndex;
                    string stringToBeReplaced;

                    string stringHtml = emailPageObj.DBEmailContent.ToString();

                    endIndex           = stringHtml.IndexOf("<!--end of eventsUpdateDiv-->");
                    startIndex         = stringHtml.IndexOf("<div id=\"divForEvents\">");
                    stringToBeReplaced = stringHtml.Substring(startIndex, endIndex - startIndex);
                    stringHtml         = stringHtml.Replace(stringToBeReplaced, "");

                    endIndex           = stringHtml.IndexOf("<!--end of componentStatusChangeDiv-->");
                    startIndex         = stringHtml.IndexOf("<div id=\"divForComponentStatusUpdate\">");
                    stringToBeReplaced = stringHtml.Substring(startIndex, endIndex - startIndex);
                    stringHtml         = stringHtml.Replace(stringToBeReplaced, "");

                    string link = "http://cha-en-pdp2:2108/?pUserCompanyName=CompanyNameVariable";                              //change url to direct to user page
                    stringHtml = stringHtml.Replace("linkToBeChanged", link);
                    stringHtml = stringHtml.Replace("CompanyNameVariable", companyObj.DBCompanyName);
                    stringHtml = stringHtml.Replace("customerNameVariable", subscriptionObj.DBName);
                    stringHtml = stringHtml.Replace("ComponentNameForDataCenterUpdateVariable", componentObj.DBComponentName);
                    stringHtml = stringHtml.Replace("DataCenterVariable", componentObj.DBCenterName);

                    performSubscriptionObj.sendEmail(subscriptionObj.DBEmail, null, "Data Center Updated", stringHtml);
                }
            }
        }
        //method to delete existing incident of particular element
        public void deletingExistingIncidentOfComponent(int pstrComponentId, int pstrCompanyId)
        {
            WinMonitorEntityModelContext contextObj = new WinMonitorEntityModelContext();
            DBIncident incidentObj = new DBIncident();
            DBComponent_With_Status componentObj = new DBComponent_With_Status();
            DBLogHistory            historyObj   = new DBLogHistory();

            componentObj = (from component in contextObj.DBComponent_With_Status
                            where component.DBCSId == pstrComponentId
                            select component).FirstOrDefault();

            incidentObj = (from incident in contextObj.DBIncidents
                           where incident.DBCSId == pstrComponentId
                           select incident).FirstOrDefault();
            historyObj = (from log in contextObj.DBLogHistories
                          where ((log.DBCompanyId == pstrCompanyId) && (log.DBCSId == pstrComponentId) && (log.DBDateTimeEnd == null))
                          select log).Last();
            historyObj.DBDateTimeStart = DateTime.UtcNow;;
            contextObj.SaveChanges();

            contextObj.DBIncidents.Remove(incidentObj);
            contextObj.SaveChanges();
        }
        //method to delete selected components
        public void DeleteSelectedComponents(List <int> pListOfComponents, int pCompanyId)
        {
            WinMonitorEntityModelContext contextObj = new WinMonitorEntityModelContext();

            foreach (int componentId in pListOfComponents)
            {
                DBIncident              incidentObj  = new DBIncident();
                DBLogHistory            historyObj   = new DBLogHistory();
                DBComponent_With_Status componentObj = new DBComponent_With_Status();
                int incidentIdStoringVariable        = -1;

                incidentObj = (from incident in contextObj.DBIncidents
                               where incident.DBCSId == componentId
                               select incident).FirstOrDefault();

                componentObj = (from component in contextObj.DBComponent_With_Status
                                where component.DBCSId == componentId
                                select component).FirstOrDefault();

                if (incidentObj != null)
                {
                    incidentIdStoringVariable = incidentObj.DBIncidentId;

                    //Adding log history
                    historyObj.DBCompanyId       = pCompanyId;
                    historyObj.DBComponentName   = componentObj.DBComponentName;
                    historyObj.DBCSId            = componentObj.DBCSId;
                    historyObj.DBDateTimeStart   = DateTime.UtcNow;;
                    historyObj.DBIncidentId      = incidentIdStoringVariable;
                    historyObj.DBDateTimeEnd     = null;
                    historyObj.DBIncidentDetails = "Incident Deleted";
                    historyObj.DBIncidentName    = incidentObj.DBIncidentName;
                    historyObj.DBLogId           = getseqDBLogId();
                    historyObj.DBStatus          = "Operational";
                    contextObj.DBLogHistories.Add(historyObj);
                    contextObj.SaveChanges();

                    contextObj.DBIncidents.Remove(incidentObj);
                    contextObj.SaveChanges();
                }
                else
                {
                    historyObj = new DBLogHistory();
                    //Adding log history
                    historyObj.DBCompanyId       = pCompanyId;
                    historyObj.DBComponentName   = componentObj.DBComponentName;
                    historyObj.DBCSId            = componentObj.DBCSId;
                    historyObj.DBDateTimeStart   = DateTime.UtcNow;;
                    historyObj.DBDateTimeEnd     = null;
                    historyObj.DBIncidentId      = incidentIdStoringVariable;
                    historyObj.DBIncidentDetails = "Component Deleted";
                    historyObj.DBIncidentName    = "Component Deleted";
                    historyObj.DBLogId           = getseqDBLogId();
                    historyObj.DBStatus          = "Operational";
                    contextObj.DBLogHistories.Add(historyObj);
                    contextObj.SaveChanges();
                }
                contextObj.DBComponent_With_Status.Remove(componentObj);
                contextObj.SaveChanges();
            }
        }
        //Method to add master component to component list of company in database
        public string AddComponentToDb(List <string> pMasterComponentListFromPage, int pCompanyId, string pPrimaryDataCenter)
        {
            WinMonitorEntityModelContext contextObj = new WinMonitorEntityModelContext();

            try
            {
                foreach (string masterComponentName in pMasterComponentListFromPage)
                {
                    DBComponent_With_Status component  = new DBComponent_With_Status();
                    DBLogHistory            historyObj = new DBLogHistory();
                    component.DBCSId                = getseqDBCSId();
                    component.DBComponentName       = masterComponentName;
                    component.DBStatus              = "Operational";
                    component.DBType                = "master";
                    component.DBCompanyId           = pCompanyId;
                    component.DBMasterComponentName = masterComponentName;
                    component.DBCenterName          = pPrimaryDataCenter;

                    //adding master component to db
                    contextObj.DBComponent_With_Status.Add(component);

                    contextObj.SaveChanges();

                    //code to add details to log hisory
                    historyObj.DBLogId           = getseqDBLogId();
                    historyObj.DBCompanyId       = pCompanyId;
                    historyObj.DBCSId            = component.DBCSId;
                    historyObj.DBIncidentId      = -1;
                    historyObj.DBComponentName   = component.DBComponentName;
                    historyObj.DBIncidentName    = "";
                    historyObj.DBIncidentDetails = "";
                    historyObj.DBDateTimeStart   = DateTime.UtcNow;;
                    historyObj.DBStatus          = component.DBStatus;
                    historyObj.DBDateTimeEnd     = null;

                    //adds history obj to database
                    contextObj.DBLogHistories.Add(historyObj);

                    contextObj.SaveChanges();
                }

                return("Master Component added added !!");
            }
            catch (DbUpdateException exUpdateDB)
            {
                Console.Write(exUpdateDB);
                return("DbUpdateException");
            }
            catch (DbEntityValidationException exEntityValidateDB)
            {
                Console.Write(exEntityValidateDB);
                return("DbEntityValidationException");
            }
            catch (NotSupportedException exNotSupportedDB)
            {
                Console.Write(exNotSupportedDB);
                return("NotSupportedException");
            }
            catch (ObjectDisposedException exObjectDisposedDB)
            {
                Console.Write(exObjectDisposedDB);
                return("ObjectDisposedException");
            }
            catch (InvalidOperationException exInvalidOperationDB)
            {
                Console.Write(exInvalidOperationDB);
                return("InvalidOperationException");
            }
        }
        //update staus of selected components
        public string UpdatingStatusOfComponentsSelected(List <int> pselectedComponentList, string pStatusToBeChanged, int pCompanyId)
        {
            try
            {
                WinMonitorEntityModelContext contextObj = new WinMonitorEntityModelContext();
                DBLogHistory historyObj = new DBLogHistory();
                DBCompany    companyObj = new DBCompany();
                string       previousStatus;
                companyObj = (from company in contextObj.DBCompanies
                              where company.DBCompanyId == pCompanyId
                              select company).FirstOrDefault();
                string strResult = String.Empty;
                if (pStatusToBeChanged == "Operational")
                {
                    foreach (int componentId in pselectedComponentList)
                    {
                        DBComponent_With_Status componentObj = new DBComponent_With_Status();
                        DBIncident incidentObj = new DBIncident();

                        componentObj = (from component in contextObj.DBComponent_With_Status
                                        where component.DBCSId == componentId
                                        select component).FirstOrDefault();
                        if (companyObj.DBSecondaryCenter != componentObj.DBCenterName)
                        {
                            incidentObj = (from incident in contextObj.DBIncidents
                                           where incident.DBCSId == componentId
                                           select incident).FirstOrDefault();
                            if (incidentObj != null)
                            {
                                historyObj = (from histObj in contextObj.DBLogHistories
                                              where ((histObj.DBCompanyId == pCompanyId) && (histObj.DBCSId == componentId) && (histObj.DBIncidentId == incidentObj.DBIncidentId))
                                              orderby histObj.DBDateTimeStart descending, histObj.DBDateTimeEnd descending
                                              select histObj).First();
                                historyObj.DBDateTimeEnd = DateTime.UtcNow;;
                                contextObj.SaveChanges();
                                contextObj.DBIncidents.Remove(incidentObj);
                                contextObj.SaveChanges();
                            }
                            componentObj.DBStatus = pStatusToBeChanged;
                            contextObj.SaveChanges();
                        }
                        List <DBSubscription> subscriptionObjList = new List <DBSubscription>();
                        subscriptionObjList = (from subObj in contextObj.DBSubscriptions
                                               where subObj.DBCompanyId == pCompanyId
                                               select subObj).ToList();
                        DBEmailPage emailPageObj = new DBEmailPage();
                        emailPageObj = (from emailPage in contextObj.DBEmailPages
                                        where emailPage.DBEmailPageId == 1
                                        select emailPage).FirstOrDefault();

                        foreach (DBSubscription subscriptionObj in subscriptionObjList)
                        {
                            PerformSubscription performSubscriptionObj = new PerformSubscription();
                            WebClient           clientObj = new WebClient();
                            int    endIndex, startIndex;
                            string stringToBeReplaced;
                            string stringHtml = emailPageObj.DBEmailContent.ToString();

                            endIndex           = stringHtml.IndexOf("<!--end of eventsUpdateDiv-->");
                            startIndex         = stringHtml.IndexOf("<div id=\"divForEvents\">");
                            stringToBeReplaced = stringHtml.Substring(startIndex, endIndex - startIndex);
                            stringHtml         = stringHtml.Replace(stringToBeReplaced, "");

                            endIndex           = stringHtml.IndexOf("<!--end of dataCenterChangeUpdate-->");
                            startIndex         = stringHtml.IndexOf("<div id=\"divForDataCenterUpdate\">");
                            stringToBeReplaced = stringHtml.Substring(startIndex, endIndex - startIndex);
                            stringHtml         = stringHtml.Replace(stringToBeReplaced, "");

                            string link = "http://cha-en-pdp2:2108/?pUserCompanyName=CompanyNameVariable";                              //change url to direct to user page
                            stringHtml = stringHtml.Replace("linkToBeChanged", link);
                            stringHtml = stringHtml.Replace("CompanyNameVariable", companyObj.DBCompanyName);
                            stringHtml = stringHtml.Replace("customerNameVariable", subscriptionObj.DBName);
                            stringHtml = stringHtml.Replace("ComponentNameForStatusUpdateVariable", componentObj.DBComponentName);
                            stringHtml = stringHtml.Replace("ComponentStatusUpdateVariable", componentObj.DBStatus);
                            performSubscriptionObj.sendEmail(subscriptionObj.DBEmail, null, "Component Status Updated", stringHtml);
                        }
                    }
                    strResult = "Incident Successfully deleted !!";
                }
                else
                {
                    foreach (int componentId in pselectedComponentList)
                    {
                        DBComponent_With_Status componentObj = new DBComponent_With_Status();

                        componentObj = (from component in contextObj.DBComponent_With_Status
                                        where component.DBCSId == componentId
                                        select component).FirstOrDefault();
                        previousStatus        = componentObj.DBStatus;
                        componentObj.DBStatus = pStatusToBeChanged;
                        contextObj.SaveChanges();
                        DBIncident incidentObj = new DBIncident();
                        incidentObj = (from incident in contextObj.DBIncidents
                                       where incident.DBCSId == componentId
                                       select incident).FirstOrDefault();
                        //code to add details to log hisory
                        historyObj.DBLogId           = getseqDBLogId();
                        historyObj.DBCompanyId       = pCompanyId;
                        historyObj.DBCSId            = componentId;
                        historyObj.DBIncidentId      = incidentObj.DBIncidentId;
                        historyObj.DBComponentName   = componentObj.DBComponentName;
                        historyObj.DBIncidentName    = incidentObj.DBIncidentName;
                        historyObj.DBIncidentDetails = incidentObj.DBDescription;
                        historyObj.DBDateTimeStart   = DateTime.UtcNow;;
                        historyObj.DBStatus          = componentObj.DBStatus;
                        historyObj.DBDateTimeEnd     = null;

                        List <DBSubscription> subscriptionObjList = new List <DBSubscription>();
                        subscriptionObjList = (from subObj in contextObj.DBSubscriptions
                                               where subObj.DBCompanyId == pCompanyId
                                               select subObj).ToList();
                        DBEmailPage emailPageObj = new DBEmailPage();
                        emailPageObj = (from emailPage in contextObj.DBEmailPages
                                        where emailPage.DBEmailPageId == 1
                                        select emailPage).FirstOrDefault();

                        foreach (DBSubscription subscriptionObj in subscriptionObjList)
                        {
                            PerformSubscription performSubscriptionObj = new PerformSubscription();
                            WebClient           clientObj = new WebClient();
                            int    endIndex, startIndex;
                            string stringToBeReplaced;
                            string stringHtml = emailPageObj.DBEmailContent.ToString();

                            endIndex           = stringHtml.IndexOf("<!--end of eventsUpdateDiv-->");
                            startIndex         = stringHtml.IndexOf("<div id=\"divForEvents\">");
                            stringToBeReplaced = stringHtml.Substring(startIndex, endIndex - startIndex);
                            stringHtml         = stringHtml.Replace(stringToBeReplaced, "");

                            endIndex           = stringHtml.IndexOf("<!--end of dataCenterChangeUpdate-->");
                            startIndex         = stringHtml.IndexOf("<div id=\"divForDataCenterUpdate\">");
                            stringToBeReplaced = stringHtml.Substring(startIndex, endIndex - startIndex);
                            stringHtml         = stringHtml.Replace(stringToBeReplaced, "");

                            string link = "http://cha-en-pdp2:2108/?pUserCompanyName=CompanyNameVariable";                              //change url to direct to user page
                            stringHtml = stringHtml.Replace("linkToBeChanged", link);
                            stringHtml = stringHtml.Replace("CompanyNameVariable", companyObj.DBCompanyName);
                            stringHtml = stringHtml.Replace("customerNameVariable", subscriptionObj.DBName);
                            stringHtml = stringHtml.Replace("ComponentNameForStatusUpdateVariable", componentObj.DBComponentName);
                            stringHtml = stringHtml.Replace("ComponentStatusUpdateVariable", componentObj.DBStatus);
                            performSubscriptionObj.sendEmail(subscriptionObj.DBEmail, null, "Component Status Updated", stringHtml);
                        }
                    }
                    strResult = "Incident Successfully added !!";
                }
                return(strResult);
            }
            catch (DbUpdateException exUpdateDB)
            {
                Console.Write(exUpdateDB);
                return("DbUpdateException");
            }
            catch (DbEntityValidationException exEntityValidateDB)
            {
                Console.Write(exEntityValidateDB);
                return("DbEntityValidationException");
            }
            catch (NotSupportedException exNotSupportedDB)
            {
                Console.Write(exNotSupportedDB);
                return("NotSupportedException");
            }
            catch (ObjectDisposedException exObjectDisposedDB)
            {
                Console.Write(exObjectDisposedDB);
                return("ObjectDisposedException");
            }
            catch (InvalidOperationException exInvalidOperationDB)
            {
                Console.Write(exInvalidOperationDB);
                return("InvalidOperationException");
            }
            catch (Exception ex)
            {
                Console.Write(ex);
                return("Misllaneous Exception");
            }
        }
        //Specific Components Save details method
        public string mSaveAddSpecificComponentDetails(string pstringSpecificComponentName, string pstringSpecificComponentStatus, int pstringSpecificComponentCompanyId, string pstringSpecificComponentDataCenter)
        {
            try
            {
                WinMonitorEntityModelContext mDBContext = new WinMonitorEntityModelContext();
                DBComponent_With_Status      mDBSpecific_Component_With_StatusObj = new DBComponent_With_Status();
                DBLogHistory mDBLogObj = new DBLogHistory();

                mDBSpecific_Component_With_StatusObj.DBCSId                = getseqDBCSId();
                mDBSpecific_Component_With_StatusObj.DBComponentName       = pstringSpecificComponentName;
                mDBSpecific_Component_With_StatusObj.DBStatus              = pstringSpecificComponentStatus;
                mDBSpecific_Component_With_StatusObj.DBType                = "Specific";
                mDBSpecific_Component_With_StatusObj.DBCompanyId           = pstringSpecificComponentCompanyId;
                mDBSpecific_Component_With_StatusObj.DBMasterComponentName = null;
                mDBSpecific_Component_With_StatusObj.DBCenterName          = pstringSpecificComponentDataCenter;


                //adds new specific component to the components table
                mDBContext.DBComponent_With_Status.Add(mDBSpecific_Component_With_StatusObj);

                //save the specific component details to the database
                mDBContext.SaveChanges();


                mDBLogObj.DBLogId           = getseqDBLogId();
                mDBLogObj.DBCompanyId       = pstringSpecificComponentCompanyId;
                mDBLogObj.DBCSId            = mDBSpecific_Component_With_StatusObj.DBCSId;
                mDBLogObj.DBIncidentId      = -1;
                mDBLogObj.DBComponentName   = mDBSpecific_Component_With_StatusObj.DBComponentName;
                mDBLogObj.DBIncidentName    = "";
                mDBLogObj.DBIncidentDetails = "";
                mDBLogObj.DBDateTimeStart   = DateTime.UtcNow;;
                mDBLogObj.DBStatus          = pstringSpecificComponentStatus;
                mDBLogObj.DBDateTimeEnd     = null;

                //adds component to log history
                mDBContext.DBLogHistories.Add(mDBLogObj);

                //save the log details to the database
                mDBContext.SaveChanges();

                return("Specific Component Successfully added !!");
            }
            catch (DbUpdateException exUpdateDB)
            {
                Console.Write(exUpdateDB);
                return("DbUpdateException");
            }
            catch (DbEntityValidationException exEntityValidateDB)
            {
                Console.Write(exEntityValidateDB);
                return("DbEntityValidationException");
            }
            catch (NotSupportedException exNotSupportedDB)
            {
                Console.Write(exNotSupportedDB);
                return("NotSupportedException");
            }
            catch (ObjectDisposedException exObjectDisposedDB)
            {
                Console.Write(exObjectDisposedDB);
                return("ObjectDisposedException");
            }
            catch (InvalidOperationException exInvalidOperationDB)
            {
                Console.Write(exInvalidOperationDB);
                return("InvalidOperationException");
            }
            catch (Exception ex)
            {
                Console.Write(ex);
                return("Misllaneous Exception");
            }
        }