001: /**********************************************************************************
002: *
003: * $Id: GradebookDependentBean.java 22044 2007-03-01 20:47:59Z louis@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 java.text.DateFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.commons.lang.StringUtils;
031: import org.sakaiproject.section.api.SectionAwareness;
032: import org.sakaiproject.tool.gradebook.Gradebook;
033: import org.sakaiproject.tool.gradebook.business.GradebookManager;
034: import org.sakaiproject.tool.gradebook.facades.Authn;
035: import org.sakaiproject.tool.gradebook.facades.UserDirectoryService;
036: import org.sakaiproject.tool.gradebook.jsf.FacesUtil;
037:
038: public abstract class GradebookDependentBean extends InitializableBean {
039: private String pageName;
040:
041: /**
042: * Marked transient to allow serializable subclasses.
043: */
044: private transient GradebookBean gradebookBean;
045: private transient PreferencesBean preferencesBean;
046:
047: /**
048: * Convenience method, for use in calling locally implemented services
049: * that assume the gradebook ID is an integer.
050: */
051: Long getGradebookId() {
052: return getGradebookBean().getGradebookId();
053: }
054:
055: /**
056: * Convenience method, for use in calling external facades
057: * that assume the gradebook ID is an string.
058: */
059: private transient String gradebookUid;
060:
061: String getGradebookUid() {
062: if (gradebookUid == null) {
063: gradebookUid = getGradebookManager().getGradebookUid(
064: getGradebookId());
065: }
066: return gradebookUid;
067: }
068:
069: /**
070: * Convenience method to hide the Authn context object.
071: */
072: public String getUserUid() {
073: return getAuthnService().getUserUid();
074: }
075:
076: /**
077: * Convenience method to load the current gradebook object.
078: */
079: Gradebook getGradebook() {
080: return getGradebookManager().getGradebook(getGradebookId());
081: }
082:
083: /**
084: * Gets a localized message string based on the locale determined by the
085: * FacesContext. Useful for adding localized FacesMessages from a backing bean.
086: *
087: * TODO Replace with direct calls to FacesUtil.
088: *
089: * @param key The key to look up the localized string
090: */
091: public String getLocalizedString(String key) {
092: return FacesUtil.getLocalizedString(key);
093: }
094:
095: /**
096: * Gets a localized message string based on the locale determined by the
097: * FacesContext. Useful for adding localized FacesMessages from a backing bean.
098: *
099: * TODO Replace with direct calls to FacesUtil.
100: *
101: * @param key The key to look up the localized string
102: * @param params The array of strings to use in replacing the placeholders
103: * in the localized string
104: */
105: public String getLocalizedString(String key, String[] params) {
106: return FacesUtil.getLocalizedString(key, params);
107: }
108:
109: // Still more convenience methods, hiding the bean configuration details.
110:
111: public GradebookManager getGradebookManager() {
112: return getGradebookBean().getGradebookManager();
113: }
114:
115: public SectionAwareness getSectionAwareness() {
116: return getGradebookBean().getSectionAwareness();
117: }
118:
119: public UserDirectoryService getUserDirectoryService() {
120: return getGradebookBean().getUserDirectoryService();
121: }
122:
123: public Authn getAuthnService() {
124: return getGradebookBean().getAuthnService();
125: }
126:
127: // Because these methods are referred to inside "rendered" tag attributes,
128: // JSF will call them multiple times in every request. To cut back on
129: // business logic traffic, cache them in request scope. They need to be
130: // declared transient, however, so that they aren't copied between
131: // requests (which would prevent changes in a user's authz status).
132: private transient Boolean userAbleToEditAssessments;
133:
134: public boolean isUserAbleToEditAssessments() {
135: if (userAbleToEditAssessments == null) {
136: userAbleToEditAssessments = new Boolean(getGradebookBean()
137: .getAuthzService().isUserAbleToEditAssessments(
138: getGradebookUid()));
139: }
140: return userAbleToEditAssessments.booleanValue();
141: }
142:
143: private transient Boolean userAbleToGradeAll;
144:
145: public boolean isUserAbleToGradeAll() {
146: if (userAbleToGradeAll == null) {
147: userAbleToGradeAll = new Boolean(getGradebookBean()
148: .getAuthzService().isUserAbleToGradeAll(
149: getGradebookUid()));
150: }
151: return userAbleToGradeAll.booleanValue();
152: }
153:
154: private transient Map userAbleToGradeSectionMap;
155:
156: public boolean isUserAbleToGradeSection(String sectionUid) {
157: if (userAbleToGradeSectionMap == null) {
158: userAbleToGradeSectionMap = new HashMap();
159: }
160: Boolean isAble = (Boolean) userAbleToGradeSectionMap
161: .get(sectionUid);
162: if (isAble == null) {
163: isAble = new Boolean(getGradebookBean().getAuthzService()
164: .isUserAbleToGradeSection(sectionUid));
165: userAbleToGradeSectionMap.put(sectionUid, isAble);
166: }
167: return isAble.booleanValue();
168: }
169:
170: public List getAvailableEnrollments() {
171: return getGradebookBean().getAuthzService()
172: .getAvailableEnrollments(getGradebookUid());
173: }
174:
175: public List getAvailableSections() {
176: return getGradebookBean().getAuthzService()
177: .getAvailableSections(getGradebookUid());
178: }
179:
180: public List getSectionEnrollments(String sectionUid) {
181: return getGradebookBean().getAuthzService()
182: .getSectionEnrollments(getGradebookUid(), sectionUid);
183: }
184:
185: public List findMatchingEnrollments(String searchString,
186: String optionalSectionUid) {
187: return getGradebookBean().getAuthzService()
188: .findMatchingEnrollments(getGradebookUid(),
189: searchString, optionalSectionUid);
190: }
191:
192: /**
193: * Get the gradebook context.
194: */
195: public GradebookBean getGradebookBean() {
196: if (gradebookBean == null) {
197: // This probably happened because gradebookBean is transient.
198: // Just restore it from the session context.
199: setGradebookBean((GradebookBean) FacesUtil
200: .resolveVariable("gradebookBean"));
201: }
202: return gradebookBean;
203: }
204:
205: /**
206: * Set the gradebook context.
207: */
208: public void setGradebookBean(GradebookBean gradebookBean) {
209: this .gradebookBean = gradebookBean;
210: }
211:
212: /**
213: * @return Returns the preferencesBean.
214: */
215: public PreferencesBean getPreferencesBean() {
216: if (preferencesBean == null) {
217: setPreferencesBean((PreferencesBean) FacesUtil
218: .resolveVariable("preferencesBean"));
219: }
220: return preferencesBean;
221: }
222:
223: /**
224: * @param preferencesBean The preferencesBean to set.
225: */
226: public void setPreferencesBean(PreferencesBean preferencesBean) {
227: this .preferencesBean = preferencesBean;
228: }
229:
230: /**
231: * Set up close relations with page and action names for easier control
232: * of menus.
233: */
234: public String getPageName() {
235: return pageName;
236: }
237:
238: public void setPageName(String pageName) {
239: this .pageName = pageName;
240: }
241:
242: /**
243: * Generates a default filename (minus the extension) for a download from this Gradebook.
244: *
245: * @param prefix for filename
246: * @return The appropriate filename for the export
247: */
248: public String getDownloadFileName(String prefix) {
249: Date now = new Date();
250: DateFormat df = new SimpleDateFormat(
251: getLocalizedString("export_filename_date_format"));
252: StringBuffer fileName = new StringBuffer(prefix);
253: String gbName = getGradebook().getName();
254: if (StringUtils.trimToNull(gbName) != null) {
255: gbName = gbName.replaceAll("\\s", "_"); // replace whitespace with '_'
256: fileName.append("-");
257: fileName.append(gbName);
258: }
259: fileName.append("-");
260: fileName.append(df.format(now));
261: return fileName.toString();
262: }
263:
264: /**
265: *
266: * @return
267: */
268: public String getAuthzLevel() {
269: return (getGradebookBean().getAuthzService()
270: .isUserAbleToGradeAll(getGradebookUid())) ? "instructor"
271: : "TA";
272: }
273:
274: }
|