Source Code Cross Referenced for MessageForumsForumManagerImpl.java in  » ERP-CRM-Financial » sakai » org » sakaiproject » component » app » messageforums » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » sakai » org.sakaiproject.component.app.messageforums 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**********************************************************************************
0002:         * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/MessageForumsForumManagerImpl.java $
0003:         * $Id: MessageForumsForumManagerImpl.java 9227 2006-05-15 15:02:42Z cwen@iupui.edu $
0004:         ***********************************************************************************
0005:         *
0006:         * Copyright (c) 2003, 2004, 2005, 2006 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.Collections;
0025:        import java.util.Comparator;
0026:        import java.util.Date;
0027:        import java.util.HashSet;
0028:        import java.util.Iterator;
0029:        import java.util.List;
0030:        import java.util.Set;
0031:
0032:        import org.apache.commons.logging.Log;
0033:        import org.apache.commons.logging.LogFactory;
0034:        import org.hibernate.Hibernate;
0035:        import org.hibernate.HibernateException;
0036:        import org.hibernate.Query;
0037:        import org.hibernate.Session;
0038:        import org.hibernate.collection.PersistentSet;
0039:        import org.sakaiproject.api.app.messageforums.ActorPermissions;
0040:        import org.sakaiproject.api.app.messageforums.Area;
0041:        import org.sakaiproject.api.app.messageforums.BaseForum;
0042:        import org.sakaiproject.api.app.messageforums.DiscussionForum;
0043:        import org.sakaiproject.api.app.messageforums.DiscussionTopic;
0044:        import org.sakaiproject.api.app.messageforums.MessageForumsForumManager;
0045:        import org.sakaiproject.api.app.messageforums.MessageForumsTypeManager;
0046:        import org.sakaiproject.api.app.messageforums.DiscussionForumService;
0047:        import org.sakaiproject.api.app.messageforums.MessageForumsUser;
0048:        import org.sakaiproject.api.app.messageforums.OpenForum;
0049:        import org.sakaiproject.api.app.messageforums.OpenTopic;
0050:        import org.sakaiproject.api.app.messageforums.PrivateForum;
0051:        import org.sakaiproject.api.app.messageforums.PrivateTopic;
0052:        import org.sakaiproject.api.app.messageforums.Topic;
0053:        import org.sakaiproject.component.app.messageforums.dao.hibernate.ActorPermissionsImpl;
0054:        import org.sakaiproject.component.app.messageforums.dao.hibernate.DiscussionForumImpl;
0055:        import org.sakaiproject.component.app.messageforums.dao.hibernate.DiscussionTopicImpl;
0056:        import org.sakaiproject.component.app.messageforums.dao.hibernate.MessageForumsUserImpl;
0057:        import org.sakaiproject.component.app.messageforums.dao.hibernate.OpenTopicImpl;
0058:        import org.sakaiproject.component.app.messageforums.dao.hibernate.PrivateForumImpl;
0059:        import org.sakaiproject.component.app.messageforums.dao.hibernate.PrivateTopicImpl;
0060:        import org.sakaiproject.component.app.messageforums.dao.hibernate.Util;
0061:        import org.sakaiproject.event.api.EventTrackingService;
0062:        import org.sakaiproject.id.api.IdManager;
0063:        import org.sakaiproject.tool.api.Placement;
0064:        import org.sakaiproject.tool.api.SessionManager;
0065:        import org.sakaiproject.tool.cover.ToolManager;
0066:        import org.springframework.orm.hibernate3.HibernateCallback;
0067:        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0068:
0069:        /**
0070:         * The forums are sorted by this java class.  The topics are sorted by the order-by in the hbm file.
0071:         *
0072:         */
0073:        public class MessageForumsForumManagerImpl extends HibernateDaoSupport
0074:                implements  MessageForumsForumManager {
0075:
0076:            private static final Log LOG = LogFactory
0077:                    .getLog(MessageForumsForumManagerImpl.class);
0078:
0079:            private static final String QUERY_FOR_PRIVATE_TOPICS = "findPrivateTopicsByForumId";
0080:
0081:            private static final String QUERY_BY_FORUM_OWNER = "findPrivateForumByOwner";
0082:
0083:            private static final String QUERY_BY_FORUM_OWNER_AREA = "findPrivateForumByOwnerArea";
0084:
0085:            private static final String QUERY_BY_FORUM_OWNER_AREA_WITH_TOPICS = "findPrivateForumByOwnerAreaWithTopics";
0086:
0087:            private static final String QUERY_BY_FORUM_OWNER_AREA_NULL = "findPrivateForumByOwnerAreaNull";
0088:
0089:            private static final String QUERY_BY_FORUM_OWNER_AREA_NULL_WITH_ALL_TOPICS = "findPrivateForumByOwnerAreaNullWithAllTopics";
0090:
0091:            private static final String QUERY_BY_FORUM_ID = "findForumById";
0092:
0093:            private static final String QUERY_BY_FORUM_ID_WITH_ATTACHMENTS = "findForumByIdWithAttachments";
0094:
0095:            private static final String QUERY_BY_FORUM_UUID = "findForumByUuid";
0096:
0097:            private static final String QUERY_BY_TYPE_AND_CONTEXT = "findForumByTypeAndContext";
0098:            private static final String QUERY_BY_FORUM_ID_AND_TOPICS = "findForumByIdWithTopics";
0099:            private static final String QUERY_BY_TYPE_AND_CONTEXT_WITH_ALL_INFO = "findForumByTypeAndContextWithAllInfo";
0100:            private static final String QUERY_BY_TYPE_AND_CONTEXT_WITH_ALL_TOPICS_MEMBERSHIP = "findForumByTypeAndContextWithTopicsMemberhips";
0101:
0102:            private static final String QUERY_TOPIC_WITH_MESSAGES_AND_ATTACHMENTS = "findTopicByIdWithMessagesAndAttachments";
0103:            private static final String QUERY_TOPIC_WITH_MESSAGES = "findTopicByIdWithMessages";
0104:
0105:            private static final String QUERY_TOPICS_WITH_MESSAGES_FOR_FORUM = "findTopicsWithMessagesForForum";
0106:            private static final String QUERY_TOPICS_WITH_MESSAGES_AND_ATTACHMENTS_FOR_FORUM = "findTopicsWithMessagesAndAttachmentsForForum";
0107:            private static final String QUERY_TOPICS_WITH_MSGS_AND_ATTACHMENTS_AND_MEMBERSHIPS_FOR_FORUM = "findTopicsWithMessagesMembershipAndAttachmentsForForum";
0108:
0109:            private static final String QUERY_BY_TOPIC_ID = "findTopicById";
0110:            private static final String QUERY_OPEN_BY_TOPIC_AND_PARENT = "findOpenTopicAndParentById";
0111:            private static final String QUERY_PRIVATE_BY_TOPIC_AND_PARENT = "findPrivateTopicAndParentById";
0112:
0113:            private static final String QUERY_BY_TOPIC_UUID = "findTopicByUuid";
0114:
0115:            private static final String QUERY_OF_SUR_KEY_BY_TOPIC = "findOFTopicSurKeyByTopicId";
0116:            private static final String QUERY_PF_SUR_KEY_BY_TOPIC = "findPFTopicSurKeyByTopicId";
0117:
0118:            private static final String QUERY_BY_TOPIC_ID_MESSAGES_ATTACHMENTS = "findTopicByIdWithAttachments";
0119:
0120:            private static final String QUERY_GET_ALL_MOD_TOPICS_IN_SITE = "findAllModeratedTopicsForSite";
0121:            private static final String QUERY_GET_NUM_MOD_TOPICS_WITH_MOD_PERM = "findNumModeratedTopicsForSiteByUserByMembership";
0122:
0123:            //public static Comparator FORUM_CREATED_DATE_COMPARATOR;
0124:
0125:            /** Sorts the forums by the sort index and if the same index then order by the creation date */
0126:            public static Comparator FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC;
0127:
0128:            private IdManager idManager;
0129:
0130:            private SessionManager sessionManager;
0131:
0132:            private MessageForumsTypeManager typeManager;
0133:
0134:            public MessageForumsForumManagerImpl() {
0135:            }
0136:
0137:            private EventTrackingService eventTrackingService;
0138:
0139:            static {
0140:                FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC = new Comparator() {
0141:                    public int compare(Object forum, Object otherForum) {
0142:                        if (forum != null && otherForum != null
0143:                                && forum instanceof  OpenForum
0144:                                && otherForum instanceof  OpenForum) {
0145:                            Integer index1 = ((OpenForum) forum).getSortIndex();
0146:                            Integer index2 = ((OpenForum) otherForum)
0147:                                    .getSortIndex();
0148:                            if (index1.intValue() != index2.intValue())
0149:                                return index1.intValue() - index2.intValue();
0150:                            Date date1 = ((OpenForum) forum).getCreated();
0151:                            Date date2 = ((OpenForum) otherForum).getCreated();
0152:                            return date2.compareTo(date1);
0153:                        }
0154:                        return -1;
0155:                    }
0156:                };
0157:
0158:                // remove 5.0 specific code
0159:                //FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC = Collections.reverseOrder(FORUM_CREATED_DATE_COMPARATOR);
0160:            }
0161:
0162:            public void init() {
0163:                LOG.info("init()");
0164:                ;
0165:            }
0166:
0167:            public EventTrackingService getEventTrackingService() {
0168:                return eventTrackingService;
0169:            }
0170:
0171:            public void setEventTrackingService(
0172:                    EventTrackingService eventTrackingService) {
0173:                this .eventTrackingService = eventTrackingService;
0174:            }
0175:
0176:            public MessageForumsTypeManager getTypeManager() {
0177:                return typeManager;
0178:            }
0179:
0180:            public void setTypeManager(MessageForumsTypeManager typeManager) {
0181:                this .typeManager = typeManager;
0182:            }
0183:
0184:            public void setSessionManager(SessionManager sessionManager) {
0185:                this .sessionManager = sessionManager;
0186:            }
0187:
0188:            public void setIdManager(IdManager idManager) {
0189:                this .idManager = idManager;
0190:            }
0191:
0192:            public IdManager getIdManager() {
0193:                return idManager;
0194:            }
0195:
0196:            public SessionManager getSessionManager() {
0197:                return sessionManager;
0198:            }
0199:
0200:            public void initializeTopicsForForum(BaseForum forum) {
0201:
0202:                getHibernateTemplate().initialize(forum);
0203:                getHibernateTemplate().initialize(forum.getTopicsSet());
0204:            }
0205:
0206:            public List getTopicsByIdWithMessages(final Long forumId) {
0207:                if (forumId == null) {
0208:                    throw new IllegalArgumentException("Null Argument");
0209:                }
0210:
0211:                HibernateCallback hcb = new HibernateCallback() {
0212:                    public Object doInHibernate(Session session)
0213:                            throws HibernateException, SQLException {
0214:                        Query q = session
0215:                                .getNamedQuery(QUERY_TOPICS_WITH_MESSAGES_FOR_FORUM);
0216:                        q.setParameter("id", forumId, Hibernate.LONG);
0217:                        return q.list();
0218:                    }
0219:                };
0220:
0221:                Topic tempTopic = null;
0222:                Set resultSet = new HashSet();
0223:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0224:                for (Iterator i = temp.iterator(); i.hasNext();) {
0225:                    Object[] results = (Object[]) i.next();
0226:
0227:                    if (results != null) {
0228:                        if (results[0] instanceof  Topic) {
0229:                            tempTopic = (Topic) results[0];
0230:                            tempTopic.setBaseForum((BaseForum) results[1]);
0231:                        } else {
0232:                            tempTopic = (Topic) results[1];
0233:                            tempTopic.setBaseForum((BaseForum) results[0]);
0234:                        }
0235:                        resultSet.add(tempTopic);
0236:                    }
0237:                }
0238:                return Util.setToList(resultSet);
0239:            }
0240:
0241:            public List getTopicsByIdWithMessagesAndAttachments(
0242:                    final Long forumId) {
0243:                if (forumId == null) {
0244:                    throw new IllegalArgumentException("Null Argument");
0245:                }
0246:
0247:                HibernateCallback hcb = new HibernateCallback() {
0248:                    public Object doInHibernate(Session session)
0249:                            throws HibernateException, SQLException {
0250:                        Query q = session
0251:                                .getNamedQuery(QUERY_TOPICS_WITH_MESSAGES_AND_ATTACHMENTS_FOR_FORUM);
0252:                        q.setParameter("id", forumId, Hibernate.LONG);
0253:                        return q.list();
0254:                    }
0255:                };
0256:
0257:                Topic tempTopic = null;
0258:                Set resultSet = new HashSet();
0259:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0260:                for (Iterator i = temp.iterator(); i.hasNext();) {
0261:                    Object[] results = (Object[]) i.next();
0262:
0263:                    if (results != null) {
0264:                        if (results[0] instanceof  Topic) {
0265:                            tempTopic = (Topic) results[0];
0266:                            tempTopic.setBaseForum((BaseForum) results[1]);
0267:                        } else {
0268:                            tempTopic = (Topic) results[1];
0269:                            tempTopic.setBaseForum((BaseForum) results[0]);
0270:                        }
0271:                        resultSet.add(tempTopic);
0272:                    }
0273:                }
0274:                return Util.setToList(resultSet);
0275:            }
0276:
0277:            public List getTopicsByIdWithMessagesMembershipAndAttachments(
0278:                    final Long forumId) {
0279:                if (forumId == null) {
0280:                    throw new IllegalArgumentException("Null Argument");
0281:                }
0282:
0283:                HibernateCallback hcb = new HibernateCallback() {
0284:                    public Object doInHibernate(Session session)
0285:                            throws HibernateException, SQLException {
0286:                        Query q = session
0287:                                .getNamedQuery(QUERY_TOPICS_WITH_MSGS_AND_ATTACHMENTS_AND_MEMBERSHIPS_FOR_FORUM);
0288:                        q.setParameter("id", forumId, Hibernate.LONG);
0289:                        return q.list();
0290:                    }
0291:                };
0292:
0293:                Topic tempTopic = null;
0294:                Set resultSet = new HashSet();
0295:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0296:                for (Iterator i = temp.iterator(); i.hasNext();) {
0297:                    Object[] results = (Object[]) i.next();
0298:
0299:                    if (results != null) {
0300:                        if (results[0] instanceof  Topic) {
0301:                            tempTopic = (Topic) results[0];
0302:                            tempTopic.setBaseForum((BaseForum) results[1]);
0303:                        } else {
0304:                            tempTopic = (Topic) results[1];
0305:                            tempTopic.setBaseForum((BaseForum) results[0]);
0306:                        }
0307:                        resultSet.add(tempTopic);
0308:                    }
0309:                }
0310:                return Util.setToList(resultSet);
0311:            }
0312:
0313:            public Topic getTopicByIdWithMessagesAndAttachments(
0314:                    final Long topicId) {
0315:
0316:                if (topicId == null) {
0317:                    throw new IllegalArgumentException("Null Argument");
0318:                }
0319:
0320:                HibernateCallback hcb = new HibernateCallback() {
0321:                    public Object doInHibernate(Session session)
0322:                            throws HibernateException, SQLException {
0323:                        Query q = session
0324:                                .getNamedQuery(QUERY_TOPIC_WITH_MESSAGES_AND_ATTACHMENTS);
0325:                        q.setParameter("id", topicId, Hibernate.LONG);
0326:                        return q.uniqueResult();
0327:                    }
0328:                };
0329:
0330:                return (Topic) getHibernateTemplate().execute(hcb);
0331:            }
0332:
0333:            public Topic getTopicByIdWithMessages(final Long topicId) {
0334:
0335:                if (topicId == null) {
0336:                    throw new IllegalArgumentException("Null Argument");
0337:                }
0338:
0339:                HibernateCallback hcb = new HibernateCallback() {
0340:                    public Object doInHibernate(Session session)
0341:                            throws HibernateException, SQLException {
0342:                        Query q = session
0343:                                .getNamedQuery(QUERY_TOPIC_WITH_MESSAGES);
0344:                        q.setParameter("id", topicId, Hibernate.LONG);
0345:                        return q.uniqueResult();
0346:                    }
0347:                };
0348:
0349:                return (Topic) getHibernateTemplate().execute(hcb);
0350:            }
0351:
0352:            public BaseForum getForumByIdWithTopics(final Long forumId) {
0353:
0354:                if (forumId == null) {
0355:                    throw new IllegalArgumentException("Null Argument");
0356:                }
0357:
0358:                HibernateCallback hcb = new HibernateCallback() {
0359:                    public Object doInHibernate(Session session)
0360:                            throws HibernateException, SQLException {
0361:                        Query q = session
0362:                                .getNamedQuery(QUERY_BY_FORUM_ID_AND_TOPICS);
0363:                        q.setParameter("id", forumId, Hibernate.LONG);
0364:                        return q.uniqueResult();
0365:                    }
0366:                };
0367:
0368:                BaseForum bForum = (BaseForum) getHibernateTemplate().execute(
0369:                        hcb);
0370:
0371:                if (bForum != null) {
0372:                    getHibernateTemplate().initialize(
0373:                            bForum.getAttachmentsSet());
0374:                }
0375:
0376:                return bForum;
0377:            }
0378:
0379:            public List getForumByTypeAndContext(final String typeUuid) {
0380:
0381:                if (typeUuid == null) {
0382:                    throw new IllegalArgumentException("Null Argument");
0383:                }
0384:
0385:                HibernateCallback hcb = new HibernateCallback() {
0386:                    public Object doInHibernate(Session session)
0387:                            throws HibernateException, SQLException {
0388:                        Query q = session
0389:                                .getNamedQuery(QUERY_BY_TYPE_AND_CONTEXT);
0390:                        q.setParameter("typeUuid", typeUuid, Hibernate.STRING);
0391:                        q.setParameter("contextId", getContextId(),
0392:                                Hibernate.STRING);
0393:                        return q.list();
0394:                    }
0395:                };
0396:
0397:                BaseForum tempForum = null;
0398:                Set resultSet = new HashSet();
0399:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0400:
0401:                for (Iterator i = temp.iterator(); i.hasNext();) {
0402:                    Object[] results = (Object[]) i.next();
0403:
0404:                    if (results != null) {
0405:                        if (results[0] instanceof  BaseForum) {
0406:                            tempForum = (BaseForum) results[0];
0407:                            tempForum.setArea((Area) results[1]);
0408:                        } else {
0409:                            tempForum = (BaseForum) results[1];
0410:                            tempForum.setArea((Area) results[0]);
0411:                        }
0412:                        resultSet.add(tempForum);
0413:                    }
0414:                }
0415:
0416:                List resultList = Util.setToList(resultSet);
0417:                Collections.sort(resultList,
0418:                        FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC);
0419:
0420:                // Now that the list is sorted, lets index the forums
0421:                int sort_index = 1;
0422:                for (Iterator i = resultList.iterator(); i.hasNext();) {
0423:                    tempForum = (BaseForum) i.next();
0424:
0425:                    tempForum.setSortIndex(new Integer(sort_index++));
0426:                }
0427:
0428:                return resultList;
0429:            }
0430:
0431:            public List getForumByTypeAndContext(final String typeUuid,
0432:                    final String contextId) {
0433:
0434:                if (typeUuid == null || contextId == null) {
0435:                    throw new IllegalArgumentException("Null Argument");
0436:                }
0437:
0438:                HibernateCallback hcb = new HibernateCallback() {
0439:                    public Object doInHibernate(Session session)
0440:                            throws HibernateException, SQLException {
0441:                        Query q = session
0442:                                .getNamedQuery(QUERY_BY_TYPE_AND_CONTEXT);
0443:                        q.setParameter("typeUuid", typeUuid, Hibernate.STRING);
0444:                        q
0445:                                .setParameter("contextId", contextId,
0446:                                        Hibernate.STRING);
0447:                        return q.list();
0448:                    }
0449:                };
0450:
0451:                BaseForum tempForum = null;
0452:                Set resultSet = new HashSet();
0453:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0454:
0455:                for (Iterator i = temp.iterator(); i.hasNext();) {
0456:                    Object[] results = (Object[]) i.next();
0457:
0458:                    if (results != null) {
0459:                        if (results[0] instanceof  BaseForum) {
0460:                            tempForum = (BaseForum) results[0];
0461:                            tempForum.setArea((Area) results[1]);
0462:                        } else {
0463:                            tempForum = (BaseForum) results[1];
0464:                            tempForum.setArea((Area) results[0]);
0465:                        }
0466:                        resultSet.add(tempForum);
0467:                    }
0468:                }
0469:
0470:                List resultList = Util.setToList(resultSet);
0471:                Collections.sort(resultList,
0472:                        FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC);
0473:
0474:                // Now that the list is sorted, lets index the forums
0475:                int sort_index = 1;
0476:                for (Iterator i = resultList.iterator(); i.hasNext();) {
0477:                    tempForum = (BaseForum) i.next();
0478:
0479:                    tempForum.setSortIndex(new Integer(sort_index++));
0480:                }
0481:
0482:                return resultList;
0483:            }
0484:
0485:            public Topic getTopicByIdWithAttachments(final Long topicId) {
0486:
0487:                if (topicId == null) {
0488:                    throw new IllegalArgumentException("Null Argument");
0489:                }
0490:
0491:                LOG
0492:                        .debug("getTopicByIdWithMessagesAndAttachments executing with topicId: "
0493:                                + topicId);
0494:
0495:                HibernateCallback hcb = new HibernateCallback() {
0496:                    public Object doInHibernate(Session session)
0497:                            throws HibernateException, SQLException {
0498:                        Query q = session
0499:                                .getNamedQuery(QUERY_BY_TOPIC_ID_MESSAGES_ATTACHMENTS);
0500:                        q.setParameter("id", topicId, Hibernate.LONG);
0501:                        return q.uniqueResult();
0502:                    }
0503:                };
0504:
0505:                return (Topic) getHibernateTemplate().execute(hcb);
0506:
0507:            }
0508:
0509:            public PrivateForum getPrivateForumByOwner(final String owner) {
0510:
0511:                if (owner == null) {
0512:                    throw new IllegalArgumentException("Null Argument");
0513:                }
0514:
0515:                LOG.debug("getForumByOwner executing with owner: " + owner);
0516:
0517:                HibernateCallback hcb = new HibernateCallback() {
0518:                    public Object doInHibernate(Session session)
0519:                            throws HibernateException, SQLException {
0520:                        Query q = session.getNamedQuery(QUERY_BY_FORUM_OWNER);
0521:                        q.setParameter("owner", owner, Hibernate.STRING);
0522:                        return q.uniqueResult();
0523:                    }
0524:                };
0525:
0526:                return (PrivateForum) getHibernateTemplate().execute(hcb);
0527:            }
0528:
0529:            public PrivateForum getPrivateForumByOwnerArea(final String owner,
0530:                    final Area area) {
0531:
0532:                if (owner == null || area == null) {
0533:                    throw new IllegalArgumentException("Null Argument");
0534:                }
0535:
0536:                LOG.debug("getForumByOwnerArea executing with owner: " + owner
0537:                        + " and area:" + area);
0538:
0539:                HibernateCallback hcb = new HibernateCallback() {
0540:                    public Object doInHibernate(Session session)
0541:                            throws HibernateException, SQLException {
0542:                        Query q = session
0543:                                .getNamedQuery(QUERY_BY_FORUM_OWNER_AREA);
0544:                        q.setParameter("owner", owner, Hibernate.STRING);
0545:                        q.setParameter("area", area);
0546:                        return q.uniqueResult();
0547:                    }
0548:                };
0549:
0550:                return (PrivateForum) getHibernateTemplate().execute(hcb);
0551:            }
0552:
0553:            public PrivateForum getPrivateForumByOwnerAreaNull(
0554:                    final String owner) {
0555:
0556:                if (owner == null) {
0557:                    throw new IllegalArgumentException("Null Argument");
0558:                }
0559:
0560:                LOG.debug("getForumByOwnerAreaNull executing with owner: "
0561:                        + owner);
0562:
0563:                HibernateCallback hcb = new HibernateCallback() {
0564:                    public Object doInHibernate(Session session)
0565:                            throws HibernateException, SQLException {
0566:                        Query q = session
0567:                                .getNamedQuery(QUERY_BY_FORUM_OWNER_AREA_NULL);
0568:                        q.setParameter("owner", owner, Hibernate.STRING);
0569:                        return q.uniqueResult();
0570:                    }
0571:                };
0572:
0573:                return (PrivateForum) getHibernateTemplate().execute(hcb);
0574:            }
0575:
0576:            public BaseForum getForumByIdWithAttachments(final Long forumId) {
0577:
0578:                if (forumId == null) {
0579:                    throw new IllegalArgumentException("Null Argument");
0580:                }
0581:
0582:                LOG
0583:                        .debug("getForumByIdWithAttachments executing with forumId: "
0584:                                + forumId);
0585:
0586:                HibernateCallback hcb = new HibernateCallback() {
0587:                    public Object doInHibernate(Session session)
0588:                            throws HibernateException, SQLException {
0589:                        Query q = session
0590:                                .getNamedQuery(QUERY_BY_FORUM_ID_WITH_ATTACHMENTS);
0591:                        q.setParameter("id", forumId, Hibernate.LONG);
0592:                        return q.uniqueResult();
0593:                    }
0594:                };
0595:
0596:                return (BaseForum) getHibernateTemplate().execute(hcb);
0597:
0598:            }
0599:
0600:            public BaseForum getForumById(boolean open, final Long forumId) {
0601:                if (forumId == null) {
0602:                    throw new IllegalArgumentException("Null Argument");
0603:                }
0604:
0605:                LOG.debug("getDiscussionForumById executing with forumId: "
0606:                        + forumId);
0607:
0608:                if (open) {
0609:                    // open works for both open and discussion forums
0610:                    return getForumByIdWithAttachments(forumId);
0611:                } else {
0612:                    return (BaseForum) getHibernateTemplate().get(
0613:                            PrivateForumImpl.class, forumId);
0614:                }
0615:            }
0616:
0617:            public BaseForum getForumByUuid(final String forumId) {
0618:                if (forumId == null) {
0619:                    throw new IllegalArgumentException("Null Argument");
0620:                }
0621:
0622:                LOG.debug("getForumByUuid executing with forumId: " + forumId);
0623:
0624:                HibernateCallback hcb = new HibernateCallback() {
0625:                    public Object doInHibernate(Session session)
0626:                            throws HibernateException, SQLException {
0627:                        Query q = session.getNamedQuery(QUERY_BY_FORUM_UUID);
0628:                        q.setParameter("uuid", forumId, Hibernate.STRING);
0629:                        return q.uniqueResult();
0630:                    }
0631:                };
0632:
0633:                return (BaseForum) getHibernateTemplate().execute(hcb);
0634:            }
0635:
0636:            public Topic getTopicById(final boolean open, final Long topicId) {
0637:                if (topicId == null) {
0638:                    throw new IllegalArgumentException("Null Argument");
0639:                }
0640:
0641:                LOG.debug("getDiscussionForumById executing with topicId: "
0642:                        + topicId);
0643:
0644:                HibernateCallback hcb = new HibernateCallback() {
0645:                    public Object doInHibernate(Session session)
0646:                            throws HibernateException, SQLException {
0647:                        String query;
0648:                        if (open) {
0649:                            query = QUERY_OPEN_BY_TOPIC_AND_PARENT;
0650:                        } else {
0651:                            query = QUERY_PRIVATE_BY_TOPIC_AND_PARENT;
0652:                        }
0653:                        Query q = session
0654:                                .getNamedQuery(QUERY_OPEN_BY_TOPIC_AND_PARENT);
0655:                        q.setParameter("id", topicId, Hibernate.LONG);
0656:                        return q.list();
0657:                    }
0658:                };
0659:
0660:                Topic res = null;
0661:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0662:                Object[] results = (Object[]) temp.get(0);
0663:                if (results != null) {
0664:                    if (results[0] instanceof  Topic) {
0665:                        res = (Topic) results[0];
0666:                        res.setBaseForum((BaseForum) results[1]);
0667:                    } else {
0668:                        res = (Topic) results[1];
0669:                        res.setBaseForum((BaseForum) results[0]);
0670:                    }
0671:                }
0672:                return res;
0673:            }
0674:
0675:            public Topic getTopicByUuid(final String uuid) {
0676:                if (uuid == null) {
0677:                    throw new IllegalArgumentException("Null Argument");
0678:                }
0679:
0680:                LOG.debug("getDiscussionForumById executing with topicId: "
0681:                        + uuid);
0682:                HibernateCallback hcb = new HibernateCallback() {
0683:                    public Object doInHibernate(Session session)
0684:                            throws HibernateException, SQLException {
0685:                        Query q = session.getNamedQuery(QUERY_BY_TOPIC_UUID);
0686:                        q.setParameter("uuid", uuid, Hibernate.STRING);
0687:                        return q.uniqueResult();
0688:                    }
0689:                };
0690:
0691:                return (Topic) getHibernateTemplate().execute(hcb);
0692:            }
0693:
0694:            public List getModeratedTopicsInSite(final String contextId) {
0695:
0696:                if (contextId == null) {
0697:                    throw new IllegalArgumentException("Null Argument");
0698:                }
0699:
0700:                LOG.debug("getModeratedTopicsInSite executing with contextId: "
0701:                        + contextId);
0702:
0703:                HibernateCallback hcb = new HibernateCallback() {
0704:                    public Object doInHibernate(Session session)
0705:                            throws HibernateException, SQLException {
0706:                        Query q = session
0707:                                .getNamedQuery(QUERY_GET_ALL_MOD_TOPICS_IN_SITE);
0708:                        q
0709:                                .setParameter("contextId", contextId,
0710:                                        Hibernate.STRING);
0711:                        return q.list();
0712:                    }
0713:                };
0714:
0715:                Topic tempTopic = null;
0716:                Set resultSet = new HashSet();
0717:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
0718:                for (Iterator i = temp.iterator(); i.hasNext();) {
0719:                    Object[] results = (Object[]) i.next();
0720:
0721:                    if (results != null) {
0722:                        if (results[0] instanceof  Topic) {
0723:                            tempTopic = (Topic) results[0];
0724:                            tempTopic.setBaseForum((BaseForum) results[1]);
0725:                        } else {
0726:                            tempTopic = (Topic) results[1];
0727:                            tempTopic.setBaseForum((BaseForum) results[0]);
0728:                        }
0729:                        resultSet.add(tempTopic);
0730:                    }
0731:                }
0732:                return Util.setToList(resultSet);
0733:            }
0734:
0735:            public DiscussionForum createDiscussionForum() {
0736:                DiscussionForum forum = new DiscussionForumImpl();
0737:                forum.setUuid(getNextUuid());
0738:                forum.setCreated(new Date());
0739:                forum.setCreatedBy(getCurrentUser());
0740:                forum.setLocked(Boolean.FALSE);
0741:                forum.setDraft(Boolean.FALSE);
0742:                forum.setTypeUuid(typeManager.getDiscussionForumType());
0743:                forum.setActorPermissions(createDefaultActorPermissions());
0744:                forum.setModerated(Boolean.FALSE);
0745:                LOG.debug("createDiscussionForum executed");
0746:                return forum;
0747:            }
0748:
0749:            public ActorPermissions createDefaultActorPermissions() {
0750:                ActorPermissions actorPermissions = new ActorPermissionsImpl();
0751:                MessageForumsUser nonSpecifiedUser = new MessageForumsUserImpl();
0752:                nonSpecifiedUser.setUserId(typeManager.getNotSpecifiedType());
0753:                nonSpecifiedUser.setUuid(typeManager.getNotSpecifiedType());
0754:                nonSpecifiedUser.setTypeUuid(typeManager.getNotSpecifiedType());
0755:
0756:                actorPermissions.addAccesssor(nonSpecifiedUser);
0757:                actorPermissions.addContributor(nonSpecifiedUser);
0758:                actorPermissions.addModerator(nonSpecifiedUser);
0759:                return actorPermissions;
0760:            }
0761:
0762:            //    public void saveActorPermissions(ActorPermissions actorPermissions){
0763:            //      boolean isNew = actorPermissions.getId() == null;
0764:            //      
0765:            //      getHibernateTemplate().saveOrUpdate(actorPermissions);
0766:            //
0767:            //      if (isNew) {
0768:            //          eventTrackingService.post(eventTrackingService.newEvent(ContentHostingService.EVENT_RESOURCE_ADD, getEventMessage(actorPermissions), false));
0769:            //      } else {
0770:            //          eventTrackingService.post(eventTrackingService.newEvent(ContentHostingService.EVENT_RESOURCE_WRITE, getEventMessage(actorPermissions), false));
0771:            //      }      
0772:            //    }
0773:
0774:            /**
0775:             * @see org.sakaiproject.api.app.messageforums.MessageForumsForumManager#createPrivateForum(java.lang.String)
0776:             */
0777:            public PrivateForum createPrivateForum(String title) {
0778:                /** set all non-null properties in case hibernate flushes session before explicit save */
0779:                PrivateForum forum = new PrivateForumImpl();
0780:                forum.setTitle(title);
0781:                forum.setUuid(idManager.createUuid());
0782:                forum.setAutoForwardEmail("");
0783:                forum.setOwner(getCurrentUser());
0784:                forum.setUuid(getNextUuid());
0785:                forum.setCreated(new Date());
0786:                forum.setCreatedBy(getCurrentUser());
0787:                forum.setSortIndex(new Integer(0));
0788:                forum.setShortDescription("short-desc");
0789:                forum.setExtendedDescription("ext desc");
0790:                forum.setAutoForward(Boolean.FALSE);
0791:                forum.setAutoForwardEmail("");
0792:                forum.setPreviewPaneEnabled(Boolean.FALSE);
0793:                forum.setModified(new Date());
0794:                forum.setModifiedBy(getCurrentUser());
0795:                forum.setTypeUuid(typeManager.getPrivateMessageAreaType());
0796:                forum.setModerated(Boolean.FALSE);
0797:                LOG.debug("createPrivateForum executed");
0798:                return forum;
0799:            }
0800:
0801:            /**
0802:             * @see org.sakaiproject.api.app.messageforums.MessageForumsForumManager#savePrivateForum(org.sakaiproject.api.app.messageforums.PrivateForum)
0803:             */
0804:            public void savePrivateForum(PrivateForum forum) {
0805:                boolean isNew = forum.getId() == null;
0806:
0807:                if (forum.getSortIndex() == null) {
0808:                    forum.setSortIndex(new Integer(0));
0809:                }
0810:
0811:                forum.setModified(new Date());
0812:                forum.setModifiedBy(getCurrentUser());
0813:                forum.setOwner(getCurrentUser());
0814:                getHibernateTemplate().saveOrUpdate(forum);
0815:
0816:                if (isNew) {
0817:                    eventTrackingService.post(eventTrackingService.newEvent(
0818:                            DiscussionForumService.EVENT_RESOURCE_ADD,
0819:                            getEventMessage(forum), false));
0820:                } else {
0821:                    eventTrackingService.post(eventTrackingService.newEvent(
0822:                            DiscussionForumService.EVENT_RESOURCE_WRITE,
0823:                            getEventMessage(forum), false));
0824:                }
0825:
0826:                LOG.debug("savePrivateForum executed with forumId: "
0827:                        + forum.getId());
0828:            }
0829:
0830:            /**
0831:             * Save a discussion forum
0832:             */
0833:            public void saveDiscussionForum(DiscussionForum forum) {
0834:                saveDiscussionForum(forum, false);
0835:            }
0836:
0837:            public void saveDiscussionForum(DiscussionForum forum, boolean draft) {
0838:                boolean isNew = forum.getId() == null;
0839:
0840:                if (forum.getSortIndex() == null) {
0841:                    forum.setSortIndex(new Integer(0));
0842:                }
0843:                if (forum.getLocked() == null) {
0844:                    forum.setLocked(Boolean.FALSE);
0845:                }
0846:                if (forum.getModerated() == null) {
0847:                    forum.setModerated(Boolean.FALSE);
0848:                }
0849:                forum.setDraft(new Boolean(draft));
0850:                forum.setModified(new Date());
0851:                forum.setModifiedBy(getCurrentUser());
0852:
0853:                // If the topics were not loaded then there is no need to redo the sort index
0854:                //     thus if it's a hibernate persistentset and initialized
0855:                if (forum.getTopicsSet() != null
0856:                        && ((forum.getTopicsSet() instanceof  PersistentSet && ((PersistentSet) forum
0857:                                .getTopicsSet()).wasInitialized()) || !(forum
0858:                                .getTopicsSet() instanceof  PersistentSet))) {
0859:                    List topics = forum.getTopics();
0860:                    boolean someTopicHasZeroSortIndex = false;
0861:
0862:                    for (Iterator i = topics.iterator(); i.hasNext();) {
0863:                        DiscussionTopic topic = (DiscussionTopic) i.next();
0864:                        if (topic.getSortIndex().intValue() == 0) {
0865:                            someTopicHasZeroSortIndex = true;
0866:                            break;
0867:                        }
0868:                    }
0869:                    if (someTopicHasZeroSortIndex) {
0870:                        for (Iterator i = topics.iterator(); i.hasNext();) {
0871:                            DiscussionTopic topic = (DiscussionTopic) i.next();
0872:                            topic.setSortIndex(new Integer(topic.getSortIndex()
0873:                                    .intValue() + 1));
0874:                        }
0875:                    }
0876:                }
0877:
0878:                getHibernateTemplate().saveOrUpdate(forum);
0879:
0880:                if (isNew) {
0881:                    eventTrackingService.post(eventTrackingService.newEvent(
0882:                            DiscussionForumService.EVENT_RESOURCE_ADD,
0883:                            getEventMessage(forum), false));
0884:                } else {
0885:                    eventTrackingService.post(eventTrackingService.newEvent(
0886:                            DiscussionForumService.EVENT_RESOURCE_WRITE,
0887:                            getEventMessage(forum), false));
0888:                }
0889:
0890:                LOG.debug("saveDiscussionForum executed with forumId: "
0891:                        + forum.getId() + ":: draft: " + draft);
0892:            }
0893:
0894:            public DiscussionTopic createDiscussionForumTopic(
0895:                    DiscussionForum forum) {
0896:                DiscussionTopic topic = new DiscussionTopicImpl();
0897:                topic.setUuid(getNextUuid());
0898:                topic.setTypeUuid(typeManager.getDiscussionForumType());
0899:                topic.setCreated(new Date());
0900:                topic.setCreatedBy(getCurrentUser());
0901:                topic.setBaseForum(forum);
0902:                topic.setLocked(Boolean.FALSE);
0903:                topic.setDraft(forum.getDraft());
0904:                topic.setModerated(Boolean.FALSE);
0905:                LOG.debug("createDiscussionForumTopic executed");
0906:                return topic;
0907:            }
0908:
0909:            public void saveDiscussionForumTopic(DiscussionTopic topic) {
0910:                saveDiscussionForumTopic(topic, false);
0911:            }
0912:
0913:            /**
0914:             * Save a discussion forum topic
0915:             */
0916:            public void saveDiscussionForumTopic(DiscussionTopic topic,
0917:                    boolean parentForumDraftStatus) {
0918:                boolean isNew = topic.getId() == null;
0919:
0920:                //        if (isForumLocked(topic.getBaseForum().getId())) {
0921:                //            LOG.info("saveDiscussionForumTopic executed [topicId: " + (isNew ? "new" : topic.getId().toString()) + "] but forum is locked -- save aborted");
0922:                //            throw new LockedException("Topic could not be saved [topicId: " + (isNew ? "new" : topic.getId().toString()) + "]");
0923:                //        }
0924:
0925:                if (topic.getMutable() == null) {
0926:                    topic.setMutable(Boolean.FALSE);
0927:                }
0928:                if (topic.getSortIndex() == null) {
0929:                    topic.setSortIndex(new Integer(0));
0930:                }
0931:                topic.setModified(new Date());
0932:                topic.setModifiedBy(getCurrentUser());
0933:
0934:                if (topic.getModerated() == null) {
0935:                    topic.setModerated(Boolean.FALSE);
0936:                }
0937:
0938:                if (topic.getId() == null) {
0939:
0940:                    DiscussionForum discussionForum = (DiscussionForum) getForumByIdWithTopics(topic
0941:                            .getBaseForum().getId());
0942:                    discussionForum.addTopic(topic);
0943:
0944:                    //          DiscussionForum forum = (DiscussionForum) topic.getBaseForum();
0945:                    //          getHibernateTemplate().lock(forum, LockMode.NONE);
0946:                    //          getHibernateTemplate().initialize(forum.getTopicsSet());          
0947:                    //          forum.addTopic(topic);                       
0948:                    //          getHibernateTemplate().saveOrUpdate(topic);
0949:                    if (topic.getDraft().equals(Boolean.TRUE)) {
0950:                        saveDiscussionForum(discussionForum, discussionForum
0951:                                .getDraft().booleanValue());
0952:                    } else
0953:                        saveDiscussionForum(discussionForum,
0954:                                parentForumDraftStatus);
0955:                    //sak-5146 saveDiscussionForum(discussionForum, parentForumDraftStatus);
0956:
0957:                } else {
0958:                    getHibernateTemplate().saveOrUpdate(topic);
0959:                }
0960:
0961:                if (isNew) {
0962:                    eventTrackingService.post(eventTrackingService.newEvent(
0963:                            DiscussionForumService.EVENT_RESOURCE_ADD,
0964:                            getEventMessage(topic), false));
0965:                } else {
0966:                    eventTrackingService.post(eventTrackingService.newEvent(
0967:                            DiscussionForumService.EVENT_RESOURCE_WRITE,
0968:                            getEventMessage(topic), false));
0969:                }
0970:
0971:                LOG.debug("saveDiscussionForumTopic executed with topicId: "
0972:                        + topic.getId());
0973:            }
0974:
0975:            public OpenTopic createOpenForumTopic(OpenForum forum) {
0976:                OpenTopic topic = new OpenTopicImpl();
0977:                topic.setUuid(getNextUuid());
0978:                topic.setTypeUuid(typeManager.getOpenDiscussionForumType());
0979:                topic.setCreated(new Date());
0980:                topic.setCreatedBy(getCurrentUser());
0981:                topic.setLocked(Boolean.FALSE);
0982:                topic.setModerated(Boolean.FALSE);
0983:                topic.setDraft(forum.getDraft());
0984:                LOG.debug("createOpenForumTopic executed");
0985:                return topic;
0986:            }
0987:
0988:            public PrivateTopic createPrivateForumTopic(String title,
0989:                    boolean forumIsParent, boolean topicIsMutable,
0990:                    String userId, Long parentId) {
0991:                /** set all non-null properties in case hibernate flushes session before explicit save */
0992:                PrivateTopic topic = new PrivateTopicImpl();
0993:                topic.setTitle(title);
0994:                topic.setUuid(getNextUuid());
0995:                topic.setCreated(new Date());
0996:                topic.setCreatedBy(getCurrentUser());
0997:                topic.setUserId(userId);
0998:                topic.setShortDescription("short-desc");
0999:                topic.setExtendedDescription("ext-desc");
1000:                topic.setMutable(new Boolean(topicIsMutable));
1001:                topic.setSortIndex(new Integer(0));
1002:                topic.setModified(new Date());
1003:                topic.setModifiedBy(getCurrentUser());
1004:                topic.setTypeUuid(typeManager.getPrivateMessageAreaType());
1005:                topic.setModerated(Boolean.FALSE);
1006:                LOG.debug("createPrivateForumTopic executed");
1007:                return topic;
1008:            }
1009:
1010:            /**
1011:             * Save a private forum topic
1012:             */
1013:            public void savePrivateForumTopic(PrivateTopic topic) {
1014:                boolean isNew = topic.getId() == null;
1015:
1016:                topic.setModified(new Date());
1017:                topic.setModifiedBy(getCurrentUser());
1018:                getHibernateTemplate().saveOrUpdate(topic);
1019:
1020:                if (isNew) {
1021:                    eventTrackingService.post(eventTrackingService.newEvent(
1022:                            DiscussionForumService.EVENT_RESOURCE_ADD,
1023:                            getEventMessage(topic), false));
1024:                } else {
1025:                    eventTrackingService.post(eventTrackingService.newEvent(
1026:                            DiscussionForumService.EVENT_RESOURCE_WRITE,
1027:                            getEventMessage(topic), false));
1028:                }
1029:
1030:                LOG.debug("savePrivateForumTopic executed with forumId: "
1031:                        + topic.getId());
1032:            }
1033:
1034:            /**
1035:             * Save an open forum topic
1036:             */
1037:            public void saveOpenForumTopic(OpenTopic topic) {
1038:                boolean isNew = topic.getId() == null;
1039:
1040:                topic.setModified(new Date());
1041:                topic.setModifiedBy(getCurrentUser());
1042:                getHibernateTemplate().saveOrUpdate(topic);
1043:
1044:                if (isNew) {
1045:                    eventTrackingService.post(eventTrackingService.newEvent(
1046:                            DiscussionForumService.EVENT_RESOURCE_ADD,
1047:                            getEventMessage(topic), false));
1048:                } else {
1049:                    eventTrackingService.post(eventTrackingService.newEvent(
1050:                            DiscussionForumService.EVENT_RESOURCE_WRITE,
1051:                            getEventMessage(topic), false));
1052:                }
1053:
1054:                LOG.debug("saveOpenForumTopic executed with forumId: "
1055:                        + topic.getId());
1056:            }
1057:
1058:            /**
1059:             * Delete a discussion forum and all topics/messages
1060:             */
1061:            public void deleteDiscussionForum(DiscussionForum forum) {
1062:                long id = forum.getId().longValue();
1063:                eventTrackingService.post(eventTrackingService.newEvent(
1064:                        DiscussionForumService.EVENT_RESOURCE_REMOVE,
1065:                        getEventMessage(forum), false));
1066:                try {
1067:                    getSession().evict(forum);
1068:                } catch (Exception e) {
1069:                    e.printStackTrace();
1070:                    LOG.error("could not evict forum: " + forum.getId(), e);
1071:                }
1072:                Area area = getAreaByContextIdAndTypeId(typeManager
1073:                        .getDiscussionForumType());
1074:                area.removeDiscussionForum(forum);
1075:                getHibernateTemplate().saveOrUpdate(area);
1076:                //getHibernateTemplate().delete(forum);
1077:                LOG.debug("deleteDiscussionForum executed with forumId: " + id);
1078:            }
1079:
1080:            public Area getAreaByContextIdAndTypeId(final String typeId) {
1081:                LOG
1082:                        .debug("getAreaByContextIdAndTypeId executing for current user: "
1083:                                + getCurrentUser());
1084:                HibernateCallback hcb = new HibernateCallback() {
1085:                    public Object doInHibernate(Session session)
1086:                            throws HibernateException, SQLException {
1087:                        Query q = session
1088:                                .getNamedQuery("findAreaByContextIdAndTypeId");
1089:                        q.setParameter("contextId", getContextId(),
1090:                                Hibernate.STRING);
1091:                        q.setParameter("typeId", typeId, Hibernate.STRING);
1092:                        return q.uniqueResult();
1093:                    }
1094:                };
1095:
1096:                return (Area) getHibernateTemplate().execute(hcb);
1097:            }
1098:
1099:            /**
1100:             * Delete a discussion forum topic
1101:             */
1102:            public void deleteDiscussionForumTopic(DiscussionTopic topic) {
1103:                long id = topic.getId().longValue();
1104:                eventTrackingService.post(eventTrackingService.newEvent(
1105:                        DiscussionForumService.EVENT_RESOURCE_REMOVE,
1106:                        getEventMessage(topic), false));
1107:                try {
1108:                    getSession().evict(topic);
1109:                } catch (Exception e) {
1110:                    e.printStackTrace();
1111:                    LOG.error("could not evict topic: " + topic.getId(), e);
1112:                }
1113:
1114:                Topic finder = getTopicById(true, topic.getId());
1115:                BaseForum forum = finder.getBaseForum();
1116:                forum.removeTopic(topic);
1117:                getHibernateTemplate().saveOrUpdate(forum);
1118:                //getHibernateTemplate().delete(topic);
1119:                LOG.debug("deleteOpenForumTopic executed with topicId: " + id);
1120:            }
1121:
1122:            /**
1123:             * Delete an open forum topic
1124:             */
1125:            public void deleteOpenForumTopic(OpenTopic topic) {
1126:                eventTrackingService.post(eventTrackingService.newEvent(
1127:                        DiscussionForumService.EVENT_RESOURCE_REMOVE,
1128:                        getEventMessage(topic), false));
1129:                getHibernateTemplate().delete(topic);
1130:                LOG.debug("deleteOpenForumTopic executed with forumId: "
1131:                        + topic.getId());
1132:            }
1133:
1134:            /**
1135:             * Delete a private forum topic
1136:             */
1137:            public void deletePrivateForumTopic(PrivateTopic topic) {
1138:                eventTrackingService.post(eventTrackingService.newEvent(
1139:                        DiscussionForumService.EVENT_RESOURCE_REMOVE,
1140:                        getEventMessage(topic), false));
1141:                getHibernateTemplate().delete(topic);
1142:                LOG.debug("deletePrivateForumTopic executed with forumId: "
1143:                        + topic.getId());
1144:            }
1145:
1146:            /**
1147:             * Returns a given number of messages if available in the time provided
1148:             * 
1149:             * @param numberMessages
1150:             *            the number of messages to retrieve
1151:             * @param numberDaysInPast
1152:             *            the number days to look back
1153:             */
1154:            public List getRecentPrivateMessages(int numberMessages,
1155:                    int numberDaysInPast) {
1156:                // TODO: Implement Me!
1157:                throw new UnsupportedOperationException();
1158:            }
1159:
1160:            /**
1161:             * Returns a given number of discussion forum messages if available in the
1162:             * time provided
1163:             * 
1164:             * @param numberMessages
1165:             *            the number of forum messages to retrieve
1166:             * @param numberDaysInPast
1167:             *            the number days to look back
1168:             */
1169:            public List getRecentDiscussionForumMessages(int numberMessages,
1170:                    int numberDaysInPast) {
1171:                // TODO: Implement Me!
1172:                throw new UnsupportedOperationException();
1173:            }
1174:
1175:            /**
1176:             * Returns a given number of open forum messages if available in the time
1177:             * provided
1178:             * 
1179:             * @param numberMessages
1180:             *            the number of forum messages to retrieve
1181:             * @param numberDaysInPast
1182:             *            the number days to look back
1183:             */
1184:            public List getRecentOpenForumMessages(int numberMessages,
1185:                    int numberDaysInPast) {
1186:                // TODO: Implement Me!
1187:                throw new UnsupportedOperationException();
1188:            }
1189:
1190:            private boolean isForumLocked(final Long id) {
1191:                if (id == null) {
1192:                    LOG.error("isForumLocked failed with id: " + id);
1193:                    throw new IllegalArgumentException("Null Argument");
1194:                }
1195:
1196:                LOG.debug("isForumLocked executing with id: " + id);
1197:
1198:                HibernateCallback hcb = new HibernateCallback() {
1199:                    public Object doInHibernate(Session session)
1200:                            throws HibernateException, SQLException {
1201:                        Query q = session
1202:                                .getNamedQuery("findForumLockedAttribute");
1203:                        q.setParameter("id", id, Hibernate.LONG);
1204:                        return q.uniqueResult();
1205:                    }
1206:                };
1207:
1208:                return ((Boolean) getHibernateTemplate().execute(hcb))
1209:                        .booleanValue();
1210:            }
1211:
1212:            public List searchTopicMessages(final Long topicId,
1213:                    final String searchText) {
1214:                if (topicId == null) {
1215:                    throw new IllegalArgumentException("Null Argument");
1216:                }
1217:
1218:                LOG.debug("getDiscussionForumById executing with topicId: "
1219:                        + topicId);
1220:
1221:                HibernateCallback hcb = new HibernateCallback() {
1222:                    public Object doInHibernate(Session session)
1223:                            throws HibernateException, SQLException {
1224:                        Query q = session
1225:                                .getNamedQuery("findMessagesBySearchText");
1226:                        q.setParameter("id", topicId, Hibernate.LONG);
1227:                        q.setParameter("searchByText", "%" + searchText + "%",
1228:                                Hibernate.STRING);
1229:                        return q.list();
1230:                    }
1231:                };
1232:
1233:                return (List) getHibernateTemplate().execute(hcb);
1234:            }
1235:
1236:            // helpers
1237:
1238:            /**
1239:             * ContextId is present site id for now.
1240:             */
1241:            private String getContextId() {
1242:                if (TestUtil.isRunningTests()) {
1243:                    return "test-context";
1244:                }
1245:                Placement placement = ToolManager.getCurrentPlacement();
1246:                String presentSiteId = placement.getContext();
1247:                return presentSiteId;
1248:            }
1249:
1250:            private String getCurrentUser() {
1251:                if (TestUtil.isRunningTests()) {
1252:                    return "test-user";
1253:                }
1254:                return sessionManager.getCurrentSessionUserId();
1255:            }
1256:
1257:            private String getNextUuid() {
1258:                return idManager.createUuid();
1259:            }
1260:
1261:            private String getEventMessage(Object object) {
1262:                return "/MessageCenter/site/" + getContextId() + "/"
1263:                        + object.toString() + "/" + getCurrentUser();
1264:                //return "MessageCenter::" + getCurrentUser() + "::" + object.toString();
1265:            }
1266:
1267:            private String getEventMessage(Object object, String context) {
1268:                return "/MessageCenter/site/" + context + "/"
1269:                        + object.toString() + "/" + getCurrentUser();
1270:            }
1271:
1272:            public List getForumByTypeAndContextWithTopicsAllAttachments(
1273:                    final String typeUuid) {
1274:                if (typeUuid == null) {
1275:                    throw new IllegalArgumentException("Null Argument");
1276:                }
1277:
1278:                HibernateCallback hcb = new HibernateCallback() {
1279:                    public Object doInHibernate(Session session)
1280:                            throws HibernateException, SQLException {
1281:                        Query q = session
1282:                                .getNamedQuery(QUERY_BY_TYPE_AND_CONTEXT_WITH_ALL_INFO);
1283:                        q.setParameter("typeUuid", typeUuid, Hibernate.STRING);
1284:                        q.setParameter("contextId", getContextId(),
1285:                                Hibernate.STRING);
1286:                        return q.list();
1287:                    }
1288:                };
1289:
1290:                BaseForum tempForum = null;
1291:                Set resultSet = new HashSet();
1292:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
1293:
1294:                for (Iterator i = temp.iterator(); i.hasNext();) {
1295:                    Object[] results = (Object[]) i.next();
1296:
1297:                    if (results != null) {
1298:                        if (results[0] instanceof  BaseForum) {
1299:                            tempForum = (BaseForum) results[0];
1300:                            tempForum.setArea((Area) results[1]);
1301:                        } else {
1302:                            tempForum = (BaseForum) results[1];
1303:                            tempForum.setArea((Area) results[0]);
1304:                        }
1305:                        resultSet.add(tempForum);
1306:                    }
1307:                }
1308:
1309:                List resultList = Util.setToList(resultSet);
1310:                Collections.sort(resultList,
1311:                        FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC);
1312:
1313:                // Now that the list is sorted, lets index the forums
1314:                int sort_index = 1;
1315:                for (Iterator i = resultList.iterator(); i.hasNext();) {
1316:                    tempForum = (BaseForum) i.next();
1317:
1318:                    tempForum.setSortIndex(new Integer(sort_index++));
1319:                }
1320:
1321:                return resultList;
1322:            }
1323:
1324:            public List getForumByTypeAndContextWithTopicsMembership(
1325:                    final String typeUuid, final String contextId) {
1326:                if (typeUuid == null || contextId == null) {
1327:                    throw new IllegalArgumentException("Null Argument");
1328:                }
1329:
1330:                HibernateCallback hcb = new HibernateCallback() {
1331:                    public Object doInHibernate(Session session)
1332:                            throws HibernateException, SQLException {
1333:                        Query q = session
1334:                                .getNamedQuery(QUERY_BY_TYPE_AND_CONTEXT_WITH_ALL_TOPICS_MEMBERSHIP);
1335:                        q.setParameter("typeUuid", typeUuid, Hibernate.STRING);
1336:                        q
1337:                                .setParameter("contextId", contextId,
1338:                                        Hibernate.STRING);
1339:                        return q.list();
1340:                    }
1341:                };
1342:
1343:                BaseForum tempForum = null;
1344:                Set resultSet = new HashSet();
1345:                List temp = (ArrayList) getHibernateTemplate().execute(hcb);
1346:
1347:                for (Iterator i = temp.iterator(); i.hasNext();) {
1348:                    Object[] results = (Object[]) i.next();
1349:
1350:                    if (results != null) {
1351:                        if (results[0] instanceof  BaseForum) {
1352:                            tempForum = (BaseForum) results[0];
1353:                            tempForum.setArea((Area) results[1]);
1354:                        } else {
1355:                            tempForum = (BaseForum) results[1];
1356:                            tempForum.setArea((Area) results[0]);
1357:                        }
1358:                        resultSet.add(tempForum);
1359:                    }
1360:                }
1361:
1362:                List resultList = Util.setToList(resultSet);
1363:                Collections.sort(resultList,
1364:                        FORUM_SORT_INDEX_CREATED_DATE_COMPARATOR_DESC);
1365:
1366:                // Now that the list is sorted, lets index the forums
1367:                int sort_index = 1;
1368:                for (Iterator i = resultList.iterator(); i.hasNext();) {
1369:                    tempForum = (BaseForum) i.next();
1370:
1371:                    tempForum.setSortIndex(new Integer(sort_index++));
1372:                }
1373:
1374:                return resultList;
1375:            }
1376:
1377:            public PrivateForum getPrivateForumByOwnerAreaWithAllTopics(
1378:                    final String owner, final Area area) {
1379:
1380:                if (owner == null || area == null) {
1381:                    throw new IllegalArgumentException("Null Argument");
1382:                }
1383:
1384:                LOG.debug("getForumByOwnerArea executing with owner: " + owner
1385:                        + " and area:" + area);
1386:
1387:                HibernateCallback hcb = new HibernateCallback() {
1388:                    public Object doInHibernate(Session session)
1389:                            throws HibernateException, SQLException {
1390:                        Query q = session
1391:                                .getNamedQuery(QUERY_BY_FORUM_OWNER_AREA_WITH_TOPICS);
1392:                        q.setParameter("owner", owner, Hibernate.STRING);
1393:                        q.setParameter("area", area);
1394:                        return q.uniqueResult();
1395:                    }
1396:                };
1397:
1398:                return (PrivateForum) getHibernateTemplate().execute(hcb);
1399:            }
1400:
1401:            public PrivateForum getPrivateForumByOwnerAreaNullWithAllTopics(
1402:                    final String owner) {
1403:                if (owner == null) {
1404:                    throw new IllegalArgumentException("Null Argument");
1405:                }
1406:
1407:                LOG.debug("getForumByOwnerAreaNull executing with owner: "
1408:                        + owner);
1409:
1410:                HibernateCallback hcb = new HibernateCallback() {
1411:                    public Object doInHibernate(Session session)
1412:                            throws HibernateException, SQLException {
1413:                        Query q = session
1414:                                .getNamedQuery(QUERY_BY_FORUM_OWNER_AREA_NULL_WITH_ALL_TOPICS);
1415:                        q.setParameter("owner", owner, Hibernate.STRING);
1416:                        return q.uniqueResult();
1417:                    }
1418:                };
1419:
1420:                return (PrivateForum) getHibernateTemplate().execute(hcb);
1421:            }
1422:
1423:            public int getNumModTopicCurrentUserHasModPermFor(
1424:                    final List membershipList) {
1425:                if (membershipList == null) {
1426:                    LOG
1427:                            .error("getNumModTopicCurrentUserHasModPermFor failed with membershipList: "
1428:                                    + membershipList);
1429:                    throw new IllegalArgumentException("Null Argument");
1430:                }
1431:
1432:                LOG
1433:                        .debug("getNumModTopicCurrentUserHasModPermFor executing with membershipItems: "
1434:                                + membershipList);
1435:
1436:                HibernateCallback hcb = new HibernateCallback() {
1437:                    public Object doInHibernate(Session session)
1438:                            throws HibernateException, SQLException {
1439:                        Query q = session
1440:                                .getNamedQuery(QUERY_GET_NUM_MOD_TOPICS_WITH_MOD_PERM);
1441:                        q.setParameterList("membershipList", membershipList);
1442:                        q.setParameter("contextId", getContextId(),
1443:                                Hibernate.STRING);
1444:                        return q.uniqueResult();
1445:                    }
1446:                };
1447:
1448:                return ((Integer) getHibernateTemplate().execute(hcb))
1449:                        .intValue();
1450:            }
1451:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.