diff --git a/src/main/java/controller/CountMessagesServlet.java b/src/main/java/controller/CountMessagesServlet.java index fe221d8..77a4e32 100644 --- a/src/main/java/controller/CountMessagesServlet.java +++ b/src/main/java/controller/CountMessagesServlet.java @@ -1,8 +1,7 @@ package controller; -import model.service.message.Conversation; +import model.service.message.MessageManager; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -15,11 +14,11 @@ public class CountMessagesServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { HttpSession session = request.getSession(); int recipientId = (int) session.getAttribute("id"); - Conversation conversation = new Conversation(); - int receivedMessageCount = conversation.countReceivedMessages(recipientId); + MessageManager messageManager = new MessageManager(); + int receivedMessageCount = messageManager.countReceivedMessages(recipientId); response.setContentType("text/plain"); // Output is a plain text integer response.getWriter().println(receivedMessageCount); diff --git a/src/main/java/controller/GetMessageServlet.java b/src/main/java/controller/GetMessageServlet.java index 9ebd746..4d5fda5 100644 --- a/src/main/java/controller/GetMessageServlet.java +++ b/src/main/java/controller/GetMessageServlet.java @@ -2,7 +2,6 @@ import java.io.IOException; import java.sql.Timestamp; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -10,21 +9,22 @@ import javax.servlet.http.HttpSession; import model.entity.Message; -import model.service.message.Conversation; +import model.service.message.MessageManager; + import java.util.List; import javax.json.*; @WebServlet("/GetMessages") public class GetMessageServlet extends HttpServlet { - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); int userId = (int) session.getAttribute("id"); int contactId = Integer.parseInt(request.getParameter("contact_id")); // Get the contact's ID from the request // Retrieve the messages between the user and the contact - Conversation conversation = new Conversation(); - List messages = conversation.retrieveMessages(userId, contactId); + MessageManager messageManager = new MessageManager(); + List messages = messageManager.retrieveMessages(userId, contactId); // Now build the JSON response JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); @@ -43,7 +43,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t jsonObjectBuilder.add("sent", formattedDate); jsonArrayBuilder.add(jsonObjectBuilder.build()); } - conversation.markMessagesAsRead(contactId, userId); + messageManager.markMessagesAsRead(contactId, userId); JsonArray jsonArray = jsonArrayBuilder.build(); response.setContentType("application/json"); @@ -51,7 +51,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t } protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { try { HttpSession session = request.getSession(); int sender = (int) session.getAttribute("id"); @@ -59,8 +59,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) String body = request.getParameter("body"); // send the message - Conversation conversation = new Conversation(); - conversation.sendMessage(sender, recipient, body); + MessageManager messageManager = new MessageManager(); + messageManager.sendMessage(sender, recipient, body); response.getWriter().write("Message successfully sent"); diff --git a/src/main/java/controller/LoginController.java b/src/main/java/controller/LoginController.java index c9c4201..0590942 100644 --- a/src/main/java/controller/LoginController.java +++ b/src/main/java/controller/LoginController.java @@ -1,13 +1,11 @@ package controller; -import model.DAO.DAOPersonalInfo; import model.entity.PersonalInfo; import model.entity.User; import model.service.login.Authenticator; import model.service.user.UserData; import model.service.user.UserRegistry; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -28,7 +26,7 @@ public void init() { } protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { String email = request.getParameter("email"); String password = request.getParameter("password"); diff --git a/src/main/java/controller/ResetPasswordServlet.java b/src/main/java/controller/ResetPasswordServlet.java index 4d123a1..1d9f27f 100644 --- a/src/main/java/controller/ResetPasswordServlet.java +++ b/src/main/java/controller/ResetPasswordServlet.java @@ -24,8 +24,5 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) if(authenticator.resetPassword(email, password)){ response.getWriter().write("Password cambiata con successo!"); } - - - } } diff --git a/src/main/java/controller/SendResetPin.java b/src/main/java/controller/SendResetPin.java index 84f1519..e26c858 100644 --- a/src/main/java/controller/SendResetPin.java +++ b/src/main/java/controller/SendResetPin.java @@ -1,6 +1,5 @@ package controller; -import model.entity.User; import model.service.login.Authenticator; import model.service.user.UserData; diff --git a/src/main/java/model/service/message/Conversation.java b/src/main/java/model/service/message/Conversation.java deleted file mode 100644 index f82b933..0000000 --- a/src/main/java/model/service/message/Conversation.java +++ /dev/null @@ -1,72 +0,0 @@ -package model.service.message; - -import model.DAO.DAOMessage; -import model.entity.Message; -import model.entity.User; -import model.service.user.UserData; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class Conversation { - private int userId; - private int unreadMessages; - private final Map> conversations = new HashMap<>(); //Integer è l'ID dell'Utente con cui chatta. La lista dei messaggi è la lista dei messaggi con quell'utente - DAOMessage db = new DAOMessage(); - - public int getUserId() { - return userId; - } - - - public int getUnreadMessagesCounter() { - return unreadMessages; - } - - public Map> getConversations() { - return conversations; - } - - public Conversation(){} - - public void markMessagesAsRead(int senderId, int recipientId) { - db.markMessagesAsRead(senderId, recipientId); - } - - public int countReceivedMessages(int recipientId) { - return db.countReceivedMessages(recipientId); - } - - public void sendMessage(int sender, int recipientId, String text) { - db.sendMessage(sender, recipientId, text); - } - public List retrieveMessages(int userId, int contact) { - return db.retrieveMessages(userId, contact); - } - public int getUnreadMessagesForConversation(int userId, int contact){ - int unreadCounter = 0; - List messages = db.retrieveMessages(userId, contact); - for (Message message : messages) { - if (message.getRecipient() == userId && !message.isRead()) { - unreadCounter++; - } - } - return unreadCounter; - } - - public List retrieveAllTheContacts(int userId){ - List contacts = new ArrayList<>(); - UserData check = new UserData(); - User user = check.getUserByIdOrEmail(userId); - if(check.isTherapist(user)) { - contacts.addAll(db.retrieveUserIdsByTherapist(userId)); - } - else { //se è un paziente - contacts.add(user.getIdTherapist()); - } - contacts.add(0); //Notifications - return contacts; - } -} diff --git a/src/main/java/model/service/message/MessageManager.java b/src/main/java/model/service/message/MessageManager.java index 625d6a5..434e870 100644 --- a/src/main/java/model/service/message/MessageManager.java +++ b/src/main/java/model/service/message/MessageManager.java @@ -1,4 +1,63 @@ package model.service.message; -public class MessageManager { +import model.DAO.DAOMessage; +import model.entity.Message; +import model.entity.User; +import model.service.user.UserData; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MessageManager implements MessageManagerInterface { + private int userId; + private int unreadMessages; + private final Map> conversations = new HashMap<>(); //Integer è l'ID dell'Utente con cui chatta. La lista dei messaggi è la lista dei messaggi con quell'utente + DAOMessage db = new DAOMessage(); + + public int getUserId() { + return userId; + } + + public MessageManager(){} + + public void markMessagesAsRead(int senderId, int recipientId) { + db.markMessagesAsRead(senderId, recipientId); + } + + public int countReceivedMessages(int recipientId) { + return db.countReceivedMessages(recipientId); + } + + public void sendMessage(int sender, int recipientId, String text) { + db.sendMessage(sender, recipientId, text); + } + public List retrieveMessages(int userId, int contact) { + return db.retrieveMessages(userId, contact); + } + public int getUnreadMessagesForConversation(int userId, int contact){ + int unreadCounter = 0; + List messages = db.retrieveMessages(userId, contact); + for (Message message : messages) { + if (message.getRecipient() == userId && !message.isRead()) { + unreadCounter++; + } + } + return unreadCounter; + } + + public List retrieveAllTheContacts(int userId){ + List contacts = new ArrayList<>(); + UserData check = new UserData(); + User user = check.getUserByIdOrEmail(userId); + if(check.isTherapist(user)) { + contacts.addAll(db.retrieveUserIdsByTherapist(userId)); + } + else { //se è un paziente + contacts.add(user.getIdTherapist()); + } + contacts.add(0); //Notifications + return contacts; + } } diff --git a/src/main/java/model/service/message/MessageManagerInterface.java b/src/main/java/model/service/message/MessageManagerInterface.java new file mode 100644 index 0000000..8728fa9 --- /dev/null +++ b/src/main/java/model/service/message/MessageManagerInterface.java @@ -0,0 +1,60 @@ +package model.service.message; + +import model.entity.Message; + +import java.util.List; + +public interface MessageManagerInterface { + + /** + * Marks the messages between a sender and a recipient as read in the database. + * This method updates the 'Read' field of the messages from the sender to the recipient to TRUE. + * + * @param senderId the ID of the sender + * @param recipientId the ID of the recipient + */ + void markMessagesAsRead(int senderId, int recipientId); + + /** + * Counts the number of received messages for a given recipient. + * + * @param recipientId the ID of the recipient + * @return the number of received messages for the recipient + */ + int countReceivedMessages(int recipientId); + + /** + * Sends a message from a sender to a recipient by inserting it into the database. + * + * @param sender the ID of the sender + * @param recipientId the ID of the recipient + * @param text the content of the message + */ + void sendMessage(int sender, int recipientId, String text); + + /** + * Retrieves the messages between a user and a contact. + * + * @param userId the ID of the user + * @param contact the ID of the contact + * @return the list of messages between the user and the contact + */ + List retrieveMessages(int userId, int contact); + + /** + * Retrieves the number of unread messages for a specific conversation between a user and a contact. + * + * @param userId the ID of the user + * @param contact the ID of the contact + * @return the number of unread messages for the conversation + */ + int getUnreadMessagesForConversation(int userId, int contact); + + /** + * Retrieves all the contacts of a user. + * + * @param userId the ID of the user + * @return a list of integers representing the IDs of the contacts + */ + List retrieveAllTheContacts(int userId); +} diff --git a/src/main/webapp/CSS/messageCenter.css b/src/main/webapp/CSS/messageCenter.css index d3ebdc0..5d5c656 100644 --- a/src/main/webapp/CSS/messageCenter.css +++ b/src/main/webapp/CSS/messageCenter.css @@ -20,7 +20,7 @@ body { top: 0; width: 100%; height: 20%; - background-color: #009688; + background-color: #f0f0f0; } .main-container { @@ -56,7 +56,8 @@ body { left: 0; width: 100%; height: 100%; - background: url(https://camo.githubusercontent.com/854a93c27d64274c4f8f5a0b6ec36ee1d053cfcd934eac6c63bed9eaef9764bd/68747470733a2f2f7765622e77686174736170702e636f6d2f696d672f62672d636861742d74696c652d6461726b5f61346265353132653731393562366237333364393131306234303866303735642e706e67); + background: url(https://wallpapers.com/images/hd/whatsapp-chat-background-wz8v2v8xnenkb4ur.jpg); + /*https://camo.githubusercontent.com/854a93c27d64274c4f8f5a0b6ec36ee1d053cfcd934eac6c63bed9eaef9764bd/68747470733a2f2f7765622e77686174736170702e636f6d2f696d672f62672d636861742d74696c652d6461726b5f61346265353132653731393562366237333364393131306234303866303735642e706e67*/ opacity: 0.5; } @@ -420,4 +421,35 @@ img { font-size: 1em; border:none; outline:none; +} + +@media (max-width: 768px) { + .main-container { + flex-direction: column; + } + .main-container .left-container, + .main-container .right-container { + width: 100%; + flex: auto; /* reset the flex property */ + } + .main-container .right-container { + display: none; /* initially hide right container */ + } + /* CSS class to be added by JS when a chat is selected */ + .main-container.show-right .left-container { + display: none; + } + .main-container.show-right .right-container { + display: block; + } + /* CSS rules for back button */ + #back-button { + position: fixed; + top: 0; + left: 0; + display: none; /* initially hidden */ + } + .main-container.show-right #back-button { + display: block; + } } \ No newline at end of file diff --git a/src/main/webapp/JS/message.js b/src/main/webapp/JS/message.js index cb70254..694c7ba 100644 --- a/src/main/webapp/JS/message.js +++ b/src/main/webapp/JS/message.js @@ -10,18 +10,35 @@ $(".newMessage-back").click(function() { }); }); +$(document).ready(function() { + // When a chat-box is clicked... + $(".chat-box").on("click", function() { + // Add a class to the main container to show right container and hide left container + $(".main-container").addClass("show-right"); + }); + + // When back button is clicked... + $("#back-button").on("click", function() { + // Remove the class from the main container to hide right container and show left container + $(".main-container").removeClass("show-right"); + }); +}); + +var count = -1; setInterval(function(){ // Make an AJAX request to your servlet, replace the URL as required $.ajax({ url: '../CountMessages', type: 'GET', - data: { //get from the session the recipientId + data: { }, success: function (response) { + if(count < 0) + count = response; if(count > 0 && response > count){ + count = response; alert('Hai dei nuovi messaggi, ricarica la pagina per visualizzarli.'); } - count = response; } }); }, 10000); // The interval set is 10000ms = 10 seconds diff --git a/src/main/webapp/JSP/messageCenter.jsp b/src/main/webapp/JSP/messageCenter.jsp index 8c94ea5..6f0473a 100644 --- a/src/main/webapp/JSP/messageCenter.jsp +++ b/src/main/webapp/JSP/messageCenter.jsp @@ -1,9 +1,10 @@ -<%@ page import="model.service.message.Conversation" %> +<%@ page import="model.service.message.MessageManager" %> <%@ page import="java.util.List" %> <%@ page import="model.service.user.UserData" %> <%@ page import="model.entity.User" %> <%@ page import="model.service.user.UserRegistry" %> <%@ page import="model.entity.PersonalInfo" %> +<%@ page import="model.service.message.MessageManager" %> @@ -15,7 +16,7 @@ - Whatsapp Clone + TalkAId - Centro Messaggi
@@ -32,12 +33,12 @@ } else { int userId = (Integer) session.getAttribute("id"); - Conversation conversation = new Conversation(); - List list = conversation.retrieveAllTheContacts(userId); + MessageManager messageManager = new MessageManager(); + List list = messageManager.retrieveAllTheContacts(userId); for(int contact : list) { UserRegistry ur = new UserRegistry(); PersonalInfo data = ur.getPersonalInfo(contact); - int toRead = conversation.getUnreadMessagesForConversation(userId, contact); + int toRead = messageManager.getUnreadMessagesForConversation(userId, contact); %>
@@ -76,7 +77,7 @@