View Javadoc

1   /*
2    * $Id: ExecutionParams.java,v 1.12 2004/06/22 17:57:24 eelco12 Exp $
3    * $Revision: 1.12 $
4    * $Date: 2004/06/22 17:57:24 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus;
32  
33  import java.io.Serializable;
34  import java.lang.reflect.Field;
35  
36  /***
37   * Each instance of FormBeanBase keeps an instance of ExecutionParams.
38   * ExecutionParams are used to influence the execution of population,
39   * validation and the general execution of FormBeanBase.
40   * 
41   * If you want to override the defaults, you can override method init in your
42   * controller, using getExecutionParams() to get the reference to the 
43   * execution params that will be used for the instance of your controller.
44   * 
45   * For more finegrained (per request) control, another option is to override
46   * method getExecutionParams() itself. This method will be called each request
47   * and by default just returns the instance variable. You can vary
48   * the processing by returning a new instance of ExecutionParams
49   * (be carefull NOT to just change values in the instance variable, as this is
50   * shared by all clients), with its properties set for that request.
51   * 
52   * @author Eelco Hillenius
53   */
54  public final class ExecutionParams implements Serializable, Cloneable
55  {
56  
57  	/*** if true, the no cache headers will be set */
58  	private boolean noCache = true;
59  	
60  	/***
61  	 * If we get an empty string, should it be translated to a null (true) or should
62  	 * the empty String be kept (false). This property is true by default
63  	 */
64  	private boolean setNullForEmptyString = true;
65  	
66  	/***
67  	 * Indicates whether the configuration parameters of the controller 
68  	 * should be used for the population process. 
69  	 * This property is false by default.
70  	 * 
71  	 * The order in which parameters/ attributes are used for population:
72  	 * 1. controller parameters (if includeControllerParameters == true)
73  	 * 2. session attributes (if includeSessionAttributes == true)
74  	 * 3. request parameters
75  	 * 4. request attributes (if includeRequestAttributes == true)
76  	 * 
77  	 * A map with parameters for the population process is filled in the above order, which
78  	 * means that every step can overwrite it's predecessor.
79  	 *  
80  	 */	
81  	private boolean includeControllerParameters = false;
82  	
83  	/***
84  	 * Indicates whether the session attributes of the current session
85  	 * should be used for the population process.
86  	 * This property is false by default.
87  	 * 
88  	 * The order in which parameters/ attributes are used for population:
89  	 * 1. controller parameters (if includeControllerParameters == true)
90  	 * 2. session attributes (if includeSessionAttributes == true)
91  	 * 3. request parameters
92  	 * 4. request attributes (if includeRequestAttributes == true)
93  	 * 
94  	 * A map with parameters for the population process is filled in the above order, which
95  	 * means that every step can overwrite it's predecessor.
96  	 * 
97  	 */	
98  	private boolean includeSessionAttributes = false;
99  	
100 	/***
101 	 * Indicates whether the attributes of the current request 
102 	 * should be used for the population process.
103 	 * This property is false by default.
104 	 * 
105 	 * The order in which parameters/ attributes are used for population:
106 	 * 1. controller parameters (if includeControllerParameters == true)
107 	 * 2. session attributes (if includeSessionAttributes == true)
108 	 * 3. request parameters
109 	 * 4. request attributes (if includeRequestAttributes == true)
110 	 * 
111 	 * A map with parameters for the population process is filled in the above order, which
112 	 * means that every step can overwrite it's predecessor.
113 	 * 
114 	 */	
115 	private boolean includeRequestAttributes = false;
116 	
117 	/***
118 	 * Indicates whether the form validators should be executed when one of the 
119 	 * field validators failed. Default == true
120 	 */
121 	private boolean doFormValidationIfFieldValidationFailed = true;
122 	
123 	/***
124 	 * Indicates whether the perform method of the control should be executed, 
125 	 * even if population/ validation failed. 
126 	 * Use this only in very special cases; extended usage will
127 	 * probably result in messy code. 
128 	 * Default == false
129 	 */
130 	private boolean doPerformIfPopulationFailed = false;
131 	
132 	/***
133 	 * Indicates whether the form bean context should be reused for multiple invocations 
134 	 * within the same request.
135 	 * Default is true.
136 	 */
137 	private boolean reuseFormBeanContext = true;
138 	
139 	/***
140 	 * If population or validation fails and this property is true,
141 	 * all request parameters will be saved as override values. This
142 	 * will give you at least the full request the client sent, and
143 	 * guards you for the situation where properties that normally
144 	 * would be loaded in the command method are not set because
145 	 * of the population/ validation failure.
146 	 * Default is true.
147 	 */
148 	private boolean saveReqParamsAsOverrideFieldsOnError = true;
149 	
150 	/***
151 	 * When an unexpected error occurs, should that be interpreted as a failure,
152 	 * or should it just be ignored. E.g. If you have request parameter
153 	 * not-a-valid-property, Ognl will throw exception 'ognl.InappropriateExpressionException'
154 	 * In strict mode, this will have the effect that the population process is
155 	 * marked as failed. If not in strict mode, the exception will be ignored.
156 	 */
157 	private boolean strictPopulationMode = true;
158 	
159 	/***
160 	 * Whether string input values should be trimmed before conversion.
161 	 * Note that the actual trimming depends on the used populator, so
162 	 * setting this parameter does not garantee trimming for all parameters.
163 	 */
164 	private boolean trimStringInputValues = true;
165 
166 	
167 	/***
168 	 * flag whether Baritus should try to populate the form bean.
169 	 * Setting this flag to false, and thus letting Baritus skip
170 	 * population and validation can be usefull when you link commands
171 	 * within the same request and want to reuse the allready populated
172 	 * form bean without doing population and validation as well.<br>
173 	 * <strong>BEWARE</strong> that this is also skips population of request attributes
174 	 * etc. that were set by the controllers earlier in the command stack.<br>
175 	 * <strong>ALSO</strong> note that if this flag is false, Baritus will consider
176 	 * population to be succesfull, even though the population of the
177 	 * prior control might not have been.
178 	 */
179 	private boolean populateAndValidate = true;
180 
181 	/***
182 	 * if true, the no cache headers will be set
183 	 * @return boolean
184 	 */
185 	public boolean isNoCache()
186 	{
187 		return noCache;
188 	}
189 
190 	/***
191 	 * if true, the no cache headers will be set
192 	 * @param noCache
193 	 */
194 	public void setNoCache(boolean noCache)
195 	{
196 		this.noCache = noCache;
197 	}
198 
199 	/***
200 	 * If we get an empty string it should be translated to a null (true) or to
201 	 * an empty String false. This property is true by default
202 	 * @return boolean
203 	 */
204 	public boolean isSetNullForEmptyString()
205 	{
206 		return setNullForEmptyString;
207 	}
208 
209 	/***
210 	 * If we get an empty string it should be translated to a null (true) or to
211 	 * an empty String false. This property is true by default
212 	 * @param b
213 	 */
214 	public void setSetNullForEmptyString(boolean b)
215 	{
216 		setNullForEmptyString = b;
217 	}
218 	
219 	/***
220 	 * include controller parameters in the population process
221 	 * 
222 	 * The order in which parameters/ attributes are used for population:
223 	 * 1. controller parameters (if includeControllerParameters == true)
224 	 * 2. session attributes (if includeSessionAttributes == true)
225 	 * 3. request parameters
226 	 * 4. request attributes (if includeRequestAttributes == true)
227 	 * 
228 	 * @return boolean include controller parameters in the population process
229 	 */
230 	public boolean isIncludeControllerParameters()
231 	{
232 		return includeControllerParameters;
233 	}
234 	
235 	/***
236 	 * set whether to include controller parameters in the population process
237 	 * 
238 	 * The order in which parameters/ attributes are used for population:
239 	 * 1. controller parameters (if includeControllerParameters == true)
240 	 * 2. session attributes (if includeSessionAttributes == true)
241 	 * 3. request parameters
242 	 * 4. request attributes (if includeRequestAttributes == true)
243 	 * 
244 	 * @param b set whether to include controller parameters in the population process
245 	 */
246 	public void setIncludeControllerParameters(boolean b)
247 	{
248 		includeControllerParameters = b;
249 	}
250 
251 	/***
252 	 * include session attributes in the population process
253 	 * 
254 	 * The order in which parameters/ attributes are used for population:
255 	 * 1. controller parameters (if includeControllerParameters == true)
256 	 * 2. session attributes (if includeSessionAttributes == true)
257 	 * 3. request parameters
258 	 * 4. request attributes (if includeRequestAttributes == true)
259 	 * 
260 	 * @return boolean include controller parameters in the population process
261 	 */
262 	public boolean isIncludeSessionAttributes()
263 	{
264 		return includeSessionAttributes;
265 	}
266 
267 	/***
268 	 * set whether to include session attributes in the population process
269 	 * 
270 	 * The order in which parameters/ attributes are used for population:
271 	 * 1. controller parameters (if includeControllerParameters == true)
272 	 * 2. session attributes (if includeSessionAttributes == true)
273 	 * 3. request parameters
274 	 * 4. request attributes (if includeRequestAttributes == true)
275 	 * 
276 	 * @param b set whether to include session attributes in the population process
277 	 */
278 	public void setIncludeSessionAttributes(boolean b)
279 	{
280 		includeSessionAttributes = b;
281 	}
282 
283 	/***
284 	 * include request attributes in the population process
285 	 * 
286 	 * The order in which parameters/ attributes are used for population:
287 	 * 1. controller parameters (if includeControllerParameters == true)
288 	 * 2. session attributes (if includeSessionAttributes == true)
289 	 * 3. request parameters
290 	 * 4. request attributes (if includeRequestAttributes == true)
291 	 * 
292 	 * @return boolean include request parameters in the population process
293 	 */
294 	public boolean isIncludeRequestAttributes()
295 	{
296 		return includeRequestAttributes;
297 	}
298 
299 	/***
300 	 * set whether to include request attributes in the population process
301 	 * 
302 	 * The order in which parameters/ attributes are used for population:
303 	 * 1. controller parameters (if includeControllerParameters == true)
304 	 * 2. session attributes (if includeSessionAttributes == true)
305 	 * 3. request parameters
306 	 * 4. request attributes (if includeRequestAttributes == true)
307 	 * 
308 	 * @param b set whether to include request attributes in the population process
309 	 */
310 	public void setIncludeRequestAttributes(boolean b)
311 	{
312 		includeRequestAttributes = b;
313 	}
314 
315 	/***
316 	 * Indicates whether the form validators should be executed when one of the 
317 	 * field validators failed. Default == true.
318 	 * 
319 	 * @return boolean Whether the form validators should be executed when one of the 
320 	 * 		field validators failed.
321 	 */
322 	public boolean isDoFormValidationIfFieldValidationFailed()
323 	{
324 		return doFormValidationIfFieldValidationFailed;
325 	}
326 
327 	/***
328 	 * Indicates whether the form validators should be executed when one of the 
329 	 * field validators failed. Default == true.
330 	 * 
331 	 * @param b whether the form validators should be executed when one of the 
332 	 * 		field validators failed.
333 	 */
334 	public void setDoFormValidationIfFieldValidationFailed(boolean b)
335 	{
336 		doFormValidationIfFieldValidationFailed = b;
337 	}
338 
339 	/***
340 	 * Whether the perform method of the control should be executed, even if the population/
341 	 * validation failed. Use this only in very special cases; extended usage will
342 	 * probably result in messy code. Default == false.
343 	 * 
344 	 * @return boolean whether the perform method of the control should be executed, even if the population/
345 	 * validation failed.
346 	 */
347 	public boolean isDoPerformIfPopulationFailed()
348 	{
349 		return doPerformIfPopulationFailed;
350 	}
351 
352 	/***
353 	 * Whether the perform method of the control should be executed, even if the population/
354 	 * validation failed. Use this only in very special cases; extended usage will
355 	 * probably result in messy code. Default == false.
356 	 * 
357 	 * @param b whether the perform method of the control should be executed, even if the population/
358 	 * validation failed.
359 	 */
360 	public void setDoPerformIfPopulationFailed(boolean b)
361 	{
362 		doPerformIfPopulationFailed = b;
363 	}
364 
365 	/***
366 	 * Whether to reuse the context for multiple invocations within the same request.
367 	 * Default is true.
368 	 * @return reuse the context for multiple invocations within the same request
369 	 */
370 	public boolean isReuseFormBeanContext()
371 	{
372 		return reuseFormBeanContext;
373 	}
374 
375 	/***
376 	 * Set whether to reuse the context for multiple invocations within the same request.
377 	 * Default is true.
378 	 * @param b reuse the context for multiple invocations within the same request
379 	 */
380 	public void setReuseFormBeanContext(boolean b)
381 	{
382 		reuseFormBeanContext = b;
383 	}
384 
385 	/***
386 	 * Gets whether all request parameters should be saved as override
387 	 * 	values when population or validation fails.
388 	 * If population or validation failes and this property is true,
389 	 * all request parameters will be saved as override values. This
390 	 * will give you at least the full request the client sent, and
391 	 * guards you for the situation where properties that normally
392 	 * would be loaded in the command method are not set because
393 	 * of the population/ validation failure.
394 	 * Default is true.
395 
396 	 * @return boolean wheter all request parameters should be saved as override
397 	 * 	values when population or validation fails.
398 	 */
399 	public boolean isSaveReqParamsAsOverrideFieldsOnError()
400 	{
401 		return saveReqParamsAsOverrideFieldsOnError;
402 	}
403 
404 	/***
405 	 * Sets whether all request parameters should be saved as override
406 	 * 	values when population or validation fails.
407 	 * If population or validation failes and this property is true,
408 	 * all request parameters will be saved as override values. This
409 	 * will give you at least the full request the client sent, and
410 	 * guards you for the situation where properties that normally
411 	 * would be loaded in the command method are not set because
412 	 * of the population/ validation failure.
413 	 * Default is true.
414 
415 	 * @param b
416 	 */
417 	public void setSaveReqParamsAsOverrideFieldsOnError(boolean b)
418 	{
419 		saveReqParamsAsOverrideFieldsOnError = b;
420 	}
421 
422 	/***
423 	 * When an unexpected error occurs, should that be interpreted as a failure,
424 	 * or should it just be ignored. E.g. If you have request parameter
425 	 * not-a-valid-property, Ognl will throw exception 'ognl.InappropriateExpressionException'
426 	 * In strict mode, this will have the effect that the population process is
427 	 * marked as failed. If not in strict mode, the exception will be ignored.
428 	 * 
429 	 * @return boolean whether the population mode is strict
430 	 */
431 	public boolean isStrictPopulationMode()
432 	{
433 		return strictPopulationMode;
434 	}
435 
436 	/***
437 	 * When an unexpected error occurs, should that be interpreted as a failure,
438 	 * or should it just be ignored. E.g. If you have request parameter
439 	 * not-a-valid-property, Ognl will throw exception 'ognl.InappropriateExpressionException'
440 	 * In strict mode, this will have the effect that the population process is
441 	 * marked as failed. If not in strict mode, the exception will be ignored.
442 	 * 
443 	 * @param b whether the population mode is strict
444 	 */
445 	public void setStrictPopulationMode(boolean b)
446 	{
447 		strictPopulationMode = b;
448 	}
449 
450 	/***
451 	 * Get whether string input values should be trimmed before conversion.
452 	 * True by default.
453 	 * @return boolean hhether string input values should be trimmed before conversion.
454 	 */
455 	public boolean isTrimStringInputValues()
456 	{
457 		return trimStringInputValues;
458 	}
459 
460 	/***
461 	 * Set whether string input values should be trimmed before conversion.
462 	 * @param b Whether string input values should be trimmed before conversion.
463 	 */
464 	public void setTrimStringInputValues(boolean b)
465 	{
466 		trimStringInputValues = b;
467 	}
468 
469 	/***
470 	 * Get flag whether Baritus should try to populate the form bean.
471 	 * Setting this flag to false, and thus letting Baritus skip
472 	 * population and validation can be usefull when you link commands
473 	 * within the same request and want to reuse the allready populated
474 	 * form bean without doing population and validation as well.<br>
475 	 * <strong>BEWARE</strong> that this is also skips population of request attributes
476 	 * etc. that were set by the controllers earlier in the command stack.<br>
477 	 * <strong>ALSO</strong> note that if this flag is false, Baritus will consider
478 	 * population to be succesfull, even though the population of the
479 	 * prior control might not have been.
480 	 * 
481 	 * @return boolean whether Baritus should try to populate the form bean.
482 	 */
483 	public boolean isPopulateAndValidate()
484 	{
485 		return populateAndValidate;
486 	}
487 
488 	/***
489 	 * Set flag whether Baritus should try to populate the form bean.
490 	 * Setting this flag to false, and thus letting Baritus skip
491 	 * population and validation can be usefull when you link commands
492 	 * within the same request and want to reuse the allready populated
493 	 * form bean without doing population and validation as well.<br>
494 	 * <strong>BEWARE</strong> that this is also skips population of request attributes
495 	 * etc. that were set by the controllers earlier in the command stack.<br>
496 	 * <strong>ALSO</strong> note that if this flag is false, Baritus will consider
497 	 * population to be succesfull, even though the population of the
498 	 * prior control might not have been.
499 	 * 
500 	 * @param populateAndValidate whether Baritus should try to populate the form bean.
501 	 */
502 	public void setPopulateAndValidate(boolean populateAndValidate)
503 	{
504 		this.populateAndValidate = populateAndValidate;
505 	}
506     
507     /***
508      * Clone this object.
509      * @return Object deep copy
510      * @see java.lang.Object#clone()
511      */
512     public Object clone()
513     {
514         try 
515         {
516             return super.clone();
517         } 
518         catch (CloneNotSupportedException e) 
519         {
520             throw new RuntimeException(e);
521         }
522     }
523 
524     /***
525      * String rep.
526      * @see java.lang.Object#toString()
527      */
528     public String toString()
529     {
530         StringBuffer b = new StringBuffer(super.toString());
531         b.append(" {");
532         try
533         {
534             Class clz = getClass();
535             Field[] fields = clz.getDeclaredFields();
536             int length = fields.length;
537             for(int i = 0; i < length; i++)
538             {
539                 b.append(fields[i].getName())
540                  .append("=")
541                  .append(fields[i].get(this));
542                 if((i+1) < length)
543                 {
544                     b.append(",");
545                 }
546             }
547         }
548         catch(Exception e)
549         {
550             e.printStackTrace();
551             b.append(e.getMessage());
552         }
553         b.append("}");
554         return b.toString();
555     }
556 
557 }