001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/java/org/sakaiproject/tool/section/jsf/backingbean/EditManagersBean.java $
003: * $Id: EditManagersBean.java 22767 2007-03-16 17:41:08Z 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.ArrayList;
024: import java.util.Collections;
025: import java.util.Comparator;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Set;
030:
031: import javax.faces.context.FacesContext;
032: import javax.faces.model.SelectItem;
033:
034: import org.apache.commons.lang.StringUtils;
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.sakaiproject.section.api.coursemanagement.CourseSection;
038: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
039: import org.sakaiproject.section.api.coursemanagement.User;
040: import org.sakaiproject.section.api.exception.RoleConfigurationException;
041: import org.sakaiproject.section.api.facade.Role;
042: import org.sakaiproject.tool.section.decorator.SectionDecorator;
043: import org.sakaiproject.tool.section.jsf.JsfUtil;
044:
045: /**
046: * Controls the edit managers page (where TAs are assigned to sections).
047: *
048: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
049: *
050: */
051: public class EditManagersBean extends CourseDependentBean implements
052: Serializable {
053:
054: private static final long serialVersionUID = 1L;
055:
056: private static final Log log = LogFactory
057: .getLog(EditManagersBean.class);
058:
059: // For the right-side list box
060: protected List<SelectItem> selectedUsers;
061:
062: // For the left-side list box
063: protected List<SelectItem> availableUsers;
064:
065: protected String sectionUuid;
066: protected String sectionTitle;
067: protected String sectionDescription;
068:
069: protected boolean externallyManaged;
070:
071: /**
072: * Compares ParticipationRecords by users' sortNames.
073: */
074: static Comparator sortNameComparator = new Comparator() {
075: public int compare(Object o1, Object o2) {
076: ParticipationRecord manager1 = (ParticipationRecord) o1;
077: ParticipationRecord manager2 = (ParticipationRecord) o2;
078: return manager1.getUser().getSortName().compareTo(
079: manager2.getUser().getSortName());
080: }
081: };
082:
083: protected SectionDecorator initializeFields() {
084: // Determine whether this course is externally managed
085: externallyManaged = getSectionManager().isExternallyManaged(
086: getCourse().getUuid());
087:
088: // Get the section to edit
089: String sectionUuidFromParam = (String) FacesContext
090: .getCurrentInstance().getExternalContext()
091: .getRequestParameterMap().get("sectionUuid");
092: if (sectionUuidFromParam != null) {
093: sectionUuid = sectionUuidFromParam;
094: }
095: SectionDecorator currentSection = new SectionDecorator(
096: getSectionManager().getSection(sectionUuid), true);
097:
098: sectionTitle = currentSection.getTitle();
099:
100: // Generate the description
101: // String sectionMeetingTimes = currentSection.getMeetings();
102: // if(StringUtils.trimToNull(sectionMeetingTimes) == null) {
103: sectionDescription = sectionTitle;
104: // } else {
105: // sectionDescription = JsfUtil.getLocalizedMessage("section_description",
106: // new String[] {sectionTitle, sectionMeetingTimes});
107: // }
108:
109: return currentSection;
110: }
111:
112: protected void populateSelectedUsers(List participationRecords) {
113: // Build the list of items for the right-side list box
114: selectedUsers = new ArrayList<SelectItem>();
115: for (Iterator iter = participationRecords.iterator(); iter
116: .hasNext();) {
117: ParticipationRecord record = (ParticipationRecord) iter
118: .next();
119: SelectItem item = new SelectItem(record.getUser()
120: .getUserUid(), record.getUser().getSortName());
121: selectedUsers.add(item);
122: }
123: }
124:
125: public void init() {
126: initializeFields();
127:
128: // Get the current users in the manager role for this section
129: List<ParticipationRecord> selectedManagers = getSectionManager()
130: .getSectionTeachingAssistants(sectionUuid);
131: Collections.sort(selectedManagers, sortNameComparator);
132:
133: populateSelectedUsers(selectedManagers);
134:
135: // Build the list of items for the left-side box. Since the selected (right-side)
136: // participation records are linked to a section, while the available records
137: // are linked to the course, we can not use collection manipulation on these
138: // objects. So, generate a set of user uuids to filter out the currently
139: // selected users from the available (left side) list.
140: Set<String> selectedUserUuids = new HashSet<String>();
141: for (Iterator<ParticipationRecord> iter = selectedManagers
142: .iterator(); iter.hasNext();) {
143: ParticipationRecord manager = iter.next();
144: selectedUserUuids.add(manager.getUser().getUserUid());
145: }
146:
147: List availableManagers = getSectionManager()
148: .getSiteTeachingAssistants(getSiteContext());
149: Collections.sort(availableManagers, sortNameComparator);
150:
151: availableUsers = new ArrayList<SelectItem>();
152: for (Iterator iter = availableManagers.iterator(); iter
153: .hasNext();) {
154: User manager = ((ParticipationRecord) iter.next())
155: .getUser();
156: if (!selectedUserUuids.contains(manager.getUserUid())) {
157: availableUsers.add(new SelectItem(manager.getUserUid(),
158: manager.getSortName()));
159: }
160: }
161: }
162:
163: public String update() {
164: CourseSection section = getSectionManager().getSection(
165: sectionUuid);
166:
167: // The section might have been deleted
168: if (section == null) {
169: JsfUtil.addErrorMessage(JsfUtil
170: .getLocalizedMessage("error_section_deleted"));
171: return "overview";
172: }
173:
174: Set userUids = getHighlightedUsers("memberForm:selectedUsers");
175: try {
176: getSectionManager().setSectionMemberships(userUids,
177: Role.TA, sectionUuid);
178: } catch (RoleConfigurationException rce) {
179: JsfUtil.addErrorMessage(JsfUtil
180: .getLocalizedMessage("role_config_error"));
181: return null;
182: }
183:
184: JsfUtil.addRedirectSafeInfoMessage(JsfUtil.getLocalizedMessage(
185: "edit_manager_successful",
186: new String[] { sectionTitle }));
187:
188: return "overview";
189: }
190:
191: public String cancel() {
192: return "overview";
193: }
194:
195: protected Set<String> getHighlightedUsers(String componentId) {
196: Set<String> userUids = new HashSet<String>();
197:
198: String[] highlighted = (String[]) FacesContext
199: .getCurrentInstance().getExternalContext()
200: .getRequestParameterValuesMap().get(componentId);
201:
202: if (highlighted != null) {
203: for (int i = 0; i < highlighted.length; i++) {
204: userUids.add(highlighted[i]);
205: }
206: }
207: return userUids;
208: }
209:
210: public List<SelectItem> getAvailableUsers() {
211: return availableUsers;
212: }
213:
214: public void setAvailableUsers(List<SelectItem> availableUsers) {
215: this .availableUsers = availableUsers;
216: }
217:
218: public List<SelectItem> getSelectedUsers() {
219: return selectedUsers;
220: }
221:
222: public void setSelectedUsers(List<SelectItem> selectedUsers) {
223: this .selectedUsers = selectedUsers;
224: }
225:
226: public String getSectionUuid() {
227: return sectionUuid;
228: }
229:
230: public void setSectionUuid(String sectionUuid) {
231: this .sectionUuid = sectionUuid;
232: }
233:
234: public String getSectionTitle() {
235: return sectionTitle;
236: }
237:
238: public boolean isExternallyManaged() {
239: return externallyManaged;
240: }
241:
242: public String getSectionDescription() {
243: return sectionDescription;
244: }
245:
246: public String getAbbreviatedSectionTitle() {
247: return StringUtils.abbreviate(sectionTitle, 15);
248: }
249: }
|