1d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** 2d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * $RCSfile$ 3d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * $Revision$ 4d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * $Date$ 5d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 6d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Copyright 2003-2007 Jive Software. 7d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 8d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 9d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * you may not use this file except in compliance with the License. 10d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * You may obtain a copy of the License at 11d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 12d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * http://www.apache.org/licenses/LICENSE-2.0 13d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 14d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Unless required by applicable law or agreed to in writing, software 15d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * distributed under the License is distributed on an "AS IS" BASIS, 16d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * See the License for the specific language governing permissions and 18d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * limitations under the License. 19d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 20d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 21d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpackage org.jivesoftware.smackx; 22d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 23d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.jivesoftware.smack.util.StringUtils; 24d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 25d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport java.util.ArrayList; 26d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport java.util.Collections; 27d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport java.util.Iterator; 28d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport java.util.List; 29d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 30d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** 31d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Represents a field of a form. The field could be used to represent a question to complete, 32d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * a completed question or a data returned from a search. The exact interpretation of the field 33d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * depends on the context where the field is used. 34d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 35d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @author Gaston Dombiak 36d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 37d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic class FormField { 38d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 39d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_BOOLEAN = "boolean"; 40d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_FIXED = "fixed"; 41d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_HIDDEN = "hidden"; 42d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_JID_MULTI = "jid-multi"; 43d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_JID_SINGLE = "jid-single"; 44d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_LIST_MULTI = "list-multi"; 45d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_LIST_SINGLE = "list-single"; 46d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_TEXT_MULTI = "text-multi"; 47d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_TEXT_PRIVATE = "text-private"; 48d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static final String TYPE_TEXT_SINGLE = "text-single"; 49d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 50d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private String description; 51d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private boolean required = false; 52d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private String label; 53d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private String variable; 54d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private String type; 55d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private final List<Option> options = new ArrayList<Option>(); 56d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private final List<String> values = new ArrayList<String>(); 57d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 58d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 59d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Creates a new FormField with the variable name that uniquely identifies the field 60d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * in the context of the form. 61d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 62d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param variable the variable name of the question. 63d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 64d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public FormField(String variable) { 65d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.variable = variable; 66d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 67d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 68d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 69d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Creates a new FormField of type FIXED. The fields of type FIXED do not define a variable 70d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * name. 71d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 72d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public FormField() { 73d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.type = FormField.TYPE_FIXED; 74d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 75d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 76d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 77d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns a description that provides extra clarification about the question. This information 78d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * could be presented to the user either in tool-tip, help button, or as a section of text 79d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * before the question.<p> 80d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <p/> 81d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * If the question is of type FIXED then the description should remain empty. 82d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 83d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return description that provides extra clarification about the question. 84d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 85d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String getDescription() { 86d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return description; 87d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 88d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 89d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 90d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns the label of the question which should give enough information to the user to 91d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * fill out the form. 92d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 93d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return label of the question. 94d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 95d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String getLabel() { 96d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return label; 97d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 98d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 99d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 100d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns an Iterator for the available options that the user has in order to answer 101d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * the question. 102d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 103d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return Iterator for the available options. 104d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 105d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public Iterator<Option> getOptions() { 106d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen synchronized (options) { 107d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return Collections.unmodifiableList(new ArrayList<Option>(options)).iterator(); 108d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 109d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 110d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 111d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 112d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns true if the question must be answered in order to complete the questionnaire. 113d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 114d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return true if the question must be answered in order to complete the questionnaire. 115d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 116d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public boolean isRequired() { 117d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return required; 118d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 119d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 120d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 121d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns an indicative of the format for the data to answer. Valid formats are: 122d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <p/> 123d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <ul> 124d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>text-single -> single line or word of text 125d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>text-private -> instead of showing the user what they typed, you show ***** to 126d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * protect it 127d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>text-multi -> multiple lines of text entry 128d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>list-single -> given a list of choices, pick one 129d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>list-multi -> given a list of choices, pick one or more 130d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0 131d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>fixed -> fixed for putting in text to show sections, or just advertise your web 132d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * site in the middle of the form 133d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>hidden -> is not given to the user at all, but returned with the questionnaire 134d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based 135d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * on the rules for a JID. 136d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>jid-multi -> multiple entries for JIDs 137d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * </ul> 138d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 139d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return format for the data to answer. 140d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 141d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String getType() { 142d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return type; 143d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 144d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 145d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 146d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns an Iterator for the default values of the question if the question is part 147d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * of a form to fill out. Otherwise, returns an Iterator for the answered values of 148d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * the question. 149d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 150d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return an Iterator for the default values or answered values of the question. 151d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 152d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public Iterator<String> getValues() { 153d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen synchronized (values) { 154d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return Collections.unmodifiableList(new ArrayList<String>(values)).iterator(); 155d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 156d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 157d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 158d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 159d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns the variable name that the question is filling out. 160d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 161d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return the variable name of the question. 162d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 163d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String getVariable() { 164d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return variable; 165d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 166d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 167d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 168d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Sets a description that provides extra clarification about the question. This information 169d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * could be presented to the user either in tool-tip, help button, or as a section of text 170d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * before the question.<p> 171d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <p/> 172d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * If the question is of type FIXED then the description should remain empty. 173d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 174d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param description provides extra clarification about the question. 175d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 176d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void setDescription(String description) { 177d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.description = description; 178d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 179d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 180d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 181d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Sets the label of the question which should give enough information to the user to 182d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * fill out the form. 183d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 184d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param label the label of the question. 185d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 186d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void setLabel(String label) { 187d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.label = label; 188d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 189d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 190d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 191d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Sets if the question must be answered in order to complete the questionnaire. 192d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 193d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param required if the question must be answered in order to complete the questionnaire. 194d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 195d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void setRequired(boolean required) { 196d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.required = required; 197d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 198d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 199d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 200d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Sets an indicative of the format for the data to answer. Valid formats are: 201d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <p/> 202d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <ul> 203d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>text-single -> single line or word of text 204d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>text-private -> instead of showing the user what they typed, you show ***** to 205d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * protect it 206d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>text-multi -> multiple lines of text entry 207d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>list-single -> given a list of choices, pick one 208d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>list-multi -> given a list of choices, pick one or more 209d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0 210d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>fixed -> fixed for putting in text to show sections, or just advertise your web 211d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * site in the middle of the form 212d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>hidden -> is not given to the user at all, but returned with the questionnaire 213d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based 214d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * on the rules for a JID. 215d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>jid-multi -> multiple entries for JIDs 216d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * </ul> 217d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 218d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param type an indicative of the format for the data to answer. 219d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 220d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void setType(String type) { 221d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.type = type; 222d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 223d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 224d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 225d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Adds a default value to the question if the question is part of a form to fill out. 226d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Otherwise, adds an answered value to the question. 227d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 228d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param value a default value or an answered value of the question. 229d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 230d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void addValue(String value) { 231d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen synchronized (values) { 232d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen values.add(value); 233d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 234d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 235d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 236d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 237d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Adds a default values to the question if the question is part of a form to fill out. 238d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Otherwise, adds an answered values to the question. 239d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 240d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param newValues default values or an answered values of the question. 241d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 242d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void addValues(List<String> newValues) { 243d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen synchronized (values) { 244d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen values.addAll(newValues); 245d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 246d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 247d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 248d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 249d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Removes all the values of the field. 250d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 251d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen protected void resetValues() { 252d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen synchronized (values) { 253d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen values.removeAll(new ArrayList<String>(values)); 254d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 255d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 256d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 257d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 258d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Adss an available options to the question that the user has in order to answer 259d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * the question. 260d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 261d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @param option a new available option for the question. 262d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 263d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public void addOption(Option option) { 264d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen synchronized (options) { 265d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen options.add(option); 266d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 267d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 268d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 269d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String toXML() { 270d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen StringBuilder buf = new StringBuilder(); 271d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("<field"); 272d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen // Add attributes 273d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (getLabel() != null) { 274d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append(" label=\"").append(getLabel()).append("\""); 275d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 276d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (getVariable() != null) { 277d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append(" var=\"").append(getVariable()).append("\""); 278d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 279d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (getType() != null) { 280d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append(" type=\"").append(getType()).append("\""); 281d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 282d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append(">"); 283d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen // Add elements 284d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (getDescription() != null) { 285d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("<desc>").append(getDescription()).append("</desc>"); 286d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 287d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (isRequired()) { 288d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("<required/>"); 289d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 290d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen // Loop through all the values and append them to the string buffer 291d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen for (Iterator<String> i = getValues(); i.hasNext();) { 292d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("<value>").append(i.next()).append("</value>"); 293d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 294d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen // Loop through all the values and append them to the string buffer 295d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen for (Iterator<Option> i = getOptions(); i.hasNext();) { 296d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append((i.next()).toXML()); 297d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 298d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("</field>"); 299d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return buf.toString(); 300d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 301d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 302d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen @Override 303d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public boolean equals(Object obj) { 304d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (obj == null) 305d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return false; 306d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (obj == this) 307d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return true; 308d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (!(obj instanceof FormField)) 309d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return false; 310d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 311d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen FormField other = (FormField) obj; 312d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 313d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return toXML().equals(other.toXML()); 314d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 315d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 316d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen @Override 317d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public int hashCode() { 318d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return toXML().hashCode(); 319d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 320d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 321d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 322d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Represents the available option of a given FormField. 323d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 324d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @author Gaston Dombiak 325d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 326d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public static class Option { 327d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 328d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private String label; 329d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen private String value; 330d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 331d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public Option(String value) { 332d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.value = value; 333d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 334d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 335d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public Option(String label, String value) { 336d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.label = label; 337d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.value = value; 338d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 339d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 340d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 341d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns the label that represents the option. 342d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 343d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return the label that represents the option. 344d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 345d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String getLabel() { 346d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return label; 347d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 348d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 349d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen /** 350d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns the value of the option. 351d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 352d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @return the value of the option. 353d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 354d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String getValue() { 355d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return value; 356d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 357d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 358d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen @Override 359d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String toString() { 360d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return getLabel(); 361d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 362d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 363d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public String toXML() { 364d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen StringBuilder buf = new StringBuilder(); 365d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("<option"); 366d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen // Add attribute 367d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (getLabel() != null) { 368d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append(" label=\"").append(getLabel()).append("\""); 369d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 370d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append(">"); 371d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen // Add element 372d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("<value>").append(StringUtils.escapeForXML(getValue())).append("</value>"); 373d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 374d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen buf.append("</option>"); 375d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return buf.toString(); 376d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 377d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 378d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen @Override 379d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public boolean equals(Object obj) { 380d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (obj == null) 381d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return false; 382d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (obj == this) 383d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return true; 384d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (obj.getClass() != getClass()) 385d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return false; 386d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 387d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen Option other = (Option) obj; 388d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 389d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (!value.equals(other.value)) 390d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return false; 391d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 392d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen String thisLabel = label == null ? "" : label; 393d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen String otherLabel = other.label == null ? "" : other.label; 394d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 395d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (!thisLabel.equals(otherLabel)) 396d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return false; 397d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 398d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return true; 399d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 400d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 401d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen @Override 402d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen public int hashCode() { 403d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen int result = 1; 404d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen result = 37 * result + value.hashCode(); 405d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen result = 37 * result + (label == null ? 0 : label.hashCode()); 406d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return result; 407d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 408d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 409d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 410