KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > muc > MUCRoomHistory


1 /**
2  * $RCSfile: MUCRoomHistory.java,v $
3  * $Revision: 1.8 $
4  * $Date: 2004/12/14 14:53:04 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.muc;
13
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.ListIterator JavaDoc;
18 import java.util.TimeZone JavaDoc;
19
20 import org.jivesoftware.messenger.user.UserNotFoundException;
21 import org.xmpp.packet.Message;
22 import org.xmpp.packet.JID;
23 import org.dom4j.Element;
24
25 /**
26  * Represent the data model for one <code>MUCRoom</code> history. Including chat transcript,
27  * joining and leaving times.
28  *
29  * @author Gaston Dombiak
30  */

31 public final class MUCRoomHistory {
32
33     private static final SimpleDateFormat JavaDoc UTC_FORMAT = new SimpleDateFormat JavaDoc("yyyyMMdd'T'HH:mm:ss");
34     static {
35         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
36     }
37
38     private MUCRoom room;
39
40     private HistoryStrategy historyStrategy;
41
42     private boolean isNonAnonymousRoom;
43
44     public MUCRoomHistory(MUCRoom mucRoom, HistoryStrategy historyStrategy) {
45         this.room = mucRoom;
46         this.isNonAnonymousRoom = mucRoom.canAnyoneDiscoverJID();
47         this.historyStrategy = historyStrategy;
48     }
49
50     public void addMessage(Message packet) {
51         // Don't keep messages whose sender is the room itself (thus address without resource)
52
// unless the message is changing the room's subject
53
if ((packet.getFrom() == null || packet.getFrom().toString().length() == 0 ||
54                 packet.getFrom().equals(room.getRole().getRoleAddress())) &&
55                 packet.getSubject() == null) {
56             return;
57         }
58         Message packetToAdd = (Message) packet.createCopy();
59
60         // TODO Analyze concurrency (on the LinkList) when adding many messages simultaneously
61

62         // Check if the room has changed its configuration
63
if (isNonAnonymousRoom != room.canAnyoneDiscoverJID()) {
64             isNonAnonymousRoom = room.canAnyoneDiscoverJID();
65             // Update the "from" attribute of the delay information in the history
66
Message message;
67             Element delayElement;
68             // TODO Make this update in a separate thread
69
for (Iterator JavaDoc it = getMessageHistory(); it.hasNext();) {
70                 message = (Message) it.next();
71                 delayElement = message.getChildElement("x", "jabber:x:delay");
72                 if (room.canAnyoneDiscoverJID()) {
73                     // Set the Full JID as the "from" attribute
74
try {
75                         MUCRole role = room.getOccupant(message.getFrom().getResource());
76                         delayElement.addAttribute("from", role.getChatUser().getAddress().toString());
77                     }
78                     catch (UserNotFoundException e) {
79                     }
80                 }
81                 else {
82                     // Set the Room JID as the "from" attribute
83
delayElement.addAttribute("from", message.getFrom().toString());
84                 }
85             }
86
87         }
88
89         // Add the delay information to the message
90
Element delayInformation = packetToAdd.addChildElement("x", "jabber:x:delay");
91         Date JavaDoc current = new Date JavaDoc();
92         delayInformation.addAttribute("stamp", UTC_FORMAT.format(current));
93         if (room.canAnyoneDiscoverJID()) {
94             // Set the Full JID as the "from" attribute
95
try {
96                 MUCRole role = room.getOccupant(packet.getFrom().getResource());
97                 delayInformation.addAttribute("from", role.getChatUser().getAddress()
98                         .toString());
99             }
100             catch (UserNotFoundException e) {
101             }
102         }
103         else {
104             // Set the Room JID as the "from" attribute
105
delayInformation.addAttribute("from", packet.getFrom().toString());
106         }
107         historyStrategy.addMessage(packetToAdd);
108     }
109
110     public Iterator JavaDoc getMessageHistory() {
111         return historyStrategy.getMessageHistory();
112     }
113
114     /**
115      * Obtain the current history to be iterated in reverse mode. This means that the returned list
116      * iterator will be positioned at the end of the history so senders of this message must
117      * traverse the list in reverse mode.
118      *
119      * @return A list iterator of Message objects positioned at the end of the list.
120      */

121     public ListIterator JavaDoc getReverseMessageHistory() {
122         return historyStrategy.getReverseMessageHistory();
123     }
124
125     /**
126      * Creates a new message and adds it to the history. The new message will be created based on
127      * the provided information. This information will likely come from the database when loading
128      * the room history from the database.
129      *
130      * @param senderJID the sender's JID of the message to add to the history.
131      * @param nickname the sender's nickname of the message to add to the history.
132      * @param sentDate the date when the message was sent to the room.
133      * @param subject the subject included in the message.
134      * @param body the body of the message.
135      */

136     public void addOldMessage(String JavaDoc senderJID, String JavaDoc nickname, Date JavaDoc sentDate, String JavaDoc subject,
137             String JavaDoc body)
138     {
139         Message message = new Message();
140         message.setType(Message.Type.groupchat);
141         message.setSubject(subject);
142         message.setBody(body);
143         // Set the sender of the message
144
if (nickname != null && nickname.trim().length() > 0) {
145             JID roomJID = room.getRole().getRoleAddress();
146             // Recreate the sender address based on the nickname and room's JID
147
message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(),
148                     nickname));
149         }
150         else {
151             // Set the room as the sender of the message
152
message.setFrom(room.getRole().getRoleAddress());
153         }
154
155         // Add the delay information to the message
156
Element delayInformation = message.addChildElement("x", "jabber:x:delay");
157         delayInformation.addAttribute("stamp", UTC_FORMAT.format(sentDate));
158         if (room.canAnyoneDiscoverJID()) {
159             // Set the Full JID as the "from" attribute
160
delayInformation.addAttribute("from", senderJID);
161         }
162         else {
163             // Set the Room JID as the "from" attribute
164
delayInformation.addAttribute("from", room.getRole().getRoleAddress().toString());
165         }
166         historyStrategy.addMessage(message);
167     }
168
169     /**
170      * Returns true if there is a message within the history of the room that has changed the
171      * room's subject.
172      *
173      * @return true if there is a message within the history of the room that has changed the
174      * room's subject.
175      */

176     public boolean hasChangedSubject() {
177         return historyStrategy.hasChangedSubject();
178     }
179 }
Popular Tags