Skip to content

Commit

Permalink
Provided partially the BackEnd
Browse files Browse the repository at this point in the history
-DAOMessage works with the DB
-Conversation is a class that retrieves all the messages for a specific user. You only need to use its builder
  • Loading branch information
panuozzo77 committed Dec 28, 2023
1 parent 91666e1 commit 4ea28d2
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/main/java/model/DAO/DAOMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package model.DAO;

import model.entity.Message;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class DAOMessage {

private Message getMessageFromResultSet(ResultSet resultSet) throws SQLException {
Message message = new Message();

message.setIdMessage(resultSet.getInt("ID_message"));
message.setSender(resultSet.getInt("Sender"));
message.setRecipient(resultSet.getInt("Recipient"));
message.setRead(resultSet.getBoolean("Read"));
message.setBody(resultSet.getString("Body"));
message.setSent(resultSet.getTimestamp("sent"));

return message;
}

/**
* Retrieves a list of user IDs based on the therapist ID.
*
* @param therapistId The ID of the therapist.
* @return A list of user IDs associated with the specified therapist.
*/
public List<Integer> retrieveUserIdsByTherapist(int therapistId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

List<Integer> userIds = new ArrayList<>();

try {
connection = DAOConnection.getConnection();

// Query to retrieve user IDs associated with the specified therapist
String query = "SELECT ID FROM user WHERE ID_Therapist = ?";

preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, therapistId);

resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
int userId = resultSet.getInt("ID");
userIds.add(userId);
}

} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

return userIds;
}

public Collection<Message> retrieveMessages(int userId, int contact) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

List<Message> messages = new ArrayList<>();

try {
connection = DAOConnection.getConnection();

// Query to retrieve messages between the two users
String query = "SELECT * FROM message WHERE (Sender = ? AND Recipient = ?) OR (Sender = ? AND Recipient = ?) ORDER BY sent";

preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, userId);
preparedStatement.setInt(2, contact);
preparedStatement.setInt(3, contact);
preparedStatement.setInt(4, userId);

resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
Message message = getMessageFromResultSet(resultSet);
messages.add(message);
}

} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

return messages;
}
}
12 changes: 12 additions & 0 deletions src/main/java/model/entity/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,17 @@ public Timestamp getSent() {
public void setSent(Timestamp sent) {
this.sent = sent;
}

@Override
public String toString() {
return "Message{" +
//"idMessage=" + idMessage +
", sender=" + sender +
", recipient=" + recipient +
", read=" + read +
//", body='" + body + '\'' +
//", sent=" + sent +
'}';
}
}

70 changes: 70 additions & 0 deletions src/main/java/model/service/message/Conversation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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<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 int getUnreadMessagesCounter() {
return unreadMessages;
}

public Map<Integer, List<Message>> getConversations() {
return conversations;
}

public Conversation(int userId){
this.userId = userId;
List<Integer> contacts= retrieveAllTheContacts(userId);
forEachContactRetrieveTheMessages(userId, contacts);
updateUnreadCounter();
}

private void updateUnreadCounter() {
unreadMessages = 0;
for (List<Message> messages : conversations.values()) {
for (Message message : messages) {
if (message.getRecipient() == userId && !message.isRead()) {
unreadMessages++;
}
}
}
}

private void forEachContactRetrieveTheMessages(int userId, List<Integer> contacts) {
for (int contact : contacts) {
conversations.put(contact, new ArrayList<>());
conversations.get(contact).addAll(db.retrieveMessages(userId, contact));
}
}

private 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 {
contacts.add(user.getIdTherapist());
}
contacts.add(0); //Notifications
return contacts;
}
}

0 comments on commit 4ea28d2

Please sign in to comment.