Skip to content

Commit

Permalink
refactoring: split ConversationDAO off from Conversation
Browse files Browse the repository at this point in the history
The DAO-like functionality that existed in the Conversation class made it hard to operate on instances. This commit splits off that functionality in a new, dedicated class named ConversationDAO.

No functional changes are expected from this.
  • Loading branch information
guusdk committed Dec 23, 2021
1 parent bf5a6e0 commit 6092da2
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 415 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private Conversation getNextElement() {
while (convIterator.hasNext()) {
try {
long conversationID = convIterator.next();
return new Conversation(conversationManager, conversationID);
return ConversationDAO.loadConversation(conversationID);
}
catch (Exception e) {
Log.error(e.getMessage(), e);
Expand Down Expand Up @@ -546,7 +546,7 @@ private Conversation getNextElement() {
try {
ScoreDoc hit = hitsIterator.next();
long conversationID = Long.parseLong(searcher.doc(hit.doc).get("conversationID"));
return new Conversation(conversationManager, conversationID);
return ConversationDAO.loadConversation(conversationID);
}
catch (Exception e) {
Log.error(e.getMessage(), e);
Expand Down
423 changes: 31 additions & 392 deletions src/java/org/jivesoftware/openfire/archive/Conversation.java

Large diffs are not rendered by default.

361 changes: 361 additions & 0 deletions src/java/org/jivesoftware/openfire/archive/ConversationDAO.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public Conversation getConversation(long conversationID) throws NotFoundExceptio
}
}
// Otherwise, it might be an archived conversation, so attempt to load it.
return new Conversation(this, conversationID);
return ConversationDAO.loadConversation(conversationID);
} else {
// Get this info from the senior cluster member when running in a cluster
String conversationXml = CacheFactory.doSynchronousClusterTask(new GetConversationTask(conversationID), ClusterManager
Expand Down Expand Up @@ -741,7 +741,7 @@ void processMessage(JID sender, JID receiver, String body, String stanza, Date d
boolean external = isExternal(server, sender) ^ isExternal(server, receiver);
// Make sure that the user joined the conversation before a message was received
Date start = new Date(date.getTime() - 1);
conversation = new Conversation(this, participants, external, start);
conversation = ConversationDAO.createConversation(this, participants, external, start);
conversations.put(conversationKey, conversation);
// Notify listeners of the newly created conversation.
for (ConversationListener listener : conversationListeners) {
Expand All @@ -764,7 +764,7 @@ else if ((date.getTime() - conversation.getLastActivity().getTime() > idleTime.t
boolean external = isExternal(server, sender) ^ isExternal(server, receiver);
// Make sure that the user joined the conversation before a message was received
Date start = new Date(date.getTime() - 1);
conversation = new Conversation(this, participants, external, start);
conversation = ConversationDAO.createConversation(this, participants, external, start);
conversations.put(conversationKey, conversation);
// Notify listeners of the newly created conversation.
for (ConversationListener listener : conversationListeners) {
Expand Down Expand Up @@ -815,7 +815,7 @@ void processRoomMessage(JID roomJID, JID sender, JID receiverIfPM, String nickna
if (conversation == null) {
// Make sure that the user joined the conversation before a message was received
Date start = new Date(date.getTime() - 1);
conversation = new Conversation(this, roomJID, false, start);
conversation = ConversationDAO.createConversation(this, roomJID, false, start);
conversations.put(conversationKey, conversation);
// Notify listeners of the newly created conversation.
for (ConversationListener listener : conversationListeners) {
Expand All @@ -829,7 +829,7 @@ else if ((date.getTime() - conversation.getLastActivity().getTime() > idleTime.t
removeConversation(conversationKey, conversation, conversation.getLastActivity());
// Make sure that the user joined the conversation before a message was received
Date start = new Date(date.getTime() - 1);
conversation = new Conversation(this, roomJID, false, start);
conversation = ConversationDAO.createConversation(this, roomJID, false, start);
conversations.put(conversationKey, conversation);
// Notify listeners of the newly created conversation.
for (ConversationListener listener : conversationListeners) {
Expand Down Expand Up @@ -877,7 +877,7 @@ else if ((date.getTime() - conversation.getLastActivity().getTime() > idleTime.t
void joinedGroupConversation(JID room, JID user, String nickname, Date date) {
Conversation conversation = getRoomConversation(room);
if (conversation != null) {
conversation.participantJoined(user, nickname, date.getTime());
conversation.participantJoined(this, user, nickname, date.getTime());
}
}

Expand All @@ -895,7 +895,7 @@ void joinedGroupConversation(JID room, JID user, String nickname, Date date) {
void leftGroupConversation(JID room, JID user, Date date) {
Conversation conversation = getRoomConversation(room);
if (conversation != null) {
conversation.participantLeft(user, date.getTime());
conversation.participantLeft(this, user, date.getTime());
}
}

Expand All @@ -909,7 +909,7 @@ void roomConversationEnded(JID room, Date date) {
private void removeConversation(String key, Conversation conversation, Date date) {
conversations.remove(key);
// Notify conversation that it has ended
conversation.conversationEnded(date);
conversation.conversationEnded(this, date);
// Notify listeners of the conversation ending.
for (ConversationListener listener : conversationListeners) {
listener.conversationEnded(conversation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
Conversation conversation;
if (conversationID > -1) {
try {
conversation = new Conversation(conversationManager, conversationID);
conversation = ConversationDAO.loadConversation(conversationID);

ByteArrayOutputStream stream = new ConversationUtils().getConversationPDF(conversation);
ByteArrayOutputStream stream = new ConversationUtils().getConversationPDF(conversationManager, conversation);

// setting some response headers
response.setHeader("Expires", "0");
Expand Down
19 changes: 10 additions & 9 deletions src/java/org/jivesoftware/openfire/archive/ConversationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public ConversationInfo getConversationInfo(long conversationID, boolean formatP

try {
Conversation conversation = conversationmanager.getConversation(conversationID);
info = toConversationInfo(conversation, formatParticipants);
info = toConversationInfo(conversationmanager, conversation, formatParticipants);
}
catch (NotFoundException e) {
Log.error(e.getMessage(), e);
Expand All @@ -130,13 +130,13 @@ public Map<String, ConversationInfo> getConversations(boolean formatParticipants
Arrays.asList(conversations.toArray(new Conversation[conversations.size()]));
for (Iterator<Conversation> i = lConversations.iterator(); i.hasNext();) {
Conversation con = i.next();
ConversationInfo info = toConversationInfo(con, formatParticipants);
ConversationInfo info = toConversationInfo(conversationManager, con, formatParticipants);
cons.put(Long.toString(con.getConversationID()), info);
}
return cons;
}

public ByteArrayOutputStream getConversationPDF(Conversation conversation) throws IOException {
public ByteArrayOutputStream getConversationPDF(ConversationManager conversationManager, Conversation conversation) throws IOException {
Map<JID, Color> colorMap = new HashMap<>();
if (conversation != null) {
Collection<JID> set = conversation.getParticipants();
Expand All @@ -158,10 +158,10 @@ public ByteArrayOutputStream getConversationPDF(Conversation conversation) throw
}


return buildPDFContent(conversation, colorMap);
return buildPDFContent(conversationManager, conversation, colorMap);
}

private ByteArrayOutputStream buildPDFContent(Conversation conversation, Map<JID, Color> colorMap) throws IOException {
private ByteArrayOutputStream buildPDFContent(ConversationManager conversationManager, Conversation conversation, Map<JID, Color> colorMap) throws IOException {

try ( final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PdfWriter writer = new PdfWriter(baos);
Expand Down Expand Up @@ -204,7 +204,7 @@ private ByteArrayOutputStream buildPDFContent(Conversation conversation, Map<JID
document.add( new Paragraph().add(new Text("\n")));

final Paragraph messageParagraph = new Paragraph();
for (ArchivedMessage message : conversation.getMessages())
for (ArchivedMessage message : conversation.getMessages(conversationManager))
{
String time = JiveGlobals.formatTime(message.getSentDate());
String from = message.getFromJID().getNode();
Expand Down Expand Up @@ -251,7 +251,8 @@ private ByteArrayOutputStream buildPDFContent(Conversation conversation, Map<JID
}
}

private ConversationInfo toConversationInfo(Conversation conversation,
private ConversationInfo toConversationInfo(ConversationManager conversationManager,
Conversation conversation,
boolean formatParticipants) {
final ConversationInfo info = new ConversationInfo();
// Set participants
Expand Down Expand Up @@ -299,7 +300,7 @@ private ConversationInfo toConversationInfo(Conversation conversation,
// Create body.
final StringBuilder builder = new StringBuilder();
builder.append("<table width=100%>");
for (ArchivedMessage message : conversation.getMessages()) {
for (ArchivedMessage message : conversation.getMessages(conversationManager)) {
String time = JiveGlobals.formatTime(message.getSentDate());
String from = message.getFromJID().getNode();
String to = message.getIsPMforNickname(); // Only non-null when this is a Private Message sent in a MUC.
Expand Down Expand Up @@ -327,7 +328,7 @@ private ConversationInfo toConversationInfo(Conversation conversation,
builder.append("</tr>");
}

if (conversation.getMessages().size() == 0) {
if (conversation.getMessages(conversationManager).size() == 0) {
builder.append("<span class=small-description>" +
LocaleUtils.getLocalizedString("archive.search.results.archive_disabled",
MonitoringConstants.NAME) +
Expand Down
5 changes: 3 additions & 2 deletions src/web/conversation-viewer.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<%@ page import="org.jivesoftware.util.*"%><%@ page import="java.util.Collection"%>
<%@ page import="org.slf4j.LoggerFactory" %>
<%@ page import="org.slf4j.Logger" %>
<%@ page import="org.jivesoftware.openfire.archive.ConversationDAO" %>
<%!
Map<String, String> colorMap = new HashMap<String, String>();
%>
Expand All @@ -27,7 +28,7 @@
Conversation conversation = null;
if (conversationID > -1) {
try {
conversation = new Conversation(conversationManager, conversationID);
conversation = ConversationDAO.loadConversation(conversationID);
}
catch (NotFoundException nfe) {
logger.error("Can't reconstruct conversation", nfe);
Expand Down Expand Up @@ -70,7 +71,7 @@
%>

<table width="100%">
<% for (ArchivedMessage message : conversation.getMessages()) { %>
<% for (ArchivedMessage message : conversation.getMessages(conversationManager)) { %>
<tr valign="top">
<td width="1%" nowrap class="jive-description" style="color:<%= getColor(message.getFromJID()) %>">
[<%= JiveGlobals.formatTime(message.getSentDate())%>] <%= message.getFromJID().getNode()%><%= message.getIsPMforNickname() == null ? "" : " -> " + message.getIsPMforNickname()%>:</td>
Expand Down

0 comments on commit 6092da2

Please sign in to comment.