public void ChangeMessageState(int idMessage, int idState, string rememberOn) { using (Data.DataContext dataContext = SSOHelper.GetDataContext()) { // Controla permisos var query = from r in dataContext.SSO_Messages join t in dataContext.SSO_Messages_Targets on r.id equals t.idMessage where r.id == idMessage && ((t.targetType == (int)SSOMessageTarget.User && t.target == SSOHelper.CurrentIdentity.Id) || (t.targetType == (int)SSOMessageTarget.Role && dataContext.SSO_UserInRole(SSOHelper.CurrentIdentity.Id, t.target) > 0)) select r; if (query.Count() > 0) { Data.SSO_Messages_States state = (from r in dataContext.SSO_Messages_States where r.idMessage == idMessage && r.idUser == SSOHelper.CurrentIdentity.Id select r).SingleOrDefault(); if (state == null) { state = new Data.SSO_Messages_States(); state.idMessage = idMessage; state.idUser = SSOHelper.CurrentIdentity.Id; dataContext.SSO_Messages_States.InsertOnSubmit(state); } state.idState = idState; state.updatedOn = DateTime.Now; state.rememberOn = String.IsNullOrEmpty(rememberOn) ? null : (DateTime?)DateTime.ParseExact(rememberOn, "yyyy/MM/dd", null).Date; dataContext.SubmitChanges(); } } }
public List <Classes.SSOMessage> GetMessages(int fromIndex, int maxRecords) { if (SSOHelper.CurrentIdentity != null) { using (Data.DataContext dataContext = SSOHelper.GetDataContext()) { List <Classes.SSOMessage> messages; DateTime?timeStamp = dataContext.hsp_Common_LastUpdated("SSO_Messages_States"); /* Consulta esta tabla porque cambia más frecuentemente que SSO_Messages */ if (HttpContext.Current.Cache[String.Format("SSO_Messages_Timestamp_{0}", SSOHelper.CurrentIdentity.Id)] as DateTime? != timeStamp) { messages = (from r in dataContext.SSO_Messages join t in dataContext.SSO_Messages_Targets on r.id equals t.idMessage where (!r.expiresOn.HasValue || (r.expiresOn.HasValue && r.expiresOn >= DateTime.Now.Date)) && r.SSO_Messages_Notifications.Count(n => n.notificationType == (int)SSOMessageNotification.Intranet) > 0 && ((t.targetType == (int)SSOMessageTarget.User && t.target == SSOHelper.CurrentIdentity.Id) || (t.targetType == (int)SSOMessageTarget.Role && dataContext.SSO_UserInRole(SSOHelper.CurrentIdentity.Id, t.target) > 0)) && ((from s in dataContext.SSO_Messages_States where s.idMessage == r.id && s.idUser == SSOHelper.CurrentIdentity.Id && s.idState == (int)SSOMessageState.Sent || (s.idState == (int)SSOMessageState.RememberOn && s.rememberOn <= DateTime.Now) select s).Count() > 0) orderby r.date descending select new Classes.SSOMessage { id = r.id, message = r.message, type = r.type, date = r.date }).Distinct().Take(2).ToList(); /* Guarda en caché sólo 10. Este número debe ser siempre >= al número esperado de mensajes 'maxRecords'. */ HttpContext.Current.Cache[String.Format("SSO_Messages_List_{0}", SSOHelper.CurrentIdentity.Id)] = messages; HttpContext.Current.Cache[String.Format("SSO_Messages_Timestamp_{0}", SSOHelper.CurrentIdentity.Id)] = timeStamp; } else { messages = HttpContext.Current.Cache[String.Format("SSO_Messages_List_{0}", SSOHelper.CurrentIdentity.Id)] as List <Classes.SSOMessage>; } if (maxRecords == 0) { return(messages.Where(m => m.id > fromIndex).ToList()); } else { return(messages.Where(m => m.id > fromIndex).Take(maxRecords).ToList()); } //return (from m in messages where m.id > fromIndex select m).Take(maxRecords); } } else { return(null); } }