001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/course-management/tags/sakai_2-4-1/cm-impl/hibernate-impl/impl/src/test/org/sakaiproject/coursemanagement/test/HibernateTestDataLoader.java $
003: * $Id: HibernateTestDataLoader.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.test;
021:
022: import java.util.Date;
023: import java.util.HashSet;
024: import java.util.Set;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.coursemanagement.api.AcademicSession;
029: import org.sakaiproject.coursemanagement.api.CourseManagementService;
030: import org.sakaiproject.coursemanagement.api.CourseOffering;
031: import org.sakaiproject.coursemanagement.api.EnrollmentSet;
032: import org.sakaiproject.coursemanagement.impl.DataLoader;
033: import org.sakaiproject.coursemanagement.impl.AcademicSessionCmImpl;
034: import org.sakaiproject.coursemanagement.impl.CanonicalCourseCmImpl;
035: import org.sakaiproject.coursemanagement.impl.CourseOfferingCmImpl;
036: import org.sakaiproject.coursemanagement.impl.CourseSetCmImpl;
037: import org.sakaiproject.coursemanagement.impl.CrossListingCmImpl;
038: import org.sakaiproject.coursemanagement.impl.EnrollmentCmImpl;
039: import org.sakaiproject.coursemanagement.impl.EnrollmentSetCmImpl;
040: import org.sakaiproject.coursemanagement.impl.MembershipCmImpl;
041: import org.sakaiproject.coursemanagement.impl.SectionCategoryCmImpl;
042: import org.sakaiproject.coursemanagement.impl.SectionCmImpl;
043: import org.springframework.context.support.ClassPathXmlApplicationContext;
044: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
045:
046: /**
047: * Loads data into the current transaction for use in a test case.
048: *
049: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
050: */
051: public class HibernateTestDataLoader extends HibernateDaoSupport
052: implements DataLoader {
053: private static final Log log = LogFactory
054: .getLog(HibernateTestDataLoader.class);
055:
056: private CourseManagementService cm;
057:
058: public void setCourseManagementService(CourseManagementService cm) {
059: this .cm = cm;
060: }
061:
062: public static void main(String[] args) {
063: ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(
064: new String[] { "spring-test.xml",
065: "spring-config-test.xml" });
066: DataLoader loader = (DataLoader) ac.getBean(DataLoader.class
067: .getName());
068: try {
069: loader.load();
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: }
074:
075: public void load() {
076: loadAcademicSessions();
077: loadCourseSetsAndMembers();
078: loadCanonicalCourses();
079: loadCourseOfferingsAndMembers();
080: loadSectionsAndMembers();
081: loadEnrollmentSets();
082: loadEnrollments();
083:
084: getHibernateTemplate().flush();
085: getHibernateTemplate().clear();
086: }
087:
088: void loadAcademicSessions() {
089: AcademicSessionCmImpl term = new AcademicSessionCmImpl();
090: term.setEid("F2006");
091: term.setTitle("Fall 2006");
092: term.setDescription("Fall 2006, starts Sept 1, 2006");
093: getHibernateTemplate().save(term);
094: }
095:
096: void loadCourseSetsAndMembers() {
097: CourseSetCmImpl cSet = new CourseSetCmImpl("BIO_DEPT",
098: "Biology Department", "Department of Biology", "DEPT",
099: null);
100: getHibernateTemplate().save(cSet);
101:
102: MembershipCmImpl courseSetMember = new MembershipCmImpl();
103: courseSetMember.setRole("departmentAdmin");
104: courseSetMember.setUserId("user1");
105: courseSetMember.setMemberContainer(cSet);
106: courseSetMember.setStatus("active");
107: getHibernateTemplate().save(courseSetMember);
108:
109: CourseSetCmImpl cSetChild = new CourseSetCmImpl(
110: "BIO_CHEM_GROUP", "Biochem Group",
111: "Biochemistry group, Department of Biology",
112: "DEPT_GROUP", cSet);
113: getHibernateTemplate().save(cSetChild);
114:
115: CourseSetCmImpl cSetEmpty = new CourseSetCmImpl(
116: "EMPTY_COURSE_SET", "Empty CourseSet",
117: "Empty CourseSet", null, null);
118: getHibernateTemplate().save(cSetEmpty);
119: }
120:
121: void loadCanonicalCourses() {
122: // Cross-list bio and chem (but not English)
123: CrossListingCmImpl cl = new CrossListingCmImpl();
124: getHibernateTemplate().save(cl);
125:
126: // Build and save the CanonicalCourses
127: CanonicalCourseCmImpl cc1 = new CanonicalCourseCmImpl();
128: cc1.setEid("BIO101");
129: cc1.setTitle("Biology 101");
130: cc1.setDescription("An intro to biology");
131: cc1.setCrossListing(cl);
132: getHibernateTemplate().save(cc1);
133:
134: CanonicalCourseCmImpl cc2 = new CanonicalCourseCmImpl();
135: cc2.setEid("CHEM101");
136: cc2.setTitle("Chem 101");
137: cc2.setDescription("An intro to chemistry");
138: cc2.setCrossListing(cl);
139: getHibernateTemplate().save(cc2);
140:
141: CanonicalCourseCmImpl cc3 = new CanonicalCourseCmImpl();
142: cc3.setEid("ENG101");
143: cc3.setTitle("English 101");
144: cc3.setDescription("An intro to English");
145: getHibernateTemplate().save(cc3);
146:
147: // Add these canonical courses to course sets
148: CourseSetCmImpl bioCset = (CourseSetCmImpl) cm
149: .getCourseSet("BIO_DEPT");
150: CourseSetCmImpl bioChemCset = (CourseSetCmImpl) cm
151: .getCourseSet("BIO_CHEM_GROUP");
152:
153: Set bioCourses = new HashSet();
154: bioCourses.add(cc1);
155: bioCset.setCanonicalCourses(bioCourses);
156:
157: getHibernateTemplate().update(bioCset);
158:
159: Set bioChemCourses = new HashSet();
160: bioChemCourses.add(cc1);
161: bioChemCourses.add(cc2);
162: bioChemCset.setCanonicalCourses(bioChemCourses);
163: getHibernateTemplate().update(bioChemCset);
164: }
165:
166: void loadCourseOfferingsAndMembers() {
167: // Get the object dependencies
168: AcademicSession term = cm.getAcademicSession("F2006");
169: CanonicalCourseCmImpl cc1 = (CanonicalCourseCmImpl) cm
170: .getCanonicalCourse("BIO101");
171: CanonicalCourseCmImpl cc2 = (CanonicalCourseCmImpl) cm
172: .getCanonicalCourse("CHEM101");
173: CanonicalCourseCmImpl cc3 = (CanonicalCourseCmImpl) cm
174: .getCanonicalCourse("ENG101");
175:
176: // Cross list bio and chem, but not English
177: CrossListingCmImpl cl = new CrossListingCmImpl();
178: getHibernateTemplate().save(cl);
179:
180: CourseOfferingCmImpl co1 = new CourseOfferingCmImpl();
181: co1.setAcademicSession(term);
182: co1.setCanonicalCourse(cc1);
183: co1.setCrossListing(cl);
184: co1.setEid("BIO101_F2006_01");
185: co1.setTitle("Bio 101: It's all about the gene");
186: co1.setDescription("Fall 2006 Bio 101 Offering");
187:
188: // Make this almost always a "current" course offering (until 2020, that is)
189: co1.setStartDate(new Date(0));
190: co1.setEndDate(new Date(1577836800000L));
191:
192: getHibernateTemplate().save(co1);
193:
194: CourseOfferingCmImpl co2 = new CourseOfferingCmImpl();
195: co2.setAcademicSession(term);
196: co2.setCanonicalCourse(cc2);
197: co2.setCrossListing(cl);
198: co2.setEid("CHEM101_F2006_01");
199: co2.setTitle("Chem 101: It's all about the gene");
200: co2.setDescription("Fall 2006 Chem 101 Offering");
201:
202: // Make this almost never a "current" course offering (until 2020, that is)
203: co2.setStartDate(new Date(1577836800000L));
204: co2.setEndDate(new Date(1577836800000L));
205:
206: getHibernateTemplate().save(co2);
207:
208: CourseOfferingCmImpl co3 = new CourseOfferingCmImpl();
209: co3.setAcademicSession(term);
210: co3.setCanonicalCourse(cc3);
211: co3.setEid("ENG101_F2006_01");
212: co3.setTitle("English 101: Intro to literature");
213: co3.setDescription("Fall 2006 Eng 101 Offering");
214: getHibernateTemplate().save(co3);
215:
216: // Add these course offerings to course sets
217: CourseSetCmImpl bioCset = (CourseSetCmImpl) cm
218: .getCourseSet("BIO_DEPT");
219: CourseSetCmImpl bioChemCset = (CourseSetCmImpl) cm
220: .getCourseSet("BIO_CHEM_GROUP");
221:
222: Set bioCourses = new HashSet();
223: bioCourses.add(co1);
224: bioCset.setCourseOfferings(bioCourses);
225:
226: getHibernateTemplate().update(bioCset);
227:
228: Set bioChemCourses = new HashSet();
229: bioChemCourses.add(co1);
230: bioChemCourses.add(co2);
231: bioChemCset.setCourseOfferings(bioChemCourses);
232: getHibernateTemplate().update(bioChemCset);
233:
234: // Add a member to some CourseOfferings
235: MembershipCmImpl member1 = new MembershipCmImpl("coUser",
236: "coRole1", co1, "active");
237: getHibernateTemplate().save(member1);
238:
239: MembershipCmImpl member2 = new MembershipCmImpl("coUser",
240: "coRole2", co2, "active");
241: getHibernateTemplate().save(member2);
242: }
243:
244: void loadSectionsAndMembers() {
245: CourseOffering co = cm.getCourseOffering("BIO101_F2006_01");
246:
247: // Add the section categories
248: getHibernateTemplate().save(
249: new SectionCategoryCmImpl("lct", "Lecture"));
250: getHibernateTemplate().save(
251: new SectionCategoryCmImpl("lab", "Lab"));
252: getHibernateTemplate().save(
253: new SectionCategoryCmImpl("dis", "Discussion"));
254:
255: // Add a section
256: SectionCmImpl section = new SectionCmImpl();
257: section.setCategory("lct");
258: section.setCourseOffering(co);
259: section.setDescription("The lecture");
260: section.setEid("BIO101_F2006_01_SEC01");
261: section.setTitle("Main lecture");
262: section.setMaxSize(new Integer(100));
263: getHibernateTemplate().save(section);
264:
265: // Add a membership to this section
266: MembershipCmImpl member = new MembershipCmImpl();
267: member.setRole("AN_ENTERPRISE_ROLE");
268: member.setUserId("AN_ENTERPRISE_USER");
269: member.setMemberContainer(section);
270: member.setStatus("active");
271: getHibernateTemplate().save(member);
272:
273: // Add a child section
274: SectionCmImpl childSection = new SectionCmImpl();
275: childSection.setCategory("lab");
276: childSection.setCourseOffering(co);
277: childSection.setDescription("Joe's monday morning lab");
278: childSection.setEid("BIO101_F2006_01_SEC02");
279: childSection.setTitle("Joe's Monday Morning Biology Lab");
280: childSection.setParent(section);
281: childSection.setMaxSize(new Integer(100));
282: getHibernateTemplate().save(childSection);
283:
284: // Add a section for the future course offering
285: CourseOffering futureCo = cm
286: .getCourseOffering("CHEM101_F2006_01");
287:
288: SectionCmImpl futureSection = new SectionCmImpl();
289: futureSection.setCategory("lab");
290: futureSection.setCourseOffering(futureCo);
291: futureSection.setDescription("Future lab");
292: futureSection.setEid("CHEM101_F2006_01_SEC01");
293: futureSection.setTitle("Future Lab");
294: futureSection.setMaxSize(new Integer(100));
295: getHibernateTemplate().save(futureSection);
296:
297: // Add a member to this future section
298: MembershipCmImpl member2 = new MembershipCmImpl();
299: member2.setRole("student");
300: member2.setUserId("josh");
301: member2.setMemberContainer(futureSection);
302: member2.setStatus("active");
303: getHibernateTemplate().save(member2);
304: }
305:
306: void loadEnrollmentSets() {
307: EnrollmentSetCmImpl enrollmentSet1 = new EnrollmentSetCmImpl();
308: enrollmentSet1.setCategory("lab");
309: enrollmentSet1.setCourseOffering(cm
310: .getCourseOffering("BIO101_F2006_01"));
311: enrollmentSet1.setDefaultEnrollmentCredits("3");
312: enrollmentSet1.setDescription("An enrollment set description");
313: enrollmentSet1.setEid("BIO101_F2006_01_ES01");
314: enrollmentSet1.setTitle("The lab enrollment set");
315: Set officialInstructors = new HashSet();
316: officialInstructors.add("grader1");
317: officialInstructors.add("grader2");
318: enrollmentSet1.setOfficialInstructors(officialInstructors);
319: getHibernateTemplate().save(enrollmentSet1);
320:
321: SectionCmImpl section1 = (SectionCmImpl) cm
322: .getSection("BIO101_F2006_01_SEC01");
323: section1.setEnrollmentSet(enrollmentSet1);
324: getHibernateTemplate().update(section1);
325:
326: EnrollmentSetCmImpl enrollmentSet2 = new EnrollmentSetCmImpl();
327: enrollmentSet2.setCategory("lab");
328: enrollmentSet2.setCourseOffering(cm
329: .getCourseOffering("CHEM101_F2006_01"));
330: enrollmentSet2.setDefaultEnrollmentCredits("3");
331: enrollmentSet2
332: .setDescription("Another enrollment set description");
333: enrollmentSet2.setEid("CHEM101_F2006_01_ES01");
334: enrollmentSet2.setTitle("The lab enrollment set");
335:
336: getHibernateTemplate().save(enrollmentSet2);
337:
338: SectionCmImpl section2 = (SectionCmImpl) cm
339: .getSection("CHEM101_F2006_01_SEC01");
340: section2.setEnrollmentSet(enrollmentSet2);
341: getHibernateTemplate().update(section2);
342: }
343:
344: void loadEnrollments() {
345: EnrollmentSet enrollmentSet = cm
346: .getEnrollmentSet("BIO101_F2006_01_ES01");
347: EnrollmentCmImpl enrollment = new EnrollmentCmImpl();
348: enrollment.setCredits("3");
349: enrollment.setEnrollmentSet(enrollmentSet);
350: enrollment.setEnrollmentStatus("waitlisted");
351: enrollment.setGradingScheme("pass/fail");
352: enrollment.setUserId("josh");
353: getHibernateTemplate().save(enrollment);
354:
355: EnrollmentSet enrollmentSet2 = cm
356: .getEnrollmentSet("CHEM101_F2006_01_ES01");
357: EnrollmentCmImpl enrollment2 = new EnrollmentCmImpl();
358: enrollment2.setCredits("3");
359: enrollment2.setEnrollmentSet(enrollmentSet2);
360: enrollment2.setEnrollmentStatus("officially enrolled");
361: enrollment2.setGradingScheme("letter grade");
362: enrollment2.setUserId("josh");
363: enrollment2.setDropped(true);
364: getHibernateTemplate().save(enrollment2);
365:
366: }
367: }
|