0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/MessageForumsMessageManagerImpl.java $
0003: * $Id: MessageForumsMessageManagerImpl.java 9227 2006-05-15 15:02:42Z cwen@iupui.edu $
0004: ***********************************************************************************
0005: *
0006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 The Sakai Foundation.
0007: *
0008: * Licensed under the Educational Community License, Version 1.0 (the "License");
0009: * you may not use this file except in compliance with the License.
0010: * You may obtain a copy of the License at
0011: *
0012: * http://www.opensource.org/licenses/ecl1.php
0013: *
0014: * Unless required by applicable law or agreed to in writing, software
0015: * distributed under the License is distributed on an "AS IS" BASIS,
0016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: * See the License for the specific language governing permissions and
0018: * limitations under the License.
0019: *
0020: **********************************************************************************/package org.sakaiproject.component.app.messageforums;
0021:
0022: import java.sql.SQLException;
0023: import java.util.ArrayList;
0024: import java.util.Collection;
0025: import java.util.Collections;
0026: import java.util.Date;
0027: import java.util.HashMap;
0028: import java.util.HashSet;
0029: import java.util.Iterator;
0030: import java.util.List;
0031: import java.util.Map;
0032: import java.util.Set;
0033:
0034: import org.apache.commons.logging.Log;
0035: import org.apache.commons.logging.LogFactory;
0036: import org.hibernate.Hibernate;
0037: import org.hibernate.HibernateException;
0038: import org.hibernate.Query;
0039: import org.hibernate.Session;
0040: import org.sakaiproject.api.app.messageforums.Attachment;
0041: import org.sakaiproject.api.app.messageforums.BaseForum;
0042: import org.sakaiproject.api.app.messageforums.DiscussionForumService;
0043: import org.sakaiproject.api.app.messageforums.Message;
0044: import org.sakaiproject.api.app.messageforums.MessageForumsMessageManager;
0045: import org.sakaiproject.api.app.messageforums.MessageForumsTypeManager;
0046: import org.sakaiproject.api.app.messageforums.PrivateMessage;
0047: import org.sakaiproject.api.app.messageforums.Topic;
0048: import org.sakaiproject.api.app.messageforums.UnreadStatus;
0049: import org.sakaiproject.component.app.messageforums.dao.hibernate.AttachmentImpl;
0050: import org.sakaiproject.component.app.messageforums.dao.hibernate.MessageImpl;
0051: import org.sakaiproject.component.app.messageforums.dao.hibernate.PrivateMessageImpl;
0052: import org.sakaiproject.component.app.messageforums.dao.hibernate.UnreadStatusImpl;
0053: import org.sakaiproject.component.app.messageforums.dao.hibernate.Util;
0054: import org.sakaiproject.component.app.messageforums.exception.LockedException;
0055: import org.sakaiproject.content.cover.ContentHostingService;
0056: import org.sakaiproject.event.api.EventTrackingService;
0057: import org.sakaiproject.exception.IdUnusedException;
0058: import org.sakaiproject.id.api.IdManager;
0059: import org.sakaiproject.site.api.Site;
0060: import org.sakaiproject.site.cover.SiteService;
0061: import org.sakaiproject.tool.api.Placement;
0062: import org.sakaiproject.tool.api.SessionManager;
0063: import org.sakaiproject.tool.api.Tool;
0064: import org.sakaiproject.tool.cover.ToolManager;
0065: import org.springframework.orm.hibernate3.HibernateCallback;
0066: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0067:
0068: public class MessageForumsMessageManagerImpl extends
0069: HibernateDaoSupport implements MessageForumsMessageManager {
0070:
0071: private static final Log LOG = LogFactory
0072: .getLog(MessageForumsMessageManagerImpl.class);
0073:
0074: //private static final String QUERY_BY_MESSAGE_ID = "findMessageById";
0075: //private static final String QUERY_ATTACHMENT_BY_ID = "findAttachmentById";
0076: private static final String QUERY_BY_MESSAGE_ID_WITH_ATTACHMENTS = "findMessageByIdWithAttachments";
0077: private static final String QUERY_COUNT_BY_READ = "findReadMessageCountByTopicId";
0078: private static final String QUERY_COUNT_BY_AUTHORED = "findAuhtoredMessageCountByTopicId";
0079: private static final String QUERY_BY_TOPIC_ID = "findMessagesByTopicId";
0080: private static final String QUERY_COUNT_VIEWABLE_BY_TOPIC_ID = "findViewableMessageCountByTopicIdByUserId";
0081: private static final String QUERY_COUNT_READ_VIEWABLE_BY_TOPIC_ID = "findReadViewableMessageCountByTopicIdByUserId";
0082: private static final String QUERY_UNREAD_STATUS = "findUnreadStatusForMessage";
0083: private static final String QUERY_CHILD_MESSAGES = "finalAllChildMessages";
0084: private static final String QUERY_READ_STATUS_WITH_MSGS_USER = "findReadStatusByMsgIds";
0085: private static final String QUERY_FIND_PENDING_MSGS_BY_CONTEXT_AND_USER = "findAllPendingMsgsByContextByMembership";
0086: private static final String QUERY_FIND_PENDING_MSGS_BY_TOPICID = "findPendingMsgsByTopicId";
0087: //private static final String ID = "id";
0088:
0089: private static final String MESSAGECENTER_HELPER_TOOL_ID = "sakai.messageforums.helper";
0090:
0091: private IdManager idManager;
0092:
0093: private MessageForumsTypeManager typeManager;
0094:
0095: private SessionManager sessionManager;
0096:
0097: private EventTrackingService eventTrackingService;
0098:
0099: public void init() {
0100: LOG.info("init()");
0101: ;
0102: }
0103:
0104: public EventTrackingService getEventTrackingService() {
0105: return eventTrackingService;
0106: }
0107:
0108: public void setEventTrackingService(
0109: EventTrackingService eventTrackingService) {
0110: this .eventTrackingService = eventTrackingService;
0111: }
0112:
0113: public MessageForumsTypeManager getTypeManager() {
0114: return typeManager;
0115: }
0116:
0117: public void setTypeManager(MessageForumsTypeManager typeManager) {
0118: this .typeManager = typeManager;
0119: }
0120:
0121: public void setSessionManager(SessionManager sessionManager) {
0122: this .sessionManager = sessionManager;
0123: }
0124:
0125: public void setIdManager(IdManager idManager) {
0126: this .idManager = idManager;
0127: }
0128:
0129: public IdManager getIdManager() {
0130: return idManager;
0131: }
0132:
0133: public SessionManager getSessionManager() {
0134: return sessionManager;
0135: }
0136:
0137: /**
0138: * FOR SYNOPTIC TOOL:
0139: * Returns the count of discussion forum messages grouped by site
0140: *
0141: * @param siteList
0142: * List of site ids user is a part of
0143: *
0144: * @return List
0145: */
0146: public List findDiscussionForumMessageCountsForAllSites(
0147: final List siteList) {
0148: if (siteList == null) {
0149: LOG
0150: .error("findDiscussionForumMessageCountsForAllSites failed with null site list.");
0151: throw new IllegalArgumentException("Null Argument");
0152: }
0153:
0154: HibernateCallback hcb = new HibernateCallback() {
0155: public Object doInHibernate(Session session)
0156: throws HibernateException, SQLException {
0157: Query q = session
0158: .getNamedQuery("findDiscussionForumMessageCountsForAllSites");
0159: q.setParameterList("siteList", siteList);
0160: return q.list();
0161: }
0162: };
0163:
0164: return (List) getHibernateTemplate().execute(hcb);
0165:
0166: }
0167:
0168: /**
0169: * FOR SYNOPTIC TOOL:
0170: * Returns the count of discussion forum messages grouped by site
0171: * that user not have READ access to
0172: *
0173: * @param siteList
0174: * List of site ids user is a part of
0175: *
0176: * @return List
0177: */
0178: public List findDiscussionForumMessageRemoveCountsForAllSites(
0179: final List siteList, final List roleList) {
0180: if (siteList == null) {
0181: LOG
0182: .error("findDiscussionForumMessageCountsForAllSites failed with null site list.");
0183: throw new IllegalArgumentException("Null Argument");
0184: }
0185:
0186: HibernateCallback hcb = new HibernateCallback() {
0187: public Object doInHibernate(Session session)
0188: throws HibernateException, SQLException {
0189: Query q = session
0190: .getNamedQuery("findDiscussionForumMessageRemoveCountsForAllSites");
0191: q.setParameterList("siteList", siteList);
0192: q.setParameterList("roleList", roleList);
0193: q.setParameter("userId", getCurrentUser(),
0194: Hibernate.STRING);
0195: return q.list();
0196: }
0197: };
0198:
0199: return (List) getHibernateTemplate().execute(hcb);
0200:
0201: }
0202:
0203: /**
0204: * FOR SYNOPTIC TOOL:
0205: * Returns the count of read discussion forum messages grouped by site
0206: *
0207: * @return List
0208: */
0209: public List findDiscussionForumReadMessageCountsForAllSites() {
0210:
0211: HibernateCallback hcb = new HibernateCallback() {
0212: public Object doInHibernate(Session session)
0213: throws HibernateException, SQLException {
0214: Query q = session
0215: .getNamedQuery("findDiscussionForumReadMessageCountsForAllSites");
0216: q.setParameter("userId", getCurrentUser(),
0217: Hibernate.STRING);
0218: return q.list();
0219: }
0220: };
0221:
0222: return (List) getHibernateTemplate().execute(hcb);
0223:
0224: }
0225:
0226: /**
0227: * FOR SYNOPTIC TOOL:
0228: * Returns the count of read discussion forum messages grouped by site
0229: * that user does not have READ access to
0230: *
0231: * @return List
0232: */
0233: public List findDiscussionForumReadMessageRemoveCountsForAllSites(
0234: final List roleList) {
0235:
0236: HibernateCallback hcb = new HibernateCallback() {
0237: public Object doInHibernate(Session session)
0238: throws HibernateException, SQLException {
0239: Query q = session
0240: .getNamedQuery("findDiscussionForumReadMessageRemoveCountsForAllSites");
0241: q.setParameter("userId", getCurrentUser(),
0242: Hibernate.STRING);
0243: q.setParameterList("roleList", roleList);
0244: return q.list();
0245: }
0246: };
0247:
0248: return (List) getHibernateTemplate().execute(hcb);
0249:
0250: }
0251:
0252: /**
0253: * FOR STATISTICS TOOL:
0254: * Returns the number of read messages by topic for specified user
0255: */
0256:
0257: public int findAuhtoredMessageCountByTopicIdByUserId(
0258: final Long topicId, final String userId) {
0259: if (topicId == null || userId == null) {
0260: LOG
0261: .error("findAuthoredMessageCountByTopicIdByUserId failed with topicId: "
0262: + topicId + " and userId: " + userId);
0263: throw new IllegalArgumentException("Null Argument");
0264: }
0265:
0266: LOG
0267: .debug("findAuthoredMessageCountByTopicIdByUserId executing with topicId: "
0268: + topicId + " and userId: " + userId);
0269:
0270: HibernateCallback hcb = new HibernateCallback() {
0271: public Object doInHibernate(Session session)
0272: throws HibernateException, SQLException {
0273: Query q = session
0274: .getNamedQuery(QUERY_COUNT_BY_AUTHORED);
0275: q.setParameter("topicId", topicId, Hibernate.LONG);
0276: q.setParameter("userId", userId, Hibernate.STRING);
0277: return q.uniqueResult();
0278: }
0279: };
0280:
0281: return ((Integer) getHibernateTemplate().execute(hcb))
0282: .intValue();
0283: }
0284:
0285: public int findReadMessageCountByTopicIdByUserId(
0286: final Long topicId, final String userId) {
0287: if (topicId == null || userId == null) {
0288: LOG
0289: .error("findReadMessageCountByTopicIdByUserId failed with topicId: "
0290: + topicId + " and userId: " + userId);
0291: throw new IllegalArgumentException("Null Argument");
0292: }
0293:
0294: LOG
0295: .debug("findReadMessageCountByTopicIdByUserId executing with topicId: "
0296: + topicId + " and userId: " + userId);
0297:
0298: HibernateCallback hcb = new HibernateCallback() {
0299: public Object doInHibernate(Session session)
0300: throws HibernateException, SQLException {
0301: Query q = session.getNamedQuery(QUERY_COUNT_BY_READ);
0302: q.setParameter("topicId", topicId, Hibernate.LONG);
0303: q.setParameter("userId", userId, Hibernate.STRING);
0304: return q.uniqueResult();
0305: }
0306: };
0307:
0308: return ((Integer) getHibernateTemplate().execute(hcb))
0309: .intValue();
0310: }
0311:
0312: /**
0313: * Returns count of all messages in a topic that have been approved or were authored by given user
0314: */
0315: public int findViewableMessageCountByTopicIdByUserId(
0316: final Long topicId, final String userId) {
0317: if (topicId == null || userId == null) {
0318: LOG
0319: .error("findViewableMessageCountByTopicIdByUserId failed with topicId: "
0320: + topicId + " and userId: " + userId);
0321: throw new IllegalArgumentException("Null Argument");
0322: }
0323:
0324: LOG
0325: .debug("findViewableMessageCountByTopicIdByUserId executing with topicId: "
0326: + topicId + " and userId: " + userId);
0327:
0328: HibernateCallback hcb = new HibernateCallback() {
0329: public Object doInHibernate(Session session)
0330: throws HibernateException, SQLException {
0331: Query q = session
0332: .getNamedQuery(QUERY_COUNT_VIEWABLE_BY_TOPIC_ID);
0333: q.setParameter("topicId", topicId, Hibernate.LONG);
0334: q.setParameter("userId", userId, Hibernate.STRING);
0335: return q.uniqueResult();
0336: }
0337: };
0338:
0339: return ((Integer) getHibernateTemplate().execute(hcb))
0340: .intValue();
0341: }
0342:
0343: /**
0344: * Returns count of all msgs in a topic that have been approved or were authored by curr user
0345: */
0346: public int findViewableMessageCountByTopicId(final Long topicId) {
0347: if (topicId == null) {
0348: LOG
0349: .error("findViewableMessageCountByTopicId failed with topicId: "
0350: + topicId);
0351: throw new IllegalArgumentException("Null Argument");
0352: }
0353:
0354: LOG
0355: .debug("findViewableMessageCountByTopicId executing with topicId: "
0356: + topicId);
0357:
0358: return findViewableMessageCountByTopicIdByUserId(topicId,
0359: getCurrentUser());
0360: }
0361:
0362: public int findUnreadMessageCountByTopicIdByUserId(
0363: final Long topicId, final String userId) {
0364: if (topicId == null || userId == null) {
0365: LOG
0366: .error("findUnreadMessageCountByTopicIdByUserId failed with topicId: "
0367: + topicId + " and userId: " + userId);
0368: throw new IllegalArgumentException("Null Argument");
0369: }
0370:
0371: LOG
0372: .debug("findUnreadMessageCountByTopicIdByUserId executing with topicId: "
0373: + topicId);
0374:
0375: return findMessageCountByTopicId(topicId)
0376: - findReadMessageCountByTopicIdByUserId(topicId, userId);
0377: }
0378:
0379: public int findUnreadMessageCountByTopicId(final Long topicId) {
0380: if (topicId == null) {
0381: LOG
0382: .error("findUnreadMessageCountByTopicId failed with topicId: "
0383: + topicId);
0384: throw new IllegalArgumentException("Null Argument");
0385: }
0386:
0387: LOG
0388: .debug("findUnreadMessageCountByTopicId executing with topicId: "
0389: + topicId);
0390:
0391: return findMessageCountByTopicId(topicId)
0392: - findReadMessageCountByTopicId(topicId);
0393: }
0394:
0395: /**
0396: * Returns count of all unread msgs for given user that have been approved or
0397: * were authored by user
0398: */
0399: public int findUnreadViewableMessageCountByTopicIdByUserId(
0400: final Long topicId, final String userId) {
0401: if (topicId == null) {
0402: LOG
0403: .error("findUnreadViewableMessageCountByTopicIdByUserId failed with topicId: "
0404: + topicId + " and userid: " + userId);
0405: throw new IllegalArgumentException("Null Argument");
0406: }
0407:
0408: LOG
0409: .debug("findUnreadViewableMessageCountByTopicIdByUserId executing with topicId: "
0410: + topicId + " userId: " + userId);
0411:
0412: return findViewableMessageCountByTopicIdByUserId(topicId,
0413: userId)
0414: - findReadViewableMessageCountByTopicIdByUserId(
0415: topicId, userId);
0416: }
0417:
0418: /**
0419: * Returns count of all unread msgs for current user that have been approved or
0420: * were authored by current user
0421: */
0422: public int findUnreadViewableMessageCountByTopicId(
0423: final Long topicId) {
0424: if (topicId == null) {
0425: LOG
0426: .error("findUnreadViewableMessageCountByTopicId failed with topicId: "
0427: + topicId);
0428: throw new IllegalArgumentException("Null Argument");
0429: }
0430:
0431: LOG
0432: .debug("findUnreadViewableMessageCountByTopicId executing with topicId: "
0433: + topicId);
0434:
0435: return findUnreadViewableMessageCountByTopicIdByUserId(topicId,
0436: getCurrentUser());
0437: }
0438:
0439: public int findReadMessageCountByTopicId(final Long topicId) {
0440: if (topicId == null) {
0441: LOG
0442: .error("findReadMessageCountByTopicId failed with topicId: "
0443: + topicId);
0444: throw new IllegalArgumentException("Null Argument");
0445: }
0446:
0447: return findReadMessageCountByTopicIdByUserId(topicId,
0448: getCurrentUser());
0449: }
0450:
0451: /**
0452: * Returns count of all read msgs for given user that have been approved or
0453: * were authored by user
0454: * @param topicId
0455: * @param userId
0456: * @return
0457: */
0458: public int findReadViewableMessageCountByTopicIdByUserId(
0459: final Long topicId, final String userId) {
0460: if (topicId == null || userId == null) {
0461: LOG
0462: .error("findReadViewableMessageCountByTopicIdByUserId failed with topicId: "
0463: + topicId + " and userId: " + userId);
0464: throw new IllegalArgumentException("Null Argument");
0465: }
0466:
0467: LOG
0468: .debug("findReadViewableMessageCountByTopicIdByUserId executing with topicId: "
0469: + topicId + " and userId: " + userId);
0470:
0471: HibernateCallback hcb = new HibernateCallback() {
0472: public Object doInHibernate(Session session)
0473: throws HibernateException, SQLException {
0474: Query q = session
0475: .getNamedQuery(QUERY_COUNT_READ_VIEWABLE_BY_TOPIC_ID);
0476: q.setParameter("topicId", topicId, Hibernate.LONG);
0477: q.setParameter("userId", userId, Hibernate.STRING);
0478: return q.uniqueResult();
0479: }
0480: };
0481:
0482: return ((Integer) getHibernateTemplate().execute(hcb))
0483: .intValue();
0484: }
0485:
0486: /**
0487: * Returns count of all read msgs for current user that have been approved or
0488: * were authored by user
0489: * @param topicId
0490: * @return
0491: */
0492: public int findReadViewableMessageCountByTopicId(final Long topicId) {
0493: if (topicId == null) {
0494: LOG
0495: .error("findReadViewableMessageCountByTopicId failed with topicId: "
0496: + topicId);
0497: throw new IllegalArgumentException("Null Argument");
0498: }
0499:
0500: return findReadViewableMessageCountByTopicIdByUserId(topicId,
0501: getCurrentUser());
0502: }
0503:
0504: public List findMessagesByTopicId(final Long topicId) {
0505: if (topicId == null) {
0506: LOG.error("findMessagesByTopicId failed with topicId: "
0507: + topicId);
0508: throw new IllegalArgumentException("Null Argument");
0509: }
0510:
0511: LOG.debug("findMessagesByTopicId executing with topicId: "
0512: + topicId);
0513:
0514: HibernateCallback hcb = new HibernateCallback() {
0515: public Object doInHibernate(Session session)
0516: throws HibernateException, SQLException {
0517: Query q = session.getNamedQuery(QUERY_BY_TOPIC_ID);
0518: q.setParameter("topicId", topicId, Hibernate.LONG);
0519: return q.list();
0520: }
0521: };
0522:
0523: return (List) getHibernateTemplate().execute(hcb);
0524: }
0525:
0526: public int findMessageCountByTopicId(final Long topicId) {
0527: if (topicId == null) {
0528: LOG.error("findMessageCountByTopicId failed with topicId: "
0529: + topicId);
0530: throw new IllegalArgumentException("Null Argument");
0531: }
0532:
0533: LOG.debug("findMessageCountByTopicId executing with topicId: "
0534: + topicId);
0535:
0536: HibernateCallback hcb = new HibernateCallback() {
0537: public Object doInHibernate(Session session)
0538: throws HibernateException, SQLException {
0539: Query q = session
0540: .getNamedQuery("findMessageCountByTopicId");
0541: q.setParameter("topicId", topicId, Hibernate.LONG);
0542: return q.uniqueResult();
0543: }
0544: };
0545:
0546: return ((Integer) getHibernateTemplate().execute(hcb))
0547: .intValue();
0548: }
0549:
0550: public UnreadStatus findUnreadStatusByUserId(final Long topicId,
0551: final Long messageId, final String userId) {
0552: if (messageId == null || topicId == null || userId == null) {
0553: LOG.error("findUnreadStatusByUserId failed with topicId: "
0554: + topicId + ", messageId: " + messageId
0555: + ", userId: " + userId);
0556: throw new IllegalArgumentException("Null Argument");
0557: }
0558:
0559: LOG.debug("findUnreadStatus executing with topicId: " + topicId
0560: + ", messageId: " + messageId);
0561:
0562: HibernateCallback hcb = new HibernateCallback() {
0563: public Object doInHibernate(Session session)
0564: throws HibernateException, SQLException {
0565: Query q = session.getNamedQuery(QUERY_UNREAD_STATUS);
0566: q.setParameter("topicId", topicId, Hibernate.LONG);
0567: q.setParameter("messageId", messageId, Hibernate.LONG);
0568: q.setParameter("userId", userId, Hibernate.STRING);
0569: return q.uniqueResult();
0570: }
0571: };
0572:
0573: return (UnreadStatus) getHibernateTemplate().execute(hcb);
0574: }
0575:
0576: public UnreadStatus findUnreadStatus(final Long topicId,
0577: final Long messageId) {
0578: if (messageId == null || topicId == null) {
0579: LOG.error("findUnreadStatus failed with topicId: "
0580: + topicId + ", messageId: " + messageId);
0581: throw new IllegalArgumentException("Null Argument");
0582: }
0583:
0584: return findUnreadStatusByUserId(topicId, messageId,
0585: getCurrentUser());
0586: }
0587:
0588: public void deleteUnreadStatus(Long topicId, Long messageId) {
0589: if (messageId == null || topicId == null) {
0590: LOG.error("deleteUnreadStatus failed with topicId: "
0591: + topicId + ", messageId: " + messageId);
0592: throw new IllegalArgumentException("Null Argument");
0593: }
0594:
0595: LOG.debug("deleteUnreadStatus executing with topicId: "
0596: + topicId + ", messageId: " + messageId);
0597:
0598: UnreadStatus status = findUnreadStatus(topicId, messageId);
0599: if (status != null) {
0600: getHibernateTemplate().delete(status);
0601: }
0602: }
0603:
0604: public void markMessageReadForUser(Long topicId, Long messageId,
0605: boolean read) {
0606: if (messageId == null || topicId == null) {
0607: LOG.error("markMessageReadForUser failed with topicId: "
0608: + topicId + ", messageId: " + messageId);
0609: throw new IllegalArgumentException("Null Argument");
0610: }
0611:
0612: LOG.debug("markMessageReadForUser executing with topicId: "
0613: + topicId + ", messageId: " + messageId);
0614:
0615: markMessageReadForUser(topicId, messageId, read,
0616: getCurrentUser());
0617: }
0618:
0619: public void markMessageReadForUser(Long topicId, Long messageId,
0620: boolean read, String userId) {
0621: if (messageId == null || topicId == null || userId == null) {
0622: LOG.error("markMessageReadForUser failed with topicId: "
0623: + topicId + ", messageId: " + messageId
0624: + ", userId: " + userId);
0625: throw new IllegalArgumentException("Null Argument");
0626: }
0627:
0628: LOG.debug("markMessageReadForUser executing with topicId: "
0629: + topicId + ", messageId: " + messageId);
0630:
0631: UnreadStatus status = findUnreadStatusByUserId(topicId,
0632: messageId, userId);
0633: if (status == null) {
0634: status = new UnreadStatusImpl();
0635: }
0636: status.setTopicId(topicId);
0637: status.setMessageId(messageId);
0638: status.setUserId(userId);
0639: status.setRead(new Boolean(read));
0640:
0641: Message message = (Message) getMessageById(messageId);
0642: eventTrackingService.post(eventTrackingService.newEvent(
0643: DiscussionForumService.EVENT_RESOURCE_READ,
0644: getEventMessage(message), false));
0645:
0646: getHibernateTemplate().saveOrUpdate(status);
0647: }
0648:
0649: public boolean isMessageReadForUser(final Long topicId,
0650: final Long messageId) {
0651: if (messageId == null || topicId == null) {
0652: LOG.error("getMessageById failed with topicId: " + topicId
0653: + ", messageId: " + messageId);
0654: throw new IllegalArgumentException("Null Argument");
0655: }
0656:
0657: LOG.debug("getMessageById executing with topicId: " + topicId
0658: + ", messageId: " + messageId);
0659:
0660: UnreadStatus status = findUnreadStatus(topicId, messageId);
0661: if (status == null) {
0662: return false; // not been saved yet, so it is unread
0663: }
0664: return status.getRead().booleanValue();
0665: }
0666:
0667: public PrivateMessage createPrivateMessage() {
0668: PrivateMessage message = new PrivateMessageImpl();
0669: message.setUuid(getNextUuid());
0670: message.setTypeUuid(typeManager.getPrivateMessageAreaType());
0671: message.setCreated(new Date());
0672: message.setCreatedBy(getCurrentUser());
0673: message.setDraft(Boolean.FALSE);
0674: message.setHasAttachments(Boolean.FALSE);
0675:
0676: LOG.info("message " + message.getUuid()
0677: + " created successfully");
0678: return message;
0679: }
0680:
0681: public Message createDiscussionMessage() {
0682: return createMessage(typeManager.getDiscussionForumType());
0683: }
0684:
0685: public Message createOpenMessage() {
0686: return createMessage(typeManager.getOpenDiscussionForumType());
0687: }
0688:
0689: public Message createMessage(String typeId) {
0690: Message message = new MessageImpl();
0691: message.setUuid(getNextUuid());
0692: message.setTypeUuid(typeId);
0693: message.setCreated(new Date());
0694: message.setCreatedBy(getCurrentUser());
0695: message.setDraft(Boolean.FALSE);
0696: message.setHasAttachments(Boolean.FALSE);
0697:
0698: LOG.info("message " + message.getUuid()
0699: + " created successfully");
0700: return message;
0701: }
0702:
0703: public Attachment createAttachment() {
0704: Attachment attachment = new AttachmentImpl();
0705: attachment.setUuid(getNextUuid());
0706: attachment.setCreated(new Date());
0707: attachment.setCreatedBy(getCurrentUser());
0708: attachment.setModified(new Date());
0709: attachment.setModifiedBy(getCurrentUser());
0710:
0711: LOG.info("attachment " + attachment.getUuid()
0712: + " created successfully");
0713: return attachment;
0714: }
0715:
0716: public void saveMessage(Message message) {
0717: boolean isNew = message.getId() == null;
0718:
0719: if (!(message instanceof PrivateMessage)) {
0720: if (isForumOrTopicLocked(message.getTopic().getBaseForum()
0721: .getId(), message.getTopic().getId())) {
0722: LOG.info("saveMessage executed [messageId: "
0723: + (isNew ? "new" : message.getId().toString())
0724: + "] but forum is locked -- save aborted");
0725: throw new LockedException(
0726: "Message could not be saved [messageId: "
0727: + (isNew ? "new" : message.getId()
0728: .toString()) + "]");
0729: }
0730: }
0731:
0732: message.setModified(new Date());
0733: message.setModifiedBy(getCurrentUser());
0734: if (message.getUuid() == null || message.getCreated() == null
0735: || message.getCreatedBy() == null
0736: || message.getModified() == null
0737: || message.getModifiedBy() == null
0738: || message.getTitle() == null
0739: || message.getAuthor() == null
0740: || message.getHasAttachments() == null
0741: || message.getTypeUuid() == null
0742: || message.getDraft() == null) {
0743: LOG
0744: .error("null attribute(s) for saving message in MessageForumsMessageManagerImpl.saveMessage");
0745: }
0746: getHibernateTemplate().saveOrUpdate(message);
0747:
0748: if (isNew) {
0749: eventTrackingService.post(eventTrackingService.newEvent(
0750: DiscussionForumService.EVENT_RESOURCE_ADD,
0751: getEventMessage(message), false));
0752: } else {
0753: eventTrackingService.post(eventTrackingService.newEvent(
0754: DiscussionForumService.EVENT_RESOURCE_WRITE,
0755: getEventMessage(message), false));
0756: }
0757: eventTrackingService.post(eventTrackingService.newEvent(
0758: DiscussionForumService.EVENT_RESOURCE_RESPONSE,
0759: getEventMessage(message), false));
0760:
0761: LOG.info("message " + message.getId() + " saved successfully");
0762: }
0763:
0764: public void deleteMessage(Message message) {
0765: long id = message.getId().longValue();
0766: message.setInReplyTo(null);
0767: getHibernateTemplate().saveOrUpdate(message);
0768: try {
0769: getSession().flush();
0770: } catch (Exception e) {
0771: e.printStackTrace();
0772: }
0773: eventTrackingService.post(eventTrackingService.newEvent(
0774: DiscussionForumService.EVENT_RESOURCE_REMOVE,
0775: getEventMessage(message), false));
0776: try {
0777: getSession().evict(message);
0778: } catch (Exception e) {
0779: e.printStackTrace();
0780: LOG.error("could not evict message: " + message.getId(), e);
0781: }
0782: Topic topic = message.getTopic();
0783: topic.removeMessage(message);
0784: getHibernateTemplate().saveOrUpdate(topic);
0785: //getHibernateTemplate().delete(message);
0786: try {
0787: getSession().flush();
0788: } catch (Exception e) {
0789: e.printStackTrace();
0790: }
0791: LOG.info("message " + id + " deleted successfully");
0792: }
0793:
0794: public Message getMessageById(final Long messageId) {
0795: if (messageId == null) {
0796: throw new IllegalArgumentException("Null Argument");
0797: }
0798:
0799: LOG.debug("getMessageById executing with messageId: "
0800: + messageId);
0801:
0802: return (Message) getHibernateTemplate().get(MessageImpl.class,
0803: messageId);
0804: }
0805:
0806: /**
0807: * @see org.sakaiproject.api.app.messageforums.MessageForumsMessageManager#getMessageByIdWithAttachments(java.lang.Long)
0808: */
0809: public Message getMessageByIdWithAttachments(final Long messageId) {
0810:
0811: if (messageId == null) {
0812: throw new IllegalArgumentException("Null Argument");
0813: }
0814:
0815: LOG
0816: .debug("getMessageByIdWithAttachments executing with messageId: "
0817: + messageId);
0818:
0819: HibernateCallback hcb = new HibernateCallback() {
0820: public Object doInHibernate(Session session)
0821: throws HibernateException, SQLException {
0822: Query q = session
0823: .getNamedQuery(QUERY_BY_MESSAGE_ID_WITH_ATTACHMENTS);
0824: q.setParameter("id", messageId, Hibernate.LONG);
0825: return q.uniqueResult();
0826: }
0827: };
0828:
0829: return (Message) getHibernateTemplate().execute(hcb);
0830: }
0831:
0832: public Attachment getAttachmentById(final Long attachmentId) {
0833: if (attachmentId == null) {
0834: throw new IllegalArgumentException("Null Argument");
0835: }
0836:
0837: LOG.debug("getAttachmentById executing with attachmentId: "
0838: + attachmentId);
0839:
0840: return (Attachment) getHibernateTemplate().get(
0841: AttachmentImpl.class, attachmentId);
0842: }
0843:
0844: public void getChildMsgs(final Long messageId, List returnList) {
0845: List tempList;
0846:
0847: HibernateCallback hcb = new HibernateCallback() {
0848: public Object doInHibernate(Session session)
0849: throws HibernateException, SQLException {
0850: Query q = session.getNamedQuery(QUERY_CHILD_MESSAGES);
0851: Query qOrdered = session
0852: .createQuery(q.getQueryString());
0853:
0854: qOrdered.setParameter("messageId", messageId,
0855: Hibernate.LONG);
0856:
0857: return qOrdered.list();
0858: }
0859: };
0860:
0861: tempList = (List) getHibernateTemplate().execute(hcb);
0862: if (tempList != null) {
0863: for (int i = 0; i < tempList.size(); i++) {
0864: getChildMsgs(((Message) tempList.get(i)).getId(),
0865: returnList);
0866: returnList.add((Message) tempList.get(i));
0867: }
0868: }
0869: }
0870:
0871: /**
0872: * Will set the approved status on the given message
0873: */
0874: public void markMessageApproval(Long messageId, boolean approved) {
0875: if (messageId == null) {
0876: LOG.error("markMessageApproval failed with messageId: "
0877: + messageId);
0878: throw new IllegalArgumentException("Null Argument");
0879: }
0880:
0881: LOG.debug("markMessageApproval executing with messageId: "
0882: + messageId);
0883:
0884: Message message = (Message) getMessageById(messageId);
0885: message.setApproved(new Boolean(approved));
0886:
0887: getHibernateTemplate().saveOrUpdate(message);
0888: }
0889:
0890: public void deleteMsgWithChild(final Long messageId) {
0891: List this List = new ArrayList();
0892: getChildMsgs(messageId, this List);
0893:
0894: for (int i = 0; i < this List.size(); i++) {
0895: //Message delMessage = getMessageByIdWithAttachments(((Message)thisList.get(i)).getId());
0896: //deleteMessage(getMessageById(((Message)thisList.get(i)).getId()));
0897: Message delMessage = getMessageById(((Message) this List
0898: .get(i)).getId());
0899: deleteMessage(delMessage);
0900: }
0901:
0902: deleteMessage(getMessageById(messageId));
0903: }
0904:
0905: public List getFirstLevelChildMsgs(final Long messageId) {
0906: HibernateCallback hcb = new HibernateCallback() {
0907: public Object doInHibernate(Session session)
0908: throws HibernateException, SQLException {
0909: Query q = session.getNamedQuery(QUERY_CHILD_MESSAGES);
0910: Query qOrdered = session
0911: .createQuery(q.getQueryString());
0912:
0913: qOrdered.setParameter("messageId", messageId,
0914: Hibernate.LONG);
0915:
0916: return qOrdered.list();
0917: }
0918: };
0919:
0920: return (List) getHibernateTemplate().executeFind(hcb);
0921: }
0922:
0923: public List sortMessageBySubject(Topic topic, boolean asc) {
0924: List list = topic.getMessages();
0925: if (asc) {
0926: Collections.sort(list, MessageImpl.SUBJECT_COMPARATOR);
0927: } else {
0928: Collections.sort(list, MessageImpl.SUBJECT_COMPARATOR_DESC);
0929: }
0930: topic.setMessages(list);
0931: return list;
0932: }
0933:
0934: public List sortMessageByAuthor(Topic topic, boolean asc) {
0935: List list = topic.getMessages();
0936: if (asc) {
0937: Collections.sort(list, MessageImpl.AUTHORED_BY_COMPARATOR);
0938: } else {
0939: Collections.sort(list,
0940: MessageImpl.AUTHORED_BY_COMPARATOR_DESC);
0941: }
0942: topic.setMessages(list);
0943: return list;
0944: }
0945:
0946: public List sortMessageByDate(Topic topic, boolean asc) {
0947: List list = topic.getMessages();
0948: if (asc) {
0949: Collections.sort(list, MessageImpl.DATE_COMPARATOR);
0950: } else {
0951: Collections.sort(list, MessageImpl.DATE_COMPARATOR_DESC);
0952: }
0953: topic.setMessages(list);
0954: return list;
0955: }
0956:
0957: public List sortMessageByDate(List list, boolean asc) {
0958: if (list == null || list.isEmpty())
0959: return null;
0960:
0961: if (asc) {
0962: Collections.sort(list, MessageImpl.DATE_COMPARATOR);
0963: } else {
0964: Collections.sort(list, MessageImpl.DATE_COMPARATOR_DESC);
0965: }
0966:
0967: return list;
0968: }
0969:
0970: private boolean isForumOrTopicLocked(final Long forumId,
0971: final Long topicId) {
0972: if (forumId == null || topicId == null) {
0973: LOG.error("isForumLocked called with null arguments");
0974: throw new IllegalArgumentException("Null Argument");
0975: }
0976:
0977: LOG.debug("isForumLocked executing with forumId: " + forumId
0978: + ":: topicId: " + topicId);
0979:
0980: HibernateCallback hcb = new HibernateCallback() {
0981: public Object doInHibernate(Session session)
0982: throws HibernateException, SQLException {
0983: Query q = session
0984: .getNamedQuery("findForumLockedAttribute");
0985: q.setParameter("id", forumId, Hibernate.LONG);
0986: return q.uniqueResult();
0987: }
0988: };
0989:
0990: HibernateCallback hcb2 = new HibernateCallback() {
0991: public Object doInHibernate(Session session)
0992: throws HibernateException, SQLException {
0993: Query q = session
0994: .getNamedQuery("findTopicLockedAttribute");
0995: q.setParameter("id", topicId, Hibernate.LONG);
0996: return q.uniqueResult();
0997: }
0998: };
0999:
1000: return ((Boolean) getHibernateTemplate().execute(hcb))
1001: .booleanValue()
1002: || ((Boolean) getHibernateTemplate().execute(hcb2))
1003: .booleanValue();
1004: }
1005:
1006: // helpers
1007:
1008: private String getCurrentUser() {
1009: if (TestUtil.isRunningTests()) {
1010: return "test-user";
1011: }
1012: return sessionManager.getCurrentSessionUserId();
1013: }
1014:
1015: private String getNextUuid() {
1016: return idManager.createUuid();
1017: }
1018:
1019: private String getEventMessage(Object object) {
1020: return "/MessageCenter/site/" + getContextId() + "/"
1021: + object.toString() + "/" + getCurrentUser();
1022: //return "MessageCenter::" + getCurrentUser() + "::" + object.toString();
1023: }
1024:
1025: public List getAllRelatedMsgs(final Long messageId) {
1026: Message rootMsg = getMessageById(messageId);
1027: while (rootMsg.getInReplyTo() != null) {
1028: rootMsg = rootMsg.getInReplyTo();
1029: }
1030: List childList = new ArrayList();
1031: getChildMsgs(rootMsg.getId(), childList);
1032: List returnList = new ArrayList();
1033: returnList.add(rootMsg);
1034: for (int i = 0; i < childList.size(); i++) {
1035: returnList.add((Message) childList.get(i));
1036: }
1037:
1038: return returnList;
1039: }
1040:
1041: /**
1042: *
1043: * @param topicId
1044: * @param searchText
1045: * @return
1046: */
1047:
1048: public List findPvtMsgsBySearchText(final String typeUuid,
1049: final String searchText, final Date searchFromDate,
1050: final Date searchToDate, final boolean searchByText,
1051: final boolean searchByAuthor, final boolean searchByBody,
1052: final boolean searchByLabel, final boolean searchByDate) {
1053:
1054: LOG.debug("findPvtMsgsBySearchText executing with searchText: "
1055: + searchText);
1056:
1057: HibernateCallback hcb = new HibernateCallback() {
1058: public Object doInHibernate(Session session)
1059: throws HibernateException, SQLException {
1060: Query q = session
1061: .getNamedQuery("findPvtMsgsBySearchText");
1062: q.setParameter("searchText", "%" + searchText + "%");
1063: q.setParameter("searchByText",
1064: convertBooleanToInteger(searchByText));
1065: q.setParameter("searchByAuthor",
1066: convertBooleanToInteger(searchByAuthor));
1067: q.setParameter("searchByBody",
1068: convertBooleanToInteger(searchByBody));
1069: q.setParameter("searchByLabel",
1070: convertBooleanToInteger(searchByLabel));
1071: q.setParameter("searchByDate",
1072: convertBooleanToInteger(searchByDate));
1073: q.setParameter("searchFromDate",
1074: (searchFromDate == null) ? new Date(0)
1075: : searchFromDate);
1076: q.setParameter("searchToDate",
1077: (searchToDate == null) ? new Date(System
1078: .currentTimeMillis()) : searchToDate);
1079: q.setParameter("userId", getCurrentUser());
1080: q.setParameter("contextId", ToolManager
1081: .getCurrentPlacement().getContext());
1082: q.setParameter("typeUuid", typeUuid);
1083: return q.list();
1084: }
1085: };
1086:
1087: return (List) getHibernateTemplate().execute(hcb);
1088: }
1089:
1090: private Integer convertBooleanToInteger(boolean value) {
1091: Integer retVal = (Boolean.TRUE.equals(value)) ? 1 : 0;
1092: return new Integer(retVal);
1093: }
1094:
1095: private String getContextId() {
1096: if (TestUtil.isRunningTests()) {
1097: return "test-context";
1098: }
1099: Placement placement = ToolManager.getCurrentPlacement();
1100: String presentSiteId = placement.getContext();
1101: return presentSiteId;
1102: }
1103:
1104: public String getAttachmentUrl(String id) {
1105: try {
1106: String tempString = ContentHostingService.getResource(id)
1107: .getUrl();
1108: String newString = new String();
1109: char[] oneChar = new char[1];
1110: for (int i = 0; i < tempString.length(); i++) {
1111: if (tempString.charAt(i) != ' ') {
1112: oneChar[0] = tempString.charAt(i);
1113: String concatString = new String(oneChar);
1114: newString = newString.concat(concatString);
1115: } else {
1116: newString = newString.concat("%20");
1117: }
1118: }
1119:
1120: return newString;
1121: } catch (Exception e) {
1122: LOG
1123: .error("MessageForumsMessageManagerImpl.getAttachmentUrl"
1124: + e);
1125: }
1126: return null;
1127: }
1128:
1129: /**
1130: * Returns true if the tool with the id passed in exists in the
1131: * current site.
1132: *
1133: * @param toolId
1134: * The tool id to search for.
1135: *
1136: * @return
1137: * TRUE if tool exists, FALSE otherwise.
1138: */
1139: public boolean currentToolMatch(String toolId) {
1140: String curToolId = ToolManager.getCurrentTool().getId();
1141:
1142: if (curToolId.equals(MESSAGECENTER_HELPER_TOOL_ID)) {
1143: curToolId = ToolManager.getCurrentPlacement().getTool()
1144: .getId();
1145: }
1146:
1147: if (toolId.equals(curToolId)) {
1148: return true;
1149: }
1150:
1151: return false;
1152: }
1153:
1154: /**
1155: * Return TRUE if tool with id passed in exists in site passed in
1156: * FALSE otherwise.
1157: *
1158: * @param thisSite
1159: * Site object to check
1160: * @param toolId
1161: * Tool id to be checked
1162: *
1163: * @return
1164: */
1165: public boolean isToolInSite(String siteId, String toolId) {
1166: Site this Site;
1167: try {
1168: this Site = SiteService.getSite(siteId);
1169:
1170: Collection toolsInSite = this Site.getTools(toolId);
1171:
1172: return !toolsInSite.isEmpty();
1173: } catch (IdUnusedException e) {
1174: // Weirdness - should not happen
1175: LOG
1176: .error("IdUnusedException attempting to get site for id "
1177: + siteId
1178: + " to check if tool "
1179: + "with id " + toolId + " is in it.");
1180: }
1181:
1182: return false;
1183: }
1184:
1185: public Map getReadStatusForMessagesWithId(final List msgIds,
1186: final String userId) {
1187: Map statusMap = new HashMap();
1188: if (msgIds != null && msgIds.size() > 0) {
1189: HibernateCallback hcb = new HibernateCallback() {
1190: public Object doInHibernate(Session session)
1191: throws HibernateException, SQLException {
1192: Query q = session
1193: .getNamedQuery(QUERY_READ_STATUS_WITH_MSGS_USER);
1194: q.setParameter("userId", userId, Hibernate.STRING);
1195: q.setParameterList("msgIds", msgIds);
1196: return q.list();
1197: }
1198: };
1199: List statusList = (List) getHibernateTemplate()
1200: .execute(hcb);
1201: if (statusList != null) {
1202: for (int i = 0; i < statusList.size(); i++) {
1203: UnreadStatus status = (UnreadStatus) statusList
1204: .get(i);
1205: if (status != null) {
1206: statusMap.put(status.getMessageId(), status
1207: .getRead());
1208: }
1209: }
1210: }
1211: }
1212: return statusMap;
1213: }
1214:
1215: public List getPendingMsgsInSiteByMembership(
1216: final List membershipList) {
1217: if (membershipList == null) {
1218: LOG
1219: .error("getPendingMsgsInSiteByUser failed with membershipList: "
1220: + membershipList);
1221: throw new IllegalArgumentException("Null Argument");
1222: }
1223:
1224: HibernateCallback hcb = new HibernateCallback() {
1225: public Object doInHibernate(Session session)
1226: throws HibernateException, SQLException {
1227: Query q = session
1228: .getNamedQuery(QUERY_FIND_PENDING_MSGS_BY_CONTEXT_AND_USER);
1229: q.setParameter("contextId", getContextId(),
1230: Hibernate.STRING);
1231: q.setParameterList("membershipList", membershipList);
1232:
1233: return q.list();
1234: }
1235: };
1236:
1237: Message tempMsg = null;
1238: Set resultSet = new HashSet();
1239: List temp = (ArrayList) getHibernateTemplate().execute(hcb);
1240: for (Iterator i = temp.iterator(); i.hasNext();) {
1241: Object[] results = (Object[]) i.next();
1242:
1243: if (results != null) {
1244: if (results[0] instanceof Message) {
1245: tempMsg = (Message) results[0];
1246: tempMsg.setTopic((Topic) results[1]);
1247: tempMsg.getTopic().setBaseForum(
1248: (BaseForum) results[2]);
1249: }
1250: resultSet.add(tempMsg);
1251: }
1252: }
1253: return Util.setToList(resultSet);
1254: }
1255:
1256: public List getPendingMsgsInTopic(final Long topicId) {
1257: if (topicId == null) {
1258: LOG.error("getNumPendingMsgsInTopic failed with topicId: "
1259: + topicId);
1260: throw new IllegalArgumentException("Null Argument");
1261: }
1262:
1263: LOG.debug("getNumPendingMsgsInTopic executing with topicId: "
1264: + topicId);
1265:
1266: HibernateCallback hcb = new HibernateCallback() {
1267: public Object doInHibernate(Session session)
1268: throws HibernateException, SQLException {
1269: Query q = session
1270: .getNamedQuery(QUERY_FIND_PENDING_MSGS_BY_TOPICID);
1271: q.setParameter("topicId", topicId, Hibernate.LONG);
1272: return q.list();
1273: }
1274: };
1275:
1276: Message tempMsg = null;
1277: Set resultSet = new HashSet();
1278: List temp = (ArrayList) getHibernateTemplate().execute(hcb);
1279: for (Iterator i = temp.iterator(); i.hasNext();) {
1280: Object[] results = (Object[]) i.next();
1281:
1282: if (results != null) {
1283: if (results[0] instanceof Message) {
1284: tempMsg = (Message) results[0];
1285: tempMsg.setTopic((Topic) results[1]);
1286: tempMsg.getTopic().setBaseForum(
1287: (BaseForum) results[2]);
1288: }
1289: resultSet.add(tempMsg);
1290: }
1291: }
1292: return Util.setToList(resultSet);
1293: }
1294: }
|