public void SendMessageToGroup(int groupID, string subject, string message, string sendOnOrAfterDateTime, string expiresAfterDateTime)
        {
            ValidateMessage(subject, message);

            using (DataAccess.CSSDataContext db = new DataAccess.CSSDataContext())
            {
                var login = DataAccess.Login.FindLoginByUsernameOrCallsign(db, HttpContext.Current.User.Identity.Name);

                // Get the groups the login has rights to send messages to.
                var availableGroups = DataAccess.Group.GetGroupsForLogin(db, login.Username, false);

                // Get the target group.
                var group = availableGroups.FirstOrDefault(p => p.Id == groupID);

                if (group == null)
                {
                    throw new Exception("Couldn't find group id: " + groupID);
                }

                // Get the GAGR for the login assigned to the group that has the SL or ASL role.
                var gagrSender = group.Group_Alias_GroupRoles.FirstOrDefault(p => p.Alias.Login.Username.Equals(login.Username, StringComparison.InvariantCultureIgnoreCase) && (p.GroupRole.Name == "Squad Leader" || p.GroupRole.Name == "Assistant Squad Leader" || p.GroupRole.Name == "Zone Lead"));

                if (gagrSender == null)
                {
                    throw new Exception(HttpContext.Current.User.Identity.Name + " does not have rights to send this message.");
                }

                DateTime dateToSend  = DateTime.Parse(sendOnOrAfterDateTime);
                DateTime dateExpires = DateTime.Parse(expiresAfterDateTime);

                DataAccess.GroupMessage groupMessage = new Allegiance.CommunitySecuritySystem.DataAccess.GroupMessage()
                {
                    DateCreated   = DateTime.Now,
                    DateExpires   = dateExpires,
                    DateToSend    = dateToSend,
                    GroupId       = group.Id,
                    Message       = message,
                    SenderAliasId = gagrSender.Alias.Id,
                    Subject       = subject
                };

                db.GroupMessages.InsertOnSubmit(groupMessage);
                db.SubmitChanges();

                foreach (var targetAlias in group.Group_Alias_GroupRoles.Select(p => p.Alias).Distinct())
                {
                    db.GroupMessage_Alias.InsertOnSubmit(new DataAccess.GroupMessage_Alias()
                    {
                        Alias        = targetAlias,
                        DateViewed   = null,
                        GroupMessage = groupMessage
                    });
                }

                db.SubmitChanges();
            }
        }
        public void SendMessageToGroup(int groupID, string subject, string message, string sendOnOrAfterDateTime, string expiresAfterDateTime)
        {
            ValidateMessage(subject, message);

            using (DataAccess.CSSDataContext db = new DataAccess.CSSDataContext())
            {
                var login = DataAccess.Login.FindLoginByUsernameOrCallsign(db, HttpContext.Current.User.Identity.Name);

                // Get the groups the login has rights to send messages to.
                var availableGroups = DataAccess.Group.GetGroupsForLogin(db, login.Username, false);

                // Get the target group.
                var group = availableGroups.FirstOrDefault(p => p.Id == groupID);

                if (group == null)
                    throw new Exception("Couldn't find group id: " + groupID);

                // Get the GAGR for the login assigned to the group that has the SL or ASL role.
                var gagrSender = group.Group_Alias_GroupRoles.FirstOrDefault(p => p.Alias.Login.Username.Equals(login.Username, StringComparison.InvariantCultureIgnoreCase) && (p.GroupRole.Name == "Squad Leader" || p.GroupRole.Name == "Assistant Squad Leader" || p.GroupRole.Name == "Zone Lead"));

                if (gagrSender == null)
                    throw new Exception(HttpContext.Current.User.Identity.Name + " does not have rights to send this message.");

                DateTime dateToSend = DateTime.Parse(sendOnOrAfterDateTime);
                DateTime dateExpires = DateTime.Parse(expiresAfterDateTime);

                DataAccess.GroupMessage groupMessage = new Allegiance.CommunitySecuritySystem.DataAccess.GroupMessage()
                {
                    DateCreated = DateTime.Now,
                    DateExpires = dateExpires,
                    DateToSend = dateToSend,
                    GroupId = group.Id,
                    Message = message,
                    SenderAliasId = gagrSender.Alias.Id,
                    Subject = subject
                };

                db.GroupMessages.InsertOnSubmit(groupMessage);
                db.SubmitChanges();

                foreach (var targetAlias in group.Group_Alias_GroupRoles.Select(p => p.Alias).Distinct())
                {
                    db.GroupMessage_Alias.InsertOnSubmit(new DataAccess.GroupMessage_Alias()
                    {
                        Alias = targetAlias,
                        DateViewed = null,
                        GroupMessage = groupMessage
                    });
                }

                db.SubmitChanges();
            }
        }