001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/chat/trunk/chat-tool/tool/src/java/org/sakaiproject/chat/tool/ChatDelivery.java $
003: * $Id: ChatDelivery.java 14062 2006-08-27 03:44:18Z csev@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.chat2.tool;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.sakaiproject.chat2.model.ChatMessage;
025: import org.sakaiproject.chat2.model.ChatManager;
026: import org.sakaiproject.time.api.Time;
027: import org.sakaiproject.time.cover.TimeService;
028: import org.sakaiproject.event.api.UsageSession;
029: import org.sakaiproject.event.cover.UsageSessionService;
030: import org.sakaiproject.chat2.model.ChatChannel;
031: import org.sakaiproject.user.api.User;
032: import org.sakaiproject.user.cover.UserDirectoryService;
033: import org.sakaiproject.user.api.UserNotDefinedException;
034: import org.sakaiproject.util.BaseDelivery;
035: import org.sakaiproject.util.StringUtil;
036: import org.sakaiproject.util.Web;
037:
038: /**
039: * <p>
040: * ChatDelivery is a Delivery that causes a chat message to be appended to a table of chat messages in the HTML element identified by the address and elementID.
041: * </p>
042: */
043: public class ChatDelivery extends BaseDelivery {
044: /** Our logger. */
045: private static Log logger = LogFactory.getLog(ChatDelivery.class);
046:
047: /** The message. it could be a string id or the actual message. */
048: protected Object m_message = null;
049:
050: protected ChatManager chatManager = null;
051:
052: protected boolean m_beepOnDelivery = false;
053:
054: protected String placementId = "";
055:
056: /**
057: * Construct.
058: *
059: * @param address
060: * The address.
061: * @param elementId
062: * The elementId.
063: */
064: public ChatDelivery(String address, String elementId,
065: Object message, String placementId, boolean beepOnDelivery,
066: ChatManager chatManager) {
067: super (address, elementId);
068: m_message = message;
069: m_beepOnDelivery = beepOnDelivery;
070: this .chatManager = chatManager;
071: this .placementId = placementId;
072: } // ChatDelivery
073:
074: public ChatMessage getMessage() {
075: if (m_message instanceof String) {
076: m_message = chatManager.getMessage((String) m_message);
077: }
078: if (m_message instanceof ChatMessage)
079: return (ChatMessage) m_message;
080: return null;
081: }
082:
083: public void setMessage(Object message) {
084: this .m_message = message;
085: }
086:
087: /**
088: * Compose a javascript message for delivery to the browser client window.
089: * This function happens in the client connection thread instead of in the event notification thread
090: *
091: * @return The javascript message to send to the browser client window.
092: */
093: public String compose() {
094: ChatMessage message = null;
095:
096: if (m_message instanceof ChatMessage) {
097: message = (ChatMessage) m_message;
098: } else if (m_message instanceof String) {
099: message = chatManager.getMessage((String) m_message);
100: } else {
101: return "";
102: }
103: if (logger.isDebugEnabled())
104: logger.debug("compose() element: " + m_elementId
105: + ", message: " + message.getId());
106:
107: // generate a string of JavaScript commands to update the message log
108:
109: User sender = null;
110: try {
111: sender = UserDirectoryService.getUser(message.getOwner());
112: } catch (UserNotDefinedException e) {
113: logger.error(e);
114: }
115: User myself = UserDirectoryService.getCurrentUser();
116:
117: ChatChannel channel = message.getChatChannel();
118:
119: // We may not have a usage session
120: UsageSession session = UsageSessionService.getSession();
121: String browserId = UsageSession.UNKNOWN;
122: if (session != null) {
123: browserId = session.getBrowserId();
124: }
125:
126: String retval = null;
127:
128: // if we don't know we can do the DOM based refresh, or we are missing channel or msg (could have been a message delete)
129: // trigger a panel refresh
130: boolean browserSupportsDomRefresh = !browserId
131: .equals(UsageSession.UNKNOWN);
132:
133: if (!browserSupportsDomRefresh || channel == null) {
134: retval = "try { this.location.replace(addAuto(this.location));} catch (error) {}";
135: }
136:
137: // otherwise setup for a browser-side javascript DOM modification to insert the message
138: else {
139: String msgbody = Web.escapeJsQuoted(Web
140: .escapeHtmlFormattedText(message.getBody()));
141:
142: Time messageTime = TimeService.newTime(message
143: .getMessageDate().getTime());
144:
145: retval = "try { appendMessage('"
146: + sender.getDisplayName()
147: + "', '"
148: + sender.getId()
149: + "', '"
150: + new Boolean(chatManager.getCanDelete(message,
151: placementId)) + "', '"
152: + messageTime.toStringLocalDate() + "', '"
153: + messageTime.toStringLocalTimeZ() + "', '"
154: + msgbody + "','" + message.getId()
155: + "'); } catch (error) {alert(error);} ";
156: }
157:
158: if (m_beepOnDelivery && (sender != null)
159: && sender.compareTo(myself) != 0) {
160: retval += "beep = true;";
161: }
162:
163: return retval;
164:
165: } // compose
166:
167: /**
168: * Display.
169: */
170: public String toString() {
171: return super .toString() + " : " + m_message;
172:
173: } // toString
174:
175: /**
176: * Are these the same?
177: *
178: * @return true if obj is the same Delivery as this one.
179: */
180: public boolean equals(Object obj) {
181: if (!super .equals(obj))
182: return false;
183:
184: ChatDelivery cob = (ChatDelivery) obj;
185: if (StringUtil.different(cob.getMessage().getId(), getMessage()
186: .getId()))
187: return false;
188:
189: return true;
190: }
191: }
|