Beispiel #1
0
        /* Deletes the message for the specified user. If both users have
         * deleted it, it is removed from database. */
        public bool deleteMessage(MessageBE m, UserBE u = null)
        {
            bool deleted = false;

            bool delSend = false, delAddr = false;
            String sender = "", addressee = "";

            SqlConnection c = new SqlConnection(connection);
            c.Open();

            SqlCommand com = new SqlCommand("SELECT sender, addressee, " +
                "deleted_sender, deleted_reader FROM Messages WHERE code = " + m.code.ToString() + "', 0)", c);

            SqlDataReader dr = com.ExecuteReader();

            while (dr.Read())
            {
                sender = dr["sender"].ToString();
                addressee = dr["addressee"].ToString();
                delSend = bool.Parse(dr["deleted_sender"].ToString());
                delAddr = bool.Parse(dr["deleted_reader"].ToString());
            }

            if (u.Email == sender)
            {
                m.DelSender = true;
                deleted = true;

                if (delAddr == true)
                {
                    com = new SqlCommand("DELETE FROM Messages WHERE code = " + m.code.ToString() + "', 0)", c);
                    com.ExecuteNonQuery();
                }
                else
                {
                    com = new SqlCommand("UPDATE Messages SET deleted_sender = TRUE WHERE code = " + m.code.ToString() + "', 0)", c);
                    com.ExecuteNonQuery();
                }
            }
            else if (u.Email == addressee)
            {
                m.DelAddressee = true;
                deleted = true;

                if (delSend == true)
                {
                    com = new SqlCommand("DELETE FROM Messages WHERE code = " + m.code.ToString() + "', 0)", c);
                    com.ExecuteNonQuery();
                }
                else
                {
                    com = new SqlCommand("UPDATE Messages SET deleted_reader = TRUE WHERE code = " + m.code.ToString() + "', 0)", c);
                    com.ExecuteNonQuery();
                }
            }

            c.Close();

            return deleted;
        }
        protected void send_Message(object sender, EventArgs e)
        {
            bool correct = true;

            if (textuserdest.Text.Length == 0)
            {
                userFeedback.Visible = true;
                correct = false;
            }
            else
            {
                UserBE user1 = new UserBE("", 0, "", "", textuserdest.Text, "");
                UserBE dest = new UserBE(user1.getUserByNick());

                if (dest.Email == "")
                {
                    correct = false;
                    existsFeedback.Visible = true;
                }
            }

            if (textsubject.Text.Length == 0)
            {
                subjectFeedback.Visible = true;
                correct = false;
            }

            if (textmessage.Text.Length == 0)
            {
                messageFeedback.Visible = true;
                correct = false;
            }
            else if (textmessage.Text.Length > 1000)
            {
                lengthFeedback.Visible = true;
                lengthFeedback.Text = "The message is too long! Delete " + (textmessage.Text.Length - 1000).ToString() + " characters.";
                correct = false;
            }
            if (correct)
            {
                String subject = textsubject.Text;
                String message = textmessage.Text;
                DateTime date = DateTime.Now;

                UserBE user1 = new UserBE("", 0, "", "", Session["UserNickname"].ToString(), "");
                UserBE sender1 = new UserBE(user1.getUserByNick());

                user1 = new UserBE("", 0, "", "", textuserdest.Text, "");
                UserBE addressee = new UserBE(user1.getUserByNick());

                MessageBE sndMessage = new MessageBE(sender1, addressee, date, subject, message);

                sndMessage.sendMessage();
                Response.Redirect("ProfileMessages.aspx?Box=Out");
            }
        }
Beispiel #3
0
        protected void reply_Message(object sender, EventArgs e)
        {
            bool correct = true;

            MessageBE message = MessageDAC.getMessage(int.Parse(Request.QueryString["ID"]));
            if (message.Sender == null || message.Addressee == null)
            {
                correct = false;
            }

            if (textmessage.Text.Length == 0)
            {
                messageFeedback.Visible = true;
                correct = false;
            }
            else if (textmessage.Text.Length > 1000)
            {
                lengthFeedback.Visible = true;
                lengthFeedback.Text = "The message is too long! Delete " + (textmessage.Text.Length - 1000).ToString() + " characters.";
                correct = false;
            }
            if (correct)
            {
                String subject = message.Subject;
                String body = textmessage.Text;
                DateTime date = DateTime.Now;

                UserBE sender1 = message.Addressee;
                UserBE addressee = message.Sender;

                MessageBE sndMessage = new MessageBE(sender1, addressee, date, subject, body);

                sndMessage.replyMessage(message);
                Response.Redirect("ProfileMessages.aspx?Box=Out");
            }
        }
