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/CourseManagementAdministrationHibernateImpl.java $
003: * $Id: CourseManagementAdministrationHibernateImpl.java 20714 2007-01-26 19:47:36Z 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.sql.Time;
023: import java.util.Date;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.hibernate.Hibernate;
030: import org.hibernate.HibernateException;
031: import org.hibernate.Query;
032: import org.hibernate.Session;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.sakaiproject.coursemanagement.api.AcademicSession;
037: import org.sakaiproject.coursemanagement.api.CanonicalCourse;
038: import org.sakaiproject.coursemanagement.api.CourseManagementAdministration;
039: import org.sakaiproject.coursemanagement.api.CourseOffering;
040: import org.sakaiproject.coursemanagement.api.CourseSet;
041: import org.sakaiproject.coursemanagement.api.Enrollment;
042: import org.sakaiproject.coursemanagement.api.EnrollmentSet;
043: import org.sakaiproject.coursemanagement.api.Meeting;
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.IdExistsException;
048: import org.sakaiproject.coursemanagement.api.exception.IdNotFoundException;
049: import org.sakaiproject.coursemanagement.impl.facade.Authentication;
050: import org.springframework.dao.DataIntegrityViolationException;
051: import org.springframework.orm.hibernate3.HibernateCallback;
052: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
053:
054: /**
055: * Manipulates course and enrollment data stored in sakai's local hibernate tables.
056: *
057: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
058: *
059: */
060: public class CourseManagementAdministrationHibernateImpl extends
061: HibernateDaoSupport implements CourseManagementAdministration {
062:
063: private static final Log log = LogFactory
064: .getLog(CourseManagementAdministrationHibernateImpl.class);
065:
066: protected Authentication authn;
067:
068: public void setAuthn(Authentication authn) {
069: this .authn = authn;
070: }
071:
072: public void init() {
073: log.info("Initializing " + getClass().getName());
074: }
075:
076: public void destroy() {
077: log.info("Destroying " + getClass().getName());
078: }
079:
080: public AcademicSession createAcademicSession(String eid,
081: String title, String description, Date startDate,
082: Date endDate) throws IdExistsException {
083: AcademicSessionCmImpl academicSession = new AcademicSessionCmImpl(
084: eid, title, description, startDate, endDate);
085: academicSession.setCreatedBy(authn.getUserEid());
086: academicSession.setCreatedDate(new Date());
087: try {
088: getHibernateTemplate().save(academicSession);
089: return academicSession;
090: } catch (DataIntegrityViolationException dive) {
091: throw new IdExistsException(eid, AcademicSession.class
092: .getName());
093: }
094: }
095:
096: public void updateAcademicSession(AcademicSession academicSession) {
097: AcademicSessionCmImpl as = (AcademicSessionCmImpl) academicSession;
098: as.setLastModifiedBy(authn.getUserEid());
099: as.setLastModifiedDate(new Date());
100: getHibernateTemplate().update(as);
101: }
102:
103: public CourseSet createCourseSet(String eid, String title,
104: String description, String category,
105: String parentCourseSetEid) throws IdExistsException {
106: CourseSet parent = null;
107: if (parentCourseSetEid != null) {
108: parent = (CourseSet) getObjectByEid(parentCourseSetEid,
109: CourseSetCmImpl.class.getName());
110: }
111: CourseSetCmImpl courseSet = new CourseSetCmImpl(eid, title,
112: description, category, parent);
113: courseSet.setCreatedBy(authn.getUserEid());
114: courseSet.setCreatedDate(new Date());
115: try {
116: getHibernateTemplate().save(courseSet);
117: return courseSet;
118: } catch (DataIntegrityViolationException dive) {
119: throw new IdExistsException(eid, CourseSet.class.getName());
120: }
121: }
122:
123: public void updateCourseSet(CourseSet courseSet) {
124: CourseSetCmImpl cs = (CourseSetCmImpl) courseSet;
125: cs.setLastModifiedBy(authn.getUserEid());
126: cs.setLastModifiedDate(new Date());
127: getHibernateTemplate().update(cs);
128: }
129:
130: public CanonicalCourse createCanonicalCourse(String eid,
131: String title, String description) throws IdExistsException {
132: CanonicalCourseCmImpl canonCourse = new CanonicalCourseCmImpl(
133: eid, title, description);
134: canonCourse.setCreatedBy(authn.getUserEid());
135: canonCourse.setCreatedDate(new Date());
136: try {
137: getHibernateTemplate().save(canonCourse);
138: return canonCourse;
139: } catch (DataIntegrityViolationException dive) {
140: throw new IdExistsException(eid, CanonicalCourse.class
141: .getName());
142: }
143: }
144:
145: public void updateCanonicalCourse(CanonicalCourse canonicalCourse) {
146: CanonicalCourseCmImpl cc = (CanonicalCourseCmImpl) canonicalCourse;
147: cc.setLastModifiedBy(authn.getUserEid());
148: cc.setLastModifiedDate(new Date());
149: getHibernateTemplate().update(cc);
150: }
151:
152: public void addCanonicalCourseToCourseSet(String courseSetEid,
153: String canonicalCourseEid) throws IdNotFoundException {
154: CourseSetCmImpl courseSet = (CourseSetCmImpl) getObjectByEid(
155: courseSetEid, CourseSetCmImpl.class.getName());
156: CanonicalCourseCmImpl canonCourse = (CanonicalCourseCmImpl) getObjectByEid(
157: canonicalCourseEid, CanonicalCourseCmImpl.class
158: .getName());
159:
160: Set<CanonicalCourse> canonCourses = courseSet
161: .getCanonicalCourses();
162: if (canonCourses == null) {
163: canonCourses = new HashSet<CanonicalCourse>();
164: courseSet.setCanonicalCourses(canonCourses);
165: }
166: canonCourses.add(canonCourse);
167:
168: courseSet.setLastModifiedBy(authn.getUserEid());
169: courseSet.setLastModifiedDate(new Date());
170: getHibernateTemplate().update(courseSet);
171: }
172:
173: public boolean removeCanonicalCourseFromCourseSet(
174: String courseSetEid, String canonicalCourseEid) {
175: CourseSetCmImpl courseSet = (CourseSetCmImpl) getObjectByEid(
176: courseSetEid, CourseSetCmImpl.class.getName());
177: CanonicalCourseCmImpl canonCourse = (CanonicalCourseCmImpl) getObjectByEid(
178: canonicalCourseEid, CanonicalCourseCmImpl.class
179: .getName());
180:
181: Set courses = courseSet.getCanonicalCourses();
182: if (courses == null || !courses.contains(canonCourse)) {
183: return false;
184: }
185: courses.remove(canonCourse);
186:
187: courseSet.setLastModifiedBy(authn.getUserEid());
188: courseSet.setLastModifiedDate(new Date());
189: getHibernateTemplate().update(courseSet);
190: return true;
191: }
192:
193: private void setEquivalents(Set crossListables) {
194: CrossListingCmImpl newCrossListing = new CrossListingCmImpl();
195: newCrossListing.setCreatedBy(authn.getUserEid());
196: newCrossListing.setCreatedDate(new Date());
197: getHibernateTemplate().save(newCrossListing);
198:
199: Set<CrossListingCmImpl> oldCrossListings = new HashSet<CrossListingCmImpl>();
200:
201: for (Iterator iter = crossListables.iterator(); iter.hasNext();) {
202: CrossListableCmImpl clable = (CrossListableCmImpl) iter
203: .next();
204: CrossListingCmImpl oldCrossListing = clable
205: .getCrossListing();
206: if (oldCrossListing != null) {
207: oldCrossListings.add(oldCrossListing);
208: }
209: if (log.isDebugEnabled())
210: log.debug("Setting crosslisting for crosslistable "
211: + clable.getEid() + " to "
212: + newCrossListing.getKey());
213: clable.setCrossListing(newCrossListing);
214:
215: clable.setLastModifiedBy(authn.getUserEid());
216: clable.setLastModifiedDate(new Date());
217: getHibernateTemplate().update(clable);
218: }
219:
220: // TODO Clean up orphaned cross listings
221: }
222:
223: public void setEquivalentCanonicalCourses(Set canonicalCourses) {
224: setEquivalents(canonicalCourses);
225: }
226:
227: private boolean removeEquiv(CrossListableCmImpl impl) {
228: boolean hadCrossListing = impl.getCrossListing() != null;
229: impl.setCrossListing(null);
230: impl.setLastModifiedBy(authn.getUserEid());
231: impl.setLastModifiedDate(new Date());
232: getHibernateTemplate().update(impl);
233: return hadCrossListing;
234: }
235:
236: public boolean removeEquivalency(CanonicalCourse canonicalCourse) {
237: return removeEquiv((CanonicalCourseCmImpl) canonicalCourse);
238: }
239:
240: public CourseOffering createCourseOffering(String eid,
241: String title, String description, String status,
242: String academicSessionEid, String canonicalCourseEid,
243: Date startDate, Date endDate) throws IdExistsException {
244: AcademicSession as = (AcademicSession) getObjectByEid(
245: academicSessionEid, AcademicSessionCmImpl.class
246: .getName());
247: CanonicalCourse cc = (CanonicalCourse) getObjectByEid(
248: canonicalCourseEid, CanonicalCourseCmImpl.class
249: .getName());
250: CourseOfferingCmImpl co = new CourseOfferingCmImpl(eid, title,
251: description, status, as, cc, startDate, endDate);
252: co.setCreatedBy(authn.getUserEid());
253: co.setCreatedDate(new Date());
254: try {
255: getHibernateTemplate().save(co);
256: return co;
257: } catch (DataIntegrityViolationException dive) {
258: throw new IdExistsException(eid, CourseOffering.class
259: .getName());
260: }
261: }
262:
263: public void updateCourseOffering(CourseOffering courseOffering) {
264: CourseOfferingCmImpl co = (CourseOfferingCmImpl) courseOffering;
265: co.setLastModifiedBy(authn.getUserEid());
266: co.setLastModifiedDate(new Date());
267: getHibernateTemplate().update(co);
268: }
269:
270: public void setEquivalentCourseOfferings(Set courseOfferings) {
271: setEquivalents(courseOfferings);
272: }
273:
274: public boolean removeEquivalency(CourseOffering courseOffering) {
275: return removeEquiv((CrossListableCmImpl) courseOffering);
276: }
277:
278: public void addCourseOfferingToCourseSet(String courseSetEid,
279: String courseOfferingEid) {
280: // CourseSet's set of courses are controlled on the CourseSet side of the bi-directional relationship
281: CourseSetCmImpl courseSet = (CourseSetCmImpl) getObjectByEid(
282: courseSetEid, CourseSetCmImpl.class.getName());
283: CourseOfferingCmImpl courseOffering = (CourseOfferingCmImpl) getObjectByEid(
284: courseOfferingEid, CourseOfferingCmImpl.class.getName());
285: Set<CourseOffering> offerings = courseSet.getCourseOfferings();
286: if (offerings == null) {
287: offerings = new HashSet<CourseOffering>();
288: }
289: offerings.add(courseOffering);
290: courseSet.setCourseOfferings(offerings);
291:
292: courseSet.setLastModifiedBy(authn.getUserEid());
293: courseSet.setLastModifiedDate(new Date());
294: getHibernateTemplate().update(courseSet);
295: }
296:
297: public boolean removeCourseOfferingFromCourseSet(
298: String courseSetEid, String courseOfferingEid) {
299: CourseSetCmImpl courseSet = (CourseSetCmImpl) getObjectByEid(
300: courseSetEid, CourseSetCmImpl.class.getName());
301: CourseOffering courseOffering = (CourseOffering) getObjectByEid(
302: courseOfferingEid, CourseOfferingCmImpl.class.getName());
303: Set offerings = courseSet.getCourseOfferings();
304: if (offerings == null || !offerings.contains(courseOffering)) {
305: return false;
306: }
307: offerings.remove(courseOffering);
308:
309: courseSet.setLastModifiedBy(authn.getUserEid());
310: courseSet.setLastModifiedDate(new Date());
311: getHibernateTemplate().update(courseSet);
312: return true;
313: }
314:
315: public EnrollmentSet createEnrollmentSet(String eid, String title,
316: String description, String category,
317: String defaultEnrollmentCredits, String courseOfferingEid,
318: Set officialGraders) throws IdExistsException {
319: if (courseOfferingEid == null) {
320: throw new IllegalArgumentException(
321: "You can not create an EnrollmentSet without specifying a courseOffering");
322: }
323: CourseOffering co = (CourseOffering) getObjectByEid(
324: courseOfferingEid, CourseOfferingCmImpl.class.getName());
325: EnrollmentSetCmImpl enrollmentSet = new EnrollmentSetCmImpl(
326: eid, title, description, category,
327: defaultEnrollmentCredits, co, officialGraders);
328: enrollmentSet.setCreatedBy(authn.getUserEid());
329: enrollmentSet.setCreatedDate(new Date());
330: try {
331: getHibernateTemplate().save(enrollmentSet);
332: return enrollmentSet;
333: } catch (DataIntegrityViolationException dive) {
334: throw new IdExistsException(eid, EnrollmentSet.class
335: .getName());
336: }
337: }
338:
339: public void updateEnrollmentSet(EnrollmentSet enrollmentSet) {
340: EnrollmentSetCmImpl es = (EnrollmentSetCmImpl) enrollmentSet;
341: es.setLastModifiedBy(authn.getUserEid());
342: es.setLastModifiedDate(new Date());
343: getHibernateTemplate().update(es);
344: }
345:
346: public Enrollment addOrUpdateEnrollment(String userId,
347: String enrollmentSetEid, String enrollmentStatus,
348: String credits, String gradingScheme) {
349: EnrollmentCmImpl enrollment = null;
350:
351: List enrollments = getHibernateTemplate()
352: .findByNamedQueryAndNamedParam("findEnrollment",
353: new String[] { "enrollmentSetEid", "userId" },
354: new Object[] { enrollmentSetEid, userId });
355: if (enrollments.isEmpty()) {
356: EnrollmentSet enrollmentSet = (EnrollmentSet) getObjectByEid(
357: enrollmentSetEid, EnrollmentSetCmImpl.class
358: .getName());
359: enrollment = new EnrollmentCmImpl(userId, enrollmentSet,
360: enrollmentStatus, credits, gradingScheme);
361: enrollment.setCreatedBy(authn.getUserEid());
362: enrollment.setCreatedDate(new Date());
363: getHibernateTemplate().save(enrollment);
364: } else {
365: enrollment = (EnrollmentCmImpl) enrollments.get(0);
366: enrollment.setEnrollmentStatus(enrollmentStatus);
367: enrollment.setCredits(credits);
368: enrollment.setGradingScheme(gradingScheme);
369: enrollment.setDropped(false);
370:
371: enrollment.setLastModifiedBy(authn.getUserEid());
372: enrollment.setLastModifiedDate(new Date());
373: getHibernateTemplate().update(enrollment);
374: }
375: return enrollment;
376: }
377:
378: public boolean removeEnrollment(String userId,
379: String enrollmentSetEid) {
380: List enrollments = getHibernateTemplate()
381: .findByNamedQueryAndNamedParam("findEnrollment",
382: new String[] { "enrollmentSetEid", "userId" },
383: new Object[] { enrollmentSetEid, userId });
384:
385: if (enrollments.isEmpty()) {
386: return false;
387: } else {
388: EnrollmentCmImpl enr = (EnrollmentCmImpl) enrollments
389: .get(0);
390: enr.setDropped(true);
391: enr.setLastModifiedBy(authn.getUserEid());
392: enr.setLastModifiedDate(new Date());
393: getHibernateTemplate().update(enr);
394: return true;
395: }
396: }
397:
398: public Section createSection(String eid, String title,
399: String description, String category,
400: String parentSectionEid, String courseOfferingEid,
401: String enrollmentSetEid) throws IdExistsException {
402:
403: // The objects related to this section
404: Section parent = null;
405: CourseOffering co = null;
406: EnrollmentSet es = null;
407: Integer maxSize = null;
408:
409: // Get the enrollment set, if needed
410: if (courseOfferingEid != null) {
411: co = (CourseOffering) getObjectByEid(courseOfferingEid,
412: CourseOfferingCmImpl.class.getName());
413: }
414:
415: // Get the parent section, if needed
416: if (parentSectionEid != null) {
417: parent = (Section) getObjectByEid(parentSectionEid,
418: SectionCmImpl.class.getName());
419: }
420:
421: // Get the enrollment set, if needed
422: if (enrollmentSetEid != null) {
423: es = (EnrollmentSet) getObjectByEid(enrollmentSetEid,
424: EnrollmentSetCmImpl.class.getName());
425: }
426:
427: SectionCmImpl section = new SectionCmImpl(eid, title,
428: description, category, parent, co, es, maxSize);
429: section.setCreatedBy(authn.getUserEid());
430: section.setCreatedDate(new Date());
431: try {
432: getHibernateTemplate().save(section);
433: return section;
434: } catch (DataIntegrityViolationException dive) {
435: throw new IdExistsException(eid, Section.class.getName());
436: }
437: }
438:
439: public void updateSection(Section section) {
440: SectionCmImpl sec = (SectionCmImpl) section;
441: sec.setLastModifiedBy(authn.getUserEid());
442: sec.setLastModifiedDate(new Date());
443: getHibernateTemplate().update(sec);
444: }
445:
446: public Membership addOrUpdateCourseSetMembership(
447: final String userId, String role,
448: final String courseSetEid, final String status)
449: throws IdNotFoundException {
450: CourseSetCmImpl cs = (CourseSetCmImpl) getObjectByEid(
451: courseSetEid, CourseSetCmImpl.class.getName());
452: MembershipCmImpl member = getMembership(userId, cs);
453: if (member == null) {
454: // Add the new member
455: member = new MembershipCmImpl(userId, role, cs, status);
456: member.setCreatedBy(authn.getUserEid());
457: member.setCreatedDate(new Date());
458: getHibernateTemplate().save(member);
459: } else {
460: // Update the existing member
461: member.setRole(role);
462: member.setStatus(status);
463: member.setLastModifiedBy(authn.getUserEid());
464: member.setLastModifiedDate(new Date());
465: getHibernateTemplate().update(member);
466: }
467: return member;
468: }
469:
470: public boolean removeCourseSetMembership(String userId,
471: String courseSetEid) {
472: MembershipCmImpl member = getMembership(userId,
473: (CourseSetCmImpl) getObjectByEid(courseSetEid,
474: CourseSetCmImpl.class.getName()));
475: if (member == null) {
476: return false;
477: } else {
478: getHibernateTemplate().delete(member);
479: return true;
480: }
481: }
482:
483: public Membership addOrUpdateCourseOfferingMembership(
484: String userId, String role, String courseOfferingEid,
485: String status) {
486: CourseOfferingCmImpl co = (CourseOfferingCmImpl) getObjectByEid(
487: courseOfferingEid, CourseOfferingCmImpl.class.getName());
488: MembershipCmImpl member = getMembership(userId, co);
489: if (member == null) {
490: // Add the new member
491: member = new MembershipCmImpl(userId, role, co, status);
492: member.setCreatedBy(authn.getUserEid());
493: member.setCreatedDate(new Date());
494: getHibernateTemplate().save(member);
495: } else {
496: // Update the existing member
497: member.setRole(role);
498: member.setStatus(status);
499: member.setLastModifiedBy(authn.getUserEid());
500: member.setLastModifiedDate(new Date());
501: getHibernateTemplate().update(member);
502: }
503: return member;
504: }
505:
506: public boolean removeCourseOfferingMembership(String userId,
507: String courseOfferingEid) {
508: CourseOfferingCmImpl courseOffering = (CourseOfferingCmImpl) getObjectByEid(
509: courseOfferingEid, CourseOfferingCmImpl.class.getName());
510: MembershipCmImpl member = getMembership(userId, courseOffering);
511: if (member == null) {
512: return false;
513: } else {
514: getHibernateTemplate().delete(member);
515: return true;
516: }
517: }
518:
519: public Membership addOrUpdateSectionMembership(String userId,
520: String role, String sectionEid, String status) {
521: SectionCmImpl sec = (SectionCmImpl) getObjectByEid(sectionEid,
522: SectionCmImpl.class.getName());
523: MembershipCmImpl member = getMembership(userId, sec);
524: if (member == null) {
525: // Add the new member
526: member = new MembershipCmImpl(userId, role, sec, status);
527: member.setCreatedBy(authn.getUserEid());
528: member.setCreatedDate(new Date());
529: getHibernateTemplate().save(member);
530: } else {
531: // Update the existing member
532: member.setRole(role);
533: member.setStatus(status);
534: member.setLastModifiedBy(authn.getUserEid());
535: member.setLastModifiedDate(new Date());
536: getHibernateTemplate().update(member);
537: }
538: return member;
539: }
540:
541: public boolean removeSectionMembership(String userId,
542: String sectionEid) {
543: SectionCmImpl sec = (SectionCmImpl) getObjectByEid(sectionEid,
544: SectionCmImpl.class.getName());
545: MembershipCmImpl member = getMembership(userId, sec);
546: if (member == null) {
547: return false;
548: } else {
549: getHibernateTemplate().delete(member);
550: return true;
551: }
552: }
553:
554: private MembershipCmImpl getMembership(final String userId,
555: final AbstractMembershipContainerCmImpl container) {
556: // This may be a dynamic proxy. In that case, make sure we're using the class
557: // that hibernate understands.
558: final String className = Hibernate.getClass(container)
559: .getName();
560:
561: final StringBuffer sb = new StringBuffer(
562: "select mbr from MembershipCmImpl as mbr, ");
563: sb.append(className);
564: sb.append(" as container where mbr.memberContainer=container ");
565: sb.append("and container.eid=:eid ");
566: sb.append("and mbr.userId=:userId");
567:
568: HibernateCallback hc = new HibernateCallback() {
569: public Object doInHibernate(Session session)
570: throws HibernateException {
571: Query q = session.createQuery(sb.toString());
572: q.setParameter("eid", container.getEid());
573: q.setParameter("userId", userId);
574: return q.uniqueResult();
575: }
576: };
577: return (MembershipCmImpl) getHibernateTemplate().execute(hc);
578: }
579:
580: public Meeting newSectionMeeting(String sectionEid,
581: String location, Time startTime, Time finishTime,
582: String notes) {
583: Section section = (Section) getObjectByEid(sectionEid,
584: SectionCmImpl.class.getName());
585: MeetingCmImpl meeting = new MeetingCmImpl(section, location,
586: startTime, finishTime, notes);
587: meeting.setCreatedBy(authn.getUserEid());
588: meeting.setCreatedDate(new Date());
589: Set<Meeting> meetings = section.getMeetings();
590: if (meetings == null) {
591: meetings = new HashSet<Meeting>();
592: section.setMeetings(meetings);
593: }
594: return meeting;
595: }
596:
597: public void removeAcademicSession(String eid) {
598: AcademicSessionCmImpl as = (AcademicSessionCmImpl) getObjectByEid(
599: eid, AcademicSessionCmImpl.class.getName());
600:
601: // Remove the course offerings in this academic session
602: List<CourseOffering> courseOfferings = getHibernateTemplate()
603: .find(
604: "select co from CourseOfferingCmImpl as co where co.academicSession.eid = ?",
605: eid);
606: for (Iterator<CourseOffering> iter = courseOfferings.iterator(); iter
607: .hasNext();) {
608: removeCourseOffering(iter.next().getEid());
609: }
610:
611: // Remove the academic session itself
612: getHibernateTemplate().delete(as);
613: }
614:
615: public void removeCanonicalCourse(String eid) {
616: CanonicalCourseCmImpl cc = (CanonicalCourseCmImpl) getObjectByEid(
617: eid, CanonicalCourseCmImpl.class.getName());
618:
619: // Remove any equivalents
620: removeEquiv(cc);
621:
622: // Remove the associated course offerings (see removeCourseOffering for further cascades)
623: Set<CourseOffering> coSet = new HashSet<CourseOffering>(
624: getHibernateTemplate().findByNamedQueryAndNamedParam(
625: "findCourseOfferingsByCanonicalCourse",
626: "canonicalCourseEid", eid));
627: for (Iterator<CourseOffering> iter = coSet.iterator(); iter
628: .hasNext();) {
629: CourseOffering co = iter.next();
630: removeCourseOffering(co.getEid());
631: }
632:
633: getHibernateTemplate().delete(cc);
634: }
635:
636: public void removeCourseOffering(String eid) {
637: CourseOfferingCmImpl co = (CourseOfferingCmImpl) getObjectByEid(
638: eid, CourseOfferingCmImpl.class.getName());
639:
640: // Remove the memberships
641: for (Iterator iter = getMemberships(co).iterator(); iter
642: .hasNext();) {
643: getHibernateTemplate().delete(iter.next());
644: }
645:
646: // Remove the sections
647: List sections = getHibernateTemplate()
648: .findByNamedQueryAndNamedParam(
649: "findTopLevelSectionsInCourseOffering",
650: "courseOffering", co);
651: for (Iterator iter = sections.iterator(); iter.hasNext();) {
652: Section sec = (Section) iter.next();
653: removeSection(sec.getEid());
654: }
655:
656: List enrollmentSets = getHibernateTemplate()
657: .findByNamedQueryAndNamedParam(
658: "findEnrollmentSetsByCourseOffering",
659: "courseOfferingEid", eid);
660: // Remove the enrollment sets
661: for (Iterator iter = enrollmentSets.iterator(); iter.hasNext();) {
662: EnrollmentSet enr = (EnrollmentSet) iter.next();
663: removeEnrollmentSet(enr.getEid());
664: }
665:
666: // Remove the course offering itself
667: getHibernateTemplate().delete(co);
668: }
669:
670: public void removeCourseSet(String eid) {
671: CourseSetCmImpl cs = (CourseSetCmImpl) getObjectByEid(eid,
672: CourseSetCmImpl.class.getName());
673:
674: // Remove the memberships
675: for (Iterator iter = getMemberships(cs).iterator(); iter
676: .hasNext();) {
677: getHibernateTemplate().delete(iter.next());
678: }
679:
680: // Remove the course set itself
681: getHibernateTemplate().delete(cs);
682: }
683:
684: public void removeEnrollmentSet(String eid) {
685: EnrollmentSetCmImpl es = (EnrollmentSetCmImpl) getObjectByEid(
686: eid, EnrollmentSetCmImpl.class.getName());
687:
688: List enrollments = getHibernateTemplate()
689: .findByNamedQueryAndNamedParam("findEnrollments",
690: "enrollmentSetEid", eid);
691: for (Iterator iter = enrollments.iterator(); iter.hasNext();) {
692: getHibernateTemplate().delete(iter.next());
693: }
694:
695: // Remove the enrollment set
696: getHibernateTemplate().delete(es);
697: }
698:
699: public void removeSection(String eid) {
700: SectionCmImpl sec = (SectionCmImpl) getObjectByEid(eid,
701: SectionCmImpl.class.getName());
702:
703: // Remove the memberships
704: for (Iterator iter = getMemberships(sec).iterator(); iter
705: .hasNext();) {
706: getHibernateTemplate().delete(iter.next());
707: }
708:
709: // Remove the section itself
710: getHibernateTemplate().delete(sec);
711: }
712:
713: public SectionCategory addSectionCategory(String categoryCode,
714: String categoryDescription) {
715: SectionCategoryCmImpl cat = new SectionCategoryCmImpl(
716: categoryCode, categoryDescription);
717: getHibernateTemplate().save(cat);
718: return cat;
719: }
720:
721: // TODO: The following two methods were copied from CM Service. Consolidate them.
722:
723: /**
724: * A generic approach to finding objects by their eid. This is "coding by convention",
725: * since it expects the parameterized query to use "eid" as the single named parameter.
726: *
727: * @param eid The eid of the object we're trying to load
728: * @param className The name of the class / interface we're looking for
729: * @return The object, if found
730: * @throws IdNotFoundException
731: */
732: private Object getObjectByEid(final String eid,
733: final String className) throws IdNotFoundException {
734: HibernateCallback hc = new HibernateCallback() {
735: public Object doInHibernate(Session session)
736: throws HibernateException {
737: StringBuffer hql = new StringBuffer();
738: hql.append("from ").append(className).append(
739: " as obj where obj.eid=:eid");
740: Query q = session.createQuery(hql.toString());
741: q.setParameter("eid", eid);
742: Object result = q.uniqueResult();
743: if (result == null) {
744: throw new IdNotFoundException(eid, className);
745: }
746: return result;
747: }
748: };
749: return getHibernateTemplate().execute(hc);
750: }
751:
752: /**
753: * Gets the memberships for a membership container. This query can not be
754: * performed using just the container's eid, since it may conflict with other kinds
755: * of objects with the same eid.
756: *
757: * @param container
758: * @return
759: */
760: private Set<Membership> getMemberships(
761: final AbstractMembershipContainerCmImpl container) {
762: // This may be a dynamic proxy. In that case, make sure we're using the class
763: // that hibernate understands.
764: final String className = Hibernate.getClass(container)
765: .getName();
766:
767: HibernateCallback hc = new HibernateCallback() {
768: public Object doInHibernate(Session session)
769: throws HibernateException {
770: StringBuffer sb = new StringBuffer(
771: "select mbr from MembershipCmImpl as mbr, ");
772: sb.append(className);
773: sb
774: .append(" as container where mbr.memberContainer=container ");
775: sb.append("and container.eid=:eid");
776: Query q = session.createQuery(sb.toString());
777: q.setParameter("eid", container.getEid());
778: return q.list();
779: }
780: };
781: return new HashSet<Membership>(getHibernateTemplate()
782: .executeFind(hc));
783: }
784:
785: }
|