001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/chat/tags/sakai_2-4-1/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.chat.tool;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.sakaiproject.chat.api.ChatMessage;
025: import org.sakaiproject.chat.cover.ChatService;
026: import org.sakaiproject.entity.api.Reference;
027: import org.sakaiproject.entity.cover.EntityManager;
028: import org.sakaiproject.event.api.UsageSession;
029: import org.sakaiproject.event.cover.UsageSessionService;
030: import org.sakaiproject.exception.IdUnusedException;
031: import org.sakaiproject.exception.PermissionException;
032: import org.sakaiproject.message.api.MessageChannel;
033: import org.sakaiproject.user.api.User;
034: import org.sakaiproject.user.cover.UserDirectoryService;
035: import org.sakaiproject.util.BaseDelivery;
036: import org.sakaiproject.util.StringUtil;
037: import org.sakaiproject.util.Web;
038:
039: /**
040: * <p>
041: * 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.
042: * </p>
043: */
044: public class ChatDelivery extends BaseDelivery {
045: /** Our logger. */
046: private static Log M_log = LogFactory.getLog(ChatDelivery.class);
047:
048: /** The messageId. */
049: protected String m_messageId = null;
050:
051: protected boolean m_beepOnDelivery = false;
052:
053: /**
054: * Construct.
055: *
056: * @param address
057: * The address.
058: * @param elementId
059: * The elementId.
060: */
061: public ChatDelivery(String address, String elementId,
062: String messageID, boolean beepOnDelivery) {
063: super (address, elementId);
064: m_messageId = messageID;
065: m_beepOnDelivery = beepOnDelivery;
066:
067: } // ChatDelivery
068:
069: /**
070: * Set the Message Id that this delivery is in reference to.
071: *
072: * @param id
073: * The message Id that this delivery is in reference to.
074: */
075: public void setMessage(String id) {
076: m_messageId = id;
077:
078: } // setMessage
079:
080: /**
081: * Access the Message Id that this delivery is in reference to.
082: *
083: * @return The Message Id that this delivery is in reference to.
084: */
085: public String getMessage() {
086: return m_messageId;
087:
088: } // getMessage
089:
090: /**
091: * Compose a javascript message for delivery to the browser client window.
092: *
093: * @return The javascript message to send to the browser client window.
094: */
095: public String compose() {
096: if (M_log.isDebugEnabled())
097: M_log.debug("compose() element: " + m_elementId
098: + ", message: " + m_messageId);
099:
100: // generate a string of JavaScript commands to update the message log
101:
102: Reference ref = EntityManager.newReference(m_messageId);
103: ChatMessage msg = (ChatMessage) ref.getEntity();
104: User sender = null;
105: if (msg != null) {
106: sender = msg.getHeader().getFrom();
107: }
108: User myself = UserDirectoryService.getCurrentUser();
109:
110: MessageChannel channel = null;
111: try {
112: String channelRef = ChatService.channelReference(ref
113: .getContext(), ref.getContainer());
114: channel = ChatService.getChannel(channelRef);
115: } catch (PermissionException e) {
116: } catch (IdUnusedException e) {
117: }
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: // TODO: temporary workaround - IE is not liking our DOM refresh, so don't use it there till we get it working -ggolden
134: if (browserId.equals(UsageSession.WIN_IE))
135: browserSupportsDomRefresh = false;
136:
137: if (!browserSupportsDomRefresh || channel == null
138: || msg == null) {
139: retval = "try { " + m_elementId
140: + ".location.replace(addAuto(" + m_elementId
141: + ".location));} catch (error) {}";
142: }
143:
144: // otherwise setup for a browser-side javascript DOM modification to insert the message
145: else {
146: String msgbody = Web.escapeJsQuoted(Web
147: .escapeHtmlFormattedText(msg.getBody()));
148:
149: // is the user we are delivering to allowed to remove this message?
150: boolean removeable = false;
151: if (channel.allowRemoveMessage(msg))
152: removeable = true;
153:
154: retval = "try { " + m_elementId + ".appendMessage('"
155: + sender.getDisplayName() + "', '" + sender.getId()
156: + "', '" + new Boolean(removeable) + "', '"
157: + msg.getHeader().getDate().toStringLocalDate()
158: + "', '"
159: + msg.getHeader().getDate().toStringLocalTimeZ()
160: + "', '" + msgbody + "','"
161: + msg.getHeader().getId()
162: + "'); } catch (error) {} ";
163: }
164:
165: if (m_beepOnDelivery && (sender != null)
166: && sender.compareTo(myself) != 0) {
167: retval += "beep = true;";
168: }
169:
170: return retval;
171:
172: } // compose
173:
174: /**
175: * Display.
176: */
177: public String toString() {
178: return super .toString() + " : " + m_messageId;
179:
180: } // toString
181:
182: /**
183: * Are these the same?
184: *
185: * @return true if obj is the same Delivery as this one.
186: */
187: public boolean equals(Object obj) {
188: if (!super .equals(obj))
189: return false;
190:
191: ChatDelivery cob = (ChatDelivery) obj;
192: if (StringUtil.different(cob.getMessage(), getMessage()))
193: return false;
194:
195: return true;
196: }
197: }
|