Beispiel #4
0
        /// <summary>
        /// Message Getter.
        /// This method gets a message being given its code.
        /// </summary>
        /// <param name="code">The message code.</param>
        /// <returns>A MessageBE is returned.</returns>
        public static MessageBE getMessage(int code)
        {
            MessageBE message = new MessageBE();

            UserDAC user = new UserDAC();
            int read = 0, delSnd = 0, delAddr = 0;

            SqlConnection c = new SqlConnection(connection);
            c.Open();
            SqlCommand com = new SqlCommand("SELECT * FROM message WHERE code= " + code.ToString(), c);

            SqlDataReader dr = com.ExecuteReader();

            while (dr.Read())
            {
                message.code = int.Parse(dr["code"].ToString());
                message.Sender = user.getUser(dr["sender"].ToString());
                message.Addressee = user.getUser(dr["addressee"].ToString());
                message.Subject = dr["issue"].ToString();
                message.Message = dr["body"].ToString();
                message.Date = DateTime.Parse(dr["date"].ToString());
                read = int.Parse(dr["isread"].ToString());
                delSnd = int.Parse(dr["deleted_sender"].ToString());
                delAddr = int.Parse(dr["deleted_reader"].ToString());
                message.ConversCode = int.Parse(dr["convers_code"].ToString());
            }

            message.Read = false;
            message.DelSender = false;
            message.DelAddressee = false;
            if (read == 1) message.Read = true;
            if (delSnd == 1) message.DelSender = true;
            if (delAddr == 1) message.DelAddressee = true;

            c.Close();

            return message;
        }
Beispiel #5
0
        /// <summary>
        /// Message Reader.
        /// Updates the field isRead of the given message so we know that it has been opened.
        /// </summary>
        /// <param name="m">The message to be read.</param>
        /// <returns>It returns a boolean: true if we could read it.</returns>
        public bool readMessage(MessageBE m)
        {
            bool read = false;
            SqlConnection c = new SqlConnection(connection);
            c.Open();

            SqlCommand com = new SqlCommand("UPDATE message SET isread = 1 WHERE code = " + m.code.ToString(), c);
            com.ExecuteNonQuery();

            c.Close();
            return read;
        }
Beispiel #6
0
        // /////////////////////////////////////////////////////////////////////
        // Methods /////////////////////////////////////////////////////////////
        // /////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Message Insertion.
        /// Inserts the given message in the database.
        /// </summary>
        /// <param name="m">The message to be inserted.</param>
        public void insertMessage(MessageBE m)
        {
            int read = 0; if (m.Read) read = 1;
            int delSend = 0; if (m.DelSender) delSend = 1;
            int delAddr = 0; if (m.DelAddressee) delAddr = 1;

            SqlConnection c = new SqlConnection(connection);
            c.Open();
            SqlCommand com;
            com = new SqlCommand("INSERT INTO message (code, issue, body, sender, addressee, date, isread, deleted_sender, deleted_reader, convers_code)" +
                "VALUES ('" + m.code.ToString() + "','" + m.Subject + "','" + m.Message + "','" + m.Sender.Email + "','" + m.Addressee.Email +
                "','" + m.Date.ToString("G") + "','" + read + "','" + delSend + "','" + delAddr + "','" + m.ConversCode.ToString() + "')", c);

            com.ExecuteNonQuery();
            c.Close();
        }
Beispiel #7
0
        /// <summary>
        /// Conversation Before.
        /// This method gets the datase of messages that compound the conversation
        /// before the given message.
        /// </summary>
        /// <param name="message">The reference message.</param>
        /// <returns>A datase is returned with the conversation.</returns>
        public DataSet getConversationBefore(MessageBE message)
        {
            DataSet d = new DataSet();
            String s = ConfigurationManager.ConnectionStrings["ShodeDDBB"].ToString();
            SqlConnection c = new SqlConnection(s);
            SqlDataAdapter da = new SqlDataAdapter("SELECT u1.nickname AS Sender, u2.nickname as Addressee, SUBSTRING(body, 1, 30) as Body, date as Date, code as Code, isRead as IsRead " +
                "FROM message, users u1, users u2 WHERE convers_code = " + message.ConversCode.ToString() + "AND code < " + message.code.ToString() +
                " AND u1.email = sender AND u2.email = addressee ORDER BY code ASC", c);
            da.Fill(d, "message");
            c.Close();

            return d;
        }
Beispiel #8
0
 /// <summary>
 /// Message Reply Creation.
 /// This method inserts or creates the message reply in the database. We generate
 /// a unique code for it and then using the DAC it is inserted. It is similar
 /// to the previous method.
 /// </summary>
 public void replyMessage(MessageBE Original)
 {
     this.Mcode = generateCode();
     this.conversCode = Original.ConversCode;
     messageDAC.insertMessage(this);
 }
Beispiel #9
0
 /* Sends a new message replying the one passed as parameter. */
 public void replyMessage(MessageBE Original)
 {
     OriginalMessage = Original;
     messageDAC.insertMessage(this);
 }
