1 18 package org.apache.beehive.netui.compiler.genmodel; 19 20 import org.apache.beehive.netui.compiler.CompilerUtils; 21 import org.apache.beehive.netui.compiler.JpfLanguageConstants; 22 import org.apache.beehive.netui.compiler.model.ActionModel; 23 import org.apache.beehive.netui.compiler.model.ForwardModel; 24 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance; 25 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration; 26 import org.apache.beehive.netui.compiler.typesystem.declaration.Declaration; 27 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration; 28 import org.apache.beehive.netui.compiler.typesystem.declaration.ParameterDeclaration; 29 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration; 30 import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType; 31 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance; 32 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 38 public class GenActionModel 39 extends ActionModel 40 implements JpfLanguageConstants 41 { 42 public GenActionModel( Declaration sourceElement, GenStrutsApp parentApp, ClassDeclaration jclass ) 43 { 44 super( parentApp ); 45 46 init( getActionName( sourceElement ), getActionAnnotation( sourceElement ), parentApp, jclass ); 47 48 setFormBeanName( getFormBean( sourceElement, parentApp ) ); 50 } 51 52 protected GenActionModel( String actionName, AnnotationInstance ann, GenStrutsApp parentApp, ClassDeclaration jclass ) 53 { 54 super( parentApp ); 55 init( actionName, ann, parentApp, jclass ); 56 } 57 58 private void init( String actionName, AnnotationInstance annotation, GenStrutsApp parentApp, ClassDeclaration jclass ) 59 { 60 setPath( '/' + actionName ); 61 62 Boolean loginRequired = CompilerUtils.getBoolean( annotation, LOGIN_REQUIRED_ATTR, true ); 66 if ( loginRequired == null ) 67 { 68 loginRequired = parentApp.getFlowControllerInfo().getMergedControllerAnnotation().isLoginRequired(); 69 } 70 if ( loginRequired != null ) setLoginRequired( loginRequired.booleanValue() ); 71 72 Boolean preventDoubleSubmit = CompilerUtils.getBoolean( annotation, PREVENT_DOUBLE_SUBMIT_ATTR, false ); 76 setPreventDoubleSubmit( preventDoubleSubmit.booleanValue() ); 77 78 Boolean readOnly = CompilerUtils.getBoolean( annotation, READONLY_ATTR, true ); 82 if ( readOnly == null ) 83 { 84 readOnly = Boolean.valueOf( parentApp.getFlowControllerInfo().getMergedControllerAnnotation().isReadOnly() ); 85 } 86 setReadonly( readOnly.booleanValue() ); 87 88 if ( loginRequired == null || loginRequired.booleanValue() ) setRolesAllowed( annotation, jclass, parentApp ); 92 93 setType( FLOW_CONTROLLER_ACTION_CLASS ); 97 setParameter( jclass.getQualifiedName() ); 98 99 setFormMember( CompilerUtils.getString( annotation, USE_FORM_BEAN_ATTR, true ) ); 103 104 getForwards( annotation, jclass, parentApp ); 108 109 AnnotationInstance validateErrFwd = CompilerUtils.getAnnotation( annotation, VALIDATION_ERROR_FORWARD_ATTR, true ); 113 boolean doValidation = false; 114 if ( validateErrFwd != null ) 115 { 116 ForwardModel fwd = new GenForwardModel( parentApp, validateErrFwd, jclass, " (validationErrorForward)" ); 117 addForward( fwd ); 118 setInput( fwd.getName() ); 119 doValidation = true; 120 } 121 122 Boolean explicitDoValidation = CompilerUtils.getBoolean( annotation, DO_VALIDATION_ATTR, true ); 126 setValidate( explicitDoValidation != null ? explicitDoValidation.booleanValue() : doValidation ); 127 128 GenExceptionModel.addCatches( annotation, this, jclass, parentApp, this ); 132 } 133 134 private void setRolesAllowed( AnnotationInstance annotation, ClassDeclaration jclass, GenStrutsApp parentApp ) 135 { 136 List rolesAllowed = CompilerUtils.getStringArray( annotation, ROLES_ALLOWED_ATTR, true ); 137 List classLevelRA = parentApp.getFlowControllerInfo().getMergedControllerAnnotation().getRolesAllowed(); 138 Iterator it = null; 139 140 if ( rolesAllowed != null && classLevelRA != null ) 141 { 142 HashSet merged = new HashSet (); 143 for ( Iterator ii = rolesAllowed.iterator(); ii.hasNext(); ) 144 { 145 String role = ( String ) ii.next(); 146 merged.add( role ); 147 } 148 for ( Iterator ii = classLevelRA.iterator(); ii.hasNext(); ) 149 { 150 String classLevelRole = ( String ) ii.next(); 151 merged.add( classLevelRole ); 152 } 153 it = merged.iterator(); 154 } 155 else if ( rolesAllowed != null ) 156 { 157 it = rolesAllowed.iterator(); 158 } 159 else if ( classLevelRA != null ) 160 { 161 it = classLevelRA.iterator(); 162 } 163 164 if ( it != null && it.hasNext() ) 165 { 166 StringBuffer rolesAllowedStr = new StringBuffer ( ( String ) it.next() ); 167 168 while ( it.hasNext() ) 169 { 170 rolesAllowedStr.append( ',' ).append( ( ( String ) it.next() ).trim() ); 171 } 172 173 setRoles( rolesAllowedStr.toString() ); 174 } 175 } 176 177 protected static String getActionName( Declaration sourceElement ) 178 { 179 return sourceElement.getSimpleName(); 180 } 181 182 185 protected String getFormBean( Declaration sourceElement, GenStrutsApp parentApp ) 186 { 187 assert sourceElement instanceof MethodDeclaration : sourceElement.getClass().getName(); 188 ParameterDeclaration[] params = ( ( MethodDeclaration ) sourceElement ).getParameters(); 189 String formBeanName = null; 190 191 if ( params.length > 0 ) 192 { 193 assert params.length == 1 : params.length; TypeInstance paramType = CompilerUtils.getGenericBoundsType( params[0].getType() ); 195 formBeanName = addFormBean( paramType, parentApp ); 196 } 197 198 return formBeanName; 199 } 200 201 protected String addFormBean( TypeInstance paramType, GenStrutsApp parentApp ) 202 { 203 paramType = CompilerUtils.getGenericBoundsType( paramType ); 204 assert paramType instanceof DeclaredType : paramType.getClass().getName(); TypeDeclaration decl = CompilerUtils.getDeclaration( ( DeclaredType ) paramType ); 206 String formBeanName = parentApp.addFormBean( decl, this ); 207 208 if ( ! CompilerUtils.isAssignableFrom( PAGEFLOW_FORM_CLASS_NAME, decl, parentApp.getEnv() ) ) 212 { 213 setFormClass( CompilerUtils.getLoadableName( decl ) ); 214 } 215 216 return formBeanName; 217 } 218 219 protected AnnotationInstance getActionAnnotation( Declaration sourceElement ) 220 { 221 assert sourceElement instanceof MethodDeclaration : sourceElement.getClass().getName(); 222 return CompilerUtils.getAnnotation( sourceElement, ACTION_TAG_NAME ); 223 } 224 225 protected void getForwards( AnnotationInstance annotation, ClassDeclaration jclass, GenStrutsApp parentApp ) 226 { 227 GenForwardModel.addForwards( annotation, this, jclass, parentApp, null ); 228 } 229 } 230 | Popular Tags |