From: <fza...@us...> - 2005-11-29 23:08:40
|
Update of /cvsroot/struts/ajaxchat/WEB-INF/src/org/apache/struts/apps/ajaxchat/dto In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21020/WEB-INF/src/org/apache/struts/apps/ajaxchat/dto Added Files: MessageDTO.java RoomDTO.java UserDTO.java package.html Log Message: --- NEW FILE: MessageDTO.java --- /* * Copyright 2005 Frank W. Zammetti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.struts.apps.ajaxchat.dto; import java.lang.reflect.Field; import java.util.Date; /** * This class is a Data Transfer Object (DTO) that describes a Message. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. */ public class MessageDTO { /** * Text of the message. */ private String text; /** * User who posted the message. */ private UserDTO postedBy; /** * Date and time the message was posted. */ private Date postedDateTime; /** * Accessor for text field. * * @return String Current value of text Field. */ public String getText() { return text; } // End getText(). /** * Mutator for text field. * * @param inText New value of text field. */ public void setText(String inText) { text = inText; } // End setText(). /** * Accessor for postedBy field. * * @return String Current value of postedBy Field. */ public UserDTO getPostedBy() { return postedBy; } // End getPostedBy(). /** * Mutator for postedBy field. * * @param inPostedBy New value of postedBy field. */ public void setPostedBy(UserDTO inPostedBy) { postedBy = inPostedBy; } // End setPostedBy(). /** * Accessor for postedDateTime field. * * @return String Current value of postedDateTime Field. */ public Date getPostedDateTime() { return postedDateTime; } // End getPostedDateTime(). /** * Mutator for postedDateTime field. * * @param inPostedDateTime New value of postedDateTime field. */ public void setPostedDateTime(Date inPostedDateTime) { postedDateTime = inPostedDateTime; } // End setPostedDateTime(). /** * Overriden toString method. * * @return A reflexively-built string representation of this bean. */ public String toString() { String str = null; StringBuffer sb = new StringBuffer(1000); sb.append("[" + super.toString() + "]={"); boolean firstPropertyDisplayed = false; try { Field[] fields = this.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (firstPropertyDisplayed) { sb.append(", "); } else { firstPropertyDisplayed = true; } sb.append(fields[i].getName() + "=" + fields[i].get(this)); } sb.append("}"); str = sb.toString().trim(); } catch (IllegalAccessException iae) { iae.printStackTrace(); } return str; } // End toString(). } // End class. --- NEW FILE: RoomDTO.java --- /* * Copyright 2005 Frank W. Zammetti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.struts.apps.ajaxchat.dto; import java.lang.reflect.Field; import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.Vector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.apps.ajaxchat.AjaxChatConfig; /** * This class is a Data Transfer Object (DTO) that describes a Room. Note that * a few of the methods here do just a tad more than I would generally like in * a DAO, but the architecture made sense this way, so I went with it. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. */ public class RoomDTO { /** * Log instance. */ private static Log log = LogFactory.getLog(RoomDTO.class); /** * The name of the room. */ private String name; /** * The list of users currently chatting in the room. */ private Vector users = new Vector(); /** * The list of messages in the room. */ private Vector messages = new Vector(); /** * Constructor. */ public RoomDTO() { } // End constructor. /** * Constructor. * * @param inName The name of the room being instantiated. */ public RoomDTO(String inName) { name = inName; } // End constructor. /** * Return the name of the room. * * @return String Current value of name Field. */ public String getName() { return name; } // End getName(). /** * Set the name of the room. * * @param inName The name of the room. */ public void setName(String inName) { name = inName; } // End setName(). /** * Return a list of all users chatting in the room. * * @return The list of users chatting in the room. */ public Vector getUserList() { return users; } // End getUserList(). /** * Adds a user to the list of users chatting in the room. The user WILL NOT * be added if they are already in the collection, which should deal with * the user clicking the Refresh button on their browser. * * @param inUser The user to add to the list of users chatting in the room. */ public void addUser(UserDTO inUser) { log.info("RoomDTO addUser()..."); boolean userAlreadyInRoom = false; for (Iterator it = users.iterator(); it.hasNext();) { UserDTO user = (UserDTO)it.next(); if (user.getUsername().equalsIgnoreCase(inUser.getUsername())) { userAlreadyInRoom = true; } } if (!userAlreadyInRoom) { log.info("Adding user to room: " + inUser); users.add(inUser); Collections.sort(users); } log.info("User added to room"); } // End addUser(). /** * Removes a user from the list of users chatting in the room. * * @param inUser The user to remove. */ public void removeUser(UserDTO inUser) { log.info("RoomDTO removeUser()..."); // Scan through all users until we find the one with the username of the // user passed in and remove the user from the list. String usernameToRemove = inUser.getUsername(); int i = 0; int indexToRemove = -1; for (Iterator it = users.iterator(); it.hasNext();) { UserDTO user = (UserDTO)it.next(); if (user.getUsername().equalsIgnoreCase(usernameToRemove)) { log.info("Found " + usernameToRemove + ", removing..."); indexToRemove = i; } i++; } if (indexToRemove != -1) { users.remove(indexToRemove); log.info(usernameToRemove + " removed"); } log.info("RoomDTO removeUser() Done"); } // End removeUser(). /** * This method returns all messages after the given datetime. * * @param inDateTime The Datetime of from which all subsequent messages * will be returned. * @return List of messages. */ public Vector getMessages(Date inDateTime) { log.info("RoomDTO getMessages()..."); // Scan through the list of messages for the room and find any that were // posted after the given datetime, add those to a list to return. Vector al = new Vector(); for (Iterator it = messages.iterator(); it.hasNext();) { MessageDTO message = (MessageDTO)it.next(); if (message.getPostedDateTime().after(inDateTime)) { log.info("Returning message : " + message); al.add(message); } } return al; } // End getMessages(). /** * Posts a message to the room. * * @param inMessage A MessageDTO instance containing all the necessary * details for the message being posted. */ public void postMessage(MessageDTO inMessage) { if (messages.size() > AjaxChatConfig.getMaxMessages()) { messages.clear(); } messages.add(inMessage); } // End addMessage(). /** * Overriden toString method. * * @return A reflexively-built string representation of this bean. */ public String toString() { String str = null; StringBuffer sb = new StringBuffer(1000); sb.append("[" + super.toString() + "]={"); boolean firstPropertyDisplayed = false; try { Field[] fields = this.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (firstPropertyDisplayed) { sb.append(", "); } else { firstPropertyDisplayed = true; } sb.append(fields[i].getName() + "=" + fields[i].get(this)); } sb.append("}"); str = sb.toString().trim(); } catch (IllegalAccessException iae) { iae.printStackTrace(); } return str; } // End toString(). } // End class. --- NEW FILE: UserDTO.java --- /* * Copyright 2005 Frank W. Zammetti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.struts.apps.ajaxchat.dto; import java.lang.reflect.Field; /** * This class is a Data Transfer Object (DTO) that describes a User. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. */ public class UserDTO implements Comparable { /** * Username of the user. */ private String username; /** * Constructor. * * @param inUsername Username for this user. */ public UserDTO(String inUsername) { username = inUsername; } // End constructor. /** * Accessor for username field. * * @return String Current value of username Field. */ public String getUsername() { return username; } // End getUsername(). /** * Mutator for username field. * * @param inUsername New value of username field. */ public void setUsername(String inUsername) { username = inUsername; } // End setUsername(). /** * Used to sort the list of users in the room when a new user joins. * * @param o UserDTO object to compare to. * @return Typical return val of compareTo() method of Comparable interface. */ public int compareTo(Object o) { return this.username.compareTo(((UserDTO)o).getUsername()); } // End compareTo(). /** * Overriden toString method. * * @return A reflexively-built string representation of this bean. */ public String toString() { String str = null; StringBuffer sb = new StringBuffer(1000); sb.append("[" + super.toString() + "]={"); boolean firstPropertyDisplayed = false; try { Field[] fields = this.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (firstPropertyDisplayed) { sb.append(", "); } else { firstPropertyDisplayed = true; } sb.append(fields[i].getName() + "=" + fields[i].get(this)); } sb.append("}"); str = sb.toString().trim(); } catch (IllegalAccessException iae) { iae.printStackTrace(); } return str; } // End toString(). } // End class. --- NEW FILE: package.html --- <body> The org.apache.struts.apps.ajaxchat.dto package contains the Data Transfer Objects (DTO) that are used to pass information around within the AjaxChat application. </body> |