Source Code Cross Referenced for SectionManagerHibernateImpl.java in  » ERP-CRM-Financial » sakai » org » sakaiproject » component » section » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » sakai » org.sakaiproject.component.section 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**********************************************************************************
0002:         * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-impl/standalone/src/java/org/sakaiproject/component/section/SectionManagerHibernateImpl.java $
0003:         * $Id: SectionManagerHibernateImpl.java 20230 2007-01-10 01:13:02Z jholtzman@berkeley.edu $
0004:         ***********************************************************************************
0005:         *
0006:         * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
0007:         * 
0008:         * Licensed under the Educational Community License, Version 1.0 (the "License"); 
0009:         * you may not use this file except in compliance with the License. 
0010:         * You may obtain a copy of the License at
0011:         * 
0012:         *      http://www.opensource.org/licenses/ecl1.php
0013:         * 
0014:         * Unless required by applicable law or agreed to in writing, software 
0015:         * distributed under the License is distributed on an "AS IS" BASIS, 
0016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
0017:         * See the License for the specific language governing permissions and 
0018:         * limitations under the License.
0019:         *
0020:         **********************************************************************************/package org.sakaiproject.component.section;
0021:
0022:        import java.sql.Time;
0023:        import java.util.ArrayList;
0024:        import java.util.Collection;
0025:        import java.util.Collections;
0026:        import java.util.Enumeration;
0027:        import java.util.HashSet;
0028:        import java.util.Iterator;
0029:        import java.util.List;
0030:        import java.util.Locale;
0031:        import java.util.MissingResourceException;
0032:        import java.util.ResourceBundle;
0033:        import java.util.Set;
0034:
0035:        import javax.servlet.ServletContext;
0036:        import javax.servlet.http.HttpServletRequest;
0037:
0038:        import org.hibernate.HibernateException;
0039:        import org.hibernate.Query;
0040:        import org.hibernate.Session;
0041:
0042:        import org.apache.commons.logging.Log;
0043:        import org.apache.commons.logging.LogFactory;
0044:        import org.sakaiproject.id.api.IdManager;
0045:        import org.sakaiproject.section.api.SectionManager;
0046:        import org.sakaiproject.section.api.coursemanagement.Course;
0047:        import org.sakaiproject.section.api.coursemanagement.CourseSection;
0048:        import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
0049:        import org.sakaiproject.section.api.coursemanagement.Meeting;
0050:        import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
0051:        import org.sakaiproject.section.api.coursemanagement.SectionEnrollments;
0052:        import org.sakaiproject.section.api.coursemanagement.User;
0053:        import org.sakaiproject.section.api.exception.MembershipException;
0054:        import org.sakaiproject.section.api.facade.Role;
0055:        import org.sakaiproject.section.api.facade.manager.Authn;
0056:        import org.sakaiproject.section.api.facade.manager.Context;
0057:        import org.springframework.orm.hibernate3.HibernateCallback;
0058:        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0059:
0060:        /**
0061:         * A standalone implementation of the Section Management API.
0062:         * 
0063:         * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
0064:         *
0065:         */
0066:        public class SectionManagerHibernateImpl extends HibernateDaoSupport
0067:                implements  SectionManager {
0068:
0069:            private static final Log log = LogFactory
0070:                    .getLog(SectionManagerHibernateImpl.class);
0071:
0072:            /** The resource bundle containing the category IDs and names */
0073:            static final String CATEGORY_BUNDLE = "org.sakaiproject.component.section.CourseSectionCategories";
0074:
0075:            // Fields configured via dep. injection
0076:            protected IdManager uuidManager;
0077:            protected Authn authn;
0078:            protected Context context;
0079:
0080:            /**
0081:             * {@inheritDoc}
0082:             */
0083:            public List<CourseSection> getSections(final String siteContext) {
0084:                if (log.isDebugEnabled())
0085:                    log.debug("Getting sections for context " + siteContext);
0086:                return getHibernateTemplate().findByNamedQueryAndNamedParam(
0087:                        "findSectionsBySiteContext", "context", siteContext);
0088:            }
0089:
0090:            /**
0091:             * {@inheritDoc}
0092:             */
0093:            public List<CourseSection> getSectionsInCategory(
0094:                    final String siteContext, final String categoryId) {
0095:                HibernateCallback hc = new HibernateCallback() {
0096:                    public Object doInHibernate(Session session)
0097:                            throws HibernateException {
0098:                        Query q = session
0099:                                .getNamedQuery("findSectionsByCategory");
0100:                        q.setParameter("categoryId", categoryId);
0101:                        q.setParameter("siteContext", siteContext);
0102:                        return q.list();
0103:                    }
0104:                };
0105:                return getHibernateTemplate().executeFind(hc);
0106:            }
0107:
0108:            /**
0109:             * {@inheritDoc}
0110:             */
0111:            public CourseSection getSection(final String sectionUuid) {
0112:                HibernateCallback hc = new HibernateCallback() {
0113:                    public Object doInHibernate(Session session)
0114:                            throws HibernateException {
0115:                        return getSection(sectionUuid, session);
0116:                    }
0117:                };
0118:                return (CourseSection) getHibernateTemplate().execute(hc);
0119:            }
0120:
0121:            /**
0122:             * {@inheritDoc}
0123:             */
0124:            public List<ParticipationRecord> getSiteInstructors(
0125:                    final String siteContext) {
0126:                HibernateCallback hc = new HibernateCallback() {
0127:                    public Object doInHibernate(Session session)
0128:                            throws HibernateException {
0129:                        Course course = getCourseFromSiteContext(siteContext,
0130:                                session);
0131:                        Query q = session.getNamedQuery("findSiteInstructors");
0132:                        q.setParameter("course", course);
0133:                        return q.list();
0134:                    }
0135:                };
0136:                return getHibernateTemplate().executeFind(hc);
0137:            }
0138:
0139:            /**
0140:             * {@inheritDoc}
0141:             */
0142:            public List<ParticipationRecord> getSiteTeachingAssistants(
0143:                    final String siteContext) {
0144:                HibernateCallback hc = new HibernateCallback() {
0145:                    public Object doInHibernate(Session session)
0146:                            throws HibernateException {
0147:                        Course course = getCourseFromSiteContext(siteContext,
0148:                                session);
0149:                        Query q = session.getNamedQuery("findSiteTAs");
0150:                        q.setParameter("course", course);
0151:                        return q.list();
0152:                    }
0153:                };
0154:                return getHibernateTemplate().executeFind(hc);
0155:            }
0156:
0157:            /**
0158:             * {@inheritDoc}
0159:             */
0160:            public List<EnrollmentRecord> getSiteEnrollments(
0161:                    final String siteContext) {
0162:                HibernateCallback hc = new HibernateCallback() {
0163:                    public Object doInHibernate(Session session)
0164:                            throws HibernateException {
0165:                        Course course = getCourseFromSiteContext(siteContext,
0166:                                session);
0167:                        Query q = session.getNamedQuery("findSiteEnrollments");
0168:                        q.setParameter("course", course);
0169:                        return q.list();
0170:                    }
0171:                };
0172:                return getHibernateTemplate().executeFind(hc);
0173:            }
0174:
0175:            /**
0176:             * {@inheritDoc}
0177:             */
0178:            public List<ParticipationRecord> getSectionTeachingAssistants(
0179:                    final String sectionUuid) {
0180:                HibernateCallback hc = new HibernateCallback() {
0181:                    public Object doInHibernate(Session session)
0182:                            throws HibernateException {
0183:                        CourseSection section = getSection(sectionUuid, session);
0184:                        Query q = session.getNamedQuery("findSectionTAs");
0185:                        q.setParameter("section", section);
0186:                        return q.list();
0187:                    }
0188:                };
0189:                return getHibernateTemplate().executeFind(hc);
0190:            }
0191:
0192:            /**
0193:             * {@inheritDoc}
0194:             */
0195:            public List<EnrollmentRecord> getSectionEnrollments(
0196:                    final String sectionUuid) {
0197:                HibernateCallback hc = new HibernateCallback() {
0198:                    public Object doInHibernate(Session session)
0199:                            throws HibernateException {
0200:                        CourseSection section = getSection(sectionUuid, session);
0201:                        Query q = session.getNamedQuery("findSectionStudents");
0202:                        q.setParameter("section", section);
0203:                        return q.list();
0204:                    }
0205:                };
0206:                return getHibernateTemplate().executeFind(hc);
0207:            }
0208:
0209:            /**
0210:             * {@inheritDoc}
0211:             */
0212:            public List<EnrollmentRecord> findSiteEnrollments(
0213:                    String siteContext, String pattern) {
0214:                List<EnrollmentRecord> fullList = getSiteEnrollments(siteContext);
0215:                List<EnrollmentRecord> filteredList = new ArrayList<EnrollmentRecord>();
0216:                for (Iterator iter = fullList.iterator(); iter.hasNext();) {
0217:                    EnrollmentRecord record = (EnrollmentRecord) iter.next();
0218:                    User user = record.getUser();
0219:                    if (user.getDisplayName().toLowerCase().startsWith(
0220:                            pattern.toLowerCase())
0221:                            || user.getSortName().toLowerCase().startsWith(
0222:                                    pattern.toLowerCase())
0223:                            || user.getDisplayId().toLowerCase().startsWith(
0224:                                    pattern.toLowerCase())) {
0225:                        filteredList.add(record);
0226:                    }
0227:                }
0228:                return filteredList;
0229:            }
0230:
0231:            /**
0232:             * {@inheritDoc}
0233:             */
0234:            public String getCategoryName(String categoryId, Locale locale) {
0235:                ResourceBundle bundle = ResourceBundle.getBundle(
0236:                        CATEGORY_BUNDLE, locale);
0237:                String name;
0238:                try {
0239:                    name = bundle.getString(categoryId);
0240:                } catch (MissingResourceException mre) {
0241:                    if (log.isDebugEnabled())
0242:                        log.debug("Could not find the name for category id = "
0243:                                + categoryId + " in locale "
0244:                                + locale.getDisplayName());
0245:                    name = null;
0246:                }
0247:                return name;
0248:            }
0249:
0250:            /**
0251:             * {@inheritDoc}
0252:             */
0253:            public List<String> getSectionCategories(String siteContext) {
0254:                ResourceBundle bundle = ResourceBundle
0255:                        .getBundle(CATEGORY_BUNDLE);
0256:
0257:                Enumeration keys = bundle.getKeys();
0258:                List<String> categoryIds = new ArrayList<String>();
0259:                while (keys.hasMoreElements()) {
0260:                    categoryIds.add(keys.nextElement().toString());
0261:                }
0262:                Collections.sort(categoryIds);
0263:                return categoryIds;
0264:            }
0265:
0266:            private CourseSectionImpl getSection(final String sectionUuid,
0267:                    Session session) throws HibernateException {
0268:                Query q = session.getNamedQuery("loadSectionByUuid");
0269:                q.setParameter("uuid", sectionUuid);
0270:                CourseSectionImpl section = (CourseSectionImpl) q
0271:                        .uniqueResult();
0272:                if (section == null) {
0273:                    throw new MembershipException(
0274:                            "No section exists with uuid=" + sectionUuid);
0275:                } else {
0276:                    return section;
0277:                }
0278:            }
0279:
0280:            private Course getCourseFromSiteContext(String siteContext,
0281:                    Session session) throws HibernateException {
0282:                Query q = session.getNamedQuery("loadCourseBySiteContext");
0283:                q.setParameter("siteContext", siteContext);
0284:                Course course = (Course) q.uniqueResult();
0285:                if (course == null) {
0286:                    throw new MembershipException(
0287:                            "No course exists for site = " + siteContext);
0288:                } else {
0289:                    return course;
0290:                }
0291:            }
0292:
0293:            private CourseImpl getCourseFromUuid(String courseUuid,
0294:                    Session session) throws HibernateException {
0295:                Query q = session.getNamedQuery("loadCourseByUuid");
0296:                q.setParameter("uuid", courseUuid);
0297:                CourseImpl course = (CourseImpl) q.uniqueResult();
0298:                if (course == null) {
0299:                    throw new MembershipException(
0300:                            "No course exists with uuid = " + courseUuid);
0301:                } else {
0302:                    return course;
0303:                }
0304:            }
0305:
0306:            /**
0307:             * {@inheritDoc}
0308:             */
0309:            public Course getCourse(final String siteContext) {
0310:                if (log.isDebugEnabled())
0311:                    log.debug("Getting course for context " + siteContext);
0312:                HibernateCallback hc = new HibernateCallback() {
0313:                    public Object doInHibernate(Session session)
0314:                            throws HibernateException {
0315:                        return getCourseFromSiteContext(siteContext, session);
0316:                    }
0317:                };
0318:                return (Course) getHibernateTemplate().execute(hc);
0319:            }
0320:
0321:            /**
0322:             * {@inheritDoc}
0323:             */
0324:            public SectionEnrollments getSectionEnrollmentsForStudents(
0325:                    final String siteContext, final Set studentUids) {
0326:                HibernateCallback hc = new HibernateCallback() {
0327:                    public Object doInHibernate(Session session)
0328:                            throws HibernateException {
0329:                        Course course = getCourse(siteContext);
0330:                        Query q = session
0331:                                .getNamedQuery("findSectionEnrollments");
0332:                        q.setParameter("course", course);
0333:                        q.setParameterList("studentUids", studentUids);
0334:                        return q.list();
0335:                    }
0336:                };
0337:                if (studentUids == null || studentUids.isEmpty()) {
0338:                    if (log.isDebugEnabled())
0339:                        log
0340:                                .debug("No student uids were passed to getSectionEnrollments.");
0341:                    return new SectionEnrollmentsImpl(new ArrayList());
0342:                }
0343:                return new SectionEnrollmentsImpl(getHibernateTemplate()
0344:                        .executeFind(hc));
0345:            }
0346:
0347:            /**
0348:             * {@inheritDoc}
0349:             */
0350:            public EnrollmentRecord joinSection(final String sectionUuid) {
0351:                HibernateCallback hc = new HibernateCallback() {
0352:                    public Object doInHibernate(Session session)
0353:                            throws HibernateException {
0354:                        try {
0355:                            getSection(sectionUuid, session);
0356:                        } catch (MembershipException me) {
0357:                            log.error(me);
0358:                            return null;
0359:                        }
0360:                        String userUid = authn.getUserUid(null);
0361:                        String siteContext = context.getContext(null);
0362:                        Query q = session.getNamedQuery("findEnrollment");
0363:                        q.setParameter("userUid", userUid);
0364:                        q.setParameter("sectionUuid", sectionUuid);
0365:                        Object enrollment = q.uniqueResult();
0366:                        if (enrollment == null) {
0367:                            CourseSection section = getSection(sectionUuid,
0368:                                    session);
0369:                            User user = getUserFromSiteParticipation(
0370:                                    siteContext, userUid, session);
0371:                            EnrollmentRecordImpl enr = new EnrollmentRecordImpl(
0372:                                    section, null, user);
0373:                            enr.setUuid(uuidManager.createUuid());
0374:                            session.save(enr);
0375:                            return enr;
0376:                        } else {
0377:                            throw new MembershipException(userUid
0378:                                    + " is already a student in this section");
0379:                        }
0380:                    }
0381:                };
0382:                return (EnrollmentRecord) getHibernateTemplate().execute(hc);
0383:            }
0384:
0385:            /**
0386:             * {@inheritDoc}
0387:             */
0388:            public void switchSection(final String newSectionUuid) {
0389:                HibernateCallback hc = new HibernateCallback() {
0390:                    public Object doInHibernate(Session session)
0391:                            throws HibernateException {
0392:                        try {
0393:                            getSection(newSectionUuid, session);
0394:                        } catch (MembershipException me) {
0395:                            log.error(me);
0396:                            return null;
0397:                        }
0398:                        String userUid = authn.getUserUid(null);
0399:                        String siteContext = context.getContext(null);
0400:                        CourseSection newSection = getSection(newSectionUuid,
0401:                                session);
0402:                        Course course = newSection.getCourse();
0403:                        String category = newSection.getCategory();
0404:
0405:                        // Find the existing section enrollment in this category, so we can drop it
0406:                        Query q = session
0407:                                .getNamedQuery("findEnrollmentInCategory");
0408:                        q.setParameter("userUid", userUid);
0409:                        q.setParameter("course", course);
0410:                        q.setParameter("category", category);
0411:                        Object result = q.uniqueResult();
0412:                        if (result == null) {
0413:                            throw new MembershipException(
0414:                                    userUid
0415:                                            + " is not enrolled in any "
0416:                                            + category
0417:                                            + " section, so s/he can not switch sections");
0418:                        } else {
0419:                            User user = getUserFromSiteParticipation(
0420:                                    siteContext, userUid, session);
0421:
0422:                            // Add the new enrollment
0423:                            EnrollmentRecordImpl enr = new EnrollmentRecordImpl(
0424:                                    newSection, null, user);
0425:                            enr.setUuid(uuidManager.createUuid());
0426:                            session.save(enr);
0427:
0428:                            // Remove the old enrollment
0429:                            session.delete(result);
0430:                        }
0431:                        return null;
0432:                    }
0433:                };
0434:                getHibernateTemplate().execute(hc);
0435:            }
0436:
0437:            /**
0438:             * {@inheritDoc}
0439:             */
0440:            public ParticipationRecord addSectionMembership(String userUid,
0441:                    Role role, String sectionUuid) throws MembershipException {
0442:                if (role.isInstructor()) {
0443:                    throw new MembershipException(
0444:                            "You can not add an instructor to a section... please add them to the course");
0445:                } else if (role.isStudent()) {
0446:                    return addSectionEnrollment(userUid, sectionUuid);
0447:                } else if (role.isTeachingAssistant()) {
0448:                    return addSectionTeachingAssistant(userUid, sectionUuid);
0449:                } else {
0450:                    throw new MembershipException(
0451:                            "You can not add a user to a section with a role of 'none'");
0452:                }
0453:            }
0454:
0455:            private EnrollmentRecordImpl addSectionEnrollment(
0456:                    final String userUid, final String sectionUuid) {
0457:                HibernateCallback hc = new HibernateCallback() {
0458:                    public Object doInHibernate(Session session)
0459:                            throws HibernateException {
0460:                        CourseSection section = getSection(sectionUuid, session);
0461:                        String siteContext = ((CourseImpl) section.getCourse())
0462:                                .getSiteContext();
0463:                        User user = getUserFromSiteParticipation(siteContext,
0464:                                userUid, session);
0465:
0466:                        Set currentEnrollments = getSectionEnrollments(userUid,
0467:                                section.getCourse().getUuid());
0468:                        for (Iterator iter = currentEnrollments.iterator(); iter
0469:                                .hasNext();) {
0470:                            EnrollmentRecord enrollment = (EnrollmentRecord) iter
0471:                                    .next();
0472:
0473:                            // Make sure they are not already enrolled in this learning context
0474:                            if (enrollment.getLearningContext().equals(section)) {
0475:                                if (log.isDebugEnabled())
0476:                                    log
0477:                                            .debug("Not adding an enrollment for student "
0478:                                                    + userUid
0479:                                                    + " in section "
0480:                                                    + sectionUuid
0481:                                                    + "... already enrolled.");
0482:                                return null;
0483:                            }
0484:
0485:                            // Make sure any enrollment in another section of the same category is removed
0486:                            if (((CourseSection) enrollment
0487:                                    .getLearningContext()).getCategory()
0488:                                    .equals(section.getCategory())) {
0489:                                if (log.isDebugEnabled())
0490:                                    log
0491:                                            .debug("Removing enrollment for student"
0492:                                                    + userUid
0493:                                                    + " in section "
0494:                                                    + enrollment
0495:                                                            .getLearningContext()
0496:                                                            .getUuid()
0497:                                                    + "... enrolling in "
0498:                                                    + sectionUuid);
0499:                                session.delete(enrollment);
0500:                            }
0501:                        }
0502:
0503:                        EnrollmentRecordImpl enrollment = new EnrollmentRecordImpl(
0504:                                section, null, user);
0505:                        enrollment.setUuid(uuidManager.createUuid());
0506:                        session.save(enrollment);
0507:                        return enrollment;
0508:                    }
0509:                };
0510:                return (EnrollmentRecordImpl) getHibernateTemplate()
0511:                        .execute(hc);
0512:            }
0513:
0514:            private TeachingAssistantRecordImpl addSectionTeachingAssistant(
0515:                    final String userUid, final String sectionUuid) {
0516:                HibernateCallback hc = new HibernateCallback() {
0517:                    public Object doInHibernate(Session session)
0518:                            throws HibernateException {
0519:                        CourseSection section = getSection(sectionUuid, session);
0520:                        String siteContext = ((CourseImpl) section.getCourse())
0521:                                .getSiteContext();
0522:                        User user = getUserFromSiteParticipation(siteContext,
0523:                                userUid, session);
0524:
0525:                        // Make sure they are not already a TA in this section
0526:                        List taRecords = getSectionTeachingAssistants(sectionUuid);
0527:                        for (Iterator iter = taRecords.iterator(); iter
0528:                                .hasNext();) {
0529:                            ParticipationRecord record = (ParticipationRecord) iter
0530:                                    .next();
0531:                            if (record.getUser().getUserUid().equals(userUid)) {
0532:                                if (log.isDebugEnabled())
0533:                                    log.debug("Not adding a TA record for "
0534:                                            + userUid
0535:                                            + "... already a TA in section "
0536:                                            + sectionUuid);
0537:                            }
0538:                        }
0539:                        TeachingAssistantRecordImpl ta = new TeachingAssistantRecordImpl(
0540:                                section, user);
0541:                        ta.setUuid(uuidManager.createUuid());
0542:                        session.save(ta);
0543:                        return ta;
0544:                    }
0545:                };
0546:                return (TeachingAssistantRecordImpl) getHibernateTemplate()
0547:                        .execute(hc);
0548:            }
0549:
0550:            /**
0551:             * {@inheritDoc}
0552:             */
0553:            public void setSectionMemberships(final Set userUids,
0554:                    final Role role, final String sectionUuid) {
0555:                HibernateCallback hc = new HibernateCallback() {
0556:                    public Object doInHibernate(Session session)
0557:                            throws HibernateException {
0558:                        try {
0559:                            getSection(sectionUuid, session);
0560:                        } catch (MembershipException me) {
0561:                            log.error(me);
0562:                            return null;
0563:                        }
0564:
0565:                        String siteContext = context.getContext(null);
0566:                        List currentMembers;
0567:                        if (role.isTeachingAssistant()) {
0568:                            currentMembers = getSectionTeachingAssistants(sectionUuid);
0569:                        } else if (role.isStudent()) {
0570:                            currentMembers = getSectionEnrollments(sectionUuid);
0571:                        } else {
0572:                            throw new RuntimeException(
0573:                                    "You can not setSectionMemberships with role "
0574:                                            + role.getDescription());
0575:                        }
0576:                        Set currentUserUids = new HashSet();
0577:                        for (Iterator iter = currentMembers.iterator(); iter
0578:                                .hasNext();) {
0579:                            ParticipationRecord membership = (ParticipationRecord) iter
0580:                                    .next();
0581:                            // Keep a set of all current userUids
0582:                            currentUserUids.add(membership.getUser()
0583:                                    .getUserUid());
0584:                            // If this current member is not in the new set of users, drop them
0585:                            if (!userUids.contains(membership.getUser()
0586:                                    .getUserUid())) {
0587:                                session.delete(membership);
0588:                            }
0589:                        }
0590:
0591:                        // Generate a set of new member userUuids
0592:                        Set newMemberUserUuids = new HashSet(userUids);
0593:                        newMemberUserUuids.removeAll(currentUserUids);
0594:
0595:                        // Get the section
0596:                        CourseSection section = getSection(sectionUuid, session);
0597:
0598:                        // Add the new members to the section
0599:                        for (Iterator iter = newMemberUserUuids.iterator(); iter
0600:                                .hasNext();) {
0601:                            String newMemberUuid = (String) iter.next();
0602:                            User user = getUserFromSiteParticipation(
0603:                                    siteContext, newMemberUuid, session);
0604:                            addMembership(user, section, role, session);
0605:                        }
0606:
0607:                        // Remove the new members from other sections in this category
0608:                        if (!newMemberUserUuids.isEmpty()) {
0609:                            removeSectionEnrollments(newMemberUserUuids,
0610:                                    section, session);
0611:                        }
0612:
0613:                        return null;
0614:                    }
0615:                };
0616:                getHibernateTemplate().execute(hc);
0617:            }
0618:
0619:            private void removeSectionEnrollments(Set newMemberUserUids,
0620:                    CourseSection section, Session session)
0621:                    throws HibernateException {
0622:                Query q = session
0623:                        .getNamedQuery("findOtherEnrollmentsInCategory");
0624:                q.setParameter("course", section.getCourse());
0625:                q.setParameter("category", section.getCategory());
0626:                q.setParameter("sectionUuid", section.getUuid());
0627:                q.setParameter("course", section.getCourse());
0628:                q.setParameterList("userUids", newMemberUserUids);
0629:                List enrollmentsToDelete = q.list();
0630:                for (Iterator iter = enrollmentsToDelete.iterator(); iter
0631:                        .hasNext();) {
0632:                    EnrollmentRecord enrollment = (EnrollmentRecord) iter
0633:                            .next();
0634:                    session.delete(enrollment);
0635:                }
0636:
0637:            }
0638:
0639:            private void addMembership(User user, CourseSection section,
0640:                    Role role, Session session) throws HibernateException {
0641:                if (role.isTeachingAssistant()) {
0642:                    TeachingAssistantRecordImpl membership = new TeachingAssistantRecordImpl(
0643:                            section, user);
0644:                    membership.setUuid(uuidManager.createUuid());
0645:                    session.save(membership);
0646:                } else if (role.isStudent()) {
0647:                    EnrollmentRecordImpl membership = new EnrollmentRecordImpl(
0648:                            section, null, user);
0649:                    membership.setUuid(uuidManager.createUuid());
0650:                    session.save(membership);
0651:                } else {
0652:                    throw new MembershipException(
0653:                            "You can not add an instructor as a section member");
0654:                }
0655:            }
0656:
0657:            /**
0658:             * {@inheritDoc}
0659:             */
0660:            public void dropSectionMembership(final String userUid,
0661:                    final String sectionUuid) {
0662:                HibernateCallback hc = new HibernateCallback() {
0663:                    public Object doInHibernate(Session session)
0664:                            throws HibernateException {
0665:                        // Get the primary section
0666:                        Query q = session
0667:                                .getNamedQuery("findParticipationRecord");
0668:                        q.setParameter("sectionUuid", sectionUuid);
0669:                        q.setParameter("userUid", userUid);
0670:                        Object result = q.uniqueResult();
0671:                        session.delete(result);
0672:                        return null;
0673:                    }
0674:                };
0675:                getHibernateTemplate().execute(hc);
0676:            }
0677:
0678:            /**
0679:             * {@inheritDoc}
0680:             */
0681:            public void dropEnrollmentFromCategory(final String studentUid,
0682:                    final String siteContext, final String category) {
0683:                HibernateCallback hc = new HibernateCallback() {
0684:                    public Object doInHibernate(Session session)
0685:                            throws HibernateException {
0686:                        Query q = session
0687:                                .getNamedQuery("findCategoryEnrollment");
0688:                        q.setParameter("studentUid", studentUid);
0689:                        q.setParameter("category", category);
0690:                        q.setParameter("siteContext", siteContext);
0691:                        Object result = q.uniqueResult();
0692:                        if (result != null) {
0693:                            session.delete(result);
0694:                        }
0695:                        return null;
0696:                    }
0697:                };
0698:                getHibernateTemplate().execute(hc);
0699:            }
0700:
0701:            /**
0702:             * {@inheritDoc}
0703:             */
0704:            public int getTotalEnrollments(final String learningContextUuid) {
0705:                HibernateCallback hc = new HibernateCallback() {
0706:                    public Object doInHibernate(Session session)
0707:                            throws HibernateException {
0708:                        // Get the primary section
0709:                        Query q = session.getNamedQuery("countEnrollments");
0710:                        q.setParameter("learningContextUuid",
0711:                                learningContextUuid);
0712:                        return q.iterate().next();
0713:                    }
0714:                };
0715:                return ((Integer) getHibernateTemplate().execute(hc))
0716:                        .intValue();
0717:            }
0718:
0719:            /**
0720:             * {@inheritDoc}
0721:             */
0722:            public CourseSection addSection(final String courseUuid,
0723:                    final String title, final String category,
0724:                    final Integer maxEnrollments, final String location,
0725:                    final Time startTime, final Time endTime,
0726:                    final boolean monday, final boolean tuesday,
0727:                    final boolean wednesday, final boolean thursday,
0728:                    final boolean friday, final boolean saturday,
0729:                    final boolean sunday) {
0730:                final String uuid = uuidManager.createUuid();
0731:                if (log.isDebugEnabled())
0732:                    log.debug("Creating section with uuid = " + uuid);
0733:                HibernateCallback hc = new HibernateCallback() {
0734:                    public Object doInHibernate(Session session)
0735:                            throws HibernateException {
0736:                        Course course = getCourseFromUuid(courseUuid, session);
0737:                        if (course == null) {
0738:                            throw new MembershipException("Course uuid = "
0739:                                    + courseUuid + "does not exist");
0740:                        }
0741:                        String uuid = uuidManager.createUuid();
0742:                        CourseSectionImpl section = new CourseSectionImpl(
0743:                                course, title, uuid, category, maxEnrollments,
0744:                                location, startTime, endTime, monday, tuesday,
0745:                                wednesday, thursday, friday, saturday, sunday);
0746:                        session.save(section);
0747:                        return section;
0748:                    }
0749:                };
0750:
0751:                return (CourseSection) getHibernateTemplate().execute(hc);
0752:            }
0753:
0754:            private List getHibernateMeetings(List meetings) {
0755:                List meetingEntities = new ArrayList();
0756:                if (meetings != null) {
0757:                    for (Iterator iter = meetings.iterator(); iter.hasNext();) {
0758:                        Meeting meeting = (Meeting) iter.next();
0759:                        meetingEntities.add(new MeetingImpl(meeting));
0760:                    }
0761:                }
0762:                return meetingEntities;
0763:            }
0764:
0765:            public CourseSection addSection(final String courseUuid,
0766:                    final String title, final String category,
0767:                    final Integer maxEnrollments, List meetings) {
0768:                // We can't be sure that the meetings are in fact hibernate entities.  I guess this is true for other kinds of objects as well!
0769:                final List meetingEntities = getHibernateMeetings(meetings);
0770:                HibernateCallback hc = new HibernateCallback() {
0771:                    public Object doInHibernate(Session session)
0772:                            throws HibernateException {
0773:                        Course course = getCourseFromUuid(courseUuid, session);
0774:                        if (course == null) {
0775:                            throw new MembershipException("Course uuid = "
0776:                                    + courseUuid + "does not exist");
0777:                        }
0778:                        String uuid = uuidManager.createUuid();
0779:                        CourseSectionImpl section = new CourseSectionImpl(
0780:                                course, title, uuid, category, maxEnrollments,
0781:                                meetingEntities);
0782:                        session.save(section);
0783:                        return section;
0784:                    }
0785:                };
0786:
0787:                return (CourseSection) getHibernateTemplate().execute(hc);
0788:            }
0789:
0790:            /**
0791:             * {@inheritDoc}
0792:             */
0793:            public void updateSection(final String sectionUuid,
0794:                    final String title, final Integer maxEnrollments,
0795:                    final String location, final Time startTime,
0796:                    final Time endTime, final boolean monday,
0797:                    final boolean tuesday, final boolean wednesday,
0798:                    final boolean thursday, final boolean friday,
0799:                    final boolean saturday, final boolean sunday) {
0800:                HibernateCallback hc = new HibernateCallback() {
0801:                    public Object doInHibernate(Session session)
0802:                            throws HibernateException {
0803:                        CourseSectionImpl section = getSection(sectionUuid,
0804:                                session);
0805:                        section.setTitle(title);
0806:                        section.setMaxEnrollments(maxEnrollments);
0807:                        List meetingList = new ArrayList(1);
0808:                        MeetingImpl meeting = new MeetingImpl(location,
0809:                                startTime, endTime, monday, tuesday, wednesday,
0810:                                thursday, friday, saturday, sunday);
0811:                        meetingList.add(meeting);
0812:                        section.setMeetings(meetingList);
0813:
0814:                        session.update(section);
0815:                        return null;
0816:                    }
0817:                };
0818:                getHibernateTemplate().execute(hc);
0819:            }
0820:
0821:            public void updateSection(final String sectionUuid,
0822:                    final String title, final Integer maxEnrollments,
0823:                    final List meetings) {
0824:                final List meetingEntities = getHibernateMeetings(meetings);
0825:                HibernateCallback hc = new HibernateCallback() {
0826:                    public Object doInHibernate(Session session)
0827:                            throws HibernateException {
0828:                        CourseSectionImpl section = getSection(sectionUuid,
0829:                                session);
0830:                        section.setTitle(title);
0831:                        section.setMaxEnrollments(maxEnrollments);
0832:                        section.setMeetings(meetingEntities);
0833:
0834:                        session.update(section);
0835:                        return null;
0836:                    }
0837:                };
0838:                getHibernateTemplate().execute(hc);
0839:            }
0840:
0841:            /**
0842:             * {@inheritDoc}
0843:             */
0844:            public void disbandSection(final String sectionUuid) {
0845:                if (log.isDebugEnabled())
0846:                    log.debug("Disbanding section " + sectionUuid);
0847:                HibernateCallback hc = new HibernateCallback() {
0848:                    public Object doInHibernate(Session session)
0849:                            throws HibernateException {
0850:                        CourseSection section = getSection(sectionUuid, session);
0851:
0852:                        // Delete all of the memberships in the section
0853:                        Query q = session
0854:                                .getNamedQuery("findParticipantsBySectionUuid");
0855:                        q.setParameter("sectionUuid", sectionUuid);
0856:                        for (Iterator iter = q.iterate(); iter.hasNext();) {
0857:                            session.delete(iter.next());
0858:                        }
0859:
0860:                        // Delete the section
0861:                        session.delete(section);
0862:                        return null;
0863:                    }
0864:                };
0865:                getHibernateTemplate().execute(hc);
0866:            }
0867:
0868:            /**
0869:             * {@inheritDoc}
0870:             */
0871:            public void disbandSections(Set<String> sectionUuids) {
0872:                for (Iterator<String> iter = sectionUuids.iterator(); iter
0873:                        .hasNext();) {
0874:                    disbandSection(iter.next());
0875:                }
0876:            }
0877:
0878:            /**
0879:             * {@inheritDoc}
0880:             */
0881:            public boolean isExternallyManaged(final String courseUuid) {
0882:                HibernateCallback hc = new HibernateCallback() {
0883:                    public Object doInHibernate(Session session)
0884:                            throws HibernateException {
0885:                        CourseImpl course = getCourseFromUuid(courseUuid,
0886:                                session);
0887:                        return Boolean.valueOf(course.isExternallyManaged());
0888:                    }
0889:                };
0890:                return ((Boolean) getHibernateTemplate().execute(hc))
0891:                        .booleanValue();
0892:            }
0893:
0894:            public void setExternallyManaged(final String courseUuid,
0895:                    final boolean externallyManaged) {
0896:                HibernateCallback hc = new HibernateCallback() {
0897:                    public Object doInHibernate(Session session)
0898:                            throws HibernateException {
0899:                        CourseImpl course = (CourseImpl) getCourseFromUuid(
0900:                                courseUuid, session);
0901:                        course.setExternallyManaged(externallyManaged);
0902:                        if (externallyManaged) {
0903:                            // Also set the self join/switch to false
0904:                            course.setSelfRegistrationAllowed(false);
0905:                            course.setSelfSwitchingAllowed(false);
0906:                        }
0907:                        session.update(course);
0908:                        return null;
0909:                    }
0910:                };
0911:                getHibernateTemplate().execute(hc);
0912:            }
0913:
0914:            /**
0915:             * {@inheritDoc}
0916:             */
0917:            public boolean isSelfRegistrationAllowed(final String courseUuid) {
0918:                HibernateCallback hc = new HibernateCallback() {
0919:                    public Object doInHibernate(Session session)
0920:                            throws HibernateException {
0921:                        CourseImpl course = getCourseFromUuid(courseUuid,
0922:                                session);
0923:                        return Boolean.valueOf(course
0924:                                .isSelfRegistrationAllowed());
0925:                    }
0926:                };
0927:                return ((Boolean) getHibernateTemplate().execute(hc))
0928:                        .booleanValue();
0929:            }
0930:
0931:            /**
0932:             * {@inheritDoc}
0933:             */
0934:            public boolean isSelfSwitchingAllowed(final String courseUuid) {
0935:                HibernateCallback hc = new HibernateCallback() {
0936:                    public Object doInHibernate(Session session)
0937:                            throws HibernateException {
0938:                        CourseImpl course = getCourseFromUuid(courseUuid,
0939:                                session);
0940:                        return Boolean.valueOf(course.isSelfSwitchingAllowed());
0941:                    }
0942:                };
0943:                return ((Boolean) getHibernateTemplate().execute(hc))
0944:                        .booleanValue();
0945:            }
0946:
0947:            /**
0948:             * {@inheritDoc}
0949:             */
0950:            public List<EnrollmentRecord> getUnsectionedEnrollments(
0951:                    final String courseUuid, final String category) {
0952:                HibernateCallback hc = new HibernateCallback() {
0953:                    public Object doInHibernate(Session session)
0954:                            throws HibernateException {
0955:                        Query q = session
0956:                                .getNamedQuery("findUnsectionedEnrollmentsInCategory");
0957:                        q.setParameter("courseUuid", courseUuid);
0958:                        q.setParameter("category", category);
0959:                        return q.list();
0960:                    }
0961:                };
0962:                return getHibernateTemplate().executeFind(hc);
0963:            }
0964:
0965:            /**
0966:             * {@inheritDoc}
0967:             */
0968:            public Set<EnrollmentRecord> getSectionEnrollments(
0969:                    final String userUid, final String courseUuid) {
0970:                HibernateCallback hc = new HibernateCallback() {
0971:                    public Object doInHibernate(Session session)
0972:                            throws HibernateException {
0973:                        Query q = session
0974:                                .getNamedQuery("findSingleStudentSectionEnrollmentsInCourse");
0975:                        q.setParameter("userUid", userUid);
0976:                        q.setParameter("courseUuid", courseUuid);
0977:                        return q.list();
0978:                    }
0979:                };
0980:                return new HashSet(getHibernateTemplate().executeFind(hc));
0981:            }
0982:
0983:            /**
0984:             * {@inheritDoc}
0985:             */
0986:            public User getSiteEnrollment(final String siteContext,
0987:                    final String studentUid) {
0988:                HibernateCallback hc = new HibernateCallback() {
0989:                    public Object doInHibernate(Session session)
0990:                            throws HibernateException {
0991:                        return getUserFromSiteParticipation(siteContext,
0992:                                studentUid, session);
0993:                    }
0994:                };
0995:                return (User) getHibernateTemplate().execute(hc);
0996:            }
0997:
0998:            private User getUserFromSiteParticipation(String siteContext,
0999:                    String userUid, Session session) throws HibernateException {
1000:                Query q = session
1001:                        .getNamedQuery("findUserFromSiteParticipation");
1002:                q.setParameter("userUid", userUid);
1003:                q.setParameter("siteContext", siteContext);
1004:                User result = (User) q.uniqueResult();
1005:                return result;
1006:            }
1007:
1008:            public ExternalIntegrationConfig getConfiguration(Object obj) {
1009:                HttpServletRequest request = (HttpServletRequest) obj;
1010:                ServletContext context = request.getSession(true)
1011:                        .getServletContext();
1012:                ExternalIntegrationConfig config = (ExternalIntegrationConfig) context
1013:                        .getAttribute(CONFIGURATION_KEY);
1014:
1015:                // Set the default if the configuration string is missing
1016:                if (config == null) {
1017:                    config = ExternalIntegrationConfig.MANUAL_DEFAULT;
1018:                    context.setAttribute(CONFIGURATION_KEY, config);
1019:                }
1020:
1021:                return config;
1022:            }
1023:
1024:            // Dependency injection
1025:
1026:            public void setAuthn(Authn authn) {
1027:                this .authn = authn;
1028:            }
1029:
1030:            public void setUuidManager(IdManager uuidManager) {
1031:                this .uuidManager = uuidManager;
1032:            }
1033:
1034:            public void setContext(Context context) {
1035:                this .context = context;
1036:            }
1037:
1038:            public void setJoinOptions(final String courseUuid,
1039:                    final boolean joinAllowed, final boolean switchAllowed) {
1040:                HibernateCallback hc = new HibernateCallback() {
1041:                    public Object doInHibernate(Session session)
1042:                            throws HibernateException {
1043:                        CourseImpl course = (CourseImpl) getCourseFromUuid(
1044:                                courseUuid, session);
1045:                        course.setSelfRegistrationAllowed(joinAllowed);
1046:                        course.setSelfSwitchingAllowed(switchAllowed);
1047:                        session.update(course);
1048:                        return null;
1049:                    }
1050:                };
1051:                getHibernateTemplate().execute(hc);
1052:            }
1053:
1054:            public Collection<CourseSection> addSections(String courseUuid,
1055:                    Collection<CourseSection> sections) {
1056:                // We need to ensure that these sections are hibernate entities, not some other impl.
1057:                sections = ensureHibernateSections(sections);
1058:
1059:                Collection<CourseSection> addedSections = new ArrayList<CourseSection>();
1060:                for (Iterator<CourseSection> iter = sections.iterator(); iter
1061:                        .hasNext();) {
1062:                    CourseSection section = iter.next();
1063:                    addedSections.add(addSection(courseUuid,
1064:                            section.getTitle(), section.getCategory(), section
1065:                                    .getMaxEnrollments(),
1066:                            ensureHibernateMeetings(section.getMeetings())));
1067:                }
1068:                return addedSections;
1069:            }
1070:
1071:            private List<MeetingImpl> ensureHibernateMeetings(
1072:                    List<Meeting> meetings) {
1073:                List<MeetingImpl> hibernateEntities = new ArrayList<MeetingImpl>();
1074:                for (Iterator<Meeting> iter = meetings.iterator(); iter
1075:                        .hasNext();) {
1076:                    Meeting meeting = iter.next();
1077:                    hibernateEntities.add(new MeetingImpl(meeting));
1078:                }
1079:                return hibernateEntities;
1080:            }
1081:
1082:            private List<CourseSection> ensureHibernateSections(
1083:                    Collection<CourseSection> sections) {
1084:                List<CourseSection> hibernateEntities = new ArrayList<CourseSection>();
1085:                for (Iterator<CourseSection> iter = sections.iterator(); iter
1086:                        .hasNext();) {
1087:                    CourseSection section = iter.next();
1088:                    hibernateEntities.add(new CourseSectionImpl(section));
1089:                }
1090:                return hibernateEntities;
1091:            }
1092:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.