-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-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
1 parent
91666e1
commit 4ea28d2
Showing
3 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |