001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/chat/tags/sakai_2-4-1/chat-api/api/src/java/org/sakaiproject/chat2/model/ChatChannel.java $
003: * $Id: ChatChannel.java 29902 2007-05-03 13:00:41Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2007 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.model;
021:
022: import java.util.Date;
023: import java.util.HashSet;
024: import java.util.Set;
025: import java.util.Stack;
026:
027: import org.sakaiproject.component.cover.ServerConfigurationService;
028: import org.sakaiproject.entity.api.Entity;
029: import org.sakaiproject.entity.api.ResourceProperties;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032:
033: public class ChatChannel implements Entity {
034:
035: /** Message filter names */
036: public static final String FILTER_BY_NUMBER = "SelectMessagesByNumber";
037: public static final String FILTER_BY_TIME = "SelectMessagesByTime";
038: public static final String FILTER_TODAY = "SelectTodaysMessages";
039: public static final String FILTER_ALL = "SelectAllMessages";
040: public static final int MAX_MESSAGES = -999;
041:
042: private String id;
043: private String context;
044: private Date creationDate;
045: private String title;
046: private String description;
047: private String filterType = FILTER_BY_TIME;
048: private int filterParam = 3;
049: private boolean contextDefaultChannel = false;
050: private boolean enableUserOverride = true;
051: private Set messages = new HashSet();
052:
053: public ChatChannel() {
054: }
055:
056: /**
057: * Set up a new ChatChannel with the set defaults
058: * @param defaults
059: */
060: public ChatChannel(ChatChannel defaults) {
061: this .filterType = defaults.getFilterType();
062: this .filterParam = defaults.getFilterParam();
063: this .enableUserOverride = defaults.isEnableUserOverride();
064: }
065:
066: public String getContext() {
067: return context;
068: }
069:
070: public void setContext(String context) {
071: this .context = context;
072: }
073:
074: public Date getCreationDate() {
075: return creationDate;
076: }
077:
078: public void setCreationDate(Date creationDate) {
079: this .creationDate = creationDate;
080: }
081:
082: public String getId() {
083: return id;
084: }
085:
086: public void setId(String id) {
087: this .id = id;
088: }
089:
090: public String getTitle() {
091: return title;
092: }
093:
094: public void setTitle(String title) {
095: this .title = title;
096: }
097:
098: public String getDescription() {
099: return description;
100: }
101:
102: public void setDescription(String description) {
103: this .description = description;
104: }
105:
106: public Set getMessages() {
107: return messages;
108: }
109:
110: public void setMessages(Set messages) {
111: this .messages = messages;
112: }
113:
114: public String getFilterType() {
115: return filterType;
116: }
117:
118: public void setFilterType(String filterType) {
119: this .filterType = filterType;
120: }
121:
122: public int getFilterParam() {
123: return filterParam;
124: }
125:
126: public void setFilterParam(int filterParam) {
127: this .filterParam = filterParam;
128: }
129:
130: public boolean isContextDefaultChannel() {
131: return contextDefaultChannel;
132: }
133:
134: public void setContextDefaultChannel(boolean contextDefaultChannel) {
135: this .contextDefaultChannel = contextDefaultChannel;
136: }
137:
138: public boolean isEnableUserOverride() {
139: return enableUserOverride;
140: }
141:
142: public void setEnableUserOverride(boolean enableUserOverride) {
143: this .enableUserOverride = enableUserOverride;
144: }
145:
146: public void setEnabledUserOverride(String enableUserOverride) {
147: new Boolean(enableUserOverride).booleanValue();
148: }
149:
150: /**
151: * Serialize the resource into XML, adding an element to the doc under the top of the stack element.
152: *
153: * @param doc
154: * The DOM doc to contain the XML (or null for a string return).
155: * @param stack
156: * The DOM elements, the top of which is the containing element of the new "resource" element.
157: * @return The newly added element.
158: */
159: public Element toXml(Document doc, Stack stack) {
160: Element channel = doc.createElement("channel");
161:
162: if (stack.isEmpty()) {
163: doc.appendChild(channel);
164: } else {
165: ((Element) stack.peek()).appendChild(channel);
166: }
167:
168: stack.push(channel);
169:
170: channel.setAttribute("context", getContext());
171: channel.setAttribute("id", getId());
172: channel.setAttribute("description", getDescription());
173: channel.setAttribute("title", getTitle());
174: channel.setAttribute("creationDate", Long
175: .toString(getCreationDate().getTime()));
176: channel.setAttribute("filterType", getFilterType());
177: channel.setAttribute("filterParam", Integer
178: .toString(getFilterParam()));
179: channel.setAttribute("contextDefaultChannel", Boolean
180: .toString(isContextDefaultChannel()));
181:
182: stack.pop();
183:
184: return channel;
185:
186: } // toXml
187:
188: /**
189: * Converts the serialized xml back to a ChatChannel object
190: * @param channelElement
191: * @return
192: */
193: public static ChatChannel xmlToChatChannel(Element channelElement,
194: String siteId) {
195: ChatChannel tmpChannel = new ChatChannel();
196: //tmpChannel.setContext(channelElement.getAttribute("context"));
197: tmpChannel.setContext(siteId);
198:
199: if (siteId.equalsIgnoreCase(channelElement
200: .getAttribute("context"))) {
201: //If importing into the same site, keep the id. We should get an update instead of saving a new one.
202: tmpChannel.setId(channelElement.getAttribute("id"));
203: }
204: tmpChannel.setDescription(channelElement
205: .getAttribute("description"));
206: tmpChannel.setTitle(channelElement.getAttribute("title"));
207: tmpChannel
208: .setCreationDate(new Date(Long.parseLong(channelElement
209: .getAttribute("creationDate"))));
210: tmpChannel.setFilterType(channelElement
211: .getAttribute("filterType"));
212: tmpChannel.setFilterParam(Integer.parseInt(channelElement
213: .getAttribute("filterParam")));
214: tmpChannel.setContextDefaultChannel(Boolean
215: .parseBoolean(channelElement
216: .getAttribute("contextDefaultChannel")));
217:
218: return tmpChannel;
219: }
220:
221: /* (non-Javadoc)
222: * @see org.sakaiproject.entity.api.Entity#getProperties()
223: */
224: public ResourceProperties getProperties() {
225: // TODO Auto-generated method stub
226: return null;
227: }
228:
229: /* (non-Javadoc)
230: * @see org.sakaiproject.entity.api.Entity#getReference()
231: */
232: public String getReference() {
233: return ChatManager.REFERENCE_ROOT + Entity.SEPARATOR
234: + ChatManager.REF_TYPE_CHANNEL + Entity.SEPARATOR
235: + context + Entity.SEPARATOR + id;
236:
237: }
238:
239: /* (non-Javadoc)
240: * @see org.sakaiproject.entity.api.Entity#getReference(java.lang.String)
241: */
242: public String getReference(String rootProperty) {
243: return getReference();
244: }
245:
246: /* (non-Javadoc)
247: * @see org.sakaiproject.entity.api.Entity#getUrl()
248: */
249: public String getUrl() {
250: return ServerConfigurationService.getAccessUrl()
251: + getReference();
252: }
253:
254: /* (non-Javadoc)
255: * @see org.sakaiproject.entity.api.Entity#getUrl(java.lang.String)
256: */
257: public String getUrl(String rootProperty) {
258: return getUrl();
259: }
260: }
|