Beispiel #10
0
        /* Gets all messages */
        public List<MessageBE> getAllMessages()
        {
            List<MessageBE> messages = new List<MessageBE>();
            MessageBE tempM = new MessageBE();
            UserDAC user = new UserDAC();

            SqlConnection c = new SqlConnection(connection);
            c.Open();
            SqlCommand com = new SqlCommand("SELECT * FROM Messages", c);
            SqlDataReader dr = com.ExecuteReader();

            while (dr.Read())
            {
                tempM = new MessageBE();
                tempM.code = int.Parse(dr["code"].ToString());
                tempM.Sender = user.getUser(dr["sender"].ToString());
                tempM.Addressee = user.getUser(dr["addressee"].ToString());
                tempM.Subject = dr["issue"].ToString();
                tempM.Message = dr["body"].ToString();
                tempM.Date = DateTime.Parse(dr["date"].ToString());
                tempM.Read = bool.Parse(dr["isread"].ToString());
                tempM.DelSender = bool.Parse(dr["deleted_sender"].ToString());
                tempM.DelAddressee = bool.Parse(dr["deleted_reader"].ToString());
                tempM.OriginalMessage = getMessage(int.Parse(dr["original"].ToString()));

                messages.Add(tempM);
            }

            c.Close();

            return messages;
        }
Beispiel #11
0
        /* ****************************************************************** */
        /* Methods                                                            */
        /* ****************************************************************** */
        /* Inserts a new message */
        public void insertMessage(MessageBE m)
        {
            SqlConnection c = new SqlConnection(connection);
            c.Open();
            SqlCommand com;

            if (m.OriginalMessage != null)
            {
                com = new SqlCommand("INSERT INTO Messages (code, issue, body, isread, deleted_sender, deleted_reader, original)" +
                    "VALUES ('" + m.code.ToString() + "','" + m.Subject + "','" + m.Message + "','" + m.Read.ToString() + "','" +
                    m.DelSender.ToString() + "','" + m.DelAddressee.ToString() + "','" + m.OriginalMessage.code + "')", c);
            }
            else
            {
                com = new SqlCommand("INSERT INTO Messages (code, issue, body, isread, deleted_sender, deleted_reader, original)" +
                    "VALUES ('" + m.code.ToString() + "','" + m.Subject + "','" + m.Message + "','" + m.Read.ToString() + "','" +
                    m.DelSender.ToString() + "','" + m.DelAddressee.ToString() + "', NULL)", c);
            }

            com.ExecuteNonQuery();
            c.Close();
        }
Beispiel #12
0
        /* If addresee is NULL, gets the list of sent messages by sender.
         * If sender is NULL, gets the list of received messages by addresee.
         * If anyone is NULL, gets the list of messages traded by sender and addressee. */
        public List<MessageBE> getMessages(UserBE sender, UserBE addressee)
        {
            List<MessageBE> messages = new List<MessageBE>();
            if (sender == null && addressee == null)
            {
                MessageBE tempM = new MessageBE();
                UserDAC user = new UserDAC();

                SqlConnection c = new SqlConnection(connection);
                c.Open();
                SqlCommand com;

                if (sender == null)
                {
                    com = new SqlCommand("SELECT * FROM Messages WHERE addressee='" + addressee.Email + "'", c);
                }
                else if (addressee == null)
                {
                    com = new SqlCommand("SELECT * FROM Messages WHERE sender='" + sender.Email + "'", c);
                }
                else
                {
                    com = new SqlCommand("SELECT * FROM Messages WHERE sender='" + sender.Email + "' and addressee='" + addressee.Email + "'", c);
                }
                SqlDataReader dr = com.ExecuteReader();

                while (dr.Read())
                {
                    tempM = new MessageBE();
                    tempM.code = int.Parse(dr["code"].ToString());
                    tempM.Sender = user.getUser(dr["sender"].ToString());
                    tempM.Addressee = user.getUser(dr["addressee"].ToString());
                    tempM.Subject = dr["issue"].ToString();
                    tempM.Message = dr["body"].ToString();
                    tempM.Date = DateTime.Parse(dr["date"].ToString());
                    tempM.Read = bool.Parse(dr["isread"].ToString());
                    tempM.DelSender = bool.Parse(dr["deleted_sender"].ToString());
                    tempM.DelAddressee = bool.Parse(dr["deleted_reader"].ToString());
                    tempM.OriginalMessage = getMessage(int.Parse(dr["original"].ToString()));

                    messages.Add(tempM);
                }

                c.Close();
            }

            return messages;
        }
Beispiel #13
0
        /* Gets the list of message that compound a conversation. */
        public List<MessageBE> getConversation(MessageBE m)
        {
            List<MessageBE> messages = new List<MessageBE>();

            if (m.OriginalMessage != null)
            {
                MessageBE message = getMessage(m.code);
                MessageBE tempM = message.OriginalMessage;

                do
                {
                    messages.Add(tempM);
                    tempM = tempM.OriginalMessage;

                } while (tempM != null);
            }

            return messages;
        }