/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Header:$
*/
package org.apache.beehive.netui.compiler.model;
import java.util.ArrayList;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Iterator;
import org.apache.beehive.netui.compiler.model.schema.struts11.ActionDocument.Action;
import org.apache.beehive.netui.compiler.model.schema.struts11.SetPropertyDocument.SetProperty;
import org.apache.beehive.netui.compiler.model.schema.struts11.ExceptionDocument;
import org.apache.beehive.netui.compiler.model.schema.struts11.ForwardDocument;
import org.apache.beehive.netui.compiler.model.schema.struts11.ActionDocument;
import org.apache.beehive.netui.compiler.JpfLanguageConstants;
import org.apache.xmlbeans.XmlObject;
/**
* Represents an ActionMapping in a Struts based web application or
* sub application.
*/
public class ActionModel
extends AbstractForwardContainer
implements ForwardContainer, ExceptionContainer, JpfLanguageConstants
{
public static final String DEFAULT_FORM_SCOPE = "request";
private static final String JPF_ACTION_MAPPING_CLASSNAME = PAGEFLOW_PACKAGE + ".config.PageFlowActionMapping";
// Struts attributes.
private ArrayList _exceptionCatches = new ArrayList();
private String _attribute;
private String _className;
private String _forward;
private String _include;
private String _input;
private String _formBeanName;
private String _parameter;
private String _path; // required to be set
private String _prefix;
private String _scope = DEFAULT_FORM_SCOPE;
private String _suffix;
private String _type;
private boolean _unknown;
private String _roles;
private boolean _validate;
// Non-struts attributes.
private String _unqualifiedActionPath;
private boolean _loginRequired;
private boolean _isOverloaded;
private boolean _readonly;
private boolean _isSimpleAction = false;
private boolean _preventDoubleSubmit = false;
private String _formMember; // pageflow-scoped form
private String _formClass; // applicable for non-ActionForm-derived types
private Map _conditionalForwards;
private String _formBeanMessageResourcesKey;
private String _defaultForwardName; // for Simple Actions
public ActionModel( String path, StrutsApp parent )
{
this( path, null, parent );
}
public ActionModel( String path, String formName, StrutsApp parent )
{
super( parent );
this._path = path;
this._formBeanName = formName;
}
protected ActionModel( StrutsApp parent )
{
this( null, null, parent );
}
/**
* Construct a copy of the given mapping, with the given path.
*/
public ActionModel( ActionModel src, String newPath )
{
super( src );
this._path = newPath;
this._formBeanName = src._formBeanName;
_exceptionCatches = ( ArrayList ) src._exceptionCatches.clone();
_attribute = src._attribute;
_className = src._className;
_forward = src._forward;
_include = src._include;
_input = src._input;
_validate = src._validate;
_parameter = src._parameter;
_prefix = src._prefix;
_scope = src._scope;
_suffix = src._suffix;
_type = src._type;
_unknown = src._unknown;
_formMember = src._formMember;
_formClass = src._formClass;
_roles = src._roles;
_loginRequired = src._loginRequired;
_preventDoubleSubmit = src._preventDoubleSubmit;
_isSimpleAction = src._isSimpleAction;
_isOverloaded = src._isOverloaded;
_readonly = src._readonly;
_unqualifiedActionPath = src._unqualifiedActionPath;
_defaultForwardName = src._defaultForwardName;
}
public void setFormBeanName( String formBeanName )
{
_formBeanName = formBeanName;
}
public void writeToXMLBean( Action xb )
{
xb.setPath( _path );
if ( xb.getName() == null && _formBeanName != null ) xb.setName( _formBeanName );
if ( xb.getClassName() == null && _className != null ) xb.setClassName( _className );
if ( xb.getType() == null && _type != null ) xb.setType( _type );
if ( xb.getAttribute() == null && _attribute != null ) xb.setAttribute( _attribute );
if ( xb.getInput() == null && _input != null ) xb.setInput( _input );
if ( xb.getParameter() == null && _parameter != null ) xb.setParameter( _parameter );
if ( xb.getPrefix() == null && _prefix != null ) xb.setPrefix( _prefix );
if ( xb.getSuffix() == null && _suffix != null ) xb.setSuffix( _suffix );
if ( xb.getScope() == null )
{
if ( _scope != null )
{
if ( _scope.equals( "request" ) )
{
xb.setScope( Action.Scope.REQUEST );
}
else if ( _scope.equals( "session" ) )
{
xb.setScope( Action.Scope.SESSION );
}
else
{
assert false : _scope;
}
}
else
{
xb.setScope( Action.Scope.REQUEST );
}
}
if ( xb.getRoles() == null && _roles != null ) xb.setRoles( _roles );
if ( xb.getForward2() == null && _forward != null ) xb.setForward2( _forward );
if ( xb.getInclude() == null && _include != null ) xb.setInclude( _include );
if ( xb.getUnknown() == null && _unknown ) xb.setUnknown( Action.Unknown.TRUE );
if ( xb.getValidate() == null ) xb.setValidate( _validate ? Action.Validate.TRUE : Action.Validate.FALSE );
if ( _unqualifiedActionPath != null ) addSetProperty( xb, "unqualifiedActionPath", _unqualifiedActionPath );
if ( _formMember != null ) addSetProperty( xb, "formMember", _formMember );
if ( _formClass != null ) addSetProperty( xb, "formClass", _formClass );
if ( _loginRequired ) addSetProperty( xb, "loginRequired", _loginRequired );
if ( _preventDoubleSubmit ) addSetProperty( xb, "preventDoubleSubmit", _preventDoubleSubmit );
if ( _isOverloaded ) addSetProperty( xb, "overloaded", _isOverloaded );
if ( _readonly ) addSetProperty( xb, "readonly", _readonly );
if ( _isSimpleAction ) addSetProperty( xb, "simpleAction", _isSimpleAction );
if ( _defaultForwardName != null ) addSetProperty( xb, "defaultForward", _defaultForwardName );
if ( _conditionalForwards != null )
{
addSetProperty( xb, "conditionalForwards", getMapString( _conditionalForwards) );
}
if ( _formBeanMessageResourcesKey != null )
{
addSetProperty( xb, "formBeanMessageResourcesKey", _formBeanMessageResourcesKey );
}
if ( _exceptionCatches != null && ! _exceptionCatches.isEmpty() )
{
ExceptionDocument.Exception[] existingExceptions = xb.getExceptionArray();
for ( int i = 0; i < _exceptionCatches.size(); ++i )
{
ExceptionModel ec = ( ExceptionModel ) _exceptionCatches.get( i );
ExceptionDocument.Exception exceptionToEdit = null;
for ( int j = 0; j < existingExceptions.length; ++j )
{
if ( ec.getType().equals( existingExceptions[j].getType() ) )
{
exceptionToEdit = existingExceptions[j];
break;
}
}
if ( exceptionToEdit == null )
{
exceptionToEdit = xb.addNewException();
}
ec.writeToXMLBean( exceptionToEdit );
}
}
//
// forwards
//
writeForwards( xb.getForwardArray(), xb );
addComment( xb );
}
private void addSetProperty( ActionDocument.Action xb, String propertyName, boolean propertyValue )
{
addSetProperty( xb, propertyName, Boolean.toString( propertyValue ) );
}
private void addSetProperty( ActionDocument.Action xb, String propertyName, String propertyValue )
{
SetProperty prop = xb.addNewSetProperty();
prop.setProperty( propertyName );
prop.setValue( propertyValue );
if ( xb.getClassName() == null ) xb.setClassName( JPF_ACTION_MAPPING_CLASSNAME );
}
protected ForwardDocument.Forward addNewForward( XmlObject xmlObject )
{
return ( ( Action ) xmlObject ).addNewForward();
}
/**
* Implemented for {@link ExceptionContainer}.
*/
public void addException( ExceptionModel em )
{
_exceptionCatches.add( em );
}
public String getAttribute()
{
return _attribute;
}
public void setAttribute( String attribute )
{
this._attribute = attribute;
}
public String getClassName()
{
return _className;
}
public void setClassName( String className )
{
this._className = className;
}
public String getForward()
{
return _forward;
}
public void setForward( String forward )
{
this._forward = forward;
}
public String getInclude()
{
return _include;
}
public void setInclude( String include )
{
this._include = include;
}
public String getInput()
{
return _input;
}
public void setInput( String input )
{
this._input = input;
}
public String getName()
{
return _formBeanName;
}
public String getFormBeanName()
{
return _formBeanName;
}
public void setName( String name )
{
this._formBeanName = name;
}
public String getParameter()
{
return _parameter;
}
public void setParameter( String parameter )
{
this._parameter = parameter;
}
public boolean isValidate()
{
return _validate;
}
public void setValidate( boolean validate )
{
_validate = validate;
}
public String getPath()
{
return _path;
}
public String getPath( boolean useUnqualifiedPath )
{
if ( useUnqualifiedPath && _unqualifiedActionPath != null )
{
return _unqualifiedActionPath;
}
else
{
return _path;
}
}
public void setPath( String path )
{
this._path = path;
}
public String getPrefix()
{
return _prefix;
}
public void setPrefix( String prefix )
{
this._prefix = prefix;
}
public String getScope()
{
return _scope == null ? DEFAULT_FORM_SCOPE : _scope;
}
public void setScope( String scope )
{
this._scope = scope;
}
public String getSuffix()
{
return _suffix;
}
public void setSuffix( String suffix )
{
this._suffix = suffix;
}
public String getType()
{
return _type;
}
public void setType( String type )
{
this._type = type;
}
public boolean isUnknown()
{
return _unknown;
}
public void setUnknown( boolean unknown )
{
this._unknown = unknown;
}
public String getUnqualifiedActionPath()
{
return _unqualifiedActionPath;
}
public void setUnqualifiedActionPath( String unqualifiedActionPath )
{
this._unqualifiedActionPath = unqualifiedActionPath;
}
public String getDefaultForwardName()
{
return _defaultForwardName;
}
public void setDefaultForwardName( String defaultForwardName )
{
_defaultForwardName = defaultForwardName;
}
public String getRoles()
{
return _roles;
}
public void setRoles( String roles )
{
_roles = roles;
}
public void setLoginRequired( boolean loginRequired )
{
_loginRequired = loginRequired;
}
public void setPreventDoubleSubmit( boolean preventDoubleSubmit )
{
_preventDoubleSubmit = preventDoubleSubmit;
}
public boolean isSimpleAction()
{
return _isSimpleAction;
}
public void setSimpleAction( boolean simpleAction )
{
_isSimpleAction = simpleAction;
}
public boolean isOverloaded()
{
return _isOverloaded;
}
public void setOverloaded( boolean overloaded )
{
_isOverloaded = overloaded;
}
public String getFormMember()
{
return _formMember;
}
public void setFormMember( String formMember )
{
_formMember = formMember;
}
public String getFormClass()
{
return _formClass;
}
public void setFormClass( String formClass )
{
_formClass = formClass;
}
public boolean isReadonly()
{
return _readonly;
}
public void setReadonly( boolean readonly )
{
_readonly = readonly;
}
public void addConditionalForward( String expression, String forwardName )
{
if ( _conditionalForwards == null ) _conditionalForwards = new LinkedHashMap();
_conditionalForwards.put( expression, forwardName );
}
private static String getMapString( Map map )
{
StringBuffer retVal = new StringBuffer();
for ( Iterator i = map.entrySet().iterator(); i.hasNext(); )
{
Map.Entry entry = ( Map.Entry ) i.next();
retVal.append( entry.getValue() ).append( ':' ).append( entry.getKey() ).append( ';' );
}
return retVal.toString();
}
public void setFormBeanMessageResourcesKey( String formBeanMessageResourcesKey )
{
_formBeanMessageResourcesKey = formBeanMessageResourcesKey;
}
}