001: package com.xoetrope.survey;
002:
003: import java.util.Vector;
004:
005: /**
006: * A data store for a question, its ID and the response options</p>
007: *
008: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
009: * the GNU Public License (GPL), please see license.txt for more details. If
010: * you make commercial use of this software you must purchase a commercial
011: * license from Xoetrope.</p>
012: * <p> $Revision: 1.8 $</p>
013: */
014: public class Question {
015: public static final String[] QUESTION_TYPES = {
016: "mutually exclusive", "multiple choice", "free text" };
017:
018: private int id;
019: private QuestionGroup group;
020: private int questionType;
021: private String text;
022: private int size;
023:
024: /**
025: * The maximum value range for the question
026: */
027: public static final int MAX_RANGE = 25;
028:
029: /**
030: * Flag identifying a questions whose options are mutually exclusive of one another
031: */
032: public static final int MUTUALLY_EXCLUSIVE = 0;
033:
034: /**
035: * Flag identifying a question that is a multiple choice question
036: */
037: public static final int MULTIPLE_CHOICE = 1;
038:
039: /**
040: * Flag identifying a question that is a edit type of question.
041: */
042: public static final int FREE_TEXT = 2;
043:
044: public static final int DEFAULT_TYPE = MULTIPLE_CHOICE;
045:
046: private int nextOption;
047:
048: private Vector options;
049:
050: /**
051: * Gets the type of the passed question
052: * @param typeText String describing the type of the question
053: * @return the type of the question
054: */
055: public static int getType(String typeText) {
056: int ret = -1;
057: for (int i = 0; i < QUESTION_TYPES.length && ret == -1; i++) {
058: if (QUESTION_TYPES[i].equals(typeText))
059: ret = i;
060: }
061: return ret;
062: }
063:
064: /**
065: * Creates a new question
066: * @param type The Question type
067: * @param qId The question id
068: * @param range The number of response
069: * @param caption The question text
070: */
071: public Question(int qId, QuestionGroup g, int type, String caption) {
072: id = qId;
073: group = g;
074: questionType = type;
075: text = caption;
076:
077: nextOption = 0;
078: options = new Vector();
079: }
080:
081: /**
082: * Creates a new question
083: * @param type the question type
084: * @param qId the id of the question
085: * @param g the question group that this
086: * question will belong to
087: */
088: public Question(int qId, QuestionGroup g) {
089: this (qId, g, DEFAULT_TYPE, "");
090: }
091:
092: public void setSize(int s) {
093: size = s;
094: }
095:
096: /**
097: * Gets the question group that this
098: * question belongs to
099: * @return the question group
100: */
101: public QuestionGroup getGroup() {
102: return group;
103: }
104:
105: /**
106: * Sets the group of this question
107: * @param g the question group
108: */
109: public void setGroup(QuestionGroup g) {
110: group = g;
111: }
112:
113: /**
114: * Gets the type of this question
115: * @return String descrinbing this question's type
116: */
117: public String getQuestionTypeText() {
118: return QUESTION_TYPES[questionType];
119: }
120:
121: /**
122: * Gets the index of the specified option
123: * of this question
124: * @param optionId the id of the option
125: * @return the localization of the option
126: */
127: public int getOptionIdx(int optionId) {
128: int idx = -1;
129: int sz = options.size();
130: for (int i = 0; ((i < sz) && (idx == -1)); i++) {
131: Option o = (Option) options.get(i);
132: if (o.getId() == optionId)
133: idx = i;
134: }
135:
136: return idx;
137: }
138:
139: /**
140: * Gets the option with the specified id
141: * @param optionId the id of the option
142: * @return option
143: */
144: public Option getOptionById(int optionId) {
145: Option option = null;
146: int sz = options.size();
147: for (int i = 0; ((i < sz) && (option == null)); i++) {
148: Option o = (Option) options.get(i);
149: if (o.getId() == optionId)
150: option = o;
151: }
152:
153: return option;
154: }
155:
156: /**
157: * Gets the id of the option with the specified
158: * text
159: * @param text the text of the option
160: * @return the id of the wanted option
161: */
162: public int getOptionIdByText(String text) {
163: int optionId = -1;
164: int sz = options.size();
165: for (int i = 0; ((i < sz) && (optionId == -1)); i++) {
166: Option o = (Option) options.get(i);
167: if (o.getText().equals(text))
168: optionId = o.getId();
169: }
170: return optionId;
171: }
172:
173: /**
174: * Add a response option
175: * @param id the response id or value
176: * @param caption the response text
177: */
178: public void addOption(int id, String caption) {
179: options.add(new Option(id, caption));
180: }
181:
182: /**
183: * Adds the option to this question
184: * @param o the option which is to be added
185: */
186: public void addOption(Option o) {
187: options.add(o);
188: }
189:
190: /**
191: * Set an option for this questions
192: * @param idx the option index
193: * @param id the id or value of the option
194: * @param caption the text of the question
195: */
196: public void setOption(int idx, int id, String caption) {
197: if (idx < options.size())
198: options.set(idx, new Option(id, caption));
199: }
200:
201: /**
202: * Set the question text
203: * @param newText the new text
204: */
205: public void setText(String newText) {
206: text = newText;
207: }
208:
209: /**
210: * Sets the new type of this question
211: * @param typeName String describing the new type of this question
212: */
213: public void setType(String typeName) {
214: for (int i = 0; i < QUESTION_TYPES.length; i++) {
215: if (QUESTION_TYPES[i].equals(typeName)) {
216: questionType = i;
217: break;
218: }
219: }
220: }
221:
222: /**
223: * Set the question ID
224: * @param newId the new ID
225: */
226: public void setId(int newId) {
227: id = newId;
228: }
229:
230: /**
231: * Gets the Id or values of am option
232: * @param index the option index
233: * @return the value
234: */
235: public int getOptionId(int index) {
236: return (0 <= index && index < options.size() ? ((Option) options
237: .get(index)).getId()
238: : -1);
239: }
240:
241: /**
242: * Gets the question text
243: * @return the text
244: */
245: public String getText() {
246: return text;
247: }
248:
249: /**
250: * Gets the question ID
251: * @return teh ID
252: */
253: public int getId() {
254: return id;
255: }
256:
257: /**
258: * Gets the number of response options
259: * @return the number of options
260: */
261: public int getNumOptions() {
262: return options.size();
263: }
264:
265: /**
266: * Gets the text of the option with the specified index
267: * @param index the index of the option
268: * @return the text or caption of the option
269: */
270: public String getOptionText(int index) {
271: return (0 <= index && index < options.size() ? ((Option) options
272: .get(index)).getText()
273: : null);
274: }
275:
276: /**
277: * Gets the option of this question with the specified index
278: * @param index the index of the option
279: * @return the wanted option
280: */
281: public Option getOption(int index) {
282: return (0 <= index && index < options.size() ? (Option) options
283: .get(index) : null);
284: }
285:
286: /**
287: * Gets this question's options
288: * @return Vector containing options
289: * of this question
290: */
291: public Vector getOptions() {
292: return options;
293: }
294:
295: /**
296: * Gets the default question type
297: * @return the default type
298: */
299: public int getDEFAULT_TYPE() {
300: return DEFAULT_TYPE;
301: }
302:
303: /**
304: * Deletes the option with the specified index
305: * @param optionIdx the index of the option
306: */
307: public void deleteOption(int optionIdx) {
308: if (optionIdx < 0 || optionIdx >= options.size())
309: return;
310:
311: options.remove(optionIdx);
312: }
313:
314: /**
315: * Gets the question type
316: * @return the type
317: */
318: public int getQuestionType() {
319: return questionType;
320: }
321:
322: /**
323: * Sets the new type of this question
324: * @param qt the new type of this question
325: */
326: public void setQuestionType(int qt) {
327: questionType = qt;
328: }
329:
330: public boolean equals(Object o) {
331: if (!(o instanceof Question))
332: return false;
333: Question question = (Question) o;
334: return (getId() == question.getId());
335: }
336:
337: public String toString() {
338: return text;
339: }
340: }
|