Source Code Cross Referenced for XSurveyManager.java in  » XML-UI » xui32 » com » xoetrope » survey » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » xui32 » com.xoetrope.survey 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.