/// <summary>
 /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUsers(User user)
 {
     base.AddObject("Users", user);
 }
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="mSISDN">Initial value of the MSISDN property.</param>
 /// <param name="authCodeSHA1">Initial value of the AuthCodeSHA1 property.</param>
 public static User CreateUser(global::System.Int32 userId, global::System.String mSISDN, global::System.String authCodeSHA1)
 {
     User user = new User();
     user.UserId = userId;
     user.MSISDN = mSISDN;
     user.AuthCodeSHA1 = authCodeSHA1;
     return user;
 }
        private void SendMessage(User senderUser, User recipientUser, 
			MessageType messageType, string msgText)
        {
            Message msg = new Message();
            msg.FromUserId = senderUser.UserId;
            msg.ToUserId = recipientUser.UserId;
            msg.MsgType = messageType.ToString();
            msg.MsgDate = DateTime.Now;
            msg.MsgText = msgText;
            CryptoChatEntities context = new CryptoChatEntities();
            context.Messages.AddObject(msg);
            context.SaveChanges();
        }
        private void DeleteWaitingMessagesAndSessions(User recipientUser)
        {
            CryptoChatEntities context = new CryptoChatEntities();

            // Delete any waiting messages for the user
            string deleteMessagesSQL = "DELETE FROM Messages WHERE ToUserId={0}";
            context.ExecuteStoreCommand(deleteMessagesSQL,
                new object[] { recipientUser.UserId });

            // Delete any existing chat sessions associated with the user
            string deleteChatSessionsSQL = "DELETE FROM ChatSessions WHERE " +
                "FromUserId={0} or ToUserId={0}";
            context.ExecuteStoreCommand(deleteChatSessionsSQL,
                new object[] { recipientUser.UserId });
        }
        private void DeleteChatSession(User senderUser, User recipientUser)
        {
            CryptoChatEntities context = new CryptoChatEntities();

            // Delete any existing chat sessions between the users
            string deleteChatSessionsSQL = "DELETE FROM ChatSessions WHERE " +
                "(FromUserId={0} and ToUserId={1}) or (FromUserId={1} and ToUserId={0})";
            context.ExecuteStoreCommand(deleteChatSessionsSQL,
                new object[] { senderUser.UserId, recipientUser.UserId });

            // Delete any waiting chat messages between the users
            string msgTypesToDelete =
                "'" + MessageType.MSG_CHALLENGE.ToString() + "'," +
                "'" + MessageType.MSG_RESPONSE.ToString() + "'," +
                "'" + MessageType.MSG_START_CHAT.ToString() + "'," +
                "'" + MessageType.MSG_CANCEL_CHAT.ToString() + "'," +
                "'" + MessageType.MSG_CHAT_MESSAGE.ToString() + "'";
            string deleteMessagesSQL = "DELETE FROM Messages WHERE " +
                "((FromUserId={0} and ToUserId={1}) or (FromUserId={1} and ToUserId={0})) AND " +
                "(MsgType in (" + msgTypesToDelete + "))";
            context.ExecuteStoreCommand(deleteMessagesSQL,
                new object[] { senderUser.UserId, recipientUser.UserId });
        }
        internal static void SendMessageToAllOnlineUsers(
			User senderUser, MessageType msgType, string msgText)
        {
            DateTime minAllowedLastActivityTime =
                DateTime.Now.AddSeconds(-Settings.Default.HttpSessionTimeoutSeconds);
            CryptoChatEntities context = new CryptoChatEntities();
            var allUserIds =
                from u in context.Users
                where u.LastActivity >= minAllowedLastActivityTime
                select u.UserId;
            foreach (var recipientUserId in allUserIds)
            {
                if (senderUser.UserId != recipientUserId)
                {
                    Message msg = new Message();
                    msg.FromUserId = senderUser.UserId;
                    msg.ToUserId = recipientUserId;
                    msg.MsgType = msgType.ToString();
                    msg.MsgText = msgText;
                    msg.MsgDate = DateTime.Now;
                    context.Messages.AddObject(msg);
                }
            }
            context.SaveChanges();
        }
        public LoginResponse RegisterNewUser(string msisdn, string authCode)
        {
            CheckMSISDN(msisdn);
            CheckAuthCode(authCode);
            LoginResponse loginResult = null;
            ExecuteAndHandleExceptions(() =>
            {
                // Register the user in the DB
                CryptoChatEntities context = new CryptoChatEntities();
                User user = new User();
                user.MSISDN = msisdn.ToLowerInvariant();
                user.AuthCodeSHA1 = authCode;
                context.Users.AddObject(user);
                context.SaveChanges();

                // Login the user into the system after the registration
                loginResult = LoginUser(msisdn, authCode);

                // Send the "user online" notification to all online users
                SendMessageToAllOnlineUsers(user,
                    MessageType.MSG_USER_ONLINE, "New user registered in the system.");
            });
            return loginResult;
        }