001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-hbm/src/java/org/sakaiproject/component/app/messageforums/dao/hibernate/MessageImpl.java $
003: * $Id: MessageImpl.java 9227 2006-05-15 15:02:42Z cwen@iupui.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.component.app.messageforums.dao.hibernate;
021:
022: import java.util.Comparator;
023: import java.util.Date;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Set;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.api.app.messageforums.Attachment;
031: import org.sakaiproject.api.app.messageforums.Message;
032: import org.sakaiproject.api.app.messageforums.Topic;
033:
034: public class MessageImpl extends MutableEntityImpl implements Message {
035:
036: private static final Log LOG = LogFactory.getLog(MessageImpl.class);
037:
038: private String title;
039: private String body;
040: private String author;
041: private Set attachmentsSet; // = new HashSet();
042: private String label;
043: private Message inReplyTo;
044: private String gradebook;
045: private String gradebookAssignment;
046: private String typeUuid;
047: private Boolean approved;
048: private Boolean draft;
049: private Topic topic;
050: private Boolean hasAttachments = Boolean.FALSE;
051: private String gradeComment;
052: private String gradeAssignmentName;
053:
054: public static Comparator ATTACHMENT_COMPARATOR;
055: public static Comparator SUBJECT_COMPARATOR;
056: public static Comparator DATE_COMPARATOR;
057: public static Comparator LABEL_COMPARATOR;
058: public static Comparator AUTHORED_BY_COMPARATOR;
059:
060: public static Comparator ATTACHMENT_COMPARATOR_DESC;
061: public static Comparator SUBJECT_COMPARATOR_DESC;
062: public static Comparator DATE_COMPARATOR_DESC;
063: public static Comparator LABEL_COMPARATOR_DESC;
064: public static Comparator AUTHORED_BY_COMPARATOR_DESC;
065:
066: // indecies for hibernate
067: //private int tindex;
068:
069: public Topic getTopic() {
070: return topic;
071: }
072:
073: public void setTopic(Topic topic) {
074: this .topic = topic;
075: }
076:
077: public MessageImpl() {
078: attachmentsSet = new HashSet();
079: }
080:
081: public Boolean getDraft() {
082: return draft;
083: }
084:
085: public void setDraft(Boolean draft) {
086: this .draft = draft;
087: }
088:
089: public Boolean getHasAttachments() {
090: return hasAttachments;
091: }
092:
093: public void setHasAttachments(Boolean hasAttachments) {
094: this .hasAttachments = hasAttachments;
095: }
096:
097: public Boolean getApproved() {
098: return approved;
099: }
100:
101: public void setApproved(Boolean approved) {
102: this .approved = approved;
103: }
104:
105: public Set getAttachmentsSet() {
106: return attachmentsSet;
107: }
108:
109: public void setAttachmentsSet(Set attachmentsSet) {
110: this .attachmentsSet = attachmentsSet;
111: }
112:
113: public List getAttachments() {
114: return Util.setToList(attachmentsSet);
115: }
116:
117: public void setAttachments(List attachments) {
118: this .attachmentsSet = Util.listToSet(attachments);
119: }
120:
121: public String getAuthor() {
122: return author;
123: }
124:
125: public void setAuthor(String author) {
126: this .author = author;
127: }
128:
129: public String getBody() {
130: return body;
131: }
132:
133: public void setBody(String body) {
134: this .body = body;
135: }
136:
137: public String getGradebook() {
138: return gradebook;
139: }
140:
141: public void setGradebook(String gradebook) {
142: this .gradebook = gradebook;
143: }
144:
145: public String getGradebookAssignment() {
146: return gradebookAssignment;
147: }
148:
149: public void setGradebookAssignment(String gradebookAssignment) {
150: this .gradebookAssignment = gradebookAssignment;
151: }
152:
153: public Message getInReplyTo() {
154: return inReplyTo;
155: }
156:
157: public void setInReplyTo(Message inReplyTo) {
158: this .inReplyTo = inReplyTo;
159: }
160:
161: public String getLabel() {
162: return label;
163: }
164:
165: public void setLabel(String label) {
166: this .label = label;
167: }
168:
169: public String getTitle() {
170: return title;
171: }
172:
173: public void setTitle(String title) {
174: this .title = title;
175: }
176:
177: public String getTypeUuid() {
178: return typeUuid;
179: }
180:
181: public void setTypeUuid(String typeUuid) {
182: this .typeUuid = typeUuid;
183: }
184:
185: public String toString() {
186: return "Message/" + id;
187: //return "Message.id:" + id;
188: }
189:
190: public boolean equals(Object obj) {
191: if (obj != null && obj instanceof Message) {
192: return getId().equals(((Message) obj).getId());
193: }
194: return false;
195: }
196:
197: // needs a better impl
198: public int hashCode() {
199: return getId() == null ? 0 : getId().hashCode();
200: }
201:
202: // //////////////////////////////////////////////////////////////////////
203: // helper methods for collections
204: // //////////////////////////////////////////////////////////////////////
205:
206: public void addAttachment(Attachment attachment) {
207: if (LOG.isDebugEnabled()) {
208: LOG.debug("addAttachment(Attachment " + attachment + ")");
209: }
210:
211: if (attachment == null) {
212: throw new IllegalArgumentException("attachment == null");
213: }
214:
215: attachment.setMessage(this );
216: attachmentsSet.add(attachment);
217:
218: if (!hasAttachments.booleanValue()) {
219: hasAttachments = Boolean.TRUE;
220: }
221:
222: }
223:
224: public void removeAttachment(Attachment attachment) {
225: if (LOG.isDebugEnabled()) {
226: LOG
227: .debug("removeAttachment(Attachment " + attachment
228: + ")");
229: }
230:
231: if (attachment == null) {
232: throw new IllegalArgumentException(
233: "Illegal attachment argument passed!");
234: }
235:
236: attachment.setMessage(null);
237: attachmentsSet.remove(attachment);
238:
239: if (attachmentsSet.size() == 0) {
240: hasAttachments = Boolean.FALSE;
241: }
242: }
243:
244: // public int getTindex()
245: // {
246: // try
247: // {
248: // return getTopic().getMessages().indexOf(this);
249: // }
250: // catch (Exception e)
251: // {
252: // return tindex;
253: // }
254: // }
255: //
256: // public void setTindex(int tindex)
257: // {
258: // this.tindex = tindex;
259: // }
260:
261: // ============================================
262: static {
263: SUBJECT_COMPARATOR = new Comparator() {
264: public int compare(Object message, Object otherMessage) {
265: if (message != null && otherMessage != null
266: && message instanceof Message
267: && otherMessage instanceof Message) {
268: String msg = ((Message) message).getTitle();
269: String msg2 = ((Message) otherMessage).getTitle();
270: return msg.compareTo(msg2);
271: }
272: return -1;
273:
274: }
275: };
276:
277: DATE_COMPARATOR = new Comparator() {
278: public int compare(Object message, Object otherMessage) {
279: if (message != null && otherMessage != null
280: && message instanceof Message
281: && otherMessage instanceof Message) {
282: Date msg = ((Message) message).getCreated();
283: Date msg2 = ((Message) otherMessage).getCreated();
284: return msg.compareTo(msg2);
285: }
286: return -1;
287: }
288: };
289:
290: AUTHORED_BY_COMPARATOR = new Comparator() {
291: public int compare(Object message, Object otherMessage) {
292: if (message != null && otherMessage != null
293: && message instanceof Message
294: && otherMessage instanceof Message) {
295: String msg = ((Message) message).getAuthor();
296: String msg2 = ((Message) otherMessage).getAuthor();
297: return msg.compareTo(msg2);
298: }
299: return -1;
300: }
301: };
302:
303: LABEL_COMPARATOR = new Comparator() {
304:
305: public int compare(Object message, Object otherMessage) {
306: if (message != null && otherMessage != null
307: && message instanceof Message
308: && otherMessage instanceof Message) {
309: String msg = ((Message) message).getLabel();
310: String msg2 = ((Message) otherMessage).getLabel();
311: return msg.compareTo(msg2);
312: }
313: return -1;
314: }
315:
316: };
317:
318: // TODO: make more generic and reuse the above
319:
320: SUBJECT_COMPARATOR_DESC = new Comparator() {
321: public int compare(Object message, Object otherMessage) {
322: if (message != null && otherMessage != null
323: && message instanceof Message
324: && otherMessage instanceof Message) {
325: String msg2 = ((Message) message).getTitle();
326: String msg = ((Message) otherMessage).getTitle();
327: return msg.compareTo(msg2);
328: }
329: return -1;
330:
331: }
332: };
333:
334: DATE_COMPARATOR_DESC = new Comparator() {
335: public int compare(Object message, Object otherMessage) {
336: if (message != null && otherMessage != null
337: && message instanceof Message
338: && otherMessage instanceof Message) {
339: Date msg2 = ((Message) message).getCreated();
340: Date msg = ((Message) otherMessage).getCreated();
341: return msg.compareTo(msg2);
342: }
343: return -1;
344: }
345: };
346:
347: AUTHORED_BY_COMPARATOR_DESC = new Comparator() {
348: public int compare(Object message, Object otherMessage) {
349: if (message != null && otherMessage != null
350: && message instanceof Message
351: && otherMessage instanceof Message) {
352: String msg2 = ((Message) message).getAuthor();
353: String msg = ((Message) otherMessage).getAuthor();
354: return msg.compareTo(msg2);
355: }
356: return -1;
357: }
358: };
359:
360: LABEL_COMPARATOR_DESC = new Comparator() {
361:
362: public int compare(Object message, Object otherMessage) {
363: if (message != null && otherMessage != null
364: && message instanceof Message
365: && otherMessage instanceof Message) {
366: String msg2 = ((Message) message).getLabel();
367: String msg = ((Message) otherMessage).getLabel();
368: return msg.compareTo(msg2);
369: }
370: return -1;
371: }
372:
373: };
374:
375: }
376:
377: public String getGradeComment() {
378: return gradeComment;
379: }
380:
381: public void setGradeComment(String gradeComment) {
382: this .gradeComment = gradeComment;
383: }
384:
385: public String getGradeAssignmentName() {
386: return gradeAssignmentName;
387: }
388:
389: public void setGradeAssignmentName(String gradeAssignmentName) {
390: this.gradeAssignmentName = gradeAssignmentName;
391: }
392:
393: }
|