001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/java/org/sakaiproject/tool/section/jsf/backingbean/CourseDependentBean.java $
003: * $Id: CourseDependentBean.java 21937 2007-02-27 00:10:58Z 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.tool.section.jsf.backingbean;
021:
022: import java.io.Serializable;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Set;
028:
029: import javax.faces.context.FacesContext;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.sakaiproject.jsf.util.LocaleUtil;
034: import org.sakaiproject.section.api.SectionManager;
035: import org.sakaiproject.section.api.SectionManager.ExternalIntegrationConfig;
036: import org.sakaiproject.section.api.coursemanagement.Course;
037: import org.sakaiproject.section.api.coursemanagement.CourseSection;
038: import org.sakaiproject.tool.section.jsf.JsfUtil;
039:
040: /**
041: * Base class for all JSF backing beans relying on knowledge of the current
042: * course context.
043: *
044: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
045: *
046: */
047: public class CourseDependentBean extends InitializableBean implements
048: Serializable {
049:
050: private static final long serialVersionUID = 1L;
051: private static final Log log = LogFactory
052: .getLog(CourseDependentBean.class);
053:
054: private transient CourseBean courseBean;
055:
056: private CourseBean getCourseBean() {
057: if (courseBean == null) {
058: courseBean = (CourseBean) JsfUtil
059: .resolveVariable("courseBean");
060: }
061: return courseBean;
062: }
063:
064: /**
065: * Gets the categories that are currently being used in this site context.
066: *
067: * @param categories
068: * @param sections
069: * @return
070: */
071: protected Set<String> getUsedCategories() {
072: Set<String> used = new HashSet<String>();
073: List sections = getAllSiteSections();
074: List categories = getSectionManager().getSectionCategories(
075: getSiteContext());
076: for (Iterator iter = sections.iterator(); iter.hasNext();) {
077: CourseSection section = (CourseSection) iter.next();
078: String cat = section.getCategory();
079: if (categories.contains(cat)) {
080: used.add(cat);
081: }
082: }
083: return used;
084: }
085:
086: protected SectionManager getSectionManager() {
087: return getCourseBean().getSectionManager();
088: }
089:
090: protected String getUserUid() {
091: return getCourseBean().authn
092: .getUserUid(FacesContext.getCurrentInstance()
093: .getExternalContext().getRequest());
094: }
095:
096: protected ExternalIntegrationConfig getApplicationConfiguration() {
097: ExternalIntegrationConfig config = getCourseBean().sectionManager
098: .getConfiguration(FacesContext.getCurrentInstance()
099: .getExternalContext().getRequest());
100: if (log.isDebugEnabled())
101: log.debug("Application configuration = " + config);
102: return config;
103: }
104:
105: protected String getSiteContext() {
106: return getCourseBean().context.getContext(null);
107: }
108:
109: protected Course getCourse() {
110: return getCourseBean().sectionManager
111: .getCourse(getSiteContext());
112: }
113:
114: protected List<CourseSection> getAllSiteSections() {
115: return getCourseBean().sectionManager
116: .getSections(getSiteContext());
117: }
118:
119: protected Set getEnrolledSections(String userUid) {
120: return getCourseBean().sectionManager.getSectionEnrollments(
121: userUid, getCourse().getUuid());
122: }
123:
124: protected String getCategoryName(String categoryId) {
125: Locale locale = LocaleUtil.getLocale(FacesContext
126: .getCurrentInstance());
127: return getCourseBean().sectionManager.getCategoryName(
128: categoryId, locale);
129: }
130:
131: protected List<String> getSectionCategories() {
132: return getCourseBean().sectionManager
133: .getSectionCategories(getSiteContext());
134: }
135:
136: public boolean isSectionManagementEnabled() {
137: return getCourseBean().authz.isSectionManagementAllowed(
138: getUserUid(), getSiteContext());
139: }
140:
141: public boolean isSectionOptionsManagementEnabled() {
142: return getCourseBean().authz.isSectionOptionsManagementAllowed(
143: getUserUid(), getSiteContext());
144: }
145:
146: public boolean isSectionEnrollmentMangementEnabled() {
147: return getCourseBean().authz
148: .isSectionEnrollmentMangementAllowed(getUserUid(),
149: getSiteContext());
150: }
151:
152: public boolean isSectionTaManagementEnabled() {
153: return getCourseBean().authz.isSectionTaManagementAllowed(
154: getUserUid(), getSiteContext());
155: }
156:
157: public boolean isViewOwnSectionsEnabled() {
158: return getCourseBean().authz.isViewOwnSectionsAllowed(
159: getUserUid(), getSiteContext());
160: }
161:
162: public boolean isViewAllSectionsEnabled() {
163: return getCourseBean().authz.isViewAllSectionsAllowed(
164: getUserUid(), getSiteContext());
165: }
166:
167: public boolean isSectionAssignable() {
168: return getCourseBean().authz.isSectionAssignable(getUserUid(),
169: getSiteContext());
170: }
171:
172: public PreferencesBean getPrefs() {
173: return getCourseBean().getPrefs();
174: }
175:
176: public String getSiteRole() {
177: return getCourseBean().authz.getRoleDescription(getUserUid(),
178: getSiteContext());
179: }
180: }
|