001: package com.xoetrope.survey;
002:
003: import java.io.BufferedReader;
004: import java.io.BufferedWriter;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.InputStreamReader;
009: import java.io.OutputStreamWriter;
010: import java.util.StringTokenizer;
011: import java.util.Vector;
012:
013: /**
014: * <p>Reads the survey file that contains the entire set of
015: * questions and groups in a survey. The question manager is responsible for
016: * managing question and group rotations.</p>
017: *
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.6 $</p>
023: */
024: public class QuestionManager extends Thread {
025: /**
026: * The token used to separate questions
027: */
028: public static final String tokenSeparator = "|";
029:
030: /**
031: * The current group of questions
032: */
033: protected QuestionGroup currentGroup;
034:
035: /**
036: * The golden group of questions (questions that must be asked as a priority)
037: */
038: protected QuestionGroup goldenGroup;
039:
040: /**
041: * The current question
042: */
043: protected Question currentQuestion;
044:
045: /**
046: * The question groups
047: */
048: protected Vector groups;
049:
050: /**
051: * The number of questions to display on an individual response card/sheet/page/screen
052: */
053: protected int numQuestionsPerCard = 6;
054:
055: /**
056: * Creates a new question manager
057: */
058: public QuestionManager() {
059: groups = new Vector();
060: }
061:
062: /**
063: * Sets the number of questions to be shown on each card.
064: * @param nqs the number of questions
065: */
066: public void setNumQuestionsPerCard(int nqs) {
067: numQuestionsPerCard = nqs;
068: }
069:
070: /**
071: * Writes the rotation state to a flat file in preparation for shutdown
072: * @param fos The output stream
073: */
074: public void writeState(FileOutputStream fos) {
075: try {
076: BufferedWriter br = new BufferedWriter(
077: new OutputStreamWriter(fos));
078: int numGroups = groups.size();
079: for (int i = 0; i < numGroups; i++) {
080: QuestionGroup grp = ((QuestionGroup) groups
081: .elementAt(i));
082: /*
083: br.write( Double.toString( grp.getWeight()) + tokenSeparator );
084: br.write( Double.toString( grp.getUsage()) + tokenSeparator );
085: br.write( Integer.toString( grp.getNextQuestionIndex()));
086: */
087: br.write("\r");
088: }
089: br.flush();
090: br.close();
091: } catch (IOException ex) {
092: ex.printStackTrace();
093: }
094: }
095:
096: /**
097: * Reads state information and restores the group weightings and usage
098: * counters so that the survey will restart from the rotation position
099: * where it last finished
100: * @param fis the input stream
101: */
102: public void readState(FileInputStream fis) {
103: try {
104: BufferedReader br = new BufferedReader(
105: new InputStreamReader(fis));
106:
107: int numGroups = groups.size();
108: for (int i = 0; i < numGroups; i++) {
109: QuestionGroup grp = ((QuestionGroup) groups
110: .elementAt(i));
111:
112: StringTokenizer tokenizer = new StringTokenizer(br
113: .readLine(), tokenSeparator);
114:
115: /*
116: grp.setWeight( new Double( tokenizer.nextToken()).doubleValue());
117: grp.setUsage( new Double( tokenizer.nextToken()).doubleValue());
118: grp.setNextQuestionIndex( new Integer( tokenizer.nextToken()).intValue());
119: */
120: }
121: } catch (Exception ex) {
122: ex.printStackTrace();
123: }
124: }
125:
126: }
|