/// <summary>
        /// Checks whether the alert should be escalated.
        /// Adds alert to the reminder list.
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool ShouldBeEscalated(ResultEntity result)
        {
            DateTime lastNotification = DateTime.MinValue;

            if (DateTime.Now < result.EscalationTime)
            {
                _Tracer.TraceEvent(TraceEventType.Verbose, 0, "{0}: Not Escalating, Escalation Time Not Met, ID:{1}, UUID:{2}, Created:{3}, Escalated:{4}, Due:{5}", DateTime.Now, result.Id, result.Uuid, result.CreationTime, result.EscalationTime, result.DueTime);
                return false;
            }

            AddReminderEntry(result.Sender);
            AddReminderEntry(result.Receiver);

            if (!result.LevelReference.IsLoaded)
                result.LevelReference.Load();

            if (!result.Level.DirectContactRequired)
            {
                _Tracer.TraceEvent(TraceEventType.Verbose, 0, "{0}: Not Escalating, Direct Contact Not Specified, ID:{1}, UUID:{2}, Created:{3}, Escalated:{4}, Due:{5}", DateTime.Now, result.Id, result.Uuid, result.CreationTime, result.EscalationTime, result.DueTime);
                return false;
            }

            result.Notifications.Load(System.Data.Objects.MergeOption.OverwriteChanges);
            if (result.Notifications.Count() == 0)
            {
                _Tracer.TraceEvent(TraceEventType.Warning, 0, "{0}: Escalating, No Notifications, ID:{1}, UUID:{2}, Created:{3}, Escalated:{4}, Due:{5}", DateTime.Now, result.Id, result.Uuid, result.CreationTime, result.EscalationTime, result.DueTime);
                return true;
            }

            foreach (NotificationEntity notification in result.Notifications)
            {
                if (lastNotification == DateTime.MinValue)
                    lastNotification = notification.CreationTime;
                else if (notification.CreationTime > lastNotification)
                    lastNotification = notification.CreationTime;
            }
            if (lastNotification < result.EscalationTime)
            {
                return true;
            }
            else
            {
                _Tracer.TraceEvent(TraceEventType.Verbose, 0, "{0}: Not Escalating, last notification was at {6}, after escalation time, ID:{1}, UUID:{2}, Created:{3}, Escalated:{4}, Due:{5}", DateTime.Now, result.Id, result.Uuid, result.CreationTime, result.EscalationTime, result.DueTime, lastNotification.ToString());
                return false;
            }
        }
        public ResultEntity CreateResultEntity(ResultEntity result)
        {
            //_ObjectContext.AddToResultEntitySet(result);  shouldn't be necessary since we have added the linkages
            _ObjectContext.SaveChanges();

            return result;
        }
 private void Escalate(ResultEntity result, string transportName)
 {
     _Tracer.TraceEvent(TraceEventType.Information, (int)AgentEvents.EscalatedAlert, "{0}: Sender: {1} Result: {2} Escalation Time: {3}", AgentEvents.EscalatedAlert.ToString(), result.Sender.UserName, result.Id, result.EscalationTime);
     _Manager.CreateResultNotification(result.Sender.UserName, transportName, result.Uuid, "Escalate", Notification.NotificationState.New_Escalated.ToString());
 }
        public ResultEntity CreateResultEntity(string message, DateTime creationTime, string receiverUserName, string senderUserName, string levelName)
        {
            ResultEntity result = new ResultEntity();
            result.Message = message;
            result.CreationTime = creationTime;
            result.Uuid = Guid.NewGuid();
            result.Sender = GetUser(senderUserName);
            result.Receiver = GetUser(receiverUserName);
            result.Level = GetLevel(levelName);

            return CreateResultEntity(result);
        }
        public ResultContextEntity CreateResultContextEntity(ResultEntity resultEntity, ContextTypeEntity contextTypeEntity, string jsonValue, string xmlValue, string patientKey, string examKey)
        {
            ResultContextEntity r = new ResultContextEntity();
            r.JsonValue = jsonValue;
            r.XmlValue = xmlValue;
            r.Result = resultEntity;
            r.ContextType = contextTypeEntity;
            r.PatientKey = patientKey;
            r.ExamKey = examKey;

            _ObjectContext.AddToResultContextEntitySet(r);
            _ObjectContext.SaveChanges();

            return r;
        }
 public Result(ResultEntity e)
 {
     _Entity = e;
     ApplyEntity();
 }
        public bool ResolveEntity()
        {
            if (_Entity != null)
            {
                return true;
            }
            _Entity = new CriticalResultsEntityManager().GetResultEntity(_Uuid);

            if (_Entity != null)
            {
                ApplyEntity();
                return true;
            }
            else
                return false;
        }
        public bool CreateEntity()
        {
            CriticalResultsEntityManager manager = new CriticalResultsEntityManager();

            ResultEntity result = new ResultEntity();
            result.Message = _Message;
            if (_CreationTime == null)
                result.CreationTime = DateTime.Now;
            else
                result.CreationTime = _CreationTime.Value;
            result.Uuid = Guid.NewGuid();

            result.Sender = manager.GetUser(_Sender.UserName);
            result.Receiver = manager.GetUser(_Receiver.UserName);
            result.Level = manager.GetLevel(_Level.Name);

            result.EscalationTime = result.CreationTime + result.Level.EscalationTimespan;
            result.DueTime = result.CreationTime + result.Level.DueTimespan;

            if (result.SenderProxy != null)
            {
                result.SenderProxy = manager.GetUser(_SenderProxy.UserName);
            }

            _Entity = manager.CreateResultEntity(result);

            foreach (ResultContext context in _Context)
            {
                ContextTypeEntity contextTypeEntity = manager.GetContextType(context.ContextType.Name);
                ResultContextEntity resultEntity = manager.CreateResultContextEntity(_Entity, contextTypeEntity, context.JsonValue, context.XmlValue, context.PatientKey, context.ExamKey);
                _Entity.ResultContexts.Add(resultEntity);
            }

            ApplyEntity();

            return true;
        }