001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-impl/standalone/src/java/org/sakaiproject/component/section/CourseManagerHibernateImpl.java $
003: * $Id: CourseManagerHibernateImpl.java 18134 2006-11-14 18:59:25Z 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.sql.SQLException;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Set;
026:
027: import org.hibernate.HibernateException;
028: import org.hibernate.Query;
029: import org.hibernate.Session;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.sakaiproject.id.api.IdManager;
034: import org.sakaiproject.section.api.CourseManager;
035: import org.sakaiproject.section.api.coursemanagement.Course;
036: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
037: import org.sakaiproject.section.api.coursemanagement.User;
038: import org.springframework.orm.hibernate3.HibernateCallback;
039: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
040:
041: /**
042: * Hibernate implementation of CourseManager. Useful for loading data in standalone mode.
043: *
044: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
045: *
046: */
047: public class CourseManagerHibernateImpl extends HibernateDaoSupport
048: implements CourseManager {
049:
050: private static final Log log = LogFactory
051: .getLog(CourseManagerHibernateImpl.class);
052:
053: protected IdManager uuidManager;
054:
055: public Course createCourse(final String siteContext,
056: final String title, final boolean selfRegAllowed,
057: final boolean selfSwitchingAllowed,
058: final boolean externallyManaged) {
059:
060: if (log.isDebugEnabled())
061: log.debug("Creating a new course offering named " + title);
062:
063: HibernateCallback hc = new HibernateCallback() {
064: public Object doInHibernate(Session session)
065: throws HibernateException, SQLException {
066: CourseImpl course = new CourseImpl();
067: course.setExternallyManaged(externallyManaged);
068: course.setSelfRegistrationAllowed(selfRegAllowed);
069: course.setSelfSwitchingAllowed(selfSwitchingAllowed);
070: course.setSiteContext(siteContext);
071: course.setTitle(title);
072: course.setUuid(uuidManager.createUuid());
073: session.save(course);
074: return course;
075: };
076: };
077:
078: return (Course) getHibernateTemplate().execute(hc);
079: }
080:
081: public boolean courseExists(final String siteContext) {
082: HibernateCallback hc = new HibernateCallback() {
083: public Object doInHibernate(Session session)
084: throws HibernateException, SQLException {
085: Query q = session
086: .getNamedQuery("loadCourseBySiteContext");
087: q.setParameter("siteContext", siteContext);
088: return q.uniqueResult();
089: };
090: };
091:
092: return getHibernateTemplate().execute(hc) != null;
093: }
094:
095: public ParticipationRecord addInstructor(final User user,
096: final Course course) {
097: HibernateCallback hc = new HibernateCallback() {
098: public Object doInHibernate(Session session)
099: throws HibernateException, SQLException {
100: InstructorRecordImpl pr = new InstructorRecordImpl(
101: course, user);
102: pr.setUuid(uuidManager.createUuid());
103: session.save(pr);
104: return pr;
105: }
106: };
107: return (ParticipationRecord) getHibernateTemplate().execute(hc);
108: }
109:
110: public ParticipationRecord addEnrollment(final User user,
111: final Course course) {
112: HibernateCallback hc = new HibernateCallback() {
113: public Object doInHibernate(Session session)
114: throws HibernateException, SQLException {
115: EnrollmentRecordImpl enr = new EnrollmentRecordImpl(
116: course, "enrolled", user);
117: enr.setUuid(uuidManager.createUuid());
118: session.save(enr);
119: return enr;
120: }
121: };
122: return (ParticipationRecord) getHibernateTemplate().execute(hc);
123: }
124:
125: public ParticipationRecord addTA(final User user,
126: final Course course) {
127: HibernateCallback hc = new HibernateCallback() {
128: public Object doInHibernate(Session session)
129: throws HibernateException, SQLException {
130: TeachingAssistantRecordImpl ta = new TeachingAssistantRecordImpl(
131: course, user);
132: ta.setUuid(uuidManager.createUuid());
133: session.save(ta);
134: return ta;
135: }
136: };
137: return (ParticipationRecord) getHibernateTemplate().execute(hc);
138: }
139:
140: public void removeCourseMembership(final String userUid,
141: final Course course) {
142: HibernateCallback hc = new HibernateCallback() {
143: public Object doInHibernate(Session session)
144: throws HibernateException, SQLException {
145: Query q = session
146: .getNamedQuery("loadSiteParticipation");
147: q.setParameter("siteContext", course.getSiteContext());
148: q.setParameter("userUid", userUid);
149: Object result = q.uniqueResult();
150: if (result != null) {
151: session.delete(result);
152: if (log.isInfoEnabled())
153: log.info("Site membership for " + userUid
154: + " in course " + course
155: + " has been deleted");
156: } else {
157: if (log.isInfoEnabled())
158: log.info("Could not find membership for "
159: + userUid + " in course " + course);
160: }
161: return null;
162: }
163: };
164: getHibernateTemplate().execute(hc);
165:
166: // Make sure we remove any orphaned section memberships
167: removeOrphans(course.getSiteContext());
168:
169: }
170:
171: /**
172: * @inheritDoc
173: */
174: public void removeOrphans(final String siteContext) {
175: final Set userUids = getSiteMemberIds(siteContext);
176: if (userUids == null || userUids.isEmpty()) {
177: return;
178: }
179: HibernateCallback hc = new HibernateCallback() {
180: public Object doInHibernate(Session session)
181: throws HibernateException, SQLException {
182: Query q = session
183: .getNamedQuery("findOrphanedSectionMemberships");
184: q.setParameter("siteContext", siteContext);
185: q.setParameterList("userUids", userUids);
186: int deleted = 0;
187: for (Iterator iter = q.iterate(); iter.hasNext(); deleted++) {
188: session.delete(iter.next());
189: }
190: if (log.isInfoEnabled())
191: log.info(deleted + " section memberships deleted");
192: return null;
193: }
194: };
195: getHibernateTemplate().execute(hc);
196: }
197:
198: /**
199: * Gets only the user IDs of the site members
200: *
201: * @param siteContext
202: * @return
203: */
204: private Set getSiteMemberIds(final String siteContext) {
205: HibernateCallback hc = new HibernateCallback() {
206: public Object doInHibernate(Session session)
207: throws HibernateException, SQLException {
208: Query q = session
209: .getNamedQuery("findSiteMemberUserUids");
210: q.setParameter("siteContext", siteContext);
211: return new HashSet(q.list());
212: };
213: };
214:
215: return (Set) getHibernateTemplate().execute(hc);
216: }
217:
218: // Dependency injection
219:
220: public void setUuidManager(IdManager uuidManager) {
221: this.uuidManager = uuidManager;
222: }
223:
224: }
|