AlertDataEHS PopulateAlertDataTest()
        {
            AlertDataEHS d = new AlertDataEHS();

            d.incidentDateTime = "Tuesday, Mar 19, 2014 4:00 PM";
            d.incidentLocation = "Location: Clamart";
            d.incidentNumber   = "Incident #: 135";
            d.incidentType     = "Near Miss";

            d.incidentDescription = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet lacinia sapien. Ut nec luctus leo. Mauris a lacus tellus. Nunc quis ultricies eros, ac molestie justo. Ut pellentesque libero commodo tempus aliquet. Vivamus molestie venenatis elit sed imperdiet.";
            d.incidentRootCause   = new List <string> {
                "The root cause for the incident described above."
            };
            d.incidentContainment = new List <string> {
                "Here is a description of the various steps we used to contain the incident that is listed."
            };
            d.incidentCorrectiveActions = new List <string> {
                "Step 1: stop this from happening again.  Step 2: Make sure it never happens at a later date."
            };

            return(d);
        }
        AlertDataEHS PopulateByIncidentId(decimal iid)
        {
            AlertDataEHS d        = new AlertDataEHS();
            var          entities = new PSsqmEntities();

            var incident = EHSIncidentMgr.SelectIncidentById(entities, iid);

            if (incident != null)
            {
                string plantName = EHSIncidentMgr.SelectPlantNameById((decimal)incident.DETECT_PLANT_ID);
                d.incidentLocation = String.Format("Location: {0}", plantName);
                d.incidentNumber   = String.Format("Incident #: {0}", iid);

                string  incidentType   = EHSIncidentMgr.SelectIncidentTypeByIncidentId(iid);
                decimal incidentTypeId = EHSIncidentMgr.SelectIncidentTypeIdByIncidentId(iid);
                decimal companyId      = incident.DETECT_COMPANY_ID;
                var     questions      = EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 0);
                questions.AddRange(EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 1));

                // Date/Time

                d.incidentDateTime = incident.INCIDENT_DT.ToLongDateString();

                var timeQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.TimeOfDay);
                if (timeQuestion != null)
                {
                    string timeAnswer = (from a in entities.INCIDENT_ANSWER
                                         where
                                         a.INCIDENT_ID == incident.INCIDENT_ID &&
                                         a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.TimeOfDay
                                         select a.ANSWER_VALUE).FirstOrDefault();

                    if (!string.IsNullOrEmpty(timeAnswer))
                    {
                        d.incidentDateTime += " " + Convert.ToDateTime(timeAnswer).ToShortTimeString();
                    }
                }

                // Incident Type

                d.incidentType = incidentType;

                // Description

                d.incidentDescription = incident.DESCRIPTION;

                // Root Cause(s)

                var rootCauseQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.RootCause);
                if (rootCauseQuestion != null)
                {
                    string rootCauseAnswer = (from a in entities.INCIDENT_ANSWER
                                              where
                                              a.INCIDENT_ID == iid &&
                                              a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.RootCause
                                              select a.ANSWER_VALUE).FirstOrDefault();

                    if (!string.IsNullOrEmpty(rootCauseAnswer))
                    {
                        d.incidentRootCause = new List <string> {
                            rootCauseAnswer
                        }
                    }
                    ;
                }


                // Containment

                var containmentQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.Containment);
                if (containmentQuestion != null)
                {
                    string containmentAnswer = (from a in entities.INCIDENT_ANSWER
                                                where
                                                a.INCIDENT_ID == iid &&
                                                a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.Containment
                                                select a.ANSWER_VALUE).FirstOrDefault();

                    if (!string.IsNullOrEmpty(containmentAnswer))
                    {
                        d.incidentContainment = new List <string> {
                            containmentAnswer
                        }
                    }
                    ;
                }

                // Corrective Actions

                var correctiveQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.CorrectiveActions);
                if (correctiveQuestion != null)
                {
                    string correctiveAnswer = (from a in entities.INCIDENT_ANSWER
                                               where
                                               a.INCIDENT_ID == iid &&
                                               a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.CorrectiveActions
                                               select a.ANSWER_VALUE).FirstOrDefault();

                    if (!string.IsNullOrEmpty(correctiveAnswer))
                    {
                        d.incidentCorrectiveActions = new List <string> {
                            correctiveAnswer
                        }
                    }
                    ;
                }


                var files = (from a in entities.ATTACHMENT
                             where
                             (a.RECORD_ID == iid && a.RECORD_TYPE == 40 && a.DISPLAY_TYPE > 0) &&
                             (a.FILE_NAME.ToLower().Contains(".jpg") || a.FILE_NAME.ToLower().Contains(".jpeg") ||
                              a.FILE_NAME.ToLower().Contains(".gif") || a.FILE_NAME.ToLower().Contains(".png") ||
                              a.FILE_NAME.ToLower().Contains(".bmp"))
                             orderby a.RECORD_TYPE, a.FILE_NAME
                             select new
                {
                    Data = (from f in entities.ATTACHMENT_FILE where f.ATTACHMENT_ID == a.ATTACHMENT_ID select f.ATTACHMENT_DATA).FirstOrDefault(),
                    Description = (string.IsNullOrEmpty(a.FILE_DESC)) ? "" : a.FILE_DESC,
                }).ToList();


                if (files.Count > 0)
                {
                    d.photoData     = new List <byte[]>();
                    d.photoCaptions = new List <string>();

                    foreach (var f in files)
                    {
                        d.photoData.Add(f.Data);
                        d.photoCaptions.Add(f.Description);
                    }
                }
            }

            return(d);
        }
        AlertDataEHS PopulateByProblemCaseId(decimal pcid)
        {
            AlertDataEHS d        = new AlertDataEHS();
            var          entities = new PSsqmEntities();

            PROB_CASE probCase = ProblemCase.LookupCase(entities, pcid);

            if (probCase != null)
            {
                List <INCIDENT> incidentList = ProblemCase.LookupProbIncidentList(entities, probCase);

                if (incidentList.Count > 0)
                {
                    var incident = incidentList[0];

                    string plantName = EHSIncidentMgr.SelectPlantNameById((decimal)incident.DETECT_PLANT_ID);
                    d.incidentLocation = String.Format("Location: {0}", plantName);
                    d.incidentNumber   = String.Format("Incident #: {0}", incident.INCIDENT_ID);

                    string  incidentType   = EHSIncidentMgr.SelectIncidentTypeByIncidentId(incident.INCIDENT_ID);
                    decimal incidentTypeId = EHSIncidentMgr.SelectIncidentTypeIdByIncidentId(incident.INCIDENT_ID);
                    decimal companyId      = incident.DETECT_COMPANY_ID;
                    var     questions      = EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 0);
                    questions.AddRange(EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 1));

                    d.incidentDateTime = incidentList[0].INCIDENT_DT.ToLongDateString();

                    var timeQuestion = questions.FirstOrDefault(q => q.QuestionId == 5);
                    if (timeQuestion != null)
                    {
                        string timeAnswer = (from a in entities.INCIDENT_ANSWER
                                             where
                                             a.INCIDENT_ID == incident.INCIDENT_ID &&
                                             a.INCIDENT_QUESTION_ID == 5
                                             select a.ANSWER_VALUE).FirstOrDefault();

                        if (!string.IsNullOrEmpty(timeAnswer))
                        {
                            d.incidentDateTime += " " + Convert.ToDateTime(timeAnswer).ToShortTimeString();
                        }
                    }

                    d.incidentType        = incidentType;
                    d.incidentDescription = probCase.DESC_LONG;


                    // Root Cause(s)

                    List <PROB_CAUSE_STEP> probCauses = (from i in entities.PROB_CAUSE_STEP
                                                         where
                                                         i.PROBCASE_ID == pcid &&
                                                         i.IS_ROOTCAUSE == true
                                                         select i).ToList();

                    if (probCauses.Count > 0)
                    {
                        d.incidentRootCause = (from rc in probCauses select rc.HOW_CONFIRMED).ToList();
                    }

                    // Containment

                    var containment = (from i in entities.PROB_CONTAIN
                                       where i.PROBCASE_ID == pcid
                                       select new
                    {
                        Recommendation = i.CONTAINMENT_DESC,
                        Action = i.INITIAL_ACTION,
                        Results = i.INITIAL_RESULTS
                    }).FirstOrDefault();

                    var actions = (from i in entities.PROB_CONTAIN_ACTION
                                   where i.PROBCASE_ID == pcid
                                   select i.ACTION_ITEM).ToList();

                    if (containment != null)
                    {
                        d.incidentContainment = new List <string>();

                        //if (!string.IsNullOrEmpty(containment.Recommendation))
                        //{
                        //	d.incidentContainment.Add("RECOMMENDATION: " + containment.Recommendation);
                        //	d.incidentContainment.Add(" ");
                        //}

                        if (actions != null)
                        {
                            string strActions;
                            int    i = 1;
                            foreach (var actionItem in actions)
                            {
                                strActions = i++ + ") " + actionItem;
                                d.incidentContainment.Add(strActions);
                            }
                        }

                        if (!string.IsNullOrEmpty(containment.Results))
                        {
                            d.incidentContainment.Add(" ");
                            d.incidentContainment.Add("RESULTS: " + containment.Results);
                        }
                    }

                    // Corrective Actions

                    var correctiveActions = (from i in entities.PROB_CAUSE_ACTION
                                             where i.PROBCASE_ID == pcid
                                             select i.ACTION_DESC).ToList();

                    if (correctiveActions.Count() > 0)
                    {
                        d.incidentCorrectiveActions = new List <string>();
                        int i = 1;
                        foreach (var ca in correctiveActions)
                        {
                            d.incidentCorrectiveActions.Add(i++ + ") " + ca);
                        }
                    }

                    // Photos

                    var files = (from a in entities.ATTACHMENT
                                 where
                                 ((a.RECORD_ID == incident.INCIDENT_ID && a.RECORD_TYPE == 40) || (a.RECORD_ID == pcid && a.RECORD_TYPE == 21)) &&
                                 (a.DISPLAY_TYPE > 0) &&
                                 (a.FILE_NAME.ToLower().Contains(".jpg") || a.FILE_NAME.ToLower().Contains(".jpeg") ||
                                  a.FILE_NAME.ToLower().Contains(".gif") || a.FILE_NAME.ToLower().Contains(".png") ||
                                  a.FILE_NAME.ToLower().Contains(".bmp"))
                                 orderby a.RECORD_TYPE, a.FILE_NAME
                                 select new
                    {
                        Data = (from f in entities.ATTACHMENT_FILE where f.ATTACHMENT_ID == a.ATTACHMENT_ID select f.ATTACHMENT_DATA).FirstOrDefault(),
                        Description = (string.IsNullOrEmpty(a.FILE_DESC)) ? "" : a.FILE_DESC,
                    }).ToList();

                    if (files.Count > 0)
                    {
                        d.photoData     = new List <byte[]>();
                        d.photoCaptions = new List <string>();

                        foreach (var f in files)
                        {
                            d.photoData.Add(f.Data);
                            d.photoCaptions.Add(f.Description);
                        }
                    }
                }
            }

            return(d);
        }