Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/questionnaire/bean/OneInManyStringsQuestion.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/questionnaire/bean/OneInManyStringsQuestion.java,v 1.13 2003/09/30 15:12:53 joe Exp $
2    * $Revision: 1.13 $
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.bean.Answer;
34  import com.RuntimeCollective.questionnaire.bean.OneStringAnswer;
35  import com.RuntimeCollective.questionnaire.bean.SimpleQuestion;
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  import java.util.Arrays;
42  import java.util.Iterator;
43  import java.util.Vector;
44  
45  /**
46   * Question implementation which offers the choice of one String between many.
47   * <p>
48   * Useful for single-option questionnaires, where only one answer of many can be chosen.
49   *
50   * @see com.RuntimeCollective.questionnaire.bean.OneStringAnswer
51   * @version $Id: OneInManyStringsQuestion.java,v 1.13 2003/09/30 15:12:53 joe Exp $
52   */
53  public class OneInManyStringsQuestion extends XInManyStringsQuestion {
54  
55  
56      /** The name of the database table for this bean type. */
57      public static final String DATABASE_TABLE = "quest_oimsquest";
58  
59      /** Fill in details of the database tables used by this class */
60      protected void setDatabaseParticulars() {
61          REAL_DATABASE_TABLE = "quest_oimsquest";
62          CHOICES_TABLE = "quest_oimsq_choices";
63          DATABASE_ID = "oimsq_id";
64          DATABASE_KEY = "oimsq_key";
65      }
66  
67  
68      // Question specific
69  
70      /** Make a new Answer to this Question, based on a row answer Object.
71       * <br>
72       * This checks the validity of the raw Object answer (which must be
73       * a String), and then creates a new OneStringAnswer.
74       */
75      public Answer makeNewAnswer(Object answer) throws InvalidAnswerException {
76  
77    // is the answer valid ?
78    if (!isAnswerValid(answer))
79        throw new InvalidAnswerException("The object "+answer+" is not a suitable choice for the OneInManyStringsQuestion "+id+".");
80  
81    else {
82        // create a new Answer object
83        OneStringAnswer oas = (OneStringAnswer) RuntimeParameters.getStore().create("com.RuntimeCollective.questionnaire.bean.OneStringAnswer");
84        oas.setAnsweredQuestion(this);
85        oas.setAnswerGiven((String) answer);
86        return oas;
87    }
88      }
89  
90  
91      /** Check the validity of a raw Object answer for this Question.
92       * <br>
93       * It should be one of the possible String choices.
94       */
95      public boolean isAnswerValid(Object answer) {
96          // is the answer a proper String
97          if (!(answer instanceof String) || (answer == null))
98              return false;
99  
100         // is it one of the possible choices?
101         String stringAnswer = (String) answer;
102         for (int i=0; i<Keys.size(); i++) {
103             if (stringAnswer.equals((String) Keys.elementAt(i))) {
104                 return true;
105             }
106         }
107         
108         // no luck
109         return false;
110     }
111 
112 
113 
114     // OneInManyStringsQuestion specific
115 
116     /** Construct a new blank OneInManyStringsQuestion, giving it a new unique ID. */
117     public OneInManyStringsQuestion() {
118   super();
119     }
120   
121     /** Get a current OneInManyStringsQuestion from the RuntimeDataSource, given an id.
122      * @param id ID of the OneInManyStringsQuestion.
123      */
124     public OneInManyStringsQuestion(int id) {
125   super(id);
126     }
127 
128 }
129 
130 
131 
132