Source Code Cross Referenced for XSurvey.java in  » XML-UI » xui32 » com » xoetrope » carousel » 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.carousel.survey 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.xoetrope.carousel.survey;
002:
003:        import com.xoetrope.survey.Condition;
004:        import com.xoetrope.survey.DefaultRuleEngine;
005:        import com.xoetrope.survey.Option;
006:        import com.xoetrope.survey.Question;
007:        import com.xoetrope.survey.RuleEngine;
008:        import java.io.BufferedReader;
009:        import java.io.BufferedWriter;
010:        import java.io.File;
011:        import java.io.FileNotFoundException;
012:        import java.io.FileReader;
013:        import java.io.FileWriter;
014:        import java.io.IOException;
015:        import java.io.Reader;
016:        import java.io.StringReader;
017:        import java.io.StringWriter;
018:        import java.io.Writer;
019:        import java.sql.PreparedStatement;
020:        import java.sql.ResultSet;
021:        import java.sql.SQLException;
022:        import java.sql.Statement;
023:        import java.util.Enumeration;
024:        import java.util.Observable;
025:        import java.util.Observer;
026:        import java.util.Properties;
027:        import java.util.Vector;
028:        import net.n3.nanoxml.IXMLElement;
029:        import net.xoetrope.editor.project.XEditorProject;
030:        import net.xoetrope.editor.project.XEditorProjectManager;
031:        import net.xoetrope.optional.data.sql.ConnectionObject;
032:        import net.xoetrope.optional.data.sql.NamedConnectionManager;
033:        import net.xoetrope.xml.XmlElement;
034:        import net.xoetrope.xml.XmlSource;
035:        import net.xoetrope.xml.XmlWriter;
036:        import net.xoetrope.xml.nanoxml.NanoXmlElement;
037:        import net.xoetrope.xml.nanoxml.NanoXmlParser;
038:        import net.xoetrope.xml.nanoxml.NanoXmlWriter;
039:        import net.xoetrope.xui.XProject;
040:        import net.xoetrope.xui.XProjectManager;
041:
042:        /**
043:         * A data model for the editor, contains the definition of a survey, question 
044:         * groups, rules, etc.
045:         *
046:         * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
047:         * the GNU Public License (GPL), please see license.txt for more details. If
048:         * you make commercial use of this software you must purchase a commercial
049:         * license from Xoetrope.</p>
050:         * <p> $Revision: 1.5 $</p>
051:         */
052:        public class XSurvey {
053:            protected Vector groups;
054:            protected Vector targetGroups;
055:            protected XProject project;
056:
057:            protected XNotifier addGroupNotifier, deleteGroupNotifier;
058:
059:            /**
060:             * Creates a new instance of XSurvey
061:             */
062:            public XSurvey() {
063:                project = XProjectManager.getCurrentProject();
064:                groups = new Vector();
065:                targetGroups = new Vector();
066:                updateTargetGroups();
067:
068:                XEditorProject editorProject = (project instanceof  XEditorProject ? (XEditorProject) project
069:                        : null);
070:                addGroupNotifier = new XNotifier(editorProject, true);
071:                deleteGroupNotifier = new XNotifier(editorProject, true);
072:            }
073:
074:            public static void setProjectModified(boolean state) {
075:                XProject project = XProjectManager.getCurrentProject();
076:                if (project instanceof  XEditorProject)
077:                    ((XEditorProject) project).setModified(state);
078:            }
079:
080:            /**
081:             * Removes all data contained in this survey
082:             */
083:            public void clear() {
084:                while (groups.size() > 0) {
085:                    XQuestionGroup group = (XQuestionGroup) groups.get(0);
086:                    deleteGroup(group);
087:                }
088:            }
089:
090:            /**
091:             * Gets the groups being used as models for combo boxes   
092:             */
093:            public Vector getTargetGroups() {
094:                return targetGroups;
095:            }
096:
097:            /**
098:             * Recreates target groups
099:             */
100:            protected void updateTargetGroups() {
101:                targetGroups.removeAllElements();
102:                targetGroups.add(new XTargetGroup(null));
103:                Enumeration enumGroups = groups.elements();
104:                while (enumGroups.hasMoreElements()) {
105:                    XQuestionGroup group = (XQuestionGroup) enumGroups
106:                            .nextElement();
107:                    targetGroups.add(new XTargetGroup(group));
108:                }
109:            }
110:
111:            /**
112:             * Removes each target group whose value is set to the
113:             * given question group. This method is invoked after the
114:             * specified question group is deleted
115:             * @param deletedGroup the group that has been deleted
116:             */
117:            public void updateTargets(XQuestionGroup deletedGroup) {
118:                Enumeration enumGroups = groups.elements();
119:                while (enumGroups.hasMoreElements()) {
120:                    XQuestionGroup group = (XQuestionGroup) enumGroups
121:                            .nextElement();
122:                    group.updateRuleTargets(deletedGroup);
123:                    if (group.getNextGroupId() == deletedGroup.getId())
124:                        group.changeNextGroupId(null);
125:                }
126:            }
127:
128:            /**
129:             * Creates and adds a new question group to this survey.
130:             * @param groupId id of a new group
131:             * @param nextGroupId id of a question group to which
132:             * this survey will proceed.
133:             * @param name the name of a new group
134:             * @return the new question group
135:             */
136:            public XQuestionGroup addNewGroup(int groupId, int nextGroupId,
137:                    String name) {
138:                XQuestionGroup group = new XQuestionGroup(groupId, nextGroupId,
139:                        name);
140:                groups.add(group);
141:                updateTargetGroups();
142:                addGroupNotifier.notifyObservers(group);
143:                return group;
144:            }
145:
146:            /**
147:             * Creates and adds a new question group to this survey.
148:             * @param the name of the new group
149:             * @return the new question group
150:             */
151:            public XQuestionGroup addNewGroup(String name) {
152:                int newGroupId = getNextGroupId();
153:                XQuestionGroup group = new XQuestionGroup(newGroupId, name);
154:                groups.add(group);
155:                updateTargetGroups();
156:                addGroupNotifier.notifyObservers(group);
157:                return group;
158:            }
159:
160:            /**
161:             * Creates and adds a new dummy group.
162:             * @return the new question group
163:             */
164:            public XQuestionGroup addNewGroup() {
165:                return addNewGroup("new group");
166:            }
167:
168:            /**
169:             * Deletes the given question group from this survey
170:             * @param group the question group which is to be deleted
171:             */
172:            public void deleteGroup(XQuestionGroup group) {
173:                if (groups.remove(group)) {
174:                    updateTargetGroups();
175:                    updateTargets(group);
176:                    deleteGroupNotifier.notifyObservers(group);
177:                }
178:            }
179:
180:            public XNotifier getAddGroupNotifier() {
181:                return addGroupNotifier;
182:            }
183:
184:            public XNotifier getDeleteGroupNotifier() {
185:                return deleteGroupNotifier;
186:            }
187:
188:            public Vector getGroups() {
189:                return groups;
190:            }
191:
192:            public void addGroup(XQuestionGroup group) {
193:                groups.add(group);
194:            }
195:
196:            public boolean removeGroup(XQuestionGroup group) {
197:                return groups.remove(group);
198:            }
199:
200:            public int getGroupIdxById(int groupId) {
201:                int idx = -1;
202:                for (int i = 0; (i < groups.size()) && (idx == -1); i++) {
203:                    XQuestionGroup group = (XQuestionGroup) groups.get(i);
204:                    if (group.getId() == groupId)
205:                        idx = i;
206:                }
207:                return idx;
208:            }
209:
210:            public XQuestionGroup getGroupById(int groupId) {
211:                XQuestionGroup questionGroup = null;
212:                Enumeration enumeration = groups.elements();
213:                while (enumeration.hasMoreElements() && (questionGroup == null)) {
214:                    XQuestionGroup q = (XQuestionGroup) enumeration
215:                            .nextElement();
216:                    if (q.getId() == groupId)
217:                        questionGroup = q;
218:                }
219:                return questionGroup;
220:            }
221:
222:            public XQuestionGroup getGroup(int idx) {
223:                return ((idx >= 0) && (idx < groups.size()) ? (XQuestionGroup) groups
224:                        .get(idx)
225:                        : null);
226:            }
227:
228:            public int getNextQuestionId() {
229:                int max = 0;
230:                Enumeration enumGroups = groups.elements();
231:                while (enumGroups.hasMoreElements()) {
232:                    XQuestionGroup group = (XQuestionGroup) enumGroups
233:                            .nextElement();
234:                    Enumeration enumQuestions = group.getQuestions().elements();
235:                    while (enumQuestions.hasMoreElements()) {
236:                        Question question = (Question) enumQuestions
237:                                .nextElement();
238:                        if (question.getId() > max)
239:                            max = question.getId();
240:                    }
241:                }
242:                return (max + 1);
243:            }
244:
245:            public int getNextOptionId() {
246:                int max = 0;
247:                Enumeration enumGroups = groups.elements();
248:                while (enumGroups.hasMoreElements()) {
249:                    XQuestionGroup group = (XQuestionGroup) enumGroups
250:                            .nextElement();
251:                    Enumeration enumQuestions = group.getQuestions().elements();
252:                    while (enumQuestions.hasMoreElements()) {
253:                        Question question = (Question) enumQuestions
254:                                .nextElement();
255:                        Enumeration enumOptions = question.getOptions()
256:                                .elements();
257:                        while (enumOptions.hasMoreElements()) {
258:                            Option option = (Option) enumOptions.nextElement();
259:                            if (option.getId() > max)
260:                                max = option.getId();
261:                        }
262:                    }
263:                }
264:                return (max + 1);
265:            }
266:
267:            public int getNextRuleId() {
268:                int max = 0;
269:                Enumeration enumGroups = groups.elements();
270:                while (enumGroups.hasMoreElements()) {
271:                    XQuestionGroup group = (XQuestionGroup) enumGroups
272:                            .nextElement();
273:                    Enumeration enumRules = group.getRules().elements();
274:                    while (enumRules.hasMoreElements()) {
275:                        XRule rule = (XRule) enumRules.nextElement();
276:                        if (rule.getId() > max)
277:                            max = rule.getId();
278:                    }
279:                }
280:                return (max + 1);
281:            }
282:
283:            public int getNextGroupId() {
284:                int max = 0;
285:                Enumeration enumGroups = groups.elements();
286:                while (enumGroups.hasMoreElements()) {
287:                    XQuestionGroup group = (XQuestionGroup) enumGroups
288:                            .nextElement();
289:                    if (group.getId() > max)
290:                        max = group.getId();
291:                }
292:                return (max + 1);
293:            }
294:
295:            public Question getQuestionById(int questionId) {
296:                Question question = null;
297:                Enumeration enumGroups = groups.elements();
298:                while (enumGroups.hasMoreElements() && question == null) {
299:                    XQuestionGroup questionGroup = (XQuestionGroup) enumGroups
300:                            .nextElement();
301:                    question = questionGroup.getQuestionById(questionId);
302:                }
303:                return question;
304:            }
305:
306:            public int getNumQuestions() {
307:                int sum = 0;
308:                Enumeration enumGroups = groups.elements();
309:                while (enumGroups.hasMoreElements()) {
310:                    XQuestionGroup group = (XQuestionGroup) enumGroups
311:                            .nextElement();
312:                    sum += group.getNumQuestions();
313:                }
314:                return sum;
315:            }
316:
317:            public void loadSurveyFile(File file) throws Exception {
318:                BufferedReader br = null;
319:                try {
320:                    br = project.getBufferedReader(file, null);
321:                    if (br != null) {
322:                        clear();
323:                        readSurvey(br);
324:                    }
325:                } finally {
326:                    br.close();
327:                }
328:            }
329:
330:            public void writeEmptySurvey(Writer w, String surveyName)
331:                    throws IOException {
332:                NanoXmlWriter writer = new NanoXmlWriter(w);
333:                XmlElement surveyXml = new NanoXmlElement("Survey");
334:                surveyXml.setAttribute("name", surveyName);
335:                writer.write(surveyXml, true, 4);
336:            }
337:
338:            protected void writeSurvey(ConnectionObject connObj,
339:                    String surveyName, String surveyLangName) throws Exception {
340:                String sql = null;
341:
342:                // get the specified language code
343:                String langCode = "en";
344:                sql = "SELECT LanguageCode FROM Languages "
345:                        + "WHERE LanguageName='" + surveyLangName + "'";
346:                ResultSet langRs = connObj.createStatement().executeQuery(sql);
347:                if (langRs.next())
348:                    langCode = langRs.getString(1);
349:
350:                int surveyId = 1;
351:                //if the specified survey already exists delete it from the database
352:                sql = "SELECT SurveyId FROM Surveys " + "WHERE SurveyName='"
353:                        + surveyName + "' AND LanguageCode='" + langCode + "'";
354:                ResultSet exRs = connObj.createStatement().executeQuery(sql);
355:                if (exRs.next()) {
356:                    surveyId = exRs.getInt(1);
357:                    connObj.closeStatement();
358:
359:                    // delete the survey content from the database
360:                    sql = "DELETE FROM Surveys WHERE SurveyID=" + surveyId;
361:                    connObj.createStatement().executeQuery(sql);
362:                    connObj.closeStatement();
363:
364:                    sql = "DELETE FROM Groups WHERE SurveyID=" + surveyId;
365:                    connObj.createStatement().executeQuery(sql);
366:                    connObj.closeStatement();
367:
368:                    sql = "DELETE FROM Options WHERE SurveyID=" + surveyId;
369:                    connObj.createStatement().executeQuery(sql);
370:                    connObj.closeStatement();
371:
372:                    sql = "DELETE FROM Questions WHERE SurveyID=" + surveyId;
373:                    connObj.createStatement().executeQuery(sql);
374:                    connObj.closeStatement();
375:
376:                    sql = "DELETE FROM Rules WHERE SurveyID=" + surveyId;
377:                    connObj.createStatement().executeQuery(sql);
378:                    connObj.closeStatement();
379:                } else {
380:                    connObj.closeStatement();
381:                }
382:
383:                // create the next id of the survey    
384:                sql = "SELECT Max(SurveyId) FROM Surveys";
385:                ResultSet surveyIdRs = connObj.createStatement().executeQuery(
386:                        sql);
387:                if (surveyIdRs.next())
388:                    surveyId = surveyIdRs.getInt(1) + 1;
389:                connObj.closeStatement();
390:
391:                // surveys table
392:                sql = "INSERT INTO Surveys VALUES(?,?,?)";
393:                PreparedStatement surveysStmt = connObj.prepareStatement(sql);
394:                surveysStmt.setInt(1, surveyId);
395:                surveysStmt.setString(2, surveyName);
396:                surveysStmt.setString(3, langCode);
397:                surveysStmt.execute();
398:                connObj.closePreparedStatement();
399:
400:                Enumeration enumGroups = groups.elements();
401:                int groupPos = 1;
402:                while (enumGroups.hasMoreElements()) {
403:                    XQuestionGroup group = (XQuestionGroup) enumGroups
404:                            .nextElement();
405:
406:                    // groups table
407:                    sql = "INSERT INTO Groups VALUES(?,?,?,?,?)";
408:                    PreparedStatement groupsStmt = connObj
409:                            .prepareStatement(sql);
410:                    int groupId = group.getId();
411:                    int nextGroupId = group.getNextGroupId();
412:                    String groupName = group.getName();
413:                    groupsStmt.setInt(1, groupId);
414:                    groupsStmt.setInt(2, nextGroupId);
415:                    groupsStmt.setString(3, groupName);
416:                    groupsStmt.setInt(4, surveyId);
417:                    groupsStmt.setInt(5, groupPos++);
418:                    groupsStmt.execute();
419:                    connObj.closePreparedStatement();
420:
421:                    int questionsPos = 1;
422:                    Enumeration enumQuestions = group.getQuestions().elements();
423:                    while (enumQuestions.hasMoreElements()) {
424:                        Question question = (Question) enumQuestions
425:                                .nextElement();
426:
427:                        // questions table
428:                        sql = "INSERT INTO Questions VALUES(?,?,?,?,?,?)";
429:                        PreparedStatement questionsStmt = connObj
430:                                .prepareStatement(sql);
431:                        int questionId = question.getId();
432:                        String questionText = question.getText();
433:                        int questionType = question.getQuestionType();
434:                        questionsStmt.setInt(1, questionId);
435:                        questionsStmt.setString(2, questionText);
436:                        questionsStmt.setInt(3, questionType);
437:                        questionsStmt.setInt(4, groupId);
438:                        questionsStmt.setInt(5, surveyId);
439:                        questionsStmt.setInt(6, questionsPos++);
440:                        questionsStmt.execute();
441:                        connObj.closePreparedStatement();
442:
443:                        int optionPos = 1;
444:                        Enumeration enumOptions = question.getOptions()
445:                                .elements();
446:                        while (enumOptions.hasMoreElements()) {
447:                            Option option = (Option) enumOptions.nextElement();
448:
449:                            // options table
450:                            sql = "INSERT INTO Options VALUES(?,?,?,?,?)";
451:                            PreparedStatement optionsStmt = connObj
452:                                    .prepareStatement(sql);
453:                            int optionId = option.getId();
454:                            String optionText = option.getText();
455:                            optionsStmt.setInt(1, optionId);
456:                            optionsStmt.setString(2, optionText);
457:                            optionsStmt.setInt(3, questionId);
458:                            optionsStmt.setInt(4, surveyId);
459:                            optionsStmt.setInt(5, optionPos++);
460:                            optionsStmt.execute();
461:                            connObj.closePreparedStatement();
462:                        }
463:                    }
464:
465:                    StringWriter sw = new StringWriter();
466:                    NanoXmlWriter writer = new NanoXmlWriter(sw);
467:                    writer.write(group.getXmlRules(), false, 0);
468:                    String rulesDesc = sw.toString();
469:                    sql = "INSERT INTO Rules VALUES(?,?,?)";
470:                    PreparedStatement rulesStmt = connObj.prepareStatement(sql);
471:                    rulesStmt.setString(1, rulesDesc);
472:                    rulesStmt.setInt(2, groupId);
473:                    rulesStmt.setInt(3, surveyId);
474:                    rulesStmt.execute();
475:                    connObj.closePreparedStatement();
476:                }
477:            }
478:
479:            public void writeSurvey(Writer w) throws IOException {
480:                NanoXmlWriter writer = new NanoXmlWriter(w);
481:
482:                XmlElement surveyXml = new NanoXmlElement("Survey");
483:                Enumeration enumGroups = groups.elements();
484:                while (enumGroups.hasMoreElements()) {
485:                    XQuestionGroup group = (XQuestionGroup) enumGroups
486:                            .nextElement();
487:                    String groupId = String.valueOf(group.getId());
488:                    String nextGroupId = String.valueOf(group.getNextGroupId());
489:                    String groupName = group.getName();
490:
491:                    XmlElement groupXml = new NanoXmlElement("Group");
492:                    groupXml.setAttribute("id", groupId);
493:                    if (group.getNextGroupId() > 0)
494:                        groupXml.setAttribute("next", nextGroupId);
495:                    groupXml.setAttribute("name", groupName);
496:
497:                    Enumeration enumQuestions = group.getQuestions().elements();
498:                    while (enumQuestions.hasMoreElements()) {
499:                        Question question = (Question) enumQuestions
500:                                .nextElement();
501:                        String questionId = String.valueOf(question.getId());
502:                        String questionText = question.getText();
503:                        String questionType = question.getQuestionTypeText();
504:
505:                        XmlElement questionXml = new NanoXmlElement("Q");
506:                        questionXml.setAttribute("id", questionId);
507:                        questionXml.setAttribute("A", questionText);
508:                        questionXml.setAttribute("type", questionType);
509:
510:                        Enumeration enumOptions = question.getOptions()
511:                                .elements();
512:                        while (enumOptions.hasMoreElements()) {
513:                            Option option = (Option) enumOptions.nextElement();
514:                            String optionId = String.valueOf(option.getId());
515:                            String optionText = option.getText();
516:
517:                            XmlElement optionXml = new NanoXmlElement("R");
518:                            optionXml.setAttribute("id", optionId);
519:                            optionXml.setAttribute("A", optionText);
520:
521:                            questionXml.addChild(optionXml);
522:                        }
523:
524:                        groupXml.addChild(questionXml);
525:                    }
526:
527:                    XmlElement rulesXml = new NanoXmlElement("Rules");
528:                    Enumeration enumRules = group.getRules().elements();
529:                    while (enumRules.hasMoreElements()) {
530:                        XRule rule = (XRule) enumRules.nextElement();
531:                        String ruleName = rule.getName();
532:                        XQuestionGroup targetGroup = rule.getTarget();
533:                        String ruleId = String.valueOf(rule.getId());
534:
535:                        XmlElement ruleXml = new NanoXmlElement("Rule");
536:                        ruleXml.setAttribute("name", ruleName);
537:                        if (targetGroup != null)
538:                            ruleXml.setAttribute("target", String
539:                                    .valueOf(targetGroup.getId()));
540:                        ruleXml.setAttribute("id", ruleId);
541:
542:                        Enumeration enumConditions = rule.getConditions()
543:                                .elements();
544:                        while (enumConditions.hasMoreElements()) {
545:                            Condition condition = (Condition) enumConditions
546:                                    .nextElement();
547:                            Question question = condition.getQuestion();
548:
549:                            for (int i = 0; i < condition.getOptions().size(); i++) {
550:                                Option option = (Option) condition.getOptions()
551:                                        .get(i);
552:                                String answer = (String) condition.getAnswers()
553:                                        .get(i);
554:
555:                                XmlElement responseXml = new NanoXmlElement(
556:                                        "Response");
557:                                String questionId = String.valueOf(question
558:                                        .getId());
559:                                String optionId = String
560:                                        .valueOf(option.getId());
561:                                responseXml
562:                                        .setAttribute("question", questionId);
563:                                responseXml.setAttribute("option", optionId);
564:                                responseXml.setAttribute("answer", answer);
565:
566:                                ruleXml.addChild(responseXml);
567:                            }
568:                        }
569:                        rulesXml.addChild(ruleXml);
570:                    }
571:                    groupXml.addChild(rulesXml);
572:                    surveyXml.addChild(groupXml);
573:                }
574:                writer.write(surveyXml, true, 4);
575:
576:            }
577:
578:            public void saveSurveyDb(Properties dbConfiguration,
579:                    String surveyName, String surveyLanguage) throws Exception {
580:                NamedConnectionManager connMgr = (NamedConnectionManager) NamedConnectionManager
581:                        .getInstance();
582:                String user = dbConfiguration.getProperty("user");
583:                String password = dbConfiguration.getProperty("password");
584:                String driver = dbConfiguration.getProperty("driver");
585:                String url = dbConfiguration.getProperty("url");
586:                connMgr.addConnection("SurveyOutputDb", driver, url, user,
587:                        password);
588:                ConnectionObject connObj = connMgr
589:                        .getConnection("SurveyOutputDb");
590:                writeSurvey(connObj, surveyName, surveyLanguage);
591:                connObj.close();
592:            }
593:
594:            public void loadSurveyDb(Properties dbConfiguration,
595:                    String surveyName, String surveyLanguage) throws Exception {
596:                NamedConnectionManager connMgr = (NamedConnectionManager) NamedConnectionManager
597:                        .getInstance();
598:                String user = dbConfiguration.getProperty("user");
599:                String password = dbConfiguration.getProperty("password");
600:                String driver = dbConfiguration.getProperty("driver");
601:                String url = dbConfiguration.getProperty("url");
602:                connMgr.addConnection("SurveyInputDb", driver, url, user,
603:                        password);
604:                ConnectionObject connObj = connMgr
605:                        .getConnection("SurveyInputDb");
606:                readSurvey(connObj, surveyName, surveyLanguage);
607:                connObj.close();
608:            }
609:
610:            public void saveSurveyFile(File file) throws Exception {
611:                FileWriter fw = null;
612:                Writer bw = null;
613:                try {
614:                    fw = new FileWriter(file);
615:                    bw = new BufferedWriter(fw);
616:                    writeSurvey(bw);
617:                    bw.flush();
618:                } finally {
619:                    bw.close();
620:                }
621:            }
622:
623:            public void createEmptySurvey(File file, String surveyName)
624:                    throws Exception {
625:                FileWriter fw = null;
626:                Writer bw = null;
627:                try {
628:                    fw = new FileWriter(file);
629:                    bw = new BufferedWriter(fw);
630:                    writeEmptySurvey(bw, surveyName);
631:                    bw.flush();
632:                } finally {
633:                    bw.close();
634:                }
635:            }
636:
637:            protected void readSurvey(ConnectionObject connObj,
638:                    String surveyName, String surveyLanguage) throws Exception {
639:                // retrieve the survey id
640:                int surveyId = 0;
641:
642:                String sql = "SELECT Surveys.SurveyId FROM Surveys, Languages "
643:                        + "WHERE Surveys.SurveyName='" + surveyName + "' "
644:                        + "AND Languages.LanguageName='" + surveyLanguage
645:                        + "' "
646:                        + "AND Languages.LanguageCode=Surveys.LanguageCode";
647:                ResultSet surveyIdRs = connObj.createStatement().executeQuery(
648:                        sql);
649:                if (surveyIdRs.next()) {
650:                    surveyId = surveyIdRs.getInt(1);
651:                    connObj.closeStatement();
652:                } else {
653:                    connObj.closeStatement();
654:                    throw new RuntimeException(
655:                            "Specified survey doesn't exist.");
656:                }
657:
658:                groups.removeAllElements();
659:                Statement groupsStmt = connObj.getConnection()
660:                        .createStatement();
661:                ResultSet groupsRs = groupsStmt
662:                        .executeQuery("SELECT * FROM Groups "
663:                                + "WHERE SurveyID=" + surveyId
664:                                + " ORDER BY Pos");
665:                while (groupsRs.next()) {
666:                    int groupId = groupsRs.getInt(1);
667:                    int nextGroupId = groupsRs.getInt(2);
668:                    String groupName = groupsRs.getString(3);
669:                    XQuestionGroup currentGroup = new XQuestionGroup(groupId,
670:                            nextGroupId, groupName);
671:                    addGroup(currentGroup);
672:
673:                    Statement questionsStmt = connObj.getConnection()
674:                            .createStatement();
675:                    ResultSet questionsRs = questionsStmt
676:                            .executeQuery("SELECT * FROM Questions WHERE "
677:                                    + "GroupID=" + groupId + " AND SurveyID="
678:                                    + surveyId + " ORDER BY Pos");
679:                    while (questionsRs.next()) {
680:                        int questionId = questionsRs.getInt(1);
681:                        String questionText = questionsRs.getString(2);
682:                        int questionType = questionsRs.getInt(3);
683:                        Question currentQuestion = new Question(questionId,
684:                                currentGroup, questionType, questionText);
685:                        currentGroup.addQuestion(currentQuestion);
686:
687:                        Statement optionsStmt = connObj.getConnection()
688:                                .createStatement();
689:                        ResultSet optionsRs = optionsStmt
690:                                .executeQuery("SELECT * FROM Options WHERE "
691:                                        + "questionID=" + questionId
692:                                        + " AND SurveyID=" + surveyId
693:                                        + " ORDER BY Pos");
694:                        while (optionsRs.next()) {
695:                            int optionId = optionsRs.getInt(1);
696:                            String optionText = optionsRs.getString(2);
697:                            currentQuestion.addOption(optionId, optionText);
698:                        }
699:                        optionsRs.close();
700:
701:                        Statement rulesStmt = connObj.getConnection()
702:                                .createStatement();
703:                        ResultSet rulesRs = rulesStmt
704:                                .executeQuery("SELECT * FROM Rules WHERE "
705:                                        + "GroupID=" + groupId
706:                                        + " AND SurveyID=" + surveyId);
707:                        if (rulesRs.next()) {
708:                            String ruleDesc = rulesRs.getString(1);
709:                            XmlElement rulesXml = XmlSource
710:                                    .read(new StringReader(ruleDesc));
711:                            currentGroup.setXmlRules(rulesXml);
712:                        }
713:                        rulesRs.close();
714:
715:                    }
716:                    questionsRs.close();
717:
718:                }
719:                groupsRs.close();
720:
721:                setRules();
722:                updateTargetGroups();
723:
724:                if (project instanceof  XEditorProject)
725:                    ((XEditorProject) project).setModified(false);
726:            }
727:
728:            protected void readSurvey(Reader r) {
729:                groups.removeAllElements();
730:                XmlElement root = XmlSource.read(r);
731:                Vector rootChildren = root.getChildren();
732:                if (rootChildren == null)
733:                    return;
734:
735:                XQuestionGroup previousGroup = null;
736:                for (int i = 0; i < rootChildren.size(); i++) {
737:                    XmlElement xmlGroup = (XmlElement) root.elementAt(i);
738:                    char c = xmlGroup.getName().charAt(0);
739:                    if (!xmlGroup.getName().equals("Group"))
740:                        continue;
741:
742:                    int groupId = new Integer(xmlGroup.getAttribute("id"))
743:                            .intValue();
744:                    String nextGroupS = xmlGroup.getAttribute("next");
745:                    int nextGroup = (nextGroupS == null
746:                            || nextGroupS.equals("") ? 0 : new Integer(
747:                            nextGroupS).intValue());
748:                    String groupName = xmlGroup.getAttribute("name");
749:                    XQuestionGroup currentGroup = new XQuestionGroup(groupId,
750:                            nextGroup, groupName);
751:
752:                    Vector groupChildren = xmlGroup.getChildren();
753:                    for (int j = 0; j < groupChildren.size(); j++) {
754:                        XmlElement groupElement = (XmlElement) groupChildren
755:                                .elementAt(j);
756:                        String groupElementName = groupElement.getName();
757:                        if (groupElementName.equals("Q")) {
758:                            int questionId = new Integer(groupElement
759:                                    .getAttribute("id")).intValue();
760:
761:                            String questionText = groupElement
762:                                    .getAttribute("A");
763:
764:                            int questionType = Question.DEFAULT_TYPE;
765:                            String typeText = groupElement.getAttribute("type");
766:                            if (typeText != null) {
767:                                if (typeText.equals("mutually exclusive"))
768:                                    questionType = Question.MUTUALLY_EXCLUSIVE;
769:                                else if (typeText.equals("multiple choice"))
770:                                    questionType = Question.MULTIPLE_CHOICE;
771:                                else {
772:                                    if (typeText.equals("free text"))
773:                                        questionType = Question.FREE_TEXT;
774:                                }
775:                            }
776:
777:                            Question currentQuestion = new Question(questionId,
778:                                    currentGroup, questionType, questionText);
779:
780:                            Vector responses = groupElement.getChildren();
781:                            for (int k = 0; k < responses.size(); k++) {
782:                                XmlElement xmlResponse = (XmlElement) responses
783:                                        .elementAt(k);
784:                                if (!xmlResponse.getName().equals("R"))
785:                                    continue;
786:
787:                                int responseId = new Integer(xmlResponse
788:                                        .getAttribute("id")).intValue();
789:                                String responseText = xmlResponse
790:                                        .getAttribute("A");
791:                                currentQuestion.addOption(responseId,
792:                                        responseText);
793:                            }
794:
795:                            currentGroup.addQuestion(currentQuestion);
796:
797:                        } else {
798:                            if (groupElementName.equals("Rules"))
799:                                currentGroup.setXmlRules(groupElement);
800:                        }
801:                    }
802:                    addGroup(currentGroup);
803:                    if (previousGroup != null
804:                            && previousGroup.getNextGroupId() == 0)
805:                        previousGroup.setNextGroupId(currentGroup.getId());
806:                    previousGroup = currentGroup;
807:                }
808:
809:                setRules();
810:                updateTargetGroups();
811:
812:                if (project instanceof  XEditorProject)
813:                    ((XEditorProject) project).setModified(false);
814:            }
815:
816:            protected void setRules() {
817:                Enumeration enumGroups = groups.elements();
818:                while (enumGroups.hasMoreElements()) {
819:                    XQuestionGroup group = (XQuestionGroup) enumGroups
820:                            .nextElement();
821:                    group.setRules(makeRules(group));
822:                }
823:            }
824:
825:            protected Vector makeRules(XQuestionGroup group) {
826:                XmlElement rulesRoot = group.getXmlRules();
827:                Vector rules = new Vector();
828:                if (rulesRoot == null)
829:                    return rules;
830:
831:                Vector xmlRules = rulesRoot.getChildren();
832:                if (xmlRules == null)
833:                    return null;
834:
835:                for (int i = 0; i < xmlRules.size(); i++) {
836:                    XmlElement xmlRule = (XmlElement) xmlRules.get(i);
837:                    if (!xmlRule.getName().equals("Rule"))
838:                        continue;
839:
840:                    int ruleId = new Integer(xmlRule.getAttribute("id"))
841:                            .intValue();
842:
843:                    XQuestionGroup target = null;
844:                    String targetIds = xmlRule.getAttribute("target");
845:                    if (targetIds != null) {
846:                        int targetId = new Integer(targetIds).intValue();
847:                        target = getGroupById(targetId);
848:                    }
849:
850:                    String ruleName = xmlRule.getAttribute("name");
851:
852:                    XRule rule = new XRule(ruleId, group, target, ruleName);
853:
854:                    Vector xmlResponses = xmlRule.getChildren();
855:                    for (int j = 0; j < xmlResponses.size(); j++) {
856:                        XmlElement xmlResponse = (XmlElement) xmlResponses
857:                                .get(j);
858:                        if (!xmlResponse.getName().equals("Response"))
859:                            continue;
860:
861:                        int questionId = new Integer(xmlResponse
862:                                .getAttribute("question")).intValue();
863:                        int optionId = new Integer(xmlResponse
864:                                .getAttribute("option")).intValue();
865:                        String answer = xmlResponse.getAttribute("answer");
866:                        Question question = getQuestionById(questionId);
867:                        if (question == null)
868:                            continue;
869:
870:                        Option option = question.getOptionById(optionId);
871:                        if (option == null)
872:                            continue;
873:
874:                        rule.addCondition(question, option, answer);
875:                    }
876:                    rules.add(rule);
877:                }
878:                return rules;
879:            }
880:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.