Skip to content

Commit

Permalink
Fixed some unused Imports, Provided MessageManagerInterface, fixed ti…
Browse files Browse the repository at this point in the history
…tle of messageCenter
  • Loading branch information
panuozzo77 committed Dec 29, 2023
1 parent 67ab77d commit 5f6ac41
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 104 deletions.
9 changes: 4 additions & 5 deletions src/main/java/controller/CountMessagesServlet.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/controller/GetMessageServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

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;
import javax.servlet.http.HttpServletResponse;
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<Message> messages = conversation.retrieveMessages(userId, contactId);
MessageManager messageManager = new MessageManager();
List<Message> messages = messageManager.retrieveMessages(userId, contactId);

// Now build the JSON response
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
Expand All @@ -43,24 +43,24 @@ 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");
response.getWriter().write(jsonArray.toString());
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws IOException {
try {
HttpSession session = request.getSession();
int sender = (int) session.getAttribute("id");
int recipient = Integer.parseInt(request.getParameter("recipient"));
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");

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/controller/ResetPasswordServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
if(authenticator.resetPassword(email, password)){
response.getWriter().write("Password cambiata con successo!");
}



}
}
1 change: 0 additions & 1 deletion src/main/java/controller/SendResetPin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controller;

import model.entity.User;
import model.service.login.Authenticator;
import model.service.user.UserData;

Expand Down
72 changes: 0 additions & 72 deletions src/main/java/model/service/message/Conversation.java

This file was deleted.

61 changes: 60 additions & 1 deletion src/main/java/model/service/message/MessageManager.java
Original file line number Diff line number Diff line change
@@ -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<Integer, List<Message>> 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<Message> retrieveMessages(int userId, int contact) {
return db.retrieveMessages(userId, contact);
}
public int getUnreadMessagesForConversation(int userId, int contact){
int unreadCounter = 0;
List<Message> messages = db.retrieveMessages(userId, contact);
for (Message message : messages) {
if (message.getRecipient() == userId && !message.isRead()) {
unreadCounter++;
}
}
return unreadCounter;
}

public List<Integer> retrieveAllTheContacts(int userId){
List<Integer> 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;
}
}
60 changes: 60 additions & 0 deletions src/main/java/model/service/message/MessageManagerInterface.java
Original file line number Diff line number Diff line change
@@ -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<Message> 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<Integer> retrieveAllTheContacts(int userId);
}
36 changes: 34 additions & 2 deletions src/main/webapp/CSS/messageCenter.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ body {
top: 0;
width: 100%;
height: 20%;
background-color: #009688;
background-color: #f0f0f0;
}

.main-container {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 5f6ac41

Please sign in to comment.