001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-impl/integration-support/src/java/org/sakaiproject/component/section/support/IntegrationSupportImpl.java $
003: * $Id: IntegrationSupportImpl.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.support;
021:
022: import java.sql.SQLException;
023: import java.sql.Time;
024: import java.util.List;
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.SectionManager;
036: import org.sakaiproject.section.api.coursemanagement.Course;
037: import org.sakaiproject.section.api.coursemanagement.CourseSection;
038: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
039: import org.sakaiproject.section.api.coursemanagement.User;
040: import org.sakaiproject.section.api.exception.RoleConfigurationException;
041: import org.sakaiproject.section.api.facade.Role;
042: import org.sakaiproject.component.section.EnrollmentRecordImpl;
043: import org.sakaiproject.component.section.InstructorRecordImpl;
044: import org.sakaiproject.component.section.TeachingAssistantRecordImpl;
045: import org.springframework.orm.hibernate3.HibernateCallback;
046: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
047:
048: /**
049: * Provides integration support using the standalone hibernate implementation.
050: */
051: public class IntegrationSupportImpl extends HibernateDaoSupport
052: implements IntegrationSupport {
053: private static final Log log = LogFactory
054: .getLog(IntegrationSupportImpl.class);
055:
056: private CourseManager courseManager;
057: private SectionManager sectionManager;
058: private UserManager userManager;
059: private IdManager uuidManager;
060:
061: public Course createCourse(String siteContext, String title,
062: boolean externallyManaged, boolean selfRegistrationAllowed,
063: boolean selfSwitchingAllowed) {
064: return courseManager.createCourse(siteContext, title,
065: selfRegistrationAllowed, selfSwitchingAllowed,
066: externallyManaged);
067: }
068:
069: public CourseSection createSection(String courseUuid, String title,
070: String category, Integer maxEnrollments, String location,
071: Time startTime, Time endTime, boolean monday,
072: boolean tuesday, boolean wednesday, boolean thursday,
073: boolean friday, boolean saturday, boolean sunday) {
074: return sectionManager.addSection(courseUuid, title, category,
075: maxEnrollments, location, startTime, endTime, monday,
076: tuesday, wednesday, thursday, friday, saturday, sunday);
077: }
078:
079: public User createUser(String userUid, String displayName,
080: String sortName, String displayId) {
081: return userManager.createUser(userUid, displayName, sortName,
082: displayId);
083: }
084:
085: public User findUser(final String userUid) {
086: return userManager.findUser(userUid);
087: }
088:
089: public List getAllSiteMemberships(final String userUid) {
090: HibernateCallback hc = new HibernateCallback() {
091: public Object doInHibernate(Session session)
092: throws HibernateException, SQLException {
093: Query q = session
094: .getNamedQuery("getUsersSiteMemberships");
095: q.setParameter("userUid", userUid);
096: return q.list();
097: }
098: };
099: return getHibernateTemplate().executeFind(hc);
100: }
101:
102: public Set getAllSectionMemberships(String userUid,
103: String siteContext) {
104: Course course = sectionManager.getCourse(siteContext);
105: return sectionManager.getSectionEnrollments(userUid, course
106: .getUuid());
107: }
108:
109: public ParticipationRecord addSiteMembership(String userUid,
110: String siteContext, Role role) {
111: User user = findUser(userUid);
112: Course course = sectionManager.getCourse(siteContext);
113:
114: ParticipationRecord record = null;
115: if (role.isInstructor()) {
116: InstructorRecordImpl ir = new InstructorRecordImpl(course,
117: user);
118: ir.setUuid(uuidManager.createUuid());
119: getHibernateTemplate().save(ir);
120: record = ir;
121: } else if (role.isTeachingAssistant()) {
122: TeachingAssistantRecordImpl tar = new TeachingAssistantRecordImpl(
123: course, user);
124: tar.setUuid(uuidManager.createUuid());
125: getHibernateTemplate().save(tar);
126: record = tar;
127: } else if (role.isStudent()) {
128: EnrollmentRecordImpl sr = new EnrollmentRecordImpl(course,
129: null, user);
130: sr.setUuid(uuidManager.createUuid());
131: getHibernateTemplate().save(sr);
132: record = sr;
133: } else {
134: throw new RuntimeException(
135: "You can not add a user without a role");
136: }
137: return record;
138: }
139:
140: public void removeSiteMembership(final String userUid,
141: final String siteContext) {
142: HibernateCallback hc = new HibernateCallback() {
143: public Object doInHibernate(Session session)
144: throws HibernateException, SQLException {
145: log.info("getting query object");
146: Query q = session
147: .getNamedQuery("loadSiteParticipation");
148: log.info("query = " + q);
149: q.setParameter("userUid", userUid);
150: q.setParameter("siteContext", siteContext);
151: return q.uniqueResult();
152: }
153: };
154: ParticipationRecord record = (ParticipationRecord) getHibernateTemplate()
155: .execute(hc);
156: if (record != null) {
157: log.info("Preparing to delete record " + record);
158: getHibernateTemplate().delete(record);
159: }
160: }
161:
162: public ParticipationRecord addSectionMembership(String userUid,
163: String sectionUuid, Role role) {
164: ParticipationRecord record;
165: try {
166: record = sectionManager.addSectionMembership(userUid, role,
167: sectionUuid);
168: return record;
169: } catch (RoleConfigurationException rce) {
170: throw new RuntimeException(rce);
171: }
172: }
173:
174: public void removeSectionMembership(String userUid,
175: String sectionUuid) {
176: sectionManager.dropSectionMembership(userUid, sectionUuid);
177: }
178:
179: // Dependency Injection
180:
181: public void setSectionManager(SectionManager sectionManager) {
182: this .sectionManager = sectionManager;
183: }
184:
185: public void setCourseManager(CourseManager courseManager) {
186: this .courseManager = courseManager;
187: }
188:
189: public void setUserManager(UserManager userManager) {
190: this .userManager = userManager;
191: }
192:
193: public void setUuidManager(IdManager uuidManager) {
194: this.uuidManager = uuidManager;
195: }
196: }
|