001package org.andromda.andromdapp; 002 003import java.util.ArrayList; 004import java.util.List; 005import org.apache.commons.lang.StringUtils; 006 007/** 008 * Represents a user prompt used by AndroMDApp. 009 * 010 * @author Chad Brandon 011 */ 012public class Prompt 013{ 014 /** 015 * The unique prompt identifier. 016 */ 017 private String id; 018 019 /** 020 * Gets the unique id of this prompt. 021 * 022 * @return Returns the id. 023 */ 024 public String getId() 025 { 026 return id; 027 } 028 029 /** 030 * Sets the unique id of this prompt. 031 * 032 * @param id The id to set. 033 */ 034 public void setId(final String id) 035 { 036 this.id = id; 037 } 038 039 /** 040 * Whether or not this prompt is required. 041 */ 042 private boolean required = true; 043 044 /** 045 * Sets whether or not this prompt is required, 046 * by default the prompt is <strong>required</strong>. 047 * 048 * @param required whether or not this prompt is required 049 */ 050 public void setRequired(final boolean required) 051 { 052 this.required = required; 053 } 054 055 /** 056 * Indicates whether or not this prompt is required. 057 * 058 * @return true/false 059 */ 060 public boolean isRequired() 061 { 062 return this.required; 063 } 064 065 /** 066 * Stores the actual text of the prompt. 067 */ 068 private String text; 069 070 /** 071 * Gets the text of the prompt. 072 * 073 * @return Returns the text. 074 */ 075 public String getText() 076 { 077 final StringBuilder text = new StringBuilder(); 078 if (this.text != null) 079 { 080 text.append(this.text); 081 } 082 if (!this.responses.isEmpty()) 083 { 084 text.append(' ').append(this.getResponsesAsString()); 085 } 086 text.append(": "); 087 return text.toString(); 088 } 089 090 /** 091 * Sets the prompt text. 092 * 093 * @param text The text to set. 094 */ 095 public void setText(final String text) 096 { 097 this.text = StringUtils.trim(text); 098 } 099 100 /** 101 * Stores the possible responses of the prompt. 102 */ 103 private final List<String> responses = new ArrayList<String>(); 104 105 /** 106 * Adds a response to the possible responses. 107 * 108 * @param response the response to add. 109 */ 110 public void addResponse(final String response) 111 { 112 if (StringUtils.isNotBlank(response)) 113 { 114 this.responses.add(response.trim()); 115 } 116 } 117 118 /** 119 * Indicates whether or not the given <code>response</code> is valid 120 * according to the valid responses contained in this prompt instance. 121 * 122 * @param response the response to check. 123 * @return true/false 124 */ 125 public boolean isValidResponse(final String response) 126 { 127 return this.responses.contains(response) || 128 (this.responses.isEmpty() && (!this.isRequired() || (StringUtils.isNotBlank(response)))); 129 } 130 131 /** 132 * Gets the response object converted to the appropriate 133 * type or just as it is (if no conversion took place or conversion 134 * failed). 135 * 136 * @param response the response to convert. 137 * @return the response as an object. 138 */ 139 public Object getResponse(final Object response) 140 { 141 return AndroMDAppUtils.convert( 142 response, 143 this.responseType); 144 } 145 146 /** 147 * Stores the response type. 148 */ 149 private String responseType; 150 151 /** 152 * Sets the response type to use (i.e the fully qualified name of the 153 * type to which it should be converted when placed into the the template context). 154 * 155 * @param responseType the fully qualified response type name. 156 */ 157 public void setResponseType(final String responseType) 158 { 159 this.responseType = responseType; 160 } 161 162 /** 163 * Gets the responses as a formatted string. 164 * 165 * @return the responses list as a string. 166 */ 167 private String getResponsesAsString() 168 { 169 final StringBuilder responsesString = new StringBuilder("["); 170 for (String response : this.responses) 171 { 172 responsesString.append(response).append(", "); 173 } 174 //remove last ", " 175 if(!this.responses.isEmpty()) 176 { 177 responsesString.delete(responsesString.length()-2, responsesString.length()); 178 } 179 responsesString.append(']'); 180 return responsesString.toString(); 181 } 182 183 /** 184 * The conditions that apply to this prompt. 185 */ 186 private final List<Condition> conditions = new ArrayList<Condition>(); 187 188 /** 189 * Adds a condition to this prompt. 190 * 191 * @param condition the condition which must apply to this prompt. 192 */ 193 public void addCondition(final Condition condition) 194 { 195 this.conditions.add(condition); 196 } 197 198 /** 199 * Gets the conditions defined in this prompt. 200 * 201 * @return the conditions that are defined within this prompt. 202 */ 203 public List<Condition> getConditions() 204 { 205 return this.conditions; 206 } 207 208 /** 209 * The preconditions that must be valid for this prompt 210 * in order for it to be executed. 211 */ 212 private final List<Conditions> preconditions = new ArrayList<Conditions>(); 213 214 /** 215 * Adds preconditions to this prompt. 216 * 217 * @param preconditions the preconditions to add. 218 */ 219 public void addPreconditions(final Conditions preconditions) 220 { 221 this.preconditions.add(preconditions); 222 } 223 224 /** 225 * Gets the preconditions for this prompt. 226 * 227 * @return the prompt preconditions. 228 */ 229 public List<Conditions> getPreconditions() 230 { 231 return this.preconditions; 232 } 233 234 /** 235 * Whether or not the value of the response should be 236 * set to a boolean value of true. 237 */ 238 private boolean setResponseAsTrue; 239 240 /** 241 * Whether or not the response should be set to a boolean value of <code>true</code>. 242 * 243 * @return Returns the setResponseAsTrue. 244 */ 245 public boolean isSetResponseAsTrue() 246 { 247 return setResponseAsTrue; 248 } 249 250 /** 251 * Sets whether or not the response should be set to a boolean value of true. 252 * 253 * @param setResponseAsBoolean The setResponseAsTrue to set. 254 */ 255 public void setSetResponseAsTrue(final boolean setResponseAsBoolean) 256 { 257 this.setResponseAsTrue = setResponseAsBoolean; 258 } 259}