001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/AreaManagerImpl.java $
003: * $Id: AreaManagerImpl.java 9227 2006-05-15 15:02:42Z cwen@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.app.messageforums;
021:
022: import java.sql.SQLException;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.ResourceBundle;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.hibernate.Hibernate;
030: import org.hibernate.HibernateException;
031: import org.hibernate.Query;
032: import org.hibernate.Session;
033: import org.hibernate.collection.PersistentSet;
034: import org.sakaiproject.api.app.messageforums.Area;
035: import org.sakaiproject.api.app.messageforums.AreaManager;
036: import org.sakaiproject.api.app.messageforums.BaseForum;
037: import org.sakaiproject.api.app.messageforums.DiscussionForumService;
038: import org.sakaiproject.api.app.messageforums.MessageForumsForumManager;
039: import org.sakaiproject.api.app.messageforums.MessageForumsTypeManager;
040: import org.sakaiproject.api.app.messageforums.UserPermissionManager;
041: import org.sakaiproject.component.app.messageforums.dao.hibernate.AreaImpl;
042: import org.sakaiproject.event.api.EventTrackingService;
043: import org.sakaiproject.id.api.IdManager;
044: import org.sakaiproject.tool.api.Placement;
045: import org.sakaiproject.tool.api.SessionManager;
046: import org.sakaiproject.tool.api.Tool;
047: import org.sakaiproject.tool.cover.ToolManager;
048: import org.springframework.orm.hibernate3.HibernateCallback;
049: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
050:
051: public class AreaManagerImpl extends HibernateDaoSupport implements
052: AreaManager {
053: private static final Log LOG = LogFactory
054: .getLog(AreaManagerImpl.class);
055:
056: private static final String QUERY_AREA_BY_CONTEXT_AND_TYPE_ID = "findAreaByContextIdAndTypeId";
057: private static final String QUERY_AREA_BY_TYPE = "findAreaByType";
058:
059: // TODO: pull titles from bundle
060: private static final String MESSAGECENTER_BUNDLE = "org.sakaiproject.api.app.messagecenter.bundle.Messages";
061: private static final String MESSAGES_TITLE = "cdfm_message_pvtarea";
062: private static final String FORUMS_TITLE = "cdfm_discussion_forums";
063:
064: private IdManager idManager;
065:
066: private MessageForumsForumManager forumManager;
067:
068: private SessionManager sessionManager;
069:
070: private MessageForumsTypeManager typeManager;
071:
072: private EventTrackingService eventTrackingService;
073:
074: private UserPermissionManager userPermissionManager;
075:
076: public void init() {
077: LOG.info("init()");
078: ;
079: }
080:
081: public UserPermissionManager getUserPermissionManager() {
082: // userPermissionManager can not be injected by spring because of
083: // circluar dependancies so it is loaded by the BeanFactory instead
084: // if (userPermissionManager == null) {
085: // try {
086: // org.springframework.core.io.Resource resource = new InputStreamResource(urlResource.openStream(), classpathUrl);
087: // BeanFactory beanFactory = new XmlBeanFactory(resource);
088: // userPermissionManager = (UserPermissionManagerImpl) beanFactory.getBean(UserPermissionManager.BEAN_NAME);
089: // } catch (Exception e) {
090: // LOG.debug("Unable to load classpath resource: " + classpathUrl);
091: // }
092: // }
093: return userPermissionManager;
094: }
095:
096: public EventTrackingService getEventTrackingService() {
097: return eventTrackingService;
098: }
099:
100: public void setEventTrackingService(
101: EventTrackingService eventTrackingService) {
102: this .eventTrackingService = eventTrackingService;
103: }
104:
105: public MessageForumsTypeManager getTypeManager() {
106: return typeManager;
107: }
108:
109: public void setTypeManager(MessageForumsTypeManager typeManager) {
110: this .typeManager = typeManager;
111: }
112:
113: public void setSessionManager(SessionManager sessionManager) {
114: this .sessionManager = sessionManager;
115: }
116:
117: public IdManager getIdManager() {
118: return idManager;
119: }
120:
121: public SessionManager getSessionManager() {
122: return sessionManager;
123: }
124:
125: public void setIdManager(IdManager idManager) {
126: this .idManager = idManager;
127: }
128:
129: public void setForumManager(MessageForumsForumManager forumManager) {
130: this .forumManager = forumManager;
131: }
132:
133: public Area getPrivateArea() {
134: Area area = getAreaByContextIdAndTypeId(typeManager
135: .getPrivateMessageAreaType());
136: if (area == null) {
137: area = createArea(typeManager.getPrivateMessageAreaType(),
138: null);
139: area.setContextId(getContextId());
140: area.setName(getResourceBundleString(MESSAGES_TITLE));
141: area.setEnabled(Boolean.FALSE);
142: area.setHidden(Boolean.TRUE);
143: area.setLocked(Boolean.FALSE);
144: area.setModerated(Boolean.FALSE);
145: saveArea(area);
146: }
147:
148: return area;
149: }
150:
151: public Area getDiscusionArea() {
152: Area area = getAreaByContextIdAndTypeId(typeManager
153: .getDiscussionForumType());
154: if (area == null) {
155: area = createArea(typeManager.getDiscussionForumType(),
156: null);
157: area.setName(getResourceBundleString(FORUMS_TITLE));
158: area.setEnabled(Boolean.TRUE);
159: area.setHidden(Boolean.TRUE);
160: area.setLocked(Boolean.FALSE);
161: area.setModerated(Boolean.FALSE);
162: saveArea(area);
163: }
164: return area;
165: }
166:
167: public boolean isPrivateAreaEnabled() {
168: return getPrivateArea().getEnabled().booleanValue();
169: }
170:
171: public Area createArea(String typeId, String contextParam) {
172:
173: if (LOG.isDebugEnabled()) {
174: LOG
175: .debug("createArea(" + typeId + "," + contextParam
176: + ")");
177: }
178:
179: Area area = new AreaImpl();
180: area.setUuid(getNextUuid());
181: area.setTypeUuid(typeId);
182: area.setCreated(new Date());
183: area.setCreatedBy(getCurrentUser());
184:
185: /** compatibility with web services*/
186: if (contextParam == null) {
187: String contextId = getContextId();
188: if (contextId == null) {
189: throw new IllegalStateException(
190: "Cannot retrive current context");
191: }
192: area.setContextId(contextId);
193: } else {
194: area.setContextId(contextParam);
195: }
196:
197: LOG.debug("createArea executed with areaId: " + area.getUuid());
198: return area;
199: }
200:
201: /**
202: * This method sets the modified user and date. It then checks all the open forums for a
203: * sort index of 0. (if a sort index on a forum is 0 then it is new). If there is a
204: * zero sort index then it increments all the sort indices by one so the new sort index
205: * becomes the first without having to rely on the creation date for the sorting.
206: *
207: * @param area Area to save
208: */
209: public void saveArea(Area area) {
210: boolean isNew = area.getId() == null;
211:
212: area.setModified(new Date());
213: area.setModifiedBy(getCurrentUser());
214:
215: boolean someForumHasZeroSortIndex = false;
216:
217: // If the open forums were not loaded then there is no need to redo the sort index
218: // thus if it's a hibernate persistentset and initialized
219: if (area.getOpenForumsSet() != null
220: && ((area.getOpenForumsSet() instanceof PersistentSet && ((PersistentSet) area
221: .getOpenForumsSet()).wasInitialized()) || !(area
222: .getOpenForumsSet() instanceof PersistentSet))) {
223: for (Iterator i = area.getOpenForums().iterator(); i
224: .hasNext();) {
225: BaseForum forum = (BaseForum) i.next();
226: if (forum.getSortIndex().intValue() == 0) {
227: someForumHasZeroSortIndex = true;
228: break;
229: }
230: }
231: if (someForumHasZeroSortIndex) {
232: for (Iterator i = area.getOpenForums().iterator(); i
233: .hasNext();) {
234: BaseForum forum = (BaseForum) i.next();
235: forum.setSortIndex(new Integer(forum.getSortIndex()
236: .intValue() + 1));
237: }
238: }
239: }
240:
241: getHibernateTemplate().saveOrUpdate(area);
242:
243: if (isNew) {
244: eventTrackingService.post(eventTrackingService.newEvent(
245: DiscussionForumService.EVENT_RESOURCE_ADD,
246: getEventMessage(area), false));
247: } else {
248: eventTrackingService.post(eventTrackingService.newEvent(
249: DiscussionForumService.EVENT_RESOURCE_WRITE,
250: getEventMessage(area), false));
251: }
252:
253: LOG.debug("saveArea executed with areaId: " + area.getId());
254: }
255:
256: public void deleteArea(Area area) {
257: eventTrackingService.post(eventTrackingService.newEvent(
258: DiscussionForumService.EVENT_RESOURCE_REMOVE,
259: getEventMessage(area), false));
260: getHibernateTemplate().delete(area);
261: LOG.debug("deleteArea executed with areaId: " + area.getId());
262: }
263:
264: /**
265: * ContextId is present site id for now.
266: */
267: private String getContextId() {
268: if (TestUtil.isRunningTests()) {
269: return "test-context";
270: }
271: Placement placement = ToolManager.getCurrentPlacement();
272: String presentSiteId = placement.getContext();
273: return presentSiteId;
274: }
275:
276: public Area getAreaByContextIdAndTypeId(final String typeId) {
277: LOG
278: .debug("getAreaByContextIdAndTypeId executing for current user: "
279: + getCurrentUser());
280: HibernateCallback hcb = new HibernateCallback() {
281: public Object doInHibernate(Session session)
282: throws HibernateException, SQLException {
283: Query q = session
284: .getNamedQuery(QUERY_AREA_BY_CONTEXT_AND_TYPE_ID);
285: q.setParameter("contextId", getContextId(),
286: Hibernate.STRING);
287: q.setParameter("typeId", typeId, Hibernate.STRING);
288: return q.uniqueResult();
289: }
290: };
291:
292: return (Area) getHibernateTemplate().execute(hcb);
293: }
294:
295: public Area getAreaByContextIdAndTypeId(final String contextId,
296: final String typeId) {
297: LOG
298: .debug("getAreaByContextIdAndTypeId executing for current user: "
299: + getCurrentUser());
300: HibernateCallback hcb = new HibernateCallback() {
301: public Object doInHibernate(Session session)
302: throws HibernateException, SQLException {
303: Query q = session
304: .getNamedQuery(QUERY_AREA_BY_CONTEXT_AND_TYPE_ID);
305: q
306: .setParameter("contextId", contextId,
307: Hibernate.STRING);
308: q.setParameter("typeId", typeId, Hibernate.STRING);
309: return q.uniqueResult();
310: }
311: };
312:
313: return (Area) getHibernateTemplate().execute(hcb);
314: }
315:
316: public Area getAreaByType(final String typeId) {
317: final String currentUser = getCurrentUser();
318: LOG
319: .debug("getAreaByContextAndTypeForUser executing for current user: "
320: + currentUser);
321: HibernateCallback hcb = new HibernateCallback() {
322: public Object doInHibernate(Session session)
323: throws HibernateException, SQLException {
324: Query q = session.getNamedQuery(QUERY_AREA_BY_TYPE);
325: q.setParameter("typeId", typeId, Hibernate.STRING);
326: return q.uniqueResult();
327: }
328: };
329: return (Area) getHibernateTemplate().execute(hcb);
330: }
331:
332: // helpers
333:
334: private String getNextUuid() {
335: return idManager.createUuid();
336: }
337:
338: private String getCurrentUser() {
339: String user = sessionManager.getCurrentSessionUserId();
340: return (user == null) ? "test-user" : user;
341: }
342:
343: private String getEventMessage(Object object) {
344: return "/MessageCenter/site/" + getContextId() + "/"
345: + object.toString() + "/" + getCurrentUser();
346: //return "MessageCenter::" + getCurrentUser() + "::" + object.toString();
347: }
348:
349: /**
350: * Gets Strings from Message Bundle (specifically for titles)
351: *
352: * @param key
353: * Message bundle key for String wanted
354: *
355: * @return
356: * String requested or "[missing key: key]" if not found
357: */
358: public String getResourceBundleString(String key) {
359: final ResourceBundle rb = ResourceBundle
360: .getBundle(MESSAGECENTER_BUNDLE);
361: return rb.getString(key);
362: }
363: }
|