001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-impl/standalone/src/java/org/sakaiproject/component/section/SectionAwarenessHibernateImpl.java $
003: * $Id: SectionAwarenessHibernateImpl.java 19678 2006-12-18 20:30:01Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
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.section;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Locale;
028: import java.util.MissingResourceException;
029: import java.util.ResourceBundle;
030:
031: import org.hibernate.HibernateException;
032: import org.hibernate.Query;
033: import org.hibernate.Session;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.sakaiproject.section.api.SectionAwareness;
038: import org.sakaiproject.section.api.coursemanagement.Course;
039: import org.sakaiproject.section.api.coursemanagement.CourseSection;
040: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
041: import org.sakaiproject.section.api.coursemanagement.User;
042: import org.sakaiproject.section.api.facade.Role;
043: import org.springframework.orm.hibernate3.HibernateCallback;
044: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
045:
046: /**
047: * Hibernate based implementation of SectionAwareness.
048: *
049: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
050: *
051: */
052: public class SectionAwarenessHibernateImpl extends HibernateDaoSupport
053: implements SectionAwareness {
054:
055: private static final Log log = LogFactory
056: .getLog(SectionAwarenessHibernateImpl.class);
057:
058: /**
059: * @inheritDoc
060: */
061: public List getSections(final String siteContext) {
062: if (log.isDebugEnabled())
063: log.debug("Getting sections for context " + siteContext);
064: HibernateCallback hc = new HibernateCallback() {
065: public Object doInHibernate(Session session)
066: throws HibernateException {
067: // Get the sections
068: Query secQuery = session
069: .getNamedQuery("findSectionsBySiteContext");
070: secQuery.setParameter("context", siteContext);
071: List list = secQuery.list();
072:
073: // Get the teams
074: Query teamQuery = session
075: .getNamedQuery("findTeamsBySiteContext");
076: teamQuery.setParameter("context", siteContext);
077:
078: // Add the teams after the sections
079: list.addAll(teamQuery.list());
080: return list;
081: }
082: };
083: return getHibernateTemplate().executeFind(hc);
084: }
085:
086: /**
087: * @inheritDoc
088: */
089: public List getSectionCategories(String siteContext) {
090: ResourceBundle bundle = ResourceBundle.getBundle(
091: SectionManagerHibernateImpl.CATEGORY_BUNDLE, Locale.US);
092:
093: Enumeration keys = bundle.getKeys();
094: List categoryIds = new ArrayList();
095: while (keys.hasMoreElements()) {
096: categoryIds.add(keys.nextElement());
097: }
098: Collections.sort(categoryIds);
099: return categoryIds;
100: }
101:
102: /**
103: * @inheritDoc
104: */
105: public CourseSection getSection(final String sectionUuid) {
106: if (log.isDebugEnabled())
107: log.debug("Getting section with uuid=" + sectionUuid);
108: HibernateCallback hc = new HibernateCallback() {
109: public Object doInHibernate(Session session)
110: throws HibernateException {
111: return getSection(sectionUuid, session);
112: }
113: };
114: return (CourseSection) getHibernateTemplate().execute(hc);
115: }
116:
117: private CourseSection getSection(final String sectionUuid,
118: Session session) throws HibernateException {
119: Query q = session.getNamedQuery("loadSectionByUuid");
120: q.setParameter("uuid", sectionUuid);
121: Object section = q.uniqueResult();
122: if (section == null) {
123: throw new IllegalArgumentException(
124: "No section exists with uuid=" + sectionUuid);
125: } else {
126: return (CourseSection) section;
127: }
128: }
129:
130: /**
131: * @inheritDoc
132: */
133: public List getSiteMembersInRole(final String siteContext,
134: final Role role) {
135: HibernateCallback hc = new HibernateCallback() {
136: public Object doInHibernate(Session session)
137: throws HibernateException {
138: Course course = getCourse(siteContext, session);
139: if (course == null) {
140: if (log.isInfoEnabled())
141: log.info("No course founf for siteContext "
142: + siteContext);
143: return new ArrayList();
144: }
145: Query q;
146: if (role.isInstructor()) {
147: q = session.getNamedQuery("findSiteInstructors");
148: } else if (role.isStudent()) {
149: q = session.getNamedQuery("findSiteEnrollments");
150: } else if (role.isTeachingAssistant()) {
151: q = session.getNamedQuery("findSiteTAs");
152: } else {
153: throw new IllegalArgumentException(
154: "There are no users without a role in a site.");
155: }
156: q.setParameter("course", course);
157: return q.list();
158: }
159: };
160: return getHibernateTemplate().executeFind(hc);
161: }
162:
163: private Course getCourse(String siteContext, Session session)
164: throws HibernateException {
165: Query q = session.getNamedQuery("loadCourseBySiteContext");
166: q.setParameter("siteContext", siteContext);
167: Object course = q.uniqueResult();
168: return (Course) course;
169: }
170:
171: /**
172: * The sakai implementation will not use the database to do this kind of searching,
173: * so I'll skip doing optimizations here.
174: *
175: * @inheritDoc
176: */
177: public List findSiteMembersInRole(final String siteContext,
178: final Role role, final String pattern) {
179: List fullList = getSiteMembersInRole(siteContext, role);
180: List filteredList = new ArrayList();
181: for (Iterator iter = fullList.iterator(); iter.hasNext();) {
182: ParticipationRecord record = (ParticipationRecord) iter
183: .next();
184: User user = record.getUser();
185: if (user.getDisplayName().toLowerCase().startsWith(
186: pattern.toLowerCase())
187: || user.getSortName().toLowerCase().startsWith(
188: pattern.toLowerCase())
189: || user.getDisplayId().toLowerCase().startsWith(
190: pattern.toLowerCase())) {
191: filteredList.add(record);
192: }
193: }
194: return filteredList;
195: }
196:
197: /**
198: * @inheritDoc
199: */
200: public boolean isSiteMemberInRole(final String siteContext,
201: final String userUid, final Role role) {
202: HibernateCallback hc = new HibernateCallback() {
203: public Object doInHibernate(Session session)
204: throws HibernateException {
205: Course course = getCourse(siteContext, session);
206: if (course == null) {
207: if (log.isInfoEnabled())
208: log.info("No course founf for siteContext "
209: + siteContext);
210: return new Boolean(false);
211: }
212: Query q = session
213: .getNamedQuery("checkForSiteMembershipInRole");
214: q.setParameter("course", course);
215: q.setParameter("userUid", userUid);
216: List list = q.list();
217: return checkRole(role, list);
218: }
219: };
220: return ((Boolean) getHibernateTemplate().execute(hc))
221: .booleanValue();
222: }
223:
224: /**
225: * This code pops up a few places...
226: *
227: * @param role The role to check
228: * @param list A list of participant records returned by hibernate
229: * @return Whether the list of participation record includes one record of
230: * the specified role.
231: */
232: private Boolean checkRole(final Role role, List list) {
233: if (list.size() == 1) {
234: ParticipationRecord record = (ParticipationRecord) list
235: .get(0);
236: if (record.getRole().equals(role)) {
237: if (log.isDebugEnabled())
238: log.debug("This user is in role "
239: + role.getDescription());
240: return new Boolean(true);
241: } else {
242: if (log.isDebugEnabled())
243: log.debug("This user is not in role "
244: + role.getDescription());
245: return new Boolean(false);
246: }
247: } else if (list.size() == 0) {
248: if (log.isDebugEnabled())
249: log
250: .debug("This user has no role in this learning context.");
251: return new Boolean(false);
252: } else {
253: throw new RuntimeException(
254: "There are multiple participation records for this user in this learning context.");
255: }
256: }
257:
258: /**
259: * @inheritDoc
260: */
261: public List getSectionMembers(final String sectionUuid) {
262: HibernateCallback hc = new HibernateCallback() {
263: public Object doInHibernate(Session session)
264: throws HibernateException {
265: Query q = session.getNamedQuery("findSectionMembers");
266: q.setParameter("sectionUuid", sectionUuid);
267: return q.list();
268: }
269: };
270: return getHibernateTemplate().executeFind(hc);
271: }
272:
273: /**
274: * @inheritDoc
275: */
276: public List getSectionMembersInRole(final String sectionUuid,
277: final Role role) {
278: HibernateCallback hc = new HibernateCallback() {
279: public Object doInHibernate(Session session)
280: throws HibernateException {
281: CourseSection section = getSection(sectionUuid, session);
282: Query q;
283: if (role.isInstructor()) {
284: q = session.getNamedQuery("findSectionInstructors");
285: } else if (role.isStudent()) {
286: q = session.getNamedQuery("findSectionStudents");
287: } else if (role.isTeachingAssistant()) {
288: q = session.getNamedQuery("findSectionTAs");
289: } else {
290: throw new IllegalArgumentException(
291: "There are no users without a role in a section.");
292: }
293: q.setParameter("section", section);
294: return q.list();
295: }
296: };
297: return getHibernateTemplate().executeFind(hc);
298: }
299:
300: /**
301: * @inheritDoc
302: */
303: public boolean isSectionMemberInRole(final String sectionUuid,
304: final String userUid, final Role role) {
305: HibernateCallback hc = new HibernateCallback() {
306: public Object doInHibernate(Session session)
307: throws HibernateException {
308: CourseSection section = getSection(sectionUuid, session);
309: Query q = session
310: .getNamedQuery("checkForSectionMembershipInRole");
311: q.setParameter("section", section);
312: q.setParameter("userUid", userUid);
313: List list = q.list();
314: return checkRole(role, list);
315: }
316: };
317: return ((Boolean) getHibernateTemplate().execute(hc))
318: .booleanValue();
319: }
320:
321: /**
322: * @inheritDoc
323: */
324: public String getSectionName(final String sectionUuid) {
325: HibernateCallback hc = new HibernateCallback() {
326: public Object doInHibernate(Session session)
327: throws HibernateException {
328: Query q = session.getNamedQuery("loadSectionName");
329: q.setParameter("sectionUuid", sectionUuid);
330: Object name = q.uniqueResult();
331: if (name != null) {
332: if (log.isDebugEnabled())
333: log.debug("Section " + sectionUuid
334: + " does not exist.");
335: }
336: return name;
337: }
338: };
339: return (String) getHibernateTemplate().execute(hc);
340: }
341:
342: /**
343: * @inheritDoc
344: */
345: public String getSectionCategory(final String sectionUuid) {
346: HibernateCallback hc = new HibernateCallback() {
347: public Object doInHibernate(Session session)
348: throws HibernateException {
349: Query q = session.getNamedQuery("loadSectionCategory");
350: q.setParameter("sectionUuid", sectionUuid);
351: Object category = q.uniqueResult();
352: if (category == null) {
353: if (log.isDebugEnabled())
354: log.debug("Section " + sectionUuid
355: + " does not exist.");
356: }
357: return category;
358: }
359: };
360: return (String) getHibernateTemplate().execute(hc);
361: }
362:
363: /**
364: * @inheritDoc
365: */
366: public List getSectionsInCategory(final String siteContext,
367: final String categoryId) {
368: HibernateCallback hc = new HibernateCallback() {
369: public Object doInHibernate(Session session)
370: throws HibernateException {
371: Query q = session
372: .getNamedQuery("findSectionsByCategory");
373: q.setParameter("categoryId", categoryId);
374: q.setParameter("siteContext", siteContext);
375: return q.list();
376: }
377: };
378: return getHibernateTemplate().executeFind(hc);
379: }
380:
381: /**
382: * @inheritDoc
383: */
384: public String getCategoryName(String categoryId, Locale locale) {
385: ResourceBundle bundle = ResourceBundle.getBundle(
386: SectionManagerHibernateImpl.CATEGORY_BUNDLE, locale);
387: String name;
388: try {
389: name = bundle.getString(categoryId);
390: } catch (MissingResourceException mre) {
391: if (log.isInfoEnabled())
392: log.info("Could not find the name for category id = "
393: + categoryId + " in locale "
394: + locale.getDisplayName());
395: name = null;
396: }
397: return name;
398: }
399:
400: /**
401: * @inheritDoc
402: */
403: public List getUnassignedMembersInRole(final String siteContext,
404: final Role role) {
405: HibernateCallback hc = new HibernateCallback() {
406: public Object doInHibernate(Session session)
407: throws HibernateException {
408: Course course = getCourse(siteContext, session);
409: Query q;
410: if (role.isStudent()) {
411: q = session
412: .getNamedQuery("findUnsectionedStudents");
413: } else if (role.isTeachingAssistant()) {
414: q = session.getNamedQuery("findUnsectionedTas");
415: } else {
416: if (log.isInfoEnabled())
417: log
418: .info(role
419: + " is never assigned to sections, so unsectioned members is empty.");
420: return new ArrayList();
421: }
422: q.setParameter("courseUuid", course.getUuid());
423: return q.list();
424: }
425: };
426: return getHibernateTemplate().executeFind(hc);
427: }
428:
429: }
|