001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/user/BookData.java,v 1.6 2004/07/01 09:03:44 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.user;
021    
022    import net.sourceforge.pavlov.library.AbstractBook;
023    import net.sourceforge.pavlov.library.AbstractChapter;
024    import java.util.*;
025    import org.apache.log4j.*;
026    
027    
028    /**
029     * Encapsulates a users responses to all questions in a book.
030     *
031     * @author <a href="mailto:tj_willis@users.sourceforge.net"> T.J. Willis </a>
032     * @version $Revision: 1.6 $
033     * @see net.sourceforge.pavlov.library.Book
034     * @see ChapterData
035     * @see QuestionData
036     */
037    public final class BookData {
038        /** Title of the net.sourceforge.pavlov.library.Book this BookData corresponds to. */
039        private String title;
040        /** Date of last response */
041        private Date last;
042        /** Holds the ChapterData objects. */
043        private Hashtable<String,ChapterData> chapters;
044    
045        /**
046         * Creates a new BookData with the given title
047         * @param title a <code>String</code> value
048         */
049        public BookData(final String title) {
050            this.title = title;
051            init();
052        }
053    
054        /** Creates a new BookData entitled  "Untitled". */
055        public BookData() {
056            this("Untitled");
057        }
058    
059        private void init(){
060            chapters = new Hashtable<String,ChapterData>();
061        }
062    
063        /**
064         * Returns the Book's title.
065         * @return a <code>String</code> value
066         */
067        public String getTitle() {
068            return title;
069        }
070        /**
071         * Sets the Book's title.
072         * @param title a <code>String</code> value
073         */
074        public void setTitle(final String title) {
075            this.title = title;
076        }
077    
078        /**
079         * Returns the named ChapterData, or null if it doesn't exist.
080         * @param chapterName a <code>String</code> value
081         * @return a <code>ChapterData</code> value
082         */
083        public ChapterData getChapter(final String chapterName) {
084            assert (chapters!=null) : "chapters is null";
085            return (ChapterData) chapters.get(chapterName);
086        }
087    
088        /**
089         * Returns the named ChapterData, or null if it doesn't exist.
090         * @param chapterName a <code>String</code> value
091         * @param questionID a <code>String</code> value
092         * @return a <code>QuestionData</code> value
093         */
094        public QuestionData getQuestionData(final String chapterName, final String questionID) {
095            ChapterData cp = getChapter(chapterName);
096            if(cp==null) return null;
097            return cp.getQuestionData(questionID);
098        }
099    
100        // FIXME: write getQuestionData(final QuestionReference ref)
101    
102        /**
103         * Adds the given ChapterData to this book, keyed on ChapterData.getTitle().
104         * @param c a <code>ChapterData</code> value
105         */
106        public void addChapter(final ChapterData chap) { 
107            assert (chapters!=null) : "chapters is null";
108             chapters.put(chap.getTitle(), chap); 
109        }
110    
111        /**
112         * Sets the time of the last response to a question in this book. 
113         * @param t a <code>String</code> value
114         * @deprecated
115         */
116        @Deprecated public void setLast(final String date) {
117            last = new Date(date);
118        }
119    
120        /**
121         * Describe <code>validate</code> method here.
122         *
123         * @param book an <code>AbstractBook</code> value
124         */
125        public void validate(AbstractBook book) {
126            // foreach chapter in book
127            // get ChapterData for chapter
128            // ChapterData.validate(chapter)
129            Collection<AbstractChapter> chaps = book.getChaptersReadOnly();
130    
131            if( chaps == null){
132                    return; // thanks junit
133            }
134            
135            for(AbstractChapter left : chaps) {
136                if(left==null) continue;
137                String s = left.getTitle();
138                if(s==null) continue;
139                ChapterData da = (ChapterData) chapters.get(s);
140    
141                if (da == null) {
142                    ChapterData x = new ChapterData(left.getTitle());
143    
144                    chapters.put(x.getTitle(), x);
145                    da = x;
146                }
147                da.validate(left);
148            }
149            chaps = null;
150        }
151    
152        /**
153         * Dumps the BookData in XML format to the given writer.  
154         * Loops throught chapters, questions.
155         * @param writer a <code>java.io.Writer</code> value
156         * @exception java.io.IOException if an error occurs
157         */
158        public void toXML(java.io.Writer writer)
159            throws java.io.IOException {
160            final Collection<ChapterData> v = chapters.values();
161    
162            // FIXME: LAST=19/Feb/02 ?!?  format this.last
163            writer.write("\t<BOOK TITLE=\"" + title + "\" LAST=\"19/Feb/02\">\n");
164            for(ChapterData da : v){
165                da.toXML(writer);
166            }
167            writer.write("\t</BOOK>\n");
168        }
169    }
170    
171    
172    
173    
174    
175    
176    
177    
178    
179