001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/postem/tags/sakai_2-4-1/postem-hbm/src/java/org/sakaiproject/component/app/postem/data/StudentGradesImpl.java $
003: * $Id: StudentGradesImpl.java 17140 2006-10-16 17:40:49Z wagnermr@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
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.component.app.postem.data;
021:
022: import java.io.Serializable;
023: import java.sql.Timestamp;
024: import java.text.DateFormat;
025: import java.text.SimpleDateFormat;
026: import java.util.ArrayList;
027: import java.util.Date;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import org.sakaiproject.api.app.postem.data.Gradebook;
032: import org.sakaiproject.api.app.postem.data.StudentGrades;
033: import org.sakaiproject.api.app.postem.data.Template;
034:
035: public class StudentGradesImpl implements StudentGrades, Comparable,
036: Serializable {
037: protected Gradebook gradebook;
038:
039: protected String username;
040:
041: protected List grades = new ArrayList();
042:
043: protected DateFormat dateFormat = new SimpleDateFormat(
044: "d MMM yyyy HH:mm");
045:
046: protected Timestamp lastChecked;
047:
048: protected Long id;
049:
050: protected Integer lockId;
051:
052: public StudentGradesImpl() {
053:
054: }
055:
056: public StudentGradesImpl(String username, List grades) {
057: this .username = username.trim();
058: this .grades = grades;
059: }
060:
061: public Integer getLockId() {
062: return lockId;
063: }
064:
065: public void setLockId(Integer lockId) {
066: this .lockId = lockId;
067: }
068:
069: public Gradebook getGradebook() {
070: return gradebook;
071: }
072:
073: public void setGradebook(Gradebook gradebook) {
074: this .gradebook = gradebook;
075: }
076:
077: public String getUsername() {
078: return username;
079: }
080:
081: public void setUsername(String username) {
082: this .username = username.trim();
083: }
084:
085: public List getGrades() {
086: return grades;
087: }
088:
089: public void setGrades(List grades) {
090: this .grades = grades;
091: }
092:
093: public String getCheckDateTime() {
094: if (lastChecked == null) {
095: return "never";
096: }
097: return dateFormat.format((Date) lastChecked);
098: }
099:
100: public Timestamp getLastChecked() {
101: return lastChecked;
102: }
103:
104: public void setLastChecked(Timestamp lastChecked) {
105: this .lastChecked = lastChecked;
106: }
107:
108: public Long getId() {
109: return id;
110: }
111:
112: public void setId(Long id) {
113: this .id = id;
114: }
115:
116: public int compareTo(Object other) {
117: if (this == other)
118: return 0;
119: final StudentGrades that = (StudentGrades) other;
120:
121: return this .getUsername().compareTo(that.getUsername());
122: }
123:
124: public boolean equals(Object other) {
125: if (this == other)
126: return true;
127: if (!(other instanceof StudentGrades))
128: return false;
129: final StudentGrades that = (StudentGrades) other;
130:
131: return this .getUsername().equals(that.getUsername());
132: }
133:
134: public int hashCode() {
135: return getUsername().hashCode();
136: }
137:
138: public boolean getReadAfterUpdate() {
139: if (lastChecked == null) {
140: return false;
141: }
142: return getLastChecked().after(gradebook.getLastUpdated());
143: }
144:
145: /**
146: * Formats the grades for display, independently of the JSF display. If a
147: * {@link Template} exists for the parent gradebook, that template's
148: * fillGrades method is used. Otherwise, the grades are formatted into a plain
149: * old table.
150: * <p>
151: * This is a bad method for including display code within it; however, I do
152: * this for a simple reason: we're already including display code at this
153: * level via the template.
154: * <p>
155: * The prettier eventual solution will be to inject a default template via the
156: * controller, or possibly in the manager class (using a defaultTemplate
157: * property). This works for the quick and dirty now.
158: */
159: public String formatGrades() {
160: if (gradebook.getTemplate() == null) {
161:
162: List h2 = new ArrayList(gradebook.getHeadings());
163:
164: StringBuffer gradeBuffer = new StringBuffer();
165: gradeBuffer.append("<table class=\"itemSummary\">");
166:
167: if (h2.size() != 0) {
168: gradeBuffer.append("<tr><th scope=\"row\">"
169: + h2.get(0).toString() + "</th><td>");
170: h2.remove(0);
171: gradeBuffer.append(getUsername());
172: gradeBuffer.append("</td></tr>");
173: Iterator ii = h2.iterator();
174: Iterator jj = grades.iterator();
175:
176: while (ii.hasNext()) {
177: gradeBuffer.append("<tr><th scope=\"row\">");
178: gradeBuffer.append((String) ii.next());
179: gradeBuffer.append("</th><td>");
180: gradeBuffer.append((String) jj.next());
181: gradeBuffer.append("</td></tr>");
182: }
183: } else {
184: gradeBuffer.append("<tr><td>");
185: gradeBuffer.append(getUsername());
186: gradeBuffer.append("</td></tr>");
187: Iterator jj = grades.iterator();
188: while (jj.hasNext()) {
189: gradeBuffer.append("<tr><td>");
190: gradeBuffer.append((String) jj.next());
191: gradeBuffer.append("</td></tr>");
192: }
193: }
194: gradeBuffer.append("</table>");
195: return gradeBuffer.toString();
196: } else {
197: return gradebook.getTemplate().fillGrades(this );
198: }
199: }
200:
201: public String getGradesRow() {
202: StringBuffer gradeBuffer = new StringBuffer();
203: // gradeBuffer.append("<table><tr>");
204: int totalWidth = 0;
205:
206: Iterator jj = grades.iterator();
207: int ii = 0;
208: while (jj.hasNext()) {
209: String current = (String) jj.next();
210: String width = gradebook.getProperWidth(ii);
211: int iwidth = Integer.parseInt(width.substring(0, width
212: .length() - 2));
213: totalWidth += iwidth;
214: /*gradeBuffer.append("<td width='");
215: gradeBuffer.append(width);
216: gradeBuffer.append("' style='min-width: ");
217: gradeBuffer.append(width);
218: gradeBuffer.append("; width: ");
219: gradeBuffer.append(width);
220: gradeBuffer.append(";' >");*/
221: gradeBuffer.append("<td style=\"padding:0.6em;\">");
222: gradeBuffer.append(current);
223: gradeBuffer.append("</td>");
224: ii++;
225: }
226: /*StringBuffer newBuffer = new StringBuffer();
227: newBuffer.append("<table width='");
228: newBuffer.append(totalWidth);
229: newBuffer.append("px' style='min-width: ");
230: newBuffer.append(totalWidth);
231: newBuffer.append("px; width: ");
232: newBuffer.append(totalWidth);
233: newBuffer.append("px;' ><tr>");
234: newBuffer.append(gradeBuffer);
235:
236: newBuffer.append("</tr></table>");
237: newBuffer.append("</tr>");*/
238: return gradeBuffer.toString();
239: }
240:
241: }
|