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/controllers/HardcopyQuiz.java,v 1.5 2004/07/01 05:50:19 tj_willis Exp $Revision: 1.5 $
019     */
020    package net.sourceforge.pavlov.controllers;
021    
022    import net.sourceforge.sillyview.*;
023    import java.awt.event.ActionListener;
024    import java.awt.event.ActionEvent;
025    import javax.swing.text.html.FormSubmitEvent;
026    import javax.swing.event.HyperlinkEvent;
027    import javax.swing.event.HyperlinkListener;
028    import java.net.URL;
029    import java.io.FileWriter;
030    import java.io.File;
031    import java.io.IOException;
032    import java.util.*;
033    import net.sourceforge.pavlov.user.*;
034    import net.sourceforge.pavlov.library.*;
035    import net.sourceforge.pavlov.main.*;
036    
037    public class HardcopyQuiz
038                            implements ActionListener,HyperlinkListener
039    {
040        //FIXED: these weren't final -- thanks FindBugs
041        public static final String NUMBER_OF_QUESTIONS = "number_questions";
042        public static final String FILENAME = "filename";
043        public static final String TEMPLATE_FILE = "template_file";
044        public static final String BAD_NUMBER_OF_QUESTIONS = "bad_number_questions";
045        public static final String BAD_TEMPLATE_FILE = "bad_template_file";
046        public static final String BAD_QUIZ = "bad_quiz";
047        public static final String BAD_FILENAME = "bad_file_name";
048        public static final String BASE_URL = "BASE_URL";
049        public static final String FILE_SAVED = "file_saved";
050        public static final String QUESTIONS = "QUESTIONS";
051        public static final String BOOK = "BOOK";
052        public static final String CHAPTER = "CHAPTER";
053        
054        private VelocityModel mod;
055        private JFrameView view;
056        private AbstractPavlovApplication pavlov;
057        private String baseURL;
058    
059            public HardcopyQuiz(final AbstractPavlovApplication pavlov)
060            throws IOException, Exception
061            {
062                    this.pavlov = pavlov;
063                    baseURL = null;
064                    File tmpg = new File("resources/views/HardCopy.vm");
065                    URL u = tmpg.toURL();
066                    mod = new VelocityModel(u);
067    
068                    view = new JFrameView(mod, JPanelView.HTMLPANE);
069                    HashMap<Object,Object> toks = new HashMap<Object,Object>();
070                    toks.put(JPanelView.TITLE,(Object)"HardCopy Quiz Creator");//^
071                    view.setToken(JPanelView.HYPERLINK_LISTENER,this);
072                    toks.put(BASE_URL,null);
073                    toks.put(FILENAME,null);
074    
075    
076                    //view.setVisible(true);
077    
078                    String directory = "resources/hctemplates";
079                    File dir = new File(directory);
080                    if( dir==null || !dir.exists()) return;
081                    File files[] = dir.listFiles();
082                    ArrayList<File> dirs = new ArrayList<File>();
083    
084    
085    for(File g : files) {
086                            if(g.isDirectory()){
087                                    String s = g.getName() + "/index.vm";
088                                    File f = new File(dir,s);
089                                    if(f!=null && f.exists()){
090                                            dirs.add(g);
091                                    } else {
092                                            //FIXME: don't ignore
093                                            //cat.info("not loading file" + f);
094                                    }
095                            }
096                    }
097                    toks.put("dirs",dirs);
098                    view.addTokens(toks);
099                    view.pack();
100                    view.reshape(0,0,400,400);
101            }
102    
103    
104            private int getNumberOfQuestions( String input){
105                    if(input==null){
106                            return -1;
107                    }
108                    int nQs = 0;
109                    try {
110                            nQs = Integer.parseInt(input);
111                    } catch (NumberFormatException e) {
112                            nQs = -1;
113                    }
114                    return nQs;
115            }
116    
117            public void hyperlinkUpdate(HyperlinkEvent e) {
118                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
119                            if(e instanceof FormSubmitEvent){
120                                    //System.out.println("In fse");
121                                    FormSubmitEvent f = (FormSubmitEvent)e;
122                                    Hashtable<String,String> la = URLParser.parseVariables(f.getData());
123                                    String numberOfQuestions = la.get(NUMBER_OF_QUESTIONS);
124                                    String filename = la.get(FILENAME);
125                                    String templateFile = la.get(TEMPLATE_FILE);
126                                    baseURL = la.get(BASE_URL);
127    
128                                    int nQs = getNumberOfQuestions(numberOfQuestions);
129    
130                                    HashMap<Object,Object> toks = getDefaultTokens();
131                                    /* toks.put(BAD_NUMBER_OF_QUESTIONS,null);
132                                    toks.put(BAD_FILENAME,null);
133                                    toks.put(BAD_TEMPLATE_FILE,null);
134                                    toks.put(BAD_QUIZ,null);
135                                    toks.put(FILE_SAVED,null); */
136    
137                                    if(nQs<1){
138                                            toks.put(BAD_NUMBER_OF_QUESTIONS,"true");
139                                            view.addTokens(toks);
140                                            view.pack();
141                                            return;
142                                    }
143    
144                                    File output = getFile(filename);
145                                    if(output==null){
146                                            toks.put(BAD_FILENAME,"true");
147                                            view.addTokens(toks);
148                                            view.pack();
149                                            return;
150                                    }
151    
152                                    VelocityModel template = getTemplate(templateFile);
153                                    if(template==null){
154                                            toks.put(BAD_TEMPLATE_FILE,true);
155                                            view.addTokens(toks);
156                                            view.pack();
157                                            return;
158                                    }
159    
160                                    toks.put(FILENAME,filename);
161                                    toks.put(BASE_URL,baseURL);
162                                    view.addTokens(toks);
163                                    //System.out.println(BAD_FILENAME+" is "+view.getValue(BAD_FILENAME));
164                                    generateHardcopy(nQs,output,template);
165                                    view.pack();
166                                    //System.out.println("Leaving fse");
167                            }
168                    }
169            }
170    
171            private HashMap<Object,Object> getDefaultTokens()
172            {
173                    HashMap<Object,Object> map = new HashMap<Object,Object>();
174                    map.put(BAD_NUMBER_OF_QUESTIONS,false);
175                    map.put(BAD_FILENAME,false);
176                    map.put(BAD_TEMPLATE_FILE,false);
177                    map.put(BAD_QUIZ,false);
178                    map.put(FILE_SAVED,false);
179                    return map;
180            }
181            
182            private void generateHardcopy(int nQs, File output, VelocityModel mod){
183                    Quiz q = pavlov.getQuiz();
184                    StringBufferView buf = new StringBufferView(mod);
185                    if(q==null){
186                            view.setToken(BAD_QUIZ,"true");
187                            view.pack();
188                            return;
189                    }
190                    ChapterData cd = q.getChapterData();
191                    if(cd==null){
192                            view.setToken(BAD_QUIZ,"true");
193                            view.pack();
194                            return;
195                    }
196                    ChapterReference cr = q.getChapterReference();
197                    if(cr==null){
198                            view.setToken(BAD_QUIZ,"true");
199                            view.pack();
200                            return;
201                    }
202                    Chapter chap = cr.getChapter();
203                    if(chap==null){
204                            view.setToken(BAD_QUIZ,"true");
205                            view.pack();
206                            return;
207                    }
208    
209                    User user = q.getUser();
210                    if(user==null){
211                            view.setToken(BAD_QUIZ,"true");
212                            view.pack();
213                            return;
214                    }
215    
216                    int oldExclusionSize = cd.getExclusionSize();
217                    cd.setExclusionSize(nQs);
218                    Vector<Question> questions = new Vector<Question>();
219                    for(int i=0;i<nQs;i++){
220                            questions.add(user.getValidQuestion(cd,chap));
221                    }
222                    cd.setExclusionSize(oldExclusionSize);
223                    buf.setToken(QUESTIONS,questions);
224                    buf.setToken(BASE_URL,baseURL);
225                    buf.setToken(CHAPTER,chap);
226                    AbstractBook bk = cr.getBook();
227                    buf.setToken(BOOK,bk);
228                    HashMap<Object,Object> toks = getDefaultTokens();
229                    try {
230                            FileWriter fw = new FileWriter(output);
231                            String vu = buf.toString();
232                            fw.write(vu,0,vu.length());
233                            fw.flush();
234                            fw.close();// i don't trust the api
235                            toks.put(FILE_SAVED,"true");
236                    } catch (IOException e) {
237                            toks.put(BAD_FILENAME,"true");
238                    }
239                    view.addTokens(toks);
240                    view.pack();
241                    //System.out.println("BUF="+buf.toString());
242            }
243    
244            private File getFile(String filename){
245                    if(filename==null) return null;
246                    File f = null;
247                    try {
248                            f = new File(filename);
249                            if(f==null){
250                                    return null;
251                            }
252                            if(!f.exists()){
253                                    f.createNewFile();
254                            }
255                            if(!f.canWrite()){
256                                    return null;
257                            }
258                            return f;
259                    } catch (Exception e) {
260                            return null;
261                    }
262                    //return null;
263            }
264    
265            private VelocityModel getTemplate(String filename){
266                    if(filename==null) return null;
267                    File f = null;
268                    try {
269                            f = new File(filename,"index.vm");
270                            if(f==null){
271                                    return null;
272                            }
273                            if(!f.exists()){
274                                    return null;
275                            }
276                            if(!f.canRead()){
277                                    return null;
278                            }
279                            //System.out.println("file = " + f);
280                            URL templateURL = f.toURL();
281                            //System.out.println("url = " + templateURL);
282                            VelocityModel vm = new VelocityModel(templateURL);
283                            //System.out.println("raw mod = " + vm.getRawModel());
284                            return vm;
285                    } catch (Exception e) {
286                            return null;
287                    }
288                    //return null;
289            }
290    
291            public void actionPerformed(ActionEvent e){
292                    if(view==null) return;
293                    if(view.isVisible()){
294                            view.setVisible(false);
295                    } else {
296                            view.setVisible(true);
297                    }
298            }
299    }