0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/postem/tags/sakai_2-4-1/postem-app/src/java/org/sakaiproject/tool/postem/PostemTool.java $
0003: * $Id: PostemTool.java 28762 2007-04-12 02:22:38Z ajpoland@iupui.edu $
0004: ***********************************************************************************
0005: *
0006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
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.tool.postem;
0021:
0022: import java.sql.Timestamp;
0023: import java.util.ArrayList;
0024: import java.util.Date;
0025: import java.util.Iterator;
0026: import java.util.List;
0027: import java.util.Map;
0028: import java.util.Set;
0029: import java.util.TreeMap;
0030: import java.util.TreeSet;
0031: import java.util.logging.Logger;
0032: import java.util.zip.DataFormatException;
0033:
0034: import javax.faces.FactoryFinder;
0035: import javax.faces.application.ApplicationFactory;
0036: import javax.faces.application.FacesMessage;
0037: import javax.faces.component.UIData;
0038: import javax.faces.context.FacesContext;
0039:
0040: import org.apache.commons.logging.Log;
0041: import org.apache.commons.logging.LogFactory;
0042: import org.apache.myfaces.util.MessageUtils;
0043: import org.sakaiproject.api.app.postem.data.Gradebook;
0044: import org.sakaiproject.api.app.postem.data.GradebookManager;
0045: import org.sakaiproject.api.app.postem.data.StudentGrades;
0046:
0047: import org.sakaiproject.authz.api.AuthzGroup;
0048: import org.sakaiproject.authz.cover.AuthzGroupService;
0049: import org.sakaiproject.authz.api.GroupNotDefinedException;
0050:
0051: import org.sakaiproject.exception.PermissionException;
0052: import org.sakaiproject.site.cover.SiteService;
0053: import org.sakaiproject.tool.api.Placement;
0054: import org.sakaiproject.tool.cover.SessionManager;
0055: import org.sakaiproject.tool.cover.ToolManager;
0056: import org.sakaiproject.util.ResourceLoader;
0057:
0058: import org.sakaiproject.user.cover.UserDirectoryService;
0059: import org.sakaiproject.user.api.UserNotDefinedException;
0060:
0061: public class PostemTool {
0062: protected GradebookManager gradebookManager;
0063:
0064: // protected Logger logger = Logger.getLogger(PostemTool.class);
0065:
0066: protected ArrayList gradebooks;
0067:
0068: protected Gradebook currentGradebook;
0069:
0070: protected Gradebook oldGradebook;
0071:
0072: protected String userId;
0073:
0074: protected String userEid;
0075:
0076: protected String filename = null;
0077:
0078: protected String csv = null;
0079:
0080: protected String siteId = null;
0081:
0082: protected UIData gradebookTable;
0083:
0084: protected String title;
0085:
0086: protected String editAble;
0087:
0088: protected boolean editable;
0089:
0090: protected String newTemplate;
0091:
0092: protected ArrayList students;
0093:
0094: protected String delimiter;
0095:
0096: protected boolean ascending = true;
0097:
0098: protected String sortBy = Gradebook.SORT_BY_TITLE;
0099:
0100: protected boolean displayErrors;
0101:
0102: protected boolean userPressedBack = false;
0103:
0104: protected boolean gradebooksExist = true;
0105:
0106: private static final int TEMPLATE_MAX_LENGTH = 4000;
0107: private static final int TITLE_MAX_LENGTH = 255;
0108:
0109: private static final String COMMA_DELIM_STR = "comma";
0110: private static final String TAB_DELIM_STR = "tab";
0111:
0112: protected Logger logger = null;
0113:
0114: // protected String release = "yes";
0115:
0116: protected String selectedStudent;
0117:
0118: protected boolean withHeader = true;
0119:
0120: protected int column = 0;
0121:
0122: public static final String messageBundle = "org.sakaiproject.tool.postem.bundle.Messages";
0123: public ResourceLoader msgs = new ResourceLoader(messageBundle);
0124:
0125: private static final Log LOG = LogFactory.getLog(PostemTool.class);
0126:
0127: public void setLogger(Logger logger) {
0128: this .logger = logger;
0129: }
0130:
0131: public int getColumn() {
0132: return column;
0133: }
0134:
0135: public void setColumn(int column) {
0136: this .column = column;
0137: }
0138:
0139: /*
0140: * public void setRelease(String release) { this.release = release; }
0141: *
0142: * public String getRelease() { return release; }
0143: */
0144: public ArrayList getGradebooks() {
0145: if (userId == null) {
0146: userId = SessionManager.getCurrentSessionUserId();
0147:
0148: if (userId != null) {
0149: try {
0150: userEid = UserDirectoryService.getUserEid(userId);
0151: } catch (UserNotDefinedException e) {
0152: LOG.error("UserNotDefinedException", e);
0153: }
0154: }
0155: }
0156:
0157: Placement placement = ToolManager.getCurrentPlacement();
0158: String currentSiteId = placement.getContext();
0159:
0160: siteId = currentSiteId;
0161: try {
0162: if (checkAccess()) {
0163: // logger.info("**** Getting by context!");
0164: gradebooks = new ArrayList(gradebookManager
0165: .getGradebooksByContext(siteId, sortBy,
0166: ascending));
0167: } else {
0168: // logger.info("**** Getting RELEASED by context!");
0169: gradebooks = new ArrayList(gradebookManager
0170: .getReleasedGradebooksByContext(siteId, sortBy,
0171: ascending));
0172: }
0173: } catch (Exception e) {
0174: gradebooks = null;
0175: }
0176:
0177: if (gradebooks != null && gradebooks.size() > 0)
0178: gradebooksExist = true;
0179: else
0180: gradebooksExist = false;
0181:
0182: return gradebooks;
0183:
0184: }
0185:
0186: public boolean getGradebooksExist() {
0187: return gradebooksExist;
0188: }
0189:
0190: public String getCsv() {
0191: return csv;
0192: }
0193:
0194: public void setCsv(String csv) {
0195: this .csv = csv;
0196: }
0197:
0198: public String getDelimiter() {
0199: return delimiter;
0200: }
0201:
0202: public void setDelimiter(String delimiter) {
0203: this .delimiter = delimiter;
0204: }
0205:
0206: public String getNewTemplate() {
0207: return newTemplate;
0208: }
0209:
0210: public void setNewTemplate(String newTemplate) {
0211: this .newTemplate = newTemplate;
0212: }
0213:
0214: public GradebookManager getGradebookManager() {
0215: return this .gradebookManager;
0216: }
0217:
0218: public void setGradebookManager(GradebookManager gradebookManager) {
0219: this .gradebookManager = gradebookManager;
0220: }
0221:
0222: public UIData getGradebookTable() {
0223: return gradebookTable;
0224: }
0225:
0226: public void setGradebookTable(UIData gradebookTable) {
0227: this .gradebookTable = gradebookTable;
0228: }
0229:
0230: public Gradebook getCurrentGradebook() {
0231: return currentGradebook;
0232: }
0233:
0234: public void setCurrentGradebook(Gradebook currentGradebook) {
0235: this .currentGradebook = currentGradebook;
0236: }
0237:
0238: public String getSelectedStudent() {
0239: return selectedStudent;
0240: }
0241:
0242: public void setSelectedStudent(String selectedStudent) {
0243: this .selectedStudent = selectedStudent;
0244: }
0245:
0246: public boolean getWithHeader() {
0247: return withHeader;
0248: }
0249:
0250: public void setWithHeader(boolean withHeader) {
0251: this .withHeader = withHeader;
0252: }
0253:
0254: public boolean getDisplayErrors() {
0255: for (Iterator iter = FacesContext.getCurrentInstance()
0256: .getMessages(); iter.hasNext();) {
0257: return true; // there is at least one message to display
0258: }
0259: return false;
0260: }
0261:
0262: public void setDisplayErrors(boolean displayErrors) {
0263: this .displayErrors = displayErrors;
0264: }
0265:
0266: public String getCurrentStudentGrades() {
0267: this .userId = SessionManager.getCurrentSessionUserId();
0268: try {
0269: this .userEid = UserDirectoryService.getUserEid(this .userId);
0270:
0271: /*
0272: * if (checkAccess()) { if (currentGradebook.getTemplate() != null) { return
0273: * currentGradebook.getTemplate().getTemplateCode(); } else { return "<p>No
0274: * template currently exists for this gradebook.</p>"; } }
0275: */
0276: if (currentGradebook == null) {
0277: return "<p>" + msgs.getString("no_gradebook_selected")
0278: + "</p>";
0279: }
0280: if (!currentGradebook.hasStudent(this .userEid)) {
0281: return "<p>" + msgs.getString("no_grades_for_user")
0282: + " " + currentGradebook.getTitle() + ".</p>";
0283: }
0284: return currentGradebook.studentGrades(this .userEid)
0285: .formatGrades();
0286:
0287: } catch (UserNotDefinedException e) {
0288: LOG.error("UserNotDefinedException:", e);
0289: }
0290:
0291: return "";
0292:
0293: }
0294:
0295: public String getFirstStudentGrades() {
0296: if (currentGradebook == null) {
0297: return "<p>" + msgs.getString("no_gradebook_selected")
0298: + "</p>";
0299: }
0300: Set students = currentGradebook.getStudents();
0301: if (students.size() == 0) {
0302: return "<p>" + msgs.getString("no_grades_in_gradebook")
0303: + " " + currentGradebook.getTitle() + ".</p>";
0304: }
0305: if (currentGradebook.getFirstUploadedUsername() != null) {
0306: StudentGrades student = currentGradebook
0307: .studentGrades(currentGradebook
0308: .getFirstUploadedUsername());
0309: return student.formatGrades();
0310: } else {
0311: StudentGrades student = (StudentGrades) students.iterator()
0312: .next();
0313: return student.formatGrades();
0314: }
0315: }
0316:
0317: public String getSelectedStudentGrades() {
0318: if (currentGradebook == null) {
0319: return "<p>" + msgs.getString("no_gradebook_selected")
0320: + "</p>";
0321: }
0322: Set students = currentGradebook.getStudents();
0323: if (students.size() == 0) {
0324: return "<p>" + msgs.getString("no_grades_in_gradebook")
0325: + " " + currentGradebook.getTitle() + ".</p>";
0326: }
0327: if (selectedStudent == null) {
0328: return msgs.getString("select_participant");
0329: }
0330:
0331: Iterator iter = students.iterator();
0332: while (iter.hasNext()) {
0333: StudentGrades student = (StudentGrades) iter.next();
0334: if (selectedStudent.equals(student.getUsername())) {
0335: return student.formatGrades();
0336: }
0337: }
0338: return msgs.getString("select_participant");
0339: }
0340:
0341: public void toggleSort(String sortByType) {
0342: if (sortBy.equals(sortByType)) {
0343: if (ascending) {
0344: ascending = false;
0345: } else {
0346: ascending = true;
0347: }
0348: } else {
0349: sortBy = sortByType;
0350: ascending = true;
0351: }
0352: }
0353:
0354: public String toggleTitleSort() {
0355: toggleSort(Gradebook.SORT_BY_TITLE);
0356: return "main";
0357: }
0358:
0359: public String toggleCreatorSort() {
0360: toggleSort(Gradebook.SORT_BY_CREATOR);
0361: return "main";
0362: }
0363:
0364: public String toggleModBySort() {
0365: toggleSort(Gradebook.SORT_BY_MOD_BY);
0366: return "main";
0367: }
0368:
0369: public String toggleModDateSort() {
0370: toggleSort(Gradebook.SORT_BY_MOD_DATE);
0371: return "main";
0372: }
0373:
0374: public String toggleReleasedSort() {
0375: toggleSort(Gradebook.SORT_BY_RELEASED);
0376: return "main";
0377: }
0378:
0379: public boolean isTitleSort() {
0380: if (sortBy.equals(Gradebook.SORT_BY_TITLE))
0381: return true;
0382: return false;
0383: }
0384:
0385: public boolean isCreatorSort() {
0386: if (sortBy.equals(Gradebook.SORT_BY_CREATOR))
0387: return true;
0388: return false;
0389: }
0390:
0391: public boolean isModBySort() {
0392: if (sortBy.equals(Gradebook.SORT_BY_MOD_BY))
0393: return true;
0394: return false;
0395: }
0396:
0397: public boolean isModDateSort() {
0398: if (sortBy.equals(Gradebook.SORT_BY_MOD_DATE))
0399: return true;
0400: return false;
0401: }
0402:
0403: public boolean isReleasedSort() {
0404: if (sortBy.equals(Gradebook.SORT_BY_RELEASED))
0405: return true;
0406: return false;
0407: }
0408:
0409: public boolean isAscending() {
0410: return ascending;
0411: }
0412:
0413: public String processCreateNew() {
0414: try {
0415: if (!this .checkAccess()) {
0416: throw new PermissionException(SessionManager
0417: .getCurrentSessionUserId(),
0418: "syllabus_access_athz", "");
0419: }
0420:
0421: } catch (PermissionException e) {
0422: // logger.info(this + ".getEntries() in PostemTool " + e);
0423: FacesContext.getCurrentInstance().addMessage(
0424: null,
0425: MessageUtils.getMessage(
0426: FacesMessage.SEVERITY_ERROR,
0427: "error_permission", (new Object[] { e
0428: .toString() }), FacesContext
0429: .getCurrentInstance()));
0430: return "permission_error";
0431: }
0432: this .userId = SessionManager.getCurrentSessionUserId();
0433: this .siteId = ToolManager.getCurrentPlacement().getContext();
0434: this .currentGradebook = gradebookManager.createEmptyGradebook(
0435: this .userId, this .siteId);
0436: this .oldGradebook = gradebookManager.createEmptyGradebook(
0437: this .userId, this .siteId);
0438: this .csv = null;
0439: this .newTemplate = null;
0440: this .delimiter = COMMA_DELIM_STR;
0441:
0442: return "create_gradebook";
0443: }
0444:
0445: public String processGradebookUpdate() {
0446: try {
0447: if (!this .checkAccess()) {
0448: throw new PermissionException(SessionManager
0449: .getCurrentSessionUserId(),
0450: "syllabus_access_athz", "");
0451: }
0452:
0453: } catch (PermissionException e) {
0454: // logger.info(this + ".getEntries() in PostemTool " + e);
0455: FacesContext.getCurrentInstance().addMessage(
0456: null,
0457: MessageUtils.getMessage(
0458: FacesMessage.SEVERITY_ERROR,
0459: "error_permission", (new Object[] { e
0460: .toString() }), FacesContext
0461: .getCurrentInstance()));
0462: this .currentGradebook = null;
0463: this .csv = null;
0464: this .newTemplate = null;
0465: return "permission_error";
0466: }
0467: this .userId = SessionManager.getCurrentSessionUserId();
0468: this .siteId = ToolManager.getCurrentPlacement().getContext();
0469: currentGradebook = (Gradebook) gradebookTable.getRowData();
0470: oldGradebook = gradebookManager.createEmptyGradebook(
0471: currentGradebook.getCreator(), currentGradebook
0472: .getContext());
0473: oldGradebook.setId(currentGradebook.getId());
0474: oldGradebook.setStudents(currentGradebook.getStudents());
0475:
0476: gradebooks = null;
0477:
0478: /*
0479: * if(new Boolean(true).equals(currentGradebook.getReleased())) {
0480: * this.release = "Yes"; } else { this.release = "No"; }
0481: */
0482: this .csv = null;
0483: this .newTemplate = null;
0484: this .delimiter = COMMA_DELIM_STR;
0485:
0486: return "create_gradebook";
0487:
0488: }
0489:
0490: public static void populateMessage(FacesMessage.Severity severity,
0491: String messageId, Object[] args) {
0492: ApplicationFactory factory = (ApplicationFactory) FactoryFinder
0493: .getFactory(FactoryFinder.APPLICATION_FACTORY);
0494: factory.getApplication().setMessageBundle(messageBundle);
0495: FacesContext.getCurrentInstance().addMessage(null,
0496: MessageUtils.getMessage(severity, messageId, args));
0497: }
0498:
0499: protected static void clearMessages() {
0500: Iterator iter = FacesContext.getCurrentInstance().getMessages(
0501: null);
0502: while (iter.hasNext()) {
0503: iter.next();
0504: iter.remove();
0505: }
0506: }
0507:
0508: public String processCreate() {
0509:
0510: try {
0511: if (!this .checkAccess()) {
0512: throw new PermissionException(SessionManager
0513: .getCurrentSessionUserId(),
0514: "syllabus_access_athz", "");
0515: }
0516:
0517: } catch (PermissionException e) {
0518: // logger.info(this + ".getEntries() in PostemTool " + e);
0519: FacesContext.getCurrentInstance().addMessage(
0520: null,
0521: MessageUtils.getMessage(
0522: FacesMessage.SEVERITY_ERROR,
0523: "error_permission", (new Object[] { e
0524: .toString() }), FacesContext
0525: .getCurrentInstance()));
0526: this .currentGradebook = null;
0527: this .csv = null;
0528: this .newTemplate = null;
0529: // this.release = null;
0530: return "permission_error";
0531: }
0532: if (currentGradebook.getId() == null) {
0533: ArrayList gb = getGradebooks();
0534: Iterator gi = gb.iterator();
0535: while (gi.hasNext()) {
0536: if (((Gradebook) gi.next()).getTitle().equals(
0537: currentGradebook.getTitle())) {
0538: //To stay consistent, remove current messages when adding a new message
0539: //so as to only display one error message before returning
0540: PostemTool.clearMessages();
0541: PostemTool.populateMessage(
0542: FacesMessage.SEVERITY_ERROR,
0543: "duplicate_title", new Object[] {});
0544: return "create_gradebook";
0545: }
0546: }
0547: }
0548: if (currentGradebook.getTitle() == null
0549: || currentGradebook.getTitle().equals("")) {
0550: //To stay consistent, remove current messages when adding a new message
0551: //so as to only display one error message before returning
0552: PostemTool.clearMessages();
0553: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
0554: "missing_title", new Object[] {});
0555: return "create_gradebook";
0556: } else if (currentGradebook.getTitle().trim().length() > TITLE_MAX_LENGTH) {
0557: PostemTool.clearMessages();
0558: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
0559: "title_too_long", new Object[] {
0560: new Integer(currentGradebook.getTitle()
0561: .trim().length()),
0562: new Integer(TITLE_MAX_LENGTH) });
0563: return "create_gradebook";
0564: }
0565:
0566: if (this .csv == null || this .csv.trim().length() == 0) {
0567: return "create_gradebook";
0568: }
0569:
0570: if (!this .delimiter.equals(COMMA_DELIM_STR)
0571: && !this .delimiter.equals(TAB_DELIM_STR)) {
0572: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
0573: "invalid_delim", new Object[] {});
0574: return "create_gradebook";
0575: }
0576:
0577: if (this .csv != null && this .csv.trim().length() > 0) {
0578: // logger.info("*** Non-Empty CSV!");
0579: try {
0580:
0581: char csv_delim = CSV.COMMA_DELIM;
0582: if (this .delimiter.equals(TAB_DELIM_STR)) {
0583: csv_delim = CSV.TAB_DELIM;
0584: }
0585:
0586: CSV grades = new CSV(csv, withHeader, csv_delim);
0587:
0588: if (withHeader == true) {
0589: if (grades.getHeaders() != null) {
0590: // Make sure there are no blank headings
0591: List headingList = grades.getHeaders();
0592: for (int col = 0; col < headingList.size(); col++) {
0593: String heading = (String) headingList.get(
0594: col).toString().trim();
0595: if (heading.equals("") || heading == null) {
0596: PostemTool.populateMessage(
0597: FacesMessage.SEVERITY_ERROR,
0598: "blank_headings",
0599: new Object[] {});
0600: return "create_gradebook";
0601: }
0602: }
0603: }
0604: }
0605:
0606: if (grades.getStudents() != null) {
0607: if (!usernamesValid(grades)) {
0608: return "create_gradebook";
0609: }
0610:
0611: if (hasADuplicateUsername(grades)) {
0612: return "create_gradebook";
0613: }
0614: }
0615:
0616: if (this .newTemplate != null
0617: && this .newTemplate.trim().length() > 0) {
0618: if (this .newTemplate.trim().length() > TEMPLATE_MAX_LENGTH) {
0619: PostemTool
0620: .populateMessage(
0621: FacesMessage.SEVERITY_ERROR,
0622: "template_too_long",
0623: new Object[] {
0624: new Integer(
0625: this .newTemplate
0626: .trim()
0627: .length()),
0628: new Integer(
0629: TEMPLATE_MAX_LENGTH) });
0630: return "create_gradebook";
0631: }
0632: }
0633:
0634: if (withHeader == true) {
0635: if (grades.getHeaders() != null) {
0636: PostemTool.populateMessage(
0637: FacesMessage.SEVERITY_ERROR,
0638: "has_headers", new Object[] {});
0639: }
0640: }
0641: if (grades.getStudents() != null) {
0642: PostemTool.populateMessage(
0643: FacesMessage.SEVERITY_ERROR,
0644: "has_students", new Object[] { new Integer(
0645: grades.getStudents().size()) });
0646: }
0647: if (withHeader == true) {
0648: currentGradebook.setHeadings(grades.getHeaders());
0649: }
0650: List slist = grades.getStudents();
0651:
0652: if (oldGradebook.getId() != null
0653: && !this .userPressedBack) {
0654: Set oldStudents = currentGradebook.getStudents();
0655: oldGradebook.setStudents(oldStudents);
0656: }
0657:
0658: currentGradebook.setStudents(new TreeSet());
0659: // gradebookManager.saveGradebook(currentGradebook);
0660: Iterator si = slist.iterator();
0661: while (si.hasNext()) {
0662: List ss = (List) si.next();
0663: String uname = ((String) ss.remove(0)).trim();
0664: // logger.info("[POSTEM] processCreate -- adding student " +
0665: // uname);
0666: gradebookManager.createStudentGradesInGradebook(
0667: uname, ss, currentGradebook);
0668: if (currentGradebook.getStudents().size() == 1) {
0669: currentGradebook
0670: .setFirstUploadedUsername(uname); //otherwise, the verify screen shows first in ABC order
0671: }
0672: }
0673: } catch (DataFormatException exception) {
0674: /*
0675: * TODO: properly subclass exception in order to allow for localized
0676: * messages (add getRowNumber/setRowNumber). Set exception message to be
0677: * key in .properties file
0678: */
0679: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
0680: exception.getMessage(), new Object[] {});
0681: return "create_gradebook";
0682: }
0683: } else if (this .csv != null) {
0684: // logger.info("**** Non Null Empty CSV!");
0685: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
0686: "has_students", new Object[] { new Integer(0) });
0687: currentGradebook.setHeadings(new ArrayList());
0688: if (oldGradebook.getId() != null) {
0689: Set oldStudents = currentGradebook.getStudents();
0690: oldGradebook.setStudents(oldStudents);
0691: }
0692:
0693: currentGradebook.setStudents(new TreeSet());
0694: }
0695:
0696: if (this .newTemplate != null
0697: && this .newTemplate.trim().length() > 0) {
0698: currentGradebook.setTemplate(gradebookManager
0699: .createTemplate(newTemplate.trim()));
0700: } else if (this .newTemplate != null) {
0701: // logger.info("*** Non Null Empty Template!");
0702: currentGradebook.setTemplate(null);
0703: }
0704:
0705: /*
0706: * if("No".equals(this.release)) { currentGradebook.setReleased(new
0707: * Boolean(false)); //logger.info("Set to No, " +
0708: * currentGradebook.getReleased()); } else {
0709: * currentGradebook.setReleased(new Boolean(true)); //logger.info("Set to
0710: * Yes, " + currentGradebook.getReleased()); }
0711: */
0712:
0713: // gradebookManager.saveGradebook(currentGradebook);
0714: // logger.info(currentGradebook.getId());
0715: // currentGradebook = null;
0716: if ((this .csv != null && this .csv.trim().length() > 0)
0717: || (this .newTemplate != null && this .newTemplate.trim()
0718: .length() > 0)) {
0719: this .csv = null;
0720: this .newTemplate = null;
0721: return "verify";
0722: }
0723:
0724: Iterator oi = oldGradebook.getStudents().iterator();
0725: while (oi.hasNext()) {
0726: gradebookManager.deleteStudentGrades((StudentGrades) oi
0727: .next());
0728: }
0729: this .userId = SessionManager.getCurrentSessionUserId();
0730: currentGradebook.setLastUpdated(new Timestamp(new Date()
0731: .getTime()));
0732: currentGradebook.setLastUpdater(this .userId);
0733: gradebookManager.saveGradebook(currentGradebook);
0734:
0735: this .currentGradebook = null;
0736: this .oldGradebook = null;
0737: this .withHeader = true;
0738: // this.gradebooks = null;
0739: return "main";
0740: }
0741:
0742: public String processCreateOk() {
0743: Iterator oi = oldGradebook.getStudents().iterator();
0744: while (oi.hasNext()) {
0745: gradebookManager.deleteStudentGrades((StudentGrades) oi
0746: .next());
0747: }
0748: this .userId = SessionManager.getCurrentSessionUserId();
0749: currentGradebook.setLastUpdated(new Timestamp(new Date()
0750: .getTime()));
0751: currentGradebook.setLastUpdater(this .userId);
0752: gradebookManager.saveGradebook(currentGradebook);
0753:
0754: this .currentGradebook = null;
0755: this .oldGradebook = null;
0756: this .withHeader = true;
0757: // this.gradebooks = null;
0758: return "main";
0759: }
0760:
0761: public String processCreateBack() {
0762: if (currentGradebook.getId() == null) {
0763: this .csv = null;
0764: currentGradebook.setStudents(null);
0765: }
0766: this .userPressedBack = true;
0767: return "create_gradebook";
0768: }
0769:
0770: public String processCancelNew() {
0771: this .currentGradebook = null;
0772: this .csv = null;
0773: this .newTemplate = null;
0774: return "main";
0775: }
0776:
0777: public String processCancelView() {
0778: currentGradebook = null;
0779: csv = null;
0780: newTemplate = null;
0781: students = null;
0782: return "main";
0783: }
0784:
0785: public String processGradebookView() {
0786: currentGradebook = (Gradebook) gradebookTable.getRowData();
0787: // logger.info("[POSTEM] processGradebookView -- " +
0788: // currentGradebook.getTitle());
0789: this .userId = SessionManager.getCurrentSessionUserId();
0790: if (currentGradebook.hasStudent(this .userEid)) {
0791: currentGradebook
0792: .studentGrades(this .userEid)
0793: .setLastChecked(new Timestamp(new Date().getTime()));
0794: gradebookManager.saveGradebook(currentGradebook);
0795: }
0796: if (checkAccess()) {
0797: TreeMap ss = currentGradebook.getStudentMap();
0798: setSelectedStudent((String) ss.firstKey());
0799: return "view_student";
0800: } else {
0801: return "view_grades";
0802: }
0803: }
0804:
0805: public String processInstructorView() {
0806: try {
0807: if (!this .checkAccess()) {
0808: throw new PermissionException(SessionManager
0809: .getCurrentSessionUserId(),
0810: "syllabus_access_athz", "");
0811: }
0812:
0813: } catch (PermissionException e) {
0814: // logger.info(this + ".getEntries() in PostemTool " + e);
0815: FacesContext.getCurrentInstance().addMessage(
0816: null,
0817: MessageUtils.getMessage(
0818: FacesMessage.SEVERITY_ERROR,
0819: "error_permission", (new Object[] { e
0820: .toString() }), FacesContext
0821: .getCurrentInstance()));
0822: return "permission_error";
0823: }
0824: currentGradebook = (Gradebook) gradebookTable.getRowData();
0825: students = new ArrayList(currentGradebook.getStudents());
0826:
0827: return "view_gradebook";
0828: }
0829:
0830: public String processGradebookDelete() {
0831: try {
0832: if (!this .checkAccess()) {
0833: throw new PermissionException(SessionManager
0834: .getCurrentSessionUserId(),
0835: "syllabus_access_athz", "");
0836: }
0837:
0838: } catch (PermissionException e) {
0839: // logger.info(this + ".getEntries() in PostemTool " + e);
0840: FacesContext.getCurrentInstance().addMessage(
0841: null,
0842: MessageUtils.getMessage(
0843: FacesMessage.SEVERITY_ERROR,
0844: "error_permission", (new Object[] { e
0845: .toString() }), FacesContext
0846: .getCurrentInstance()));
0847: return "permission_error";
0848: }
0849: currentGradebook = (Gradebook) gradebookTable.getRowData();
0850:
0851: return "delete_confirm";
0852:
0853: }
0854:
0855: public String processDelete() {
0856: try {
0857: if (!this .checkAccess()) {
0858: throw new PermissionException(SessionManager
0859: .getCurrentSessionUserId(),
0860: "syllabus_access_athz", "");
0861: }
0862:
0863: } catch (PermissionException e) {
0864: // logger.info(this + ".getEntries() in PostemTool " + e);
0865: FacesContext.getCurrentInstance().addMessage(
0866: null,
0867: MessageUtils.getMessage(
0868: FacesMessage.SEVERITY_ERROR,
0869: "error_permission", (new Object[] { e
0870: .toString() }), FacesContext
0871: .getCurrentInstance()));
0872: return "permission_error";
0873: }
0874: gradebookManager.deleteGradebook(currentGradebook);
0875: gradebooks = null;
0876: currentGradebook = null;
0877: csv = null;
0878: newTemplate = null;
0879: students = null;
0880: return "main";
0881: }
0882:
0883: public String processCsvDownload() {
0884: try {
0885: if (!this .checkAccess()) {
0886: throw new PermissionException(SessionManager
0887: .getCurrentSessionUserId(),
0888: "syllabus_access_athz", "");
0889: }
0890:
0891: } catch (PermissionException e) {
0892: // logger.info(this + ".getEntries() in PostemTool " + e);
0893: FacesContext.getCurrentInstance().addMessage(
0894: null,
0895: MessageUtils.getMessage(
0896: FacesMessage.SEVERITY_ERROR,
0897: "error_permission", (new Object[] { e
0898: .toString() }), FacesContext
0899: .getCurrentInstance()));
0900: return "permission_error";
0901: }
0902: currentGradebook = (Gradebook) gradebookTable.getRowData();
0903:
0904: List csvContents = new ArrayList();
0905: if (currentGradebook.getHeadings().size() > 0) {
0906: csvContents.add(currentGradebook.getHeadings());
0907: }
0908: Iterator si = currentGradebook.getStudents().iterator();
0909: while (si.hasNext()) {
0910: List sgl = new ArrayList();
0911: StudentGrades sg = (StudentGrades) si.next();
0912: sgl.add(sg.getUsername());
0913: sgl.addAll(sg.getGrades());
0914: csvContents.add(sgl);
0915: }
0916:
0917: CSV newCsv = new CSV(csvContents, currentGradebook
0918: .getHeadings().size() > 0);
0919:
0920: this .csv = newCsv.getCsv();
0921: return "download_csv";
0922: }
0923:
0924: public String processTemplateDownload() {
0925: try {
0926: if (!this .checkAccess()) {
0927: throw new PermissionException(SessionManager
0928: .getCurrentSessionUserId(),
0929: "syllabus_access_athz", "");
0930: }
0931:
0932: } catch (PermissionException e) {
0933: // logger.info(this + ".getEntries() in PostemTool " + e);
0934: FacesContext.getCurrentInstance().addMessage(
0935: null,
0936: MessageUtils.getMessage(
0937: FacesMessage.SEVERITY_ERROR,
0938: "error_permission", (new Object[] { e
0939: .toString() }), FacesContext
0940: .getCurrentInstance()));
0941: return "permission_error";
0942: }
0943: currentGradebook = (Gradebook) gradebookTable.getRowData();
0944:
0945: return "download_template";
0946:
0947: }
0948:
0949: public ArrayList getStudents() {
0950: return students;
0951:
0952: }
0953:
0954: public String getTitle() {
0955: return ToolManager.getCurrentTool().getTitle();
0956: }
0957:
0958: public boolean getEditable() {
0959: /*
0960: * if(checkAccess()) { this.editable = "true"; } else { this.editable =
0961: * null; } return this.editable;
0962: */
0963: // return true;
0964: return checkAccess();
0965:
0966: }
0967:
0968: public void setEditable(boolean editable) {
0969: this .editable = editable;
0970: }
0971:
0972: public boolean checkAccess() {
0973: // return true;
0974: return SiteService.allowUpdateSite(ToolManager
0975: .getCurrentPlacement().getContext());
0976: }
0977:
0978: // perhaps this should be moved to GradebookImpl
0979: public Map getStatsColumns() {
0980:
0981: Map columns = new TreeMap();
0982:
0983: if (currentGradebook == null) {
0984: return columns;
0985: }
0986: Set students = currentGradebook.getStudents();
0987: if (students.size() == 0) {
0988: return columns;
0989: }
0990: StudentGrades student = (StudentGrades) students.iterator()
0991: .next();
0992:
0993: int size = student.getGrades().size();
0994: for (int current = 0; current < size; current++) {
0995: Column nc = new Column(currentGradebook, current);
0996: // logger.info("** Checking if column " + current + " is stat-able!");
0997: if (nc.getSummary() != null) {
0998: if (nc.getHasName()) {
0999: columns.put(nc.getName(), new Integer(current));
1000: } else {
1001: columns.put(Integer.toString(current), new Integer(
1002: current));
1003: }
1004: }
1005: }
1006: return columns;
1007: }
1008:
1009: public Column getCurrentColumn() {
1010: return new Column(currentGradebook, column);
1011: }
1012:
1013: private boolean hasADuplicateUsername(CSV studentGrades) {
1014: List usernameList = studentGrades.getStudentUsernames();
1015: List duplicatesList = new ArrayList();
1016:
1017: while (usernameList.size() > 0) {
1018: String username = (String) usernameList.get(0);
1019: usernameList.remove(username);
1020: if (usernameList.contains(username)
1021: && !duplicatesList.contains(username)) {
1022: duplicatesList.add(username);
1023: }
1024: }
1025:
1026: if (duplicatesList.size() <= 0) {
1027: return false;
1028: }
1029:
1030: if (duplicatesList.size() == 1) {
1031: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1032: "single_duplicate_username", new Object[] {});
1033: } else {
1034: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1035: "mult_duplicate_usernames", new Object[] {});
1036: }
1037:
1038: for (int i = 0; i < duplicatesList.size(); i++) {
1039: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1040: "duplicate_username", new Object[] { duplicatesList
1041: .get(i) });
1042: }
1043:
1044: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1045: "duplicate_username_dir", new Object[] {});
1046:
1047: return true;
1048: }
1049:
1050: private boolean usernamesValid(CSV studentGrades) {
1051: boolean usersAreValid = true;
1052: List blankRows = new ArrayList();
1053: List invalidUsernames = new ArrayList();
1054: int row = 1;
1055:
1056: List studentList = studentGrades.getStudentUsernames();
1057: Iterator studentIter = studentList.iterator();
1058: while (studentIter.hasNext()) {
1059: row++;
1060: String usr = (String) studentIter.next();
1061:
1062: if (usr.equals("") || usr == null) {
1063: usersAreValid = false;
1064: blankRows.add(new Integer(row));
1065: } else if (!isSiteMember(getUserId(usr))) {
1066: usersAreValid = false;
1067: invalidUsernames.add(usr);
1068: }
1069: }
1070:
1071: if (blankRows.size() == 1) {
1072: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1073: "missing_single_username", new Object[] {});
1074: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1075: "missing_location",
1076: new Object[] { blankRows.get(0) });
1077: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1078: "missing_username_dir", new Object[] {});
1079: } else if (blankRows.size() > 1) {
1080: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1081: "missing_mult_usernames", new Object[] {});
1082: for (int i = 0; i < blankRows.size(); i++) {
1083: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1084: "missing_location", new Object[] { blankRows
1085: .get(i) });
1086: }
1087: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1088: "missing_username_dir", new Object[] {});
1089: }
1090:
1091: if (invalidUsernames.size() == 1) {
1092: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1093: "blank", new Object[] {});
1094: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1095: "single_invalid_username", new Object[] {});
1096: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1097: "invalid_username", new Object[] { invalidUsernames
1098: .get(0) });
1099: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1100: "single_invalid_username_dir", new Object[] {});
1101: } else if (invalidUsernames.size() > 1) {
1102: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1103: "blank", new Object[] {});
1104: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1105: "mult_invalid_usernames", new Object[] {});
1106: for (int j = 0; j < invalidUsernames.size(); j++) {
1107: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1108: "invalid_username",
1109: new Object[] { invalidUsernames.get(j) });
1110: }
1111: PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR,
1112: "mult_invalid_usernames_dir", new Object[] {});
1113: }
1114: return usersAreValid;
1115: }
1116:
1117: private boolean isSiteMember(String uid) {
1118: AuthzGroup realm;
1119: try {
1120: realm = AuthzGroupService.getAuthzGroup("/site/"
1121: + getCurrentSiteId());
1122: return realm.getUsers().contains(uid);
1123: } catch (GroupNotDefinedException e) {
1124: LOG.error("IdUnusedException:", e);
1125: }
1126: return false;
1127: }
1128:
1129: private String getCurrentSiteId() {
1130: Placement placement = ToolManager.getCurrentPlacement();
1131: return placement.getContext();
1132: }
1133:
1134: private String getUserId(String usr) {
1135: try {
1136: return UserDirectoryService.getUserId(usr);
1137: } catch (UserNotDefinedException e) {
1138: return usr;
1139: }
1140: }
1141: }
|