001: package com.xoetrope.survey;
002:
003: import java.util.Vector;
004: import java.util.Enumeration;
005: import net.xoetrope.xml.XmlElement;
006: import net.xoetrope.xui.XProject;
007: import net.xoetrope.xui.XProjectManager;
008:
009: /**
010: * <p>A group is a set of questions of some association. </p>
011: *
012: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
013: * the GNU Public License (GPL), please see license.txt for more details. If
014: * you make commercial use of this software you must purchase a commercial
015: * license from Xoetrope.</p>
016: * <p> $Revision: 1.8 $</p>
017: */
018: public class QuestionGroup {
019: protected Vector questions;
020: protected XmlElement rules;
021: protected int groupId;
022: protected int nextGroup;
023: protected String name;
024: protected SurveyManager surveyManager;
025:
026: /**
027: * Creates a new instance of the quetion group
028: * @param id the group ID
029: * @param ng the id of the next question group
030: * @param n the name of this group
031: */
032: public QuestionGroup(int id, int ng, String n) {
033: groupId = id;
034: nextGroup = ng;
035: name = (n != null ? n : "group" + id);
036: questions = new Vector();
037:
038: XProject currentProject = XProjectManager.getCurrentProject();
039: surveyManager = (SurveyManager) currentProject
040: .getObject("SurveyManager");
041: }
042:
043: /**
044: * Creates a new instance of QuestionGroup
045: * @param id the id of this group
046: * @param n the name of this group
047: */
048: public QuestionGroup(int id, String n) {
049: this (id, -1, n);
050: }
051:
052: /**
053: * Gets the group ID
054: * @return the ID
055: */
056: public int getId() {
057: return groupId;
058: }
059:
060: /**
061: * Sets the id of this group
062: */
063: public void setId(int newId) {
064: groupId = newId;
065: }
066:
067: /**
068: * Gets the name of this group
069: * @return the name
070: */
071: public String getName() {
072: return name;
073: }
074:
075: /**
076: * Sets the name of this group
077: */
078: public void setName(String gName) {
079: name = gName;
080: }
081:
082: /**
083: * Gets the id of the next question group
084: * @return the id
085: */
086: public int getNextGroupId() {
087: return nextGroup;
088: }
089:
090: /**
091: * Sets the id of the next question group
092: * @param ng the next group id
093: */
094: public void setNextGroupId(int ng) {
095: nextGroup = ng;
096: }
097:
098: /**
099: * Gets the rules associated with this question group
100: * @return xml element describing the rules of this group.
101: */
102: public XmlElement getXmlRules() {
103: return rules;
104: }
105:
106: /**
107: * Gets the questions of this group
108: * @return Vector containing the questions
109: */
110: public Vector getQuestions() {
111: return questions;
112: }
113:
114: /**
115: * Gets the number of questions in the group
116: * @return the number of questions
117: */
118: public int getNumQuestions() {
119: return questions.size();
120: }
121:
122: /**
123: * Sets the rules of this question group
124: * @param xmlRules xml element describing the rules
125: */
126: public void setXmlRules(XmlElement xmlRules) {
127: rules = xmlRules;
128: }
129:
130: /**
131: * Adds a question to this question group.
132: * @param q the new question
133: */
134: public void addQuestion(Question q) {
135: questions.addElement(q);
136: }
137:
138: /**
139: * Removes the specified question from this question group
140: * @param q the question to be removed
141: */
142: public void removeQuestion(Question q) {
143: questions.remove(q);
144: }
145:
146: /**
147: * Creates and returns a new question object.
148: * @return question object
149: */
150: public Question createNewQuestion() {
151: Survey currentSurvey = surveyManager.getCurrentSurvey();
152: int questionId = currentSurvey.getNextQuestionId();
153: Question question = new Question(questionId, this );
154: questions.add(question);
155: return question;
156: }
157:
158: /**
159: * Gets the question with the specified index
160: * @param idx the index of the questiond
161: * @return the question
162: */
163: public Question getQuestion(int idx) {
164: return ((0 <= idx) && (idx < questions.size()) ? (Question) questions
165: .get(idx)
166: : null);
167: }
168:
169: /**
170: * Gets the question with the specified id
171: * @param questionId the id of the wanted question
172: * @return the question
173: */
174: public Question getQuestionById(int questionId) {
175: Question question = null;
176: Enumeration enumeration = questions.elements();
177: while (enumeration.hasMoreElements() && (question == null)) {
178: Question q = (Question) enumeration.nextElement();
179: if (q.getId() == questionId)
180: question = q;
181: }
182: return question;
183: }
184:
185: /**
186: * Gets the index of the question with the given id.
187: * @param questionId the id of the question
188: * @return the index of the question
189: */
190: public int getQuestionIdxById(int questionId) {
191: int idx = -1;
192: Question question = null;
193: for (int i = 0; (i < questions.size()) && (idx == -1); i++) {
194: if (((Question) questions.get(i)).getId() == questionId)
195: idx = i;
196: }
197: return idx;
198: }
199:
200: public String toString() {
201: return ("" + groupId + " - " + name);
202: }
203:
204: public boolean equals(Object o) {
205: if (!(o instanceof QuestionGroup))
206: return false;
207:
208: QuestionGroup group = (QuestionGroup) o;
209: return (groupId == group.getId());
210: }
211:
212: }
|