001: package com.xoetrope.survey;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.Writer;
007: import java.util.Date;
008: import java.util.Vector;
009:
010: import net.n3.nanoxml.XMLElement;
011: import net.n3.nanoxml.XMLWriter;
012: import net.xoetrope.debug.DebugLogger;
013: import com.xoetrope.carousel.build.BuildProperties;
014: import net.xoetrope.xui.XProject;
015: import net.xoetrope.xui.data.XDataBinding;
016:
017: /**
018: * <p>Loads and manages surveys. The manager can maintain a number of surveys so
019: * that different surveys can be served up by the same application instance if
020: * for example a different set of questions were to be asked depending on the
021: * user (e.g. in a hotel a different survey would be asked for a conference
022: * attendee and a residential guest).</p>
023: *
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.10 $</p>
029: */
030: public class XSurveyManager {
031: private XProject currentProject;
032: private Vector surveys;
033: private XSurvey currentSurvey;
034:
035: private int languageId;
036: private int sessionCount;
037: private int maxCachedSessions;
038:
039: /**
040: * Create a new survey manager
041: */
042: public XSurveyManager(XProject proj) {
043: currentProject = proj;
044: surveys = new Vector();
045: sessionCount = 0;
046: maxCachedSessions = 5;
047:
048: String mcs = null;
049: maxCachedSessions = 5;
050: try {
051: mcs = currentProject.getStartupParam("maxCachedSessions");
052: if (mcs != null)
053: maxCachedSessions = new Integer(mcs).intValue();
054: } catch (Exception ex) {
055: }
056: }
057:
058: /**
059: * Get the next question
060: * @return the next question instance
061: */
062: public Question getNextQuestion() {
063: return currentSurvey.getNextQuestion();
064: }
065:
066: /**
067: * Gets the number of surveys currently loaded
068: * @return the number of surveys
069: */
070: public int getNumSurveys() {
071: return surveys.size();
072: }
073:
074: /**
075: * Gets a survey
076: * @param index the index of the survey to return
077: * @return the survey
078: */
079: public XSurvey getSurvey(int index) {
080: return (XSurvey) surveys.elementAt(index);
081: }
082:
083: /**
084: * Loads a survey from file such that the file names is <name>_<language>.xml
085: * e.g. mysurvey_en.xml
086: * @param name the name of the survey
087: * @param index the index of the survey
088: * @param language the ISO language code
089: */
090: public void loadSurvey(int index, String name, String language) {
091: XSurvey oldSurvey = currentSurvey;
092: if (index >= surveys.size()) {
093: currentSurvey = new XSurvey(name);
094: surveys.add(currentSurvey);
095: } else
096: currentSurvey = getSurvey(index);
097:
098: String fileName = name + "_" + language + ".xml";
099: try {
100: java.io.BufferedReader br = currentProject
101: .getBufferedReader(fileName);
102: if (br != null)
103: currentSurvey.read(br);
104: } catch (Exception ex) {
105: ex.printStackTrace();
106: }
107: }
108:
109: /**
110: * Bind the current survey to some data - a repository for question data.
111: * @param binding the data binding
112: */
113: public void bindQuestion(XDataBinding binding) {
114: if (currentSurvey != null)
115: currentSurvey.bindQuestion(binding);
116: }
117:
118: /**
119: * Reset the current survey. Any unsaved session data will be saved prior to
120: * reset. The reset puts the survey into the same state as following the
121: * initial load. All counters are reset.
122: */
123: public void reset() {
124: if (sessionCount++ > maxCachedSessions) {
125: saveSessions();
126: sessionCount = 0;
127: }
128: currentSurvey.reset();
129: }
130:
131: /**
132: * Writes the surveys to the output stream
133: * @param writer the output stream
134: */
135: public void write(Writer writer) {
136: try {
137: int numSurveys = surveys.size();
138: XMLElement xml = new XMLElement("Data");
139: for (int i = 0; i < numSurveys; i++) {
140: XMLElement surveyXml = new XMLElement("Survey");
141: XSurvey survey = (XSurvey) surveys.elementAt(i);
142: surveyXml.setAttribute("name", survey.getName());
143:
144: XResponseSet responses = survey.getResponses();
145: XMLElement questionXml = new XMLElement("Questions");
146: questionXml.setAttribute("values", responses
147: .getQuestions());
148:
149: int numSessions = responses.getNumSessions();
150: for (int j = 0; j < numSessions; j++) {
151: responses.selectSession(j);
152: XMLElement responseXml = new XMLElement("Response");
153: responseXml.setAttribute("values", responses
154: .getResponse());
155: responseXml.setAttribute("start", responses
156: .getStartTime());
157: responseXml.setAttribute("end", responses
158: .getEndTime());
159: questionXml.addChild(responseXml);
160: }
161:
162: surveyXml.addChild(questionXml);
163: xml.addChild(surveyXml);
164: }
165: XMLWriter xmlWriter = new XMLWriter(writer);
166: xmlWriter.write(xml, true, 4);
167: } catch (IOException ex) {
168: if (BuildProperties.DEBUG)
169: DebugLogger.logError("Unable to write survey results");
170: }
171: }
172:
173: /**
174: * Checks for saved sessions
175: * @return true if there are saved sessions, otherwise false
176: */
177: public boolean hasSessions() {
178: int numSurveys = surveys.size();
179: for (int i = 0; i < numSurveys; i++) {
180: XSurvey survey = (XSurvey) surveys.elementAt(i);
181: XResponseSet responses = survey.getResponses();
182: if (responses.getNumSessions() > 0)
183: return true;
184: }
185: return false;
186: }
187:
188: /**
189: * Restart the session recording
190: */
191: public void restart() {
192: int numSurveys = surveys.size();
193: for (int i = 0; i < numSurveys; i++)
194: ((XSurvey) surveys.elementAt(i)).restart();
195: }
196:
197: /**
198: * Save the cached session results
199: */
200: public void saveSessions() {
201: if (hasSessions()) {
202: try {
203: File file = null;
204: String savePath = currentProject
205: .getStartupParam("SavePath");
206: if ((savePath == null) || (savePath.length() == 0))
207: file = File.createTempFile(
208: "sd_"
209: + new Long(new Date().getTime())
210: .toString(), ".xml");
211: else
212: file = new File(savePath, "sd_"
213: + new Long(new Date().getTime()).toString()
214: + ".xml");
215:
216: FileWriter fw = new FileWriter(file);
217: write(fw);
218: restart();
219: fw.flush();
220: fw.close();
221: } catch (Exception ex) {
222: if (BuildProperties.DEBUG)
223: ex.printStackTrace();
224: }
225: }
226: }
227: }
|