001: /**********************************************************************************
002: *
003: * $Id: GradebookArchive.java 9271 2006-05-10 21:52:49Z 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;
022:
023: import java.beans.XMLDecoder;
024: import java.beans.XMLEncoder;
025: import java.io.BufferedInputStream;
026: import java.io.BufferedOutputStream;
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.util.Collection;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * Models a gradebook and all of its dependent objects, which can all be
036: * serialized as xml for archiving.
037: *
038: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
039: */
040: public class GradebookArchive {
041: private static Log log = LogFactory.getLog(GradebookArchive.class);
042:
043: private Gradebook gradebook;
044: private GradeMapping selectedGradeMapping;
045: private Collection gradeMappings;
046: private CourseGrade courseGrade;
047: private Collection assignments;
048:
049: public GradebookArchive() {
050: // Allows for creating the archive, then populating it via readArchive()
051: }
052:
053: /**
054: * @param gradebook
055: * @param selectedGradeMapping
056: * @param gradeMappings
057: * @param courseGrade
058: * @param assignments
059: */
060: public GradebookArchive(Gradebook gradebook,
061: GradeMapping selectedGradeMapping,
062: Collection gradeMappings, CourseGrade courseGrade,
063: Collection assignments) {
064: super ();
065: this .gradebook = gradebook;
066: this .selectedGradeMapping = selectedGradeMapping;
067: this .gradeMappings = gradeMappings;
068: this .courseGrade = courseGrade;
069: this .assignments = assignments;
070: }
071:
072: /**
073: * Serializes this gradebook archive into an xml document
074: */
075: public String archive() {
076: if (log.isDebugEnabled())
077: log.debug("GradebookArchive.archive() called");
078: ByteArrayOutputStream baos = new ByteArrayOutputStream();
079: XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
080: baos));
081: encoder.writeObject(this );
082: encoder.flush();
083: String xml = baos.toString();
084: if (log.isDebugEnabled())
085: log.debug("GradebookArchive.archive() finished");
086: return xml;
087: }
088:
089: /**
090: * Read a gradebook archive from an xml input stream.
091: *
092: * @param xml The input stream containing the serialized gradebook archive
093: * @return A gradebook archive object modeling the data in the xml stream
094: */
095: public void readArchive(String xml) {
096: ByteArrayInputStream in = new ByteArrayInputStream(xml
097: .getBytes());
098: XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in));
099: GradebookArchive archive = (GradebookArchive) decoder
100: .readObject();
101: decoder.close();
102: this .gradebook = archive.getGradebook();
103: this .courseGrade = archive.getCourseGrade();
104: this .assignments = archive.getAssignments();
105: }
106:
107: public Collection getAssignments() {
108: return assignments;
109: }
110:
111: public void setAssignments(Collection assignments) {
112: this .assignments = assignments;
113: }
114:
115: public CourseGrade getCourseGrade() {
116: return courseGrade;
117: }
118:
119: public void setCourseGrade(CourseGrade courseGrade) {
120: this .courseGrade = courseGrade;
121: }
122:
123: public Gradebook getGradebook() {
124: return gradebook;
125: }
126:
127: public void setGradebook(Gradebook gradebook) {
128: this .gradebook = gradebook;
129: }
130:
131: public Collection getGradeMappings() {
132: return gradeMappings;
133: }
134:
135: public void setGradeMappings(Collection gradeMappings) {
136: this .gradeMappings = gradeMappings;
137: }
138:
139: public GradeMapping getSelectedGradeMapping() {
140: return selectedGradeMapping;
141: }
142:
143: public void setSelectedGradeMapping(
144: GradeMapping selectedGradeMapping) {
145: this.selectedGradeMapping = selectedGradeMapping;
146: }
147: }
|