package jetbrains.buildServer.serverSide; import org.jetbrains.annotations.NotNull; import org.json.JSONException; import org.json.JSONWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; /** * @author Eugene Petrenko * Created: 31.03.2010 13:04:57 */ public class ChatMessagesCollection implements JSONAble{ private final Collection myMessages; private final int myLastMessageId; private final UserContext myUserNames; private final TimeFactory myTimeFactory; private final Collection myRoomIds; public ChatMessagesCollection(int lastMessageId, Collection messages, UserContext userNames, TimeFactory timeFactory, Collection roomIds) { myLastMessageId = lastMessageId; myMessages = messages; myUserNames = userNames; myTimeFactory = timeFactory; myRoomIds = new ArrayList(roomIds); } public int getLastMessageId() { return myLastMessageId; } private boolean getDisableChat() { for (ChatMessage message : myMessages) { if (message.isDisableChat() && myUserNames.isMe(message.getToUser())) return true; } return false; } @NotNull public Collection getMessages() { return Collections.unmodifiableCollection(myMessages); } public void writeJSON(JSONWriter w) throws JSONException { w.object(); w.key("nextRequestId").value(getLastMessageId()); w.key("disableChat").value(getDisableChat()); w.key("messages").array(); for (ChatMessage message : myMessages) { message.writeJSON(myUserNames, myTimeFactory, w); } w.endArray(); w.key("rooms").array(); for (Integer id : myRoomIds) { w.value(id); } w.endArray(); w.endObject(); } }