Source code: com/RuntimeCollective/questionnaire/bean/SimpleQuestion.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/questionnaire/bean/SimpleQuestion.java,v 1.9 2003/09/30 15:12:53 joe Exp $
2 * $Revision: 1.9 $
3 * $Date: 2003/09/30 15:12:53 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.questionnaire.bean;
31
32 import com.RuntimeCollective.questionnaire.InvalidAnswerException;
33 import com.RuntimeCollective.questionnaire.QuestionnaireException;
34 import com.RuntimeCollective.questionnaire.bean.Answer;
35 import com.RuntimeCollective.questionnaire.bean.Question;
36 import com.RuntimeCollective.questionnaire.bean.UserAnswers;
37 import com.RuntimeCollective.webapps.RuntimeDataSource;
38 import com.RuntimeCollective.webapps.RuntimeParameters;
39
40 import java.sql.SQLException;
41
42 /**
43 * Basic and incomplete implementation of the Question interface, which you
44 * can subclass at will to create new types of Questions.
45 * <br>
46 * In your sub classes, you should define some Fields to store the possible
47 * answers (*not* Answers), and override <code>isAnswerValid</code> and
48 * <code>makeNewAnswer</code>, and (most probably) <code>save</code>,
49 * <code>delete</code> and the 2 usual constructors.
50 *
51 * @version $Id: SimpleQuestion.java,v 1.9 2003/09/30 15:12:53 joe Exp $
52 */
53 public abstract class SimpleQuestion implements Question {
54
55
56 /** The name of the database table for this bean type. */
57 public static final String DATABASE_TABLE = "quest_question";
58
59 // EntityBean specific
60
61 /** This object's id */
62 protected int id;
63
64 /** Set the unique id of this bean instance. */
65 public void setId(int id) {
66 this.id = id;
67 }
68
69 /** Get the unique id of this bean instance. */
70 public int getId() {
71 return id;
72 }
73
74 /** Save this bean to the database. */
75 public void save() {
76 try {
77 String qid = null;
78 if (QuestionnaireId != -1)
79 qid = ""+QuestionnaireId;
80
81 String pos = null;
82 if (QuestionnaireId != -1) {
83 Questionnaire q = getQuestionnaire();
84 if (q != null) {
85 int posInt = q.getIndexOfQuestion(this);
86 if (posInt != -1)
87 pos = ""+posInt;
88 }
89 }
90
91 // write Question data, don't save the questions
92 RuntimeDataSource.save(id, DATABASE_TABLE, new String[] { "name", "description", "questionnaire_id", "position_no" }, new Object[] { Name, Description, qid, pos } );
93
94 } catch (SQLException e) {
95 throw new QuestionnaireException("SimpleQuestion could not save() : "+e);
96 }
97 }
98
99 /** Delete this bean from the database. */
100 public void delete() {
101 try {
102 RuntimeDataSource.update("delete from "+DATABASE_TABLE+" where id = "+id);
103
104 } catch (SQLException e) {
105 throw new QuestionnaireException("SimpleQuestion could not delete() : "+e);
106 }
107 }
108
109
110
111 // Question specific
112
113 /** The Name of this SimpleQuestion */
114 protected String Name = null;
115
116 /** Set the Name of this SimpleQuestion*/
117 public void setName(String name) {
118 Name = name;
119 }
120
121 /** Get the Name of this SimpleQuestion */
122 public String getName() {
123 return Name;
124 }
125
126 /** The Description of this SimpleQuestion */
127 protected String Description = null;
128
129 /** Set the Description of this SimpleQuestion*/
130 public void setDescription(String description) {
131 Description = description;
132 }
133
134 /** Get the Description of this SimpleQuestion */
135 public String getDescription() {
136 return Description;
137 }
138
139 /** The Questionnaire */
140 protected int QuestionnaireId = -1;
141
142 /** Set the Questionnaire of this Question */
143 public void setQuestionnaire(Questionnaire questionnaire) {
144 if (questionnaire != null)
145 QuestionnaireId = questionnaire.getId();
146 else
147 QuestionnaireId = -1;
148 }
149
150 /** Get the Questionnaire of this Question */
151 public Questionnaire getQuestionnaire() {
152 if (QuestionnaireId != -1)
153 return (Questionnaire) RuntimeParameters.getStore().get(Questionnaire.class.getName(), QuestionnaireId);
154 else
155 return null;
156 }
157
158 /** Make a new Answer to this Question, based on a row answer Object.
159 * <br>
160 * This checks the validity of the raw Object answer.
161 */
162 public abstract Answer makeNewAnswer(Object answer) throws InvalidAnswerException;
163
164 /** Check the validity of a raw Object answer for this Question. */
165 public abstract boolean isAnswerValid(Object answer);
166
167
168
169
170 // SimpleQuestion specific
171
172 /** Construct a new blank SimpleQuestion, giving it a new unique ID. */
173 public SimpleQuestion() {
174 try {
175 setId(RuntimeDataSource.nextId());
176
177 } catch (SQLException e) {
178 throw new QuestionnaireException("SimpleQuestion could not SimpleQuestion() : "+e);
179 }
180 }
181
182 /** Get a current SimpleQuestion from the RuntimeDataSource, given an id.
183 * @param id ID of the SimpleQuestion.
184 */
185 public SimpleQuestion(int id) {
186 try {
187
188 int no_result = 3;
189 Object[] result = RuntimeDataSource.queryRow("select t.name, t.description, t.questionnaire_id from "+DATABASE_TABLE+" t where t.id = "+id);
190 if (result.length != no_result) {
191 throw new QuestionnaireException("Cannot load SimpleQuestion "+id+": "+result.length+" fields found in "+DATABASE_TABLE+" instead of "+no_result+".");
192 }
193
194 this.id = id;
195 Name = (String) result[0];
196 Description = (String) result[1];
197 if (result[2] != null)
198 QuestionnaireId = Integer.parseInt(result[2].toString());
199 else
200 QuestionnaireId = -1;
201
202 } catch (SQLException e) {
203 throw new QuestionnaireException("SimpleQuestion could not SimpleQuestion("+id+") : "+e);
204 }
205 }
206
207
208 }
209
210
211
212