001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/course-management/tags/sakai_2-4-1/cm-impl/hibernate-impl/impl/src/java/org/sakaiproject/coursemanagement/impl/CourseManagementServiceHibernateImpl.java $
003: * $Id: CourseManagementServiceHibernateImpl.java 21050 2007-02-06 16:12:06Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.coursemanagement.impl;
021:
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import org.hibernate.Hibernate;
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.coursemanagement.api.AcademicSession;
038: import org.sakaiproject.coursemanagement.api.CanonicalCourse;
039: import org.sakaiproject.coursemanagement.api.CourseManagementService;
040: import org.sakaiproject.coursemanagement.api.CourseOffering;
041: import org.sakaiproject.coursemanagement.api.CourseSet;
042: import org.sakaiproject.coursemanagement.api.Enrollment;
043: import org.sakaiproject.coursemanagement.api.EnrollmentSet;
044: import org.sakaiproject.coursemanagement.api.Membership;
045: import org.sakaiproject.coursemanagement.api.Section;
046: import org.sakaiproject.coursemanagement.api.SectionCategory;
047: import org.sakaiproject.coursemanagement.api.exception.IdNotFoundException;
048: import org.springframework.orm.hibernate3.HibernateCallback;
049: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
050:
051: /**
052: * Provides access to course and enrollment data stored in sakai's local hibernate tables.
053: *
054: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
055: *
056: */
057: public class CourseManagementServiceHibernateImpl extends
058: HibernateDaoSupport implements CourseManagementService {
059: private static final Log log = LogFactory
060: .getLog(CourseManagementServiceHibernateImpl.class);
061:
062: public void init() {
063: log.info("Initializing " + getClass().getName());
064: }
065:
066: public void destroy() {
067: log.info("Destroying " + getClass().getName());
068: }
069:
070: /**
071: * A generic approach to finding objects by their eid. This is "coding by convention",
072: * since it expects the parameterized query to use "eid" as the single named parameter.
073: *
074: * @param eid The eid of the object we're trying to load
075: * @param className The name of the class / interface we're looking for
076: * @return The object, if found
077: * @throws IdNotFoundException
078: */
079: private Object getObjectByEid(final String eid,
080: final String className) throws IdNotFoundException {
081: HibernateCallback hc = new HibernateCallback() {
082: public Object doInHibernate(Session session)
083: throws HibernateException {
084: StringBuffer hql = new StringBuffer();
085: hql.append("from ").append(className).append(
086: " as obj where obj.eid=:eid");
087: Query q = session.createQuery(hql.toString());
088: q.setParameter("eid", eid);
089: Object result = q.uniqueResult();
090: if (result == null) {
091: throw new IdNotFoundException(eid, className);
092: }
093: return result;
094: }
095: };
096: return getHibernateTemplate().execute(hc);
097: }
098:
099: public CourseSet getCourseSet(String eid)
100: throws IdNotFoundException {
101: return (CourseSet) getObjectByEid(eid, CourseSetCmImpl.class
102: .getName());
103: }
104:
105: public Set<CourseSet> getChildCourseSets(
106: final String parentCourseSetEid) throws IdNotFoundException {
107: // Ensure that the parent exists
108: if (!isCourseSetDefined(parentCourseSetEid)) {
109: throw new IdNotFoundException(parentCourseSetEid,
110: CourseSetCmImpl.class.getName());
111: }
112: return new HashSet<CourseSet>(getHibernateTemplate()
113: .findByNamedQueryAndNamedParam("findChildCourseSets",
114: "parentEid", parentCourseSetEid));
115: }
116:
117: public Set<CourseSet> getCourseSets() {
118: return new HashSet<CourseSet>(getHibernateTemplate()
119: .findByNamedQuery("findTopLevelCourseSets"));
120: }
121:
122: public Set<Membership> getCourseSetMemberships(String courseSetEid)
123: throws IdNotFoundException {
124: return getMemberships((CourseSetCmImpl) getCourseSet(courseSetEid));
125: }
126:
127: public CanonicalCourse getCanonicalCourse(String eid)
128: throws IdNotFoundException {
129: return (CanonicalCourse) getObjectByEid(eid,
130: CanonicalCourseCmImpl.class.getName());
131: }
132:
133: public Set<CanonicalCourse> getEquivalentCanonicalCourses(
134: String canonicalCourseEid) {
135: final CanonicalCourseCmImpl canonicalCourse = (CanonicalCourseCmImpl) getCanonicalCourse(canonicalCourseEid);
136: HibernateCallback hc = new HibernateCallback() {
137: public Object doInHibernate(Session session)
138: throws HibernateException {
139: Query q = session
140: .getNamedQuery("findEquivalentCanonicalCourses");
141: q.setParameter("crossListing", canonicalCourse
142: .getCrossListing());
143: q.setParameter("canonicalCourse", canonicalCourse);
144: return q.list();
145: }
146: };
147: return new HashSet<CanonicalCourse>(getHibernateTemplate()
148: .executeFind(hc));
149: }
150:
151: public Set<CanonicalCourse> getCanonicalCourses(
152: final String courseSetEid) throws IdNotFoundException {
153: return ((CourseSetCmImpl) getCourseSet(courseSetEid))
154: .getCanonicalCourses();
155: }
156:
157: public List<AcademicSession> getAcademicSessions() {
158: return getHibernateTemplate().findByNamedQuery(
159: "findAcademicSessions");
160: }
161:
162: public List<AcademicSession> getCurrentAcademicSessions() {
163: return getHibernateTemplate().findByNamedQuery(
164: "findCurrentAcademicSessions");
165: }
166:
167: public AcademicSession getAcademicSession(final String eid)
168: throws IdNotFoundException {
169: return (AcademicSession) getObjectByEid(eid,
170: AcademicSessionCmImpl.class.getName());
171: }
172:
173: public CourseOffering getCourseOffering(String eid)
174: throws IdNotFoundException {
175: return (CourseOffering) getObjectByEid(eid,
176: CourseOfferingCmImpl.class.getName());
177: }
178:
179: public Set<CourseOffering> getCourseOfferingsInCourseSet(
180: final String courseSetEid) throws IdNotFoundException {
181: if (!isCourseSetDefined(courseSetEid)) {
182: throw new IdNotFoundException(courseSetEid,
183: CourseOfferingCmImpl.class.getName());
184: }
185: return ((CourseSetCmImpl) getCourseSet(courseSetEid))
186: .getCourseOfferings();
187: }
188:
189: public Set<CourseOffering> getEquivalentCourseOfferings(
190: String courseOfferingEid) throws IdNotFoundException {
191: final CourseOfferingCmImpl courseOffering = (CourseOfferingCmImpl) getCourseOffering(courseOfferingEid);
192: HibernateCallback hc = new HibernateCallback() {
193: public Object doInHibernate(Session session)
194: throws HibernateException {
195: Query q = session
196: .getNamedQuery("findEquivalentCourseOfferings");
197: q.setParameter("crossListing", courseOffering
198: .getCrossListing());
199: q.setParameter("courseOffering", courseOffering);
200: return q.list();
201: }
202: };
203: return new HashSet<CourseOffering>(getHibernateTemplate()
204: .executeFind(hc));
205: }
206:
207: public Set<Membership> getCourseOfferingMemberships(
208: String courseOfferingEid) throws IdNotFoundException {
209: return getMemberships((CourseOfferingCmImpl) getCourseOffering(courseOfferingEid));
210: }
211:
212: /**
213: * Gets the memberships for a membership container. This query can not be
214: * performed using just the container's eid, since it may conflict with other kinds
215: * of objects with the same eid.
216: *
217: * @param container
218: * @return
219: */
220: private Set<Membership> getMemberships(
221: final AbstractMembershipContainerCmImpl container) {
222:
223: // This may be a dynamic proxy. In that case, make sure we're using the class
224: // that hibernate understands.
225: final String className = Hibernate.getClass(container)
226: .getName();
227:
228: HibernateCallback hc = new HibernateCallback() {
229: public Object doInHibernate(Session session)
230: throws HibernateException {
231: StringBuffer sb = new StringBuffer(
232: "select mbr from MembershipCmImpl as mbr, ");
233: sb.append(className);
234: sb
235: .append(" as container where mbr.memberContainer=container ");
236: sb.append("and container.eid=:eid");
237: Query q = session.createQuery(sb.toString());
238: q.setParameter("eid", container.getEid());
239: return q.list();
240: }
241: };
242: return new HashSet<Membership>(getHibernateTemplate()
243: .executeFind(hc));
244: }
245:
246: public Section getSection(String eid) throws IdNotFoundException {
247: return (Section) getObjectByEid(eid, SectionCmImpl.class
248: .getName());
249: }
250:
251: public Set<Section> getSections(String courseOfferingEid)
252: throws IdNotFoundException {
253: CourseOffering courseOffering = getCourseOffering(courseOfferingEid);
254: return new HashSet<Section>(getHibernateTemplate()
255: .findByNamedQueryAndNamedParam(
256: "findTopLevelSectionsInCourseOffering",
257: "courseOffering", courseOffering));
258: }
259:
260: public Set<Section> getChildSections(final String parentSectionEid)
261: throws IdNotFoundException {
262: if (!isSectionDefined(parentSectionEid)) {
263: throw new IdNotFoundException(parentSectionEid,
264: SectionCmImpl.class.getName());
265: }
266: return new HashSet<Section>(getHibernateTemplate()
267: .findByNamedQueryAndNamedParam("findChildSections",
268: "parentEid", parentSectionEid));
269: }
270:
271: public Set<Membership> getSectionMemberships(String sectionEid)
272: throws IdNotFoundException {
273: return getMemberships((SectionCmImpl) getSection(sectionEid));
274: }
275:
276: public EnrollmentSet getEnrollmentSet(String eid)
277: throws IdNotFoundException {
278: return (EnrollmentSet) getObjectByEid(eid,
279: EnrollmentSetCmImpl.class.getName());
280: }
281:
282: public Set<EnrollmentSet> getEnrollmentSets(
283: final String courseOfferingEid) throws IdNotFoundException {
284: if (!isCourseOfferingDefined(courseOfferingEid)) {
285: throw new IdNotFoundException(courseOfferingEid,
286: CourseOfferingCmImpl.class.getName());
287: }
288: return new HashSet<EnrollmentSet>(getHibernateTemplate()
289: .findByNamedQueryAndNamedParam(
290: "findEnrollmentSetsByCourseOffering",
291: "courseOfferingEid", courseOfferingEid));
292: }
293:
294: public Set<Enrollment> getEnrollments(final String enrollmentSetEid)
295: throws IdNotFoundException {
296: if (!isEnrollmentSetDefined(enrollmentSetEid)) {
297: throw new IdNotFoundException(enrollmentSetEid,
298: EnrollmentSetCmImpl.class.getName());
299: }
300: return new HashSet<Enrollment>(getHibernateTemplate()
301: .findByNamedQueryAndNamedParam("findEnrollments",
302: "enrollmentSetEid", enrollmentSetEid));
303: }
304:
305: public boolean isEnrolled(final String userId,
306: final Set<String> enrollmentSetEids) {
307: HibernateCallback hc = new HibernateCallback() {
308: public Object doInHibernate(Session session)
309: throws HibernateException {
310: Query q = session.getNamedQuery("countEnrollments");
311: q.setParameter("userId", userId);
312: q.setParameterList("enrollmentSetEids",
313: enrollmentSetEids);
314: return q.iterate().next();
315: }
316: };
317: Integer i = (Integer) getHibernateTemplate().execute(hc);
318: if (log.isDebugEnabled())
319: log.debug(userId + " is enrolled in " + i + " of these "
320: + enrollmentSetEids.size() + " EnrollmentSets");
321: return i.intValue() > 0;
322: }
323:
324: public boolean isEnrolled(String userId, String enrollmentSetEid) {
325: HashSet<String> enrollmentSetEids = new HashSet<String>();
326: enrollmentSetEids.add(enrollmentSetEid);
327: return isEnrolled(userId, enrollmentSetEids);
328: }
329:
330: public Enrollment findEnrollment(final String userId,
331: final String enrollmentSetEid) {
332: if (!isEnrollmentSetDefined(enrollmentSetEid)) {
333: log.warn("Could not find an enrollment set with eid="
334: + enrollmentSetEid);
335: return null;
336: }
337: HibernateCallback hc = new HibernateCallback() {
338: public Object doInHibernate(Session session)
339: throws HibernateException {
340: Query q = session.getNamedQuery("findEnrollment");
341: q.setParameter("userId", userId);
342: q.setParameter("enrollmentSetEid", enrollmentSetEid);
343: return q.uniqueResult();
344: }
345: };
346: return (Enrollment) getHibernateTemplate().execute(hc);
347: }
348:
349: public Set<String> getInstructorsOfRecordIds(String enrollmentSetEid)
350: throws IdNotFoundException {
351: EnrollmentSet es = getEnrollmentSet(enrollmentSetEid);
352: return es.getOfficialInstructors();
353: }
354:
355: public Set<EnrollmentSet> findCurrentlyEnrolledEnrollmentSets(
356: final String userId) {
357: return new HashSet<EnrollmentSet>(getHibernateTemplate()
358: .findByNamedQueryAndNamedParam(
359: "findCurrentlyEnrolledEnrollmentSets",
360: "userId", userId));
361: }
362:
363: public Set<EnrollmentSet> findCurrentlyInstructingEnrollmentSets(
364: final String userId) {
365: return new HashSet<EnrollmentSet>(getHibernateTemplate()
366: .findByNamedQueryAndNamedParam(
367: "findCurrentlyInstructingEnrollmentSets",
368: "userId", userId));
369: }
370:
371: public Set<Section> findInstructingSections(final String userId) {
372: return new HashSet<Section>(getHibernateTemplate()
373: .findByNamedQueryAndNamedParam(
374: "findInstructingSections", "userId", userId));
375: }
376:
377: public Set<Section> findEnrolledSections(final String userId) {
378: return new HashSet<Section>(getHibernateTemplate()
379: .findByNamedQueryAndNamedParam("findEnrolledSections",
380: "userId", userId));
381: }
382:
383: public Set<Section> findInstructingSections(final String userId,
384: final String academicSessionEid) {
385: HibernateCallback hc = new HibernateCallback() {
386: public Object doInHibernate(Session session)
387: throws HibernateException {
388: Query q = session
389: .getNamedQuery("findInstructingSectionsByAcademicSession");
390: q.setParameter("userId", userId);
391: q
392: .setParameter("academicSessionEid",
393: academicSessionEid);
394: return q.list();
395: }
396: };
397: return new HashSet<Section>(getHibernateTemplate().executeFind(
398: hc));
399: }
400:
401: public Set<CourseOffering> findCourseOfferings(
402: final String courseSetEid, final String academicSessionEid)
403: throws IdNotFoundException {
404: HibernateCallback hc = new HibernateCallback() {
405: public Object doInHibernate(Session session)
406: throws HibernateException {
407: Query q = session
408: .getNamedQuery("findCourseOfferingsByCourseSetAndAcademicSession");
409: q.setParameter("courseSetEid", courseSetEid);
410: q
411: .setParameter("academicSessionEid",
412: academicSessionEid);
413: return q.list();
414: }
415: };
416: return new HashSet<CourseOffering>(getHibernateTemplate()
417: .executeFind(hc));
418: }
419:
420: public boolean isEmpty(final String courseSetEid) {
421: HibernateCallback hc = new HibernateCallback() {
422: public Object doInHibernate(Session session)
423: throws HibernateException {
424: Query q = session
425: .getNamedQuery("findNonEmptyCourseSet");
426: q.setParameter("eid", courseSetEid);
427: return Boolean.valueOf(!q.iterate().hasNext());
428: }
429: };
430: return ((Boolean) getHibernateTemplate().execute(hc))
431: .booleanValue();
432: }
433:
434: public List<CourseSet> findCourseSets(final String category) {
435: return getHibernateTemplate().findByNamedQueryAndNamedParam(
436: "findCourseSetByCategory", "category", category);
437: }
438:
439: public Map<String, String> findCourseOfferingRoles(
440: final String userEid) {
441: // Keep track of CourseOfferings that we've already queried
442: Set<String> queriedCourseOfferingEids = new HashSet<String>();
443: List results = getHibernateTemplate()
444: .findByNamedQueryAndNamedParam(
445: "findCourseOfferingRoles", "userEid", userEid);
446: Map<String, String> courseOfferingRoleMap = new HashMap<String, String>();
447: for (Iterator iter = results.iterator(); iter.hasNext();) {
448: Object[] oa = (Object[]) iter.next();
449: courseOfferingRoleMap.put((String) oa[0], (String) oa[1]);
450: queriedCourseOfferingEids.add((String) oa[0]);
451: }
452: return courseOfferingRoleMap;
453: }
454:
455: public Map<String, String> findCourseSetRoles(final String userEid) {
456: List results = getHibernateTemplate()
457: .findByNamedQueryAndNamedParam("findCourseSetRoles",
458: "userEid", userEid);
459: Map<String, String> courseSetRoleMap = new HashMap<String, String>();
460: for (Iterator iter = results.iterator(); iter.hasNext();) {
461: Object[] oa = (Object[]) iter.next();
462: courseSetRoleMap.put((String) oa[0], (String) oa[1]);
463: }
464: return courseSetRoleMap;
465: }
466:
467: public Map<String, String> findSectionRoles(final String userEid) {
468: List results = getHibernateTemplate()
469: .findByNamedQueryAndNamedParam("findSectionRoles",
470: "userEid", userEid);
471: Map<String, String> sectionRoleMap = new HashMap<String, String>();
472: for (Iterator iter = results.iterator(); iter.hasNext();) {
473: Object[] oa = (Object[]) iter.next();
474: sectionRoleMap.put((String) oa[0], (String) oa[1]);
475: }
476: return sectionRoleMap;
477: }
478:
479: public Set<CourseOffering> getCourseOfferingsInCanonicalCourse(
480: final String canonicalCourseEid) throws IdNotFoundException {
481: if (!isCanonicalCourseDefined(canonicalCourseEid)) {
482: throw new IdNotFoundException(canonicalCourseEid,
483: CanonicalCourseCmImpl.class.getName());
484: }
485: return new HashSet<CourseOffering>(getHibernateTemplate()
486: .findByNamedQueryAndNamedParam(
487: "findCourseOfferingsByCanonicalCourse",
488: "canonicalCourseEid", canonicalCourseEid));
489: }
490:
491: public boolean isAcademicSessionDefined(String eid) {
492: return ((Integer) getHibernateTemplate()
493: .findByNamedQueryAndNamedParam(
494: "isAcademicSessionDefined", "eid", eid).get(0))
495: .intValue() == 1;
496: }
497:
498: public boolean isCanonicalCourseDefined(String eid) {
499: return ((Integer) getHibernateTemplate()
500: .findByNamedQueryAndNamedParam(
501: "isCanonicalCourseDefined", "eid", eid).get(0))
502: .intValue() == 1;
503: }
504:
505: public boolean isCourseOfferingDefined(String eid) {
506: return ((Integer) getHibernateTemplate()
507: .findByNamedQueryAndNamedParam(
508: "isCourseOfferingDefined", "eid", eid).get(0))
509: .intValue() == 1;
510: }
511:
512: public boolean isCourseSetDefined(String eid) {
513: return ((Integer) getHibernateTemplate()
514: .findByNamedQueryAndNamedParam("isCourseSetDefined",
515: "eid", eid).get(0)).intValue() == 1;
516: }
517:
518: public boolean isEnrollmentSetDefined(String eid) {
519: return ((Integer) getHibernateTemplate()
520: .findByNamedQueryAndNamedParam(
521: "isEnrollmentSetDefined", "eid", eid).get(0))
522: .intValue() == 1;
523: }
524:
525: public boolean isSectionDefined(String eid) {
526: return ((Integer) getHibernateTemplate()
527: .findByNamedQueryAndNamedParam("isSectionDefined",
528: "eid", eid).get(0)).intValue() == 1;
529: }
530:
531: public List<String> getSectionCategories() {
532: return getHibernateTemplate().findByNamedQuery(
533: "findSectionCategories");
534: }
535:
536: public String getSectionCategoryDescription(String categoryCode) {
537: if (categoryCode == null) {
538: return null;
539: }
540: SectionCategory cat = (SectionCategory) getHibernateTemplate()
541: .get(SectionCategoryCmImpl.class, categoryCode);
542: if (cat == null) {
543: return null;
544: } else {
545: return cat.getCategoryDescription();
546: }
547: }
548:
549: public Map<String, String> getEnrollmentStatusDescriptions(
550: Locale locale) {
551: Map<String, String> map = new HashMap<String, String>();
552: map.put("enrolled", "Enrolled");
553: map.put("wait", "Waitlisted");
554: return map;
555: }
556:
557: public Map<String, String> getGradingSchemeDescriptions(
558: Locale locale) {
559: Map<String, String> map = new HashMap<String, String>();
560: map.put("standard", "Letter Grades");
561: map.put("pnp", "Pass / Not Pass");
562: return map;
563: }
564:
565: public Map<String, String> getMembershipStatusDescriptions(
566: Locale locale) {
567: Map<String, String> map = new HashMap<String, String>();
568: map.put("member", "Member");
569: map.put("guest", "Guest");
570: return map;
571: }
572:
573: }
|