View Javadoc
1   package org.andromda.andromdapp;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import org.apache.commons.lang.StringUtils;
6   
7   /**
8    * Represents a user prompt used by AndroMDApp.
9    *
10   * @author Chad Brandon
11   */
12  public class Prompt
13  {
14      /**
15       * The unique prompt identifier.
16       */
17      private String id;
18  
19      /**
20       * Gets the unique id of this prompt.
21       *
22       * @return Returns the id.
23       */
24      public String getId()
25      {
26          return id;
27      }
28  
29      /**
30       * Sets the unique id of this prompt.
31       *
32       * @param id The id to set.
33       */
34      public void setId(final String id)
35      {
36          this.id = id;
37      }
38  
39      /**
40       * Whether or not this prompt is required.
41       */
42      private boolean required = true;
43  
44      /**
45       * Sets whether or not this prompt is required,
46       * by default the prompt is <strong>required</strong>.
47       *
48       * @param required whether or not this prompt is required
49       */
50      public void setRequired(final boolean required)
51      {
52          this.required = required;
53      }
54  
55      /**
56       * Indicates whether or not this prompt is required.
57       *
58       * @return true/false
59       */
60      public boolean isRequired()
61      {
62          return this.required;
63      }
64  
65      /**
66       * Stores the actual text of the prompt.
67       */
68      private String text;
69  
70      /**
71       * Gets the text of the prompt.
72       *
73       * @return Returns the text.
74       */
75      public String getText()
76      {
77          final StringBuilder text = new StringBuilder();
78          if (this.text != null)
79          {
80              text.append(this.text);
81          }
82          if (!this.responses.isEmpty())
83          {
84              text.append(' ').append(this.getResponsesAsString());
85          }
86          text.append(": ");
87          return text.toString();
88      }
89  
90      /**
91       * Sets the prompt text.
92       *
93       * @param text The text to set.
94       */
95      public void setText(final String text)
96      {
97          this.text = StringUtils.trim(text);
98      }
99  
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 }