001: /**********************************************************************************
002: *
003: * $Id: GradebookBean.java 22061 2007-03-01 22:54:37Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.ui;
022:
023: import javax.faces.context.FacesContext;
024: import javax.servlet.ServletRequest;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpSession;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.sakaiproject.section.api.SectionAwareness;
032:
033: import org.sakaiproject.service.gradebook.shared.GradebookNotFoundException;
034: import org.sakaiproject.tool.gradebook.Gradebook;
035: import org.sakaiproject.tool.gradebook.business.GradebookManager;
036: import org.sakaiproject.tool.gradebook.facades.*;
037:
038: /**
039: * Provide a UI handle to the selected gradebook.
040: *
041: * Since all application-specific backing beans (a group that doesn't include
042: * authentication handlers) require a gradebook ID to use with any of the
043: * business or facade services, this bean is also a reasonable place to centralize
044: * configuration of and access to those services.
045: */
046: public class GradebookBean extends InitializableBean {
047: private static final Log logger = LogFactory
048: .getLog(GradebookBean.class);
049:
050: private Long gradebookId;
051: private String gradebookUid;
052:
053: // These interfaces are defined application-wide (through Spring, although the
054: // UI classes don't know that).
055: private GradebookManager gradebookManager;
056: private SectionAwareness sectionAwareness;
057: private UserDirectoryService userDirectoryService;
058: private Authn authnService;
059: private Authz authzService;
060: private ContextManagement contextManagementService;
061: private EventTrackingService eventTrackingService;
062: private ConfigurationBean configurationBean;
063:
064: /**
065: * @return Returns the gradebookId.
066: */
067: public final Long getGradebookId() {
068: refreshFromRequest();
069: return gradebookId;
070: }
071:
072: private final void setGradebookId(Long gradebookId) {
073: this .gradebookId = gradebookId;
074: }
075:
076: /**
077: * @param newGradebookUid The gradebookId to set.
078: * Since this is coming from the client, the application should NOT
079: * trust that the current user actually has access to the gradebook
080: * with this UID. This design assumes that authorization will come
081: * into play on each request.
082: */
083: public final void setGradebookUid(String newGradebookUid) {
084: Long newGradebookId = null;
085: if (newGradebookUid != null) {
086: Gradebook gradebook = null;
087: try {
088: gradebook = getGradebookManager().getGradebook(
089: newGradebookUid);
090: } catch (GradebookNotFoundException gnfe) {
091: logger
092: .error("Request made for inaccessible gradebookUid="
093: + newGradebookUid);
094: newGradebookUid = null;
095: }
096: newGradebookId = gradebook.getId();
097: if (logger.isInfoEnabled())
098: logger.info("setGradebookUid gradebookUid="
099: + newGradebookUid + ", gradebookId="
100: + newGradebookId);
101: }
102: this .gradebookUid = newGradebookUid;
103: setGradebookId(newGradebookId);
104: }
105:
106: private final void refreshFromRequest() {
107: String requestUid = contextManagementService
108: .getGradebookUid(FacesContext.getCurrentInstance()
109: .getExternalContext().getRequest());
110: if ((requestUid != null) && (!requestUid.equals(gradebookUid))) {
111: if (logger.isInfoEnabled())
112: logger.info("resetting gradebookUid from "
113: + gradebookUid);
114: setGradebookUid(requestUid);
115: }
116: }
117:
118: /**
119: * Static method to pick up the gradebook UID, if any, held by the current GradebookBean, if any.
120: * Meant to be called from a servlet filter.
121: */
122: public static String getGradebookUidFromRequest(
123: ServletRequest request) {
124: String gradebookUid = null;
125: HttpSession session = ((HttpServletRequest) request)
126: .getSession();
127: GradebookBean gradebookBean = (GradebookBean) session
128: .getAttribute("gradebookBean");
129: if (gradebookBean != null) {
130: gradebookUid = gradebookBean.gradebookUid;
131: }
132: return gradebookUid;
133: }
134:
135: // The following getters are used by other backing beans. The setters are used only by
136: // the bean factory.
137:
138: /**
139: * @return Returns the gradebookManager.
140: */
141: public GradebookManager getGradebookManager() {
142: return gradebookManager;
143: }
144:
145: /**
146: * @param gradebookManager The gradebookManager to set.
147: */
148: public void setGradebookManager(GradebookManager gradebookManager) {
149: this .gradebookManager = gradebookManager;
150: }
151:
152: public SectionAwareness getSectionAwareness() {
153: return sectionAwareness;
154: }
155:
156: public void setSectionAwareness(SectionAwareness sectionAwareness) {
157: this .sectionAwareness = sectionAwareness;
158: }
159:
160: /**
161: * @return Returns the userDirectoryService.
162: */
163: public UserDirectoryService getUserDirectoryService() {
164: return userDirectoryService;
165: }
166:
167: /**
168: * @param userDirectoryService The userDirectoryService to set.
169: */
170: public void setUserDirectoryService(
171: UserDirectoryService userDirectoryService) {
172: this .userDirectoryService = userDirectoryService;
173: }
174:
175: /**
176: * @return Returns the authnService.
177: */
178: public Authn getAuthnService() {
179: return authnService;
180: }
181:
182: /**
183: * @param authnService The authnService to set.
184: */
185: public void setAuthnService(Authn authnService) {
186: this .authnService = authnService;
187: }
188:
189: public Authz getAuthzService() {
190: return authzService;
191: }
192:
193: public void setAuthzService(Authz authzService) {
194: this .authzService = authzService;
195: }
196:
197: /**
198: * @return Returns the contextManagementService.
199: */
200: public ContextManagement getContextManagementService() {
201: return contextManagementService;
202: }
203:
204: /**
205: * @param contextManagementService The contextManagementService to set.
206: */
207: public void setContextManagementService(
208: ContextManagement contextManagementService) {
209: this .contextManagementService = contextManagementService;
210: }
211:
212: public EventTrackingService getEventTrackingService() {
213: return eventTrackingService;
214: }
215:
216: public void setEventTrackingService(
217: EventTrackingService eventTrackingService) {
218: this .eventTrackingService = eventTrackingService;
219: }
220:
221: public ConfigurationBean getConfigurationBean() {
222: return configurationBean;
223: }
224:
225: public void setConfigurationBean(ConfigurationBean configurationBean) {
226: this.configurationBean = configurationBean;
227: }
228: }
|