KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > model > ActionModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.compiler.model;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.beehive.netui.compiler.model.schema.struts11.ActionDocument.Action;
26 import org.apache.beehive.netui.compiler.model.schema.struts11.SetPropertyDocument.SetProperty;
27 import org.apache.beehive.netui.compiler.model.schema.struts11.ExceptionDocument;
28 import org.apache.beehive.netui.compiler.model.schema.struts11.ForwardDocument;
29 import org.apache.beehive.netui.compiler.model.schema.struts11.ActionDocument;
30 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
31 import org.apache.xmlbeans.XmlObject;
32
33
34 /**
35  * Represents an ActionMapping in a Struts based web application or
36  * sub application.
37  */

38 public class ActionModel
39         extends AbstractForwardContainer
40         implements ForwardContainer, ExceptionContainer, JpfLanguageConstants
41 {
42     public static final String JavaDoc DEFAULT_FORM_SCOPE = "request";
43
44     private static final String JavaDoc JPF_ACTION_MAPPING_CLASSNAME = PAGEFLOW_PACKAGE + ".config.PageFlowActionMapping";
45
46     
47     // Struts attributes.
48
private ArrayList JavaDoc _exceptionCatches = new ArrayList JavaDoc();
49     private String JavaDoc _attribute;
50     private String JavaDoc _className;
51     private String JavaDoc _forward;
52     private String JavaDoc _include;
53     private String JavaDoc _input;
54     private String JavaDoc _formBeanName;
55     private String JavaDoc _parameter;
56     private String JavaDoc _path; // required to be set
57
private String JavaDoc _prefix;
58     private String JavaDoc _scope = DEFAULT_FORM_SCOPE;
59     private String JavaDoc _suffix;
60     private String JavaDoc _type;
61     private boolean _unknown;
62     private String JavaDoc _roles;
63     private boolean _validate;
64
65     // Non-struts attributes.
66
private String JavaDoc _unqualifiedActionPath;
67     private boolean _loginRequired;
68     private boolean _isOverloaded;
69     private boolean _readonly;
70     private boolean _isSimpleAction = false;
71     private boolean _preventDoubleSubmit = false;
72     private String JavaDoc _formMember; // pageflow-scoped form
73
private String JavaDoc _formClass; // applicable for non-ActionForm-derived types
74
private Map JavaDoc _conditionalForwards;
75     private String JavaDoc _formBeanMessageResourcesKey;
76     private String JavaDoc _defaultForwardName; // for Simple Actions
77

78
79     public ActionModel( String JavaDoc path, StrutsApp parent )
80     {
81         this( path, null, parent );
82     }
83
84
85     public ActionModel( String JavaDoc path, String JavaDoc formName, StrutsApp parent )
86     {
87         super( parent );
88         this._path = path;
89         this._formBeanName = formName;
90     }
91     
92     protected ActionModel( StrutsApp parent )
93     {
94         this( null, null, parent );
95     }
96
97     /**
98      * Construct a copy of the given mapping, with the given path.
99      */

100     public ActionModel( ActionModel src, String JavaDoc newPath )
101     {
102         super( src );
103         this._path = newPath;
104         this._formBeanName = src._formBeanName;
105         _exceptionCatches = ( ArrayList JavaDoc ) src._exceptionCatches.clone();
106         _attribute = src._attribute;
107         _className = src._className;
108         _forward = src._forward;
109         _include = src._include;
110         _input = src._input;
111         _validate = src._validate;
112         _parameter = src._parameter;
113         _prefix = src._prefix;
114         _scope = src._scope;
115         _suffix = src._suffix;
116         _type = src._type;
117         _unknown = src._unknown;
118         _formMember = src._formMember;
119         _formClass = src._formClass;
120         _roles = src._roles;
121         _loginRequired = src._loginRequired;
122         _preventDoubleSubmit = src._preventDoubleSubmit;
123         _isSimpleAction = src._isSimpleAction;
124         _isOverloaded = src._isOverloaded;
125         _readonly = src._readonly;
126         _unqualifiedActionPath = src._unqualifiedActionPath;
127         _defaultForwardName = src._defaultForwardName;
128     }
129     
130     public void setFormBeanName( String JavaDoc formBeanName )
131     {
132         _formBeanName = formBeanName;
133     }
134     
135     public void writeToXMLBean( Action xb )
136     {
137         xb.setPath( _path );
138         
139         if ( xb.getName() == null && _formBeanName != null ) xb.setName( _formBeanName );
140         if ( xb.getClassName() == null && _className != null ) xb.setClassName( _className );
141         if ( xb.getType() == null && _type != null ) xb.setType( _type );
142         if ( xb.getAttribute() == null && _attribute != null ) xb.setAttribute( _attribute );
143         if ( xb.getInput() == null && _input != null ) xb.setInput( _input );
144         if ( xb.getParameter() == null && _parameter != null ) xb.setParameter( _parameter );
145         if ( xb.getPrefix() == null && _prefix != null ) xb.setPrefix( _prefix );
146         if ( xb.getSuffix() == null && _suffix != null ) xb.setSuffix( _suffix );
147         
148         if ( xb.getScope() == null )
149         {
150             if ( _scope != null )
151             {
152                 if ( _scope.equals( "request" ) )
153                 {
154                     xb.setScope( Action.Scope.REQUEST );
155                 }
156                 else if ( _scope.equals( "session" ) )
157                 {
158                     xb.setScope( Action.Scope.SESSION );
159                 }
160                 else
161                 {
162                     assert false : _scope;
163                 }
164             }
165             else
166             {
167                 xb.setScope( Action.Scope.REQUEST );
168             }
169         }
170         
171         if ( xb.getRoles() == null && _roles != null ) xb.setRoles( _roles );
172         if ( xb.getForward2() == null && _forward != null ) xb.setForward2( _forward );
173         if ( xb.getInclude() == null && _include != null ) xb.setInclude( _include );
174         if ( xb.getUnknown() == null && _unknown ) xb.setUnknown( Action.Unknown.TRUE );
175         if ( xb.getValidate() == null ) xb.setValidate( _validate ? Action.Validate.TRUE : Action.Validate.FALSE );
176         
177         if ( _unqualifiedActionPath != null ) addSetProperty( xb, "unqualifiedActionPath", _unqualifiedActionPath );
178         if ( _formMember != null ) addSetProperty( xb, "formMember", _formMember );
179         if ( _formClass != null ) addSetProperty( xb, "formClass", _formClass );
180         if ( _loginRequired ) addSetProperty( xb, "loginRequired", _loginRequired );
181         if ( _preventDoubleSubmit ) addSetProperty( xb, "preventDoubleSubmit", _preventDoubleSubmit );
182         if ( _isOverloaded ) addSetProperty( xb, "overloaded", _isOverloaded );
183         if ( _readonly ) addSetProperty( xb, "readonly", _readonly );
184         if ( _isSimpleAction ) addSetProperty( xb, "simpleAction", _isSimpleAction );
185         if ( _defaultForwardName != null ) addSetProperty( xb, "defaultForward", _defaultForwardName );
186         
187         if ( _conditionalForwards != null )
188         {
189             addSetProperty( xb, "conditionalForwards", getMapString( _conditionalForwards) );
190         }
191         
192         if ( _formBeanMessageResourcesKey != null )
193         {
194             addSetProperty( xb, "formBeanMessageResourcesKey", _formBeanMessageResourcesKey );
195         }
196                 
197         if ( _exceptionCatches != null && ! _exceptionCatches.isEmpty() )
198         {
199             ExceptionDocument.Exception[] existingExceptions = xb.getExceptionArray();
200             
201             for ( int i = 0; i < _exceptionCatches.size(); ++i )
202             {
203                 ExceptionModel ec = ( ExceptionModel ) _exceptionCatches.get( i );
204                 ExceptionDocument.Exception exceptionToEdit = null;
205                 
206                 for ( int j = 0; j < existingExceptions.length; ++j )
207                 {
208                     if ( ec.getType().equals( existingExceptions[j].getType() ) )
209                     {
210                         exceptionToEdit = existingExceptions[j];
211                         break;
212                     }
213                 }
214                 
215                 if ( exceptionToEdit == null )
216                 {
217                     exceptionToEdit = xb.addNewException();
218                 }
219                 
220                 ec.writeToXMLBean( exceptionToEdit );
221             }
222         }
223                 
224         //
225
// forwards
226
//
227
writeForwards( xb.getForwardArray(), xb );
228
229         addComment( xb );
230     }
231     
232     private void addSetProperty( ActionDocument.Action xb, String JavaDoc propertyName, boolean propertyValue )
233     {
234         addSetProperty( xb, propertyName, Boolean.toString( propertyValue ) );
235     }
236     
237     private void addSetProperty( ActionDocument.Action xb, String JavaDoc propertyName, String JavaDoc propertyValue )
238     {
239         SetProperty prop = xb.addNewSetProperty();
240         prop.setProperty( propertyName );
241         prop.setValue( propertyValue );
242         if ( xb.getClassName() == null ) xb.setClassName( JPF_ACTION_MAPPING_CLASSNAME );
243     }
244     
245     protected ForwardDocument.Forward addNewForward( XmlObject xmlObject )
246     {
247         return ( ( Action ) xmlObject ).addNewForward();
248     }
249     
250     /**
251      * Implemented for {@link ExceptionContainer}.
252      */

253     public void addException( ExceptionModel em )
254     {
255         _exceptionCatches.add( em );
256     }
257
258     public String JavaDoc getAttribute()
259     {
260         return _attribute;
261     }
262
263     public void setAttribute( String JavaDoc attribute )
264     {
265         this._attribute = attribute;
266     }
267
268     public String JavaDoc getClassName()
269     {
270         return _className;
271     }
272
273     public void setClassName( String JavaDoc className )
274     {
275         this._className = className;
276     }
277
278     public String JavaDoc getForward()
279     {
280         return _forward;
281     }
282
283     public void setForward( String JavaDoc forward )
284     {
285         this._forward = forward;
286     }
287
288     public String JavaDoc getInclude()
289     {
290         return _include;
291     }
292
293     public void setInclude( String JavaDoc include )
294     {
295         this._include = include;
296     }
297
298     public String JavaDoc getInput()
299     {
300         return _input;
301     }
302
303     public void setInput( String JavaDoc input )
304     {
305         this._input = input;
306     }
307
308     public String JavaDoc getName()
309     {
310         return _formBeanName;
311     }
312     
313     public String JavaDoc getFormBeanName()
314     {
315         return _formBeanName;
316     }
317
318     public void setName( String JavaDoc name )
319     {
320         this._formBeanName = name;
321     }
322
323     public String JavaDoc getParameter()
324     {
325         return _parameter;
326     }
327
328     public void setParameter( String JavaDoc parameter )
329     {
330         this._parameter = parameter;
331     }
332
333     public boolean isValidate()
334     {
335         return _validate;
336     }
337
338     public void setValidate( boolean validate )
339     {
340         _validate = validate;
341     }
342
343     public String JavaDoc getPath()
344     {
345         return _path;
346     }
347     
348     public String JavaDoc getPath( boolean useUnqualifiedPath )
349     {
350         if ( useUnqualifiedPath && _unqualifiedActionPath != null )
351         {
352             return _unqualifiedActionPath;
353         }
354         else
355         {
356             return _path;
357         }
358     }
359
360     public void setPath( String JavaDoc path )
361     {
362         this._path = path;
363     }
364
365     public String JavaDoc getPrefix()
366     {
367         return _prefix;
368     }
369
370     public void setPrefix( String JavaDoc prefix )
371     {
372         this._prefix = prefix;
373     }
374
375     public String JavaDoc getScope()
376     {
377         return _scope == null ? DEFAULT_FORM_SCOPE : _scope;
378     }
379
380     public void setScope( String JavaDoc scope )
381     {
382         this._scope = scope;
383     }
384
385     public String JavaDoc getSuffix()
386     {
387         return _suffix;
388     }
389
390     public void setSuffix( String JavaDoc suffix )
391     {
392         this._suffix = suffix;
393     }
394
395     public String JavaDoc getType()
396     {
397         return _type;
398     }
399
400     public void setType( String JavaDoc type )
401     {
402         this._type = type;
403     }
404
405     public boolean isUnknown()
406     {
407         return _unknown;
408     }
409
410     public void setUnknown( boolean unknown )
411     {
412         this._unknown = unknown;
413     }
414
415     public String JavaDoc getUnqualifiedActionPath()
416     {
417         return _unqualifiedActionPath;
418     }
419
420     public void setUnqualifiedActionPath( String JavaDoc unqualifiedActionPath )
421     {
422         this._unqualifiedActionPath = unqualifiedActionPath;
423     }
424
425     public String JavaDoc getDefaultForwardName()
426     {
427         return _defaultForwardName;
428     }
429
430     public void setDefaultForwardName( String JavaDoc defaultForwardName )
431     {
432         _defaultForwardName = defaultForwardName;
433     }
434
435     public String JavaDoc getRoles()
436     {
437         return _roles;
438     }
439
440     public void setRoles( String JavaDoc roles )
441     {
442         _roles = roles;
443     }
444
445     public void setLoginRequired( boolean loginRequired )
446     {
447         _loginRequired = loginRequired;
448     }
449     
450     public void setPreventDoubleSubmit( boolean preventDoubleSubmit )
451     {
452         _preventDoubleSubmit = preventDoubleSubmit;
453     }
454
455     public boolean isSimpleAction()
456     {
457         return _isSimpleAction;
458     }
459
460     public void setSimpleAction( boolean simpleAction )
461     {
462         _isSimpleAction = simpleAction;
463     }
464
465     public boolean isOverloaded()
466     {
467         return _isOverloaded;
468     }
469
470     public void setOverloaded( boolean overloaded )
471     {
472         _isOverloaded = overloaded;
473     }
474
475     public String JavaDoc getFormMember()
476     {
477         return _formMember;
478     }
479
480     public void setFormMember( String JavaDoc formMember )
481     {
482         _formMember = formMember;
483     }
484
485     public String JavaDoc getFormClass()
486     {
487         return _formClass;
488     }
489
490     public void setFormClass( String JavaDoc formClass )
491     {
492         _formClass = formClass;
493     }
494
495     public boolean isReadonly()
496     {
497         return _readonly;
498     }
499
500     public void setReadonly( boolean readonly )
501     {
502         _readonly = readonly;
503     }
504     
505     public void addConditionalForward( String JavaDoc expression, String JavaDoc forwardName )
506     {
507         if ( _conditionalForwards == null ) _conditionalForwards = new LinkedHashMap JavaDoc();
508         _conditionalForwards.put( expression, forwardName );
509     }
510     
511     private static String JavaDoc getMapString( Map JavaDoc map )
512     {
513         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
514         
515         for ( Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext(); )
516         {
517             Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) i.next();
518             retVal.append( entry.getValue() ).append( ':' ).append( entry.getKey() ).append( ';' );
519         }
520         
521         return retVal.toString();
522     }
523
524     public void setFormBeanMessageResourcesKey( String JavaDoc formBeanMessageResourcesKey )
525     {
526         _formBeanMessageResourcesKey = formBeanMessageResourcesKey;
527     }
528     
529
530 }
531
Popular Tags