001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/MessageForumsUserManagerImpl.java $
003: * $Id: MessageForumsUserManagerImpl.java 9227 2006-05-15 15:02:42Z cwen@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.app.messageforums;
021:
022: import java.sql.SQLException;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.hibernate.Hibernate;
027: import org.hibernate.HibernateException;
028: import org.hibernate.Query;
029: import org.hibernate.Session;
030: import org.sakaiproject.api.app.messageforums.MessageForumsUser;
031: import org.sakaiproject.api.app.messageforums.DiscussionForumService;
032: import org.sakaiproject.api.app.messageforums.MessageForumsUserManager;
033: import org.sakaiproject.component.app.messageforums.dao.hibernate.MessageForumsUserImpl;
034: import org.sakaiproject.event.api.EventTrackingService;
035: import org.sakaiproject.id.api.IdManager;
036: import org.sakaiproject.tool.api.Placement;
037: import org.sakaiproject.tool.cover.ToolManager;
038: import org.sakaiproject.user.api.UserDirectoryService;
039: import org.sakaiproject.user.api.UserNotDefinedException;
040: import org.springframework.orm.hibernate3.HibernateCallback;
041: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
042:
043: public class MessageForumsUserManagerImpl extends HibernateDaoSupport
044: implements MessageForumsUserManager {
045:
046: private static final Log LOG = LogFactory
047: .getLog(MessageForumsUserManagerImpl.class);
048: private static final String QUERY_BY_USER_ID = "findUserByUserId";
049:
050: /** sakai dependencies */
051: protected IdManager idManager;
052: protected UserDirectoryService userDirectoryService;
053:
054: private EventTrackingService eventTrackingService;
055:
056: public void init() {
057: LOG.info("init()");
058: ;
059: }
060:
061: public EventTrackingService getEventTrackingService() {
062: return eventTrackingService;
063: }
064:
065: public void setEventTrackingService(
066: EventTrackingService eventTrackingService) {
067: this .eventTrackingService = eventTrackingService;
068: }
069:
070: /**
071: * @throws UserNotDefinedException
072: * @see org.sakaiproject.api.app.messageforums.MessageForumsUserManager#getForumUser(java.lang.String)
073: */
074: public MessageForumsUser getForumUser(final String userId) {
075: if (LOG.isDebugEnabled()) {
076: LOG.debug("getForumUser(userId: " + userId + ")");
077: }
078:
079: if (userId == null) {
080: throw new IllegalArgumentException("Null Argument");
081: }
082:
083: if (TestUtil.isRunningTests()) {
084: MessageForumsUser newUser = new MessageForumsUserImpl();
085: newUser.setUuid(getNextUuid());
086: newUser.setUserId(userId);
087: saveForumUser(newUser);
088: return newUser;
089: }
090:
091: HibernateCallback hcb = new HibernateCallback() {
092: public Object doInHibernate(Session session)
093: throws HibernateException, SQLException {
094: Query q = session.getNamedQuery(QUERY_BY_USER_ID);
095: q.setParameter("userId", userId, Hibernate.STRING);
096: return q.uniqueResult();
097: }
098: };
099:
100: MessageForumsUser user = (MessageForumsUser) getHibernateTemplate()
101: .execute(hcb);
102:
103: if (user == null) {
104:
105: /** ensure user exists from user provider */
106: try {
107: userDirectoryService.getUser(userId);
108: } catch (UserNotDefinedException e) {
109: e.printStackTrace();
110: }
111: MessageForumsUser newUser = new MessageForumsUserImpl();
112: newUser.setUuid(getNextUuid());
113: newUser.setUserId(userId);
114: saveForumUser(newUser);
115: eventTrackingService.post(eventTrackingService.newEvent(
116: DiscussionForumService.EVENT_RESOURCE_ADD,
117: getEventMessage(newUser), false));
118: return newUser;
119: } else {
120: return user;
121: }
122: }
123:
124: /**
125: * @see org.sakaiproject.api.app.messageforums.MessageForumsUserManager#saveForumUser(org.sakaiproject.api.app.messageforums.MessageForumsUser)
126: */
127: public void saveForumUser(MessageForumsUser user) {
128: getHibernateTemplate().saveOrUpdate(user);
129: LOG.debug("saveDiscussionForumTopic executed with topicId: "
130: + user.getUuid());
131: }
132:
133: private String getNextUuid() {
134: return idManager.createUuid();
135: }
136:
137: public void setIdManager(IdManager idManager) {
138: this .idManager = idManager;
139: }
140:
141: public void setUserDirectoryService(
142: UserDirectoryService userDirectoryService) {
143: this .userDirectoryService = userDirectoryService;
144: }
145:
146: private String getEventMessage(Object object) {
147: return "/MessageCenter/site/" + getContextId() + "/"
148: + object.toString();
149: //return "MessageCenter::" + object.toString();
150: }
151:
152: private String getContextId() {
153: if (TestUtil.isRunningTests()) {
154: return "test-context";
155: }
156: Placement placement = ToolManager.getCurrentPlacement();
157: String presentSiteId = placement.getContext();
158: return presentSiteId;
159: }
160:
161: }
|