001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/postem/tags/sakai_2-4-1/postem-impl/src/java/org/sakaiproject/component/app/postem/GradebookManagerImpl.java $
003: * $Id: GradebookManagerImpl.java 14724 2006-09-15 16:00:15Z lance@indiana.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 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.component.app.postem;
021:
022: import java.io.Serializable;
023: import java.sql.SQLException;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.SortedSet;
027: import java.util.TreeSet;
028: import java.util.Comparator;
029:
030: import org.hibernate.Criteria;
031: import org.hibernate.FetchMode;
032: import org.hibernate.HibernateException;
033: import org.hibernate.Session;
034: import org.hibernate.criterion.Expression;
035: import org.sakaiproject.api.app.postem.data.Gradebook;
036: import org.sakaiproject.api.app.postem.data.GradebookManager;
037: import org.sakaiproject.api.app.postem.data.StudentGrades;
038: import org.sakaiproject.api.app.postem.data.Template;
039: import org.sakaiproject.component.app.postem.data.GradebookImpl;
040: import org.sakaiproject.component.app.postem.data.StudentGradesImpl;
041: import org.sakaiproject.component.app.postem.data.TemplateImpl;
042: import org.springframework.orm.hibernate3.HibernateCallback;
043: import org.springframework.orm.hibernate3.HibernateTemplate;
044: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
045:
046: public class GradebookManagerImpl extends HibernateDaoSupport implements
047: GradebookManager, Serializable {
048:
049: public static final String TITLE = "title";
050:
051: public static final String CONTEXT = "context";
052:
053: public static final String STUDENTS = "students";
054:
055: public static final String ID = "id";
056:
057: public static final String RELEASED = "released";
058:
059: public Gradebook createGradebook(String title, String creator,
060: String context, List headings, SortedSet students,
061: Template template) {
062: if (title == null || creator == null || context == null
063: || headings == null || students == null) {
064: throw new IllegalArgumentException("Null Argument");
065: } else {
066:
067: Gradebook grades = new GradebookImpl(title, creator,
068: context, headings, students, template);
069: Iterator si = students.iterator();
070: while (si.hasNext()) {
071: ((StudentGradesImpl) si.next()).setGradebook(grades);
072: }
073: saveGradebook(grades);
074: return grades;
075: }
076:
077: }
078:
079: public Gradebook createEmptyGradebook(String creator, String context) {
080: if (creator == null || context == null) {
081: throw new IllegalArgumentException("Null Argument");
082: } else {
083: Gradebook grades = new GradebookImpl("", creator, context,
084: null, null, null);
085: // saveGradebook(grades);
086:
087: return grades;
088: }
089:
090: }
091:
092: public StudentGrades createStudentGradesInGradebook(
093: String username, List grades, Gradebook gradebook) {
094: if (username == null || grades == null || gradebook == null) {
095: throw new IllegalArgumentException("Null Argument");
096: } else {
097: StudentGrades student = new StudentGradesImpl(username,
098: grades);
099: student.setGradebook(gradebook);
100: gradebook.getStudents().add(student);
101: return student;
102: }
103: }
104:
105: public StudentGrades createStudentGrades(String username,
106: List grades) {
107: return (StudentGrades) new StudentGradesImpl(username, grades);
108: }
109:
110: public Template createTemplate(String template) {
111: Template temp = new TemplateImpl();
112: temp.setTemplateCode(template);
113: return temp;
114: }
115:
116: public void deleteGradebook(final Gradebook gradebook) {
117: if (gradebook != null) {
118: Iterator si = gradebook.getStudents().iterator();
119: while (si.hasNext()) {
120: deleteStudentGrades((StudentGrades) si.next());
121: }
122: HibernateCallback hcb = new HibernateCallback() {
123: public Object doInHibernate(Session session)
124: throws HibernateException, SQLException {
125:
126: session.delete(gradebook);
127: return null;
128: }
129: };
130: getHibernateTemplate().execute(hcb);
131:
132: }
133:
134: }
135:
136: public void deleteStudentGrades(final StudentGrades student) {
137: if (student != null) {
138: HibernateCallback hcb = new HibernateCallback() {
139: public Object doInHibernate(Session session)
140: throws HibernateException, SQLException {
141:
142: session.delete(student);
143: return null;
144: }
145: };
146: getHibernateTemplate().execute(hcb);
147:
148: }
149: }
150:
151: public Gradebook getGradebookByTitleAndContext(final String title,
152: final String context) {
153: if (title == null || context == null) {
154: throw new IllegalArgumentException("Null Argument");
155: } else {
156: HibernateCallback hcb = new HibernateCallback() {
157: public Object doInHibernate(Session session)
158: throws HibernateException, SQLException {
159:
160: Criteria crit = session.createCriteria(
161: GradebookImpl.class).add(
162: Expression.eq(TITLE, title)).add(
163: Expression.eq(CONTEXT, context))
164: .setFetchMode(STUDENTS, FetchMode.EAGER);
165:
166: Gradebook gradebook = (Gradebook) crit
167: .uniqueResult();
168:
169: return gradebook;
170: }
171: };
172: return (Gradebook) getHibernateTemplate().execute(hcb);
173: }
174:
175: }
176:
177: public SortedSet getGradebooksByContext(final String context,
178: final String sortBy, final boolean ascending) {
179: if (context == null) {
180: throw new IllegalArgumentException("Null Argument");
181: } else {
182: HibernateCallback hcb = new HibernateCallback() {
183: public Object doInHibernate(Session session)
184: throws HibernateException, SQLException {
185:
186: Criteria crit = session.createCriteria(
187: GradebookImpl.class).add(
188: Expression.eq(CONTEXT, context));
189:
190: List gbs = crit.list();
191:
192: Comparator gbComparator = determineComparator(
193: sortBy, ascending);
194:
195: SortedSet gradebooks = new TreeSet(gbComparator);
196:
197: Iterator gbIterator = gbs.iterator();
198:
199: while (gbIterator.hasNext()) {
200: gradebooks.add((Gradebook) gbIterator.next());
201:
202: }
203:
204: return gradebooks;
205: }
206: };
207:
208: return (SortedSet) getHibernateTemplate().execute(hcb);
209: }
210: }
211:
212: public SortedSet getReleasedGradebooksByContext(
213: final String context, final String sortBy,
214: final boolean ascending) {
215: if (context == null) {
216: throw new IllegalArgumentException("Null Argument");
217: } else {
218: HibernateCallback hcb = new HibernateCallback() {
219: public Object doInHibernate(Session session)
220: throws HibernateException, SQLException {
221:
222: Criteria crit = session.createCriteria(
223: GradebookImpl.class).add(
224: Expression.eq(CONTEXT, context)).add(
225: Expression.eq(RELEASED, new Boolean(true)));
226:
227: List gbs = crit.list();
228:
229: Comparator gbComparator = determineComparator(
230: sortBy, ascending);
231:
232: SortedSet gradebooks = new TreeSet(gbComparator);
233:
234: Iterator gbIterator = gbs.iterator();
235:
236: while (gbIterator.hasNext()) {
237: gradebooks.add((Gradebook) gbIterator.next());
238:
239: }
240:
241: return gradebooks;
242: }
243: };
244:
245: return (SortedSet) getHibernateTemplate().execute(hcb);
246: }
247: }
248:
249: public SortedSet getStudentGradesForGradebook(
250: final Gradebook gradebook) throws IllegalArgumentException {
251: if (gradebook == null) {
252: throw new IllegalArgumentException("Null Argument");
253: } else {
254: HibernateCallback hcb = new HibernateCallback() {
255: public Object doInHibernate(Session session)
256: throws HibernateException, SQLException {
257: // get syllabi in an eager fetch mode
258: Criteria crit = session.createCriteria(
259: Gradebook.class).add(
260: Expression.eq(ID, gradebook.getId()))
261: .setFetchMode(STUDENTS, FetchMode.EAGER);
262:
263: Gradebook grades = (Gradebook) crit.uniqueResult();
264:
265: if (grades != null) {
266: return grades.getStudents();
267: }
268: return new TreeSet();
269: }
270: };
271: return (SortedSet) getHibernateTemplate().execute(hcb);
272: }
273: }
274:
275: public void saveGradebook(Gradebook gradebook)
276: throws IllegalArgumentException {
277: if (gradebook == null) {
278: throw new IllegalArgumentException("Null Argument");
279: } else {
280: HibernateTemplate temp = getHibernateTemplate();
281: temp.saveOrUpdate(gradebook);
282: /*
283: * Iterator iter = gradebook.getStudents().iterator(); while
284: * (iter.hasNext()) { temp.saveOrUpdate((StudentGradesImpl) iter.next()); }
285: */
286: }
287: }
288:
289: public void updateGrades(Gradebook gradebook, List headings,
290: SortedSet students) {
291: gradebook.setHeadings(headings);
292: gradebook.setStudents(students);
293: getHibernateTemplate().saveOrUpdate(gradebook);
294: }
295:
296: public void updateTemplate(Gradebook gradebook, String template) {
297: gradebook.setTemplate(createTemplate(template));
298: getHibernateTemplate().saveOrUpdate(gradebook);
299: }
300:
301: private Comparator determineComparator(String sortBy,
302: boolean ascending) {
303: if (ascending) {
304: if (sortBy.equals(Gradebook.SORT_BY_CREATOR)) {
305: return GradebookImpl.CreatorAscComparator;
306: } else if (sortBy.equals(Gradebook.SORT_BY_MOD_BY)) {
307: return GradebookImpl.ModByAscComparator;
308: } else if (sortBy.equals(Gradebook.SORT_BY_MOD_DATE)) {
309: return GradebookImpl.ModDateAscComparator;
310: } else if (sortBy.equals(Gradebook.SORT_BY_RELEASED)) {
311: return GradebookImpl.ReleasedAscComparator;
312: } else {
313: return GradebookImpl.TitleAscComparator;
314: }
315: } else {
316: if (sortBy.equals(Gradebook.SORT_BY_CREATOR)) {
317: return GradebookImpl.CreatorDescComparator;
318: } else if (sortBy.equals(Gradebook.SORT_BY_MOD_BY)) {
319: return GradebookImpl.ModByDescComparator;
320: } else if (sortBy.equals(Gradebook.SORT_BY_MOD_DATE)) {
321: return GradebookImpl.ModDateDescComparator;
322: } else if (sortBy.equals(Gradebook.SORT_BY_RELEASED)) {
323: return GradebookImpl.ReleasedDescComparator;
324: } else {
325: return GradebookImpl.TitleDescComparator;
326: }
327: }
328: }
329:
330: }
|