struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stephan Schröder" <simon.v...@gmx.de>
Subject Re: RE: [S2] Does struts set null values for pojo fields?
Date Thu, 21 Aug 2008 13:30:14 GMT

> Can you define an interface that is common across all the actions/model
> objects. Then you can have the relevant Actions implement the interface
> and
> write an interceptor to check the value of the specified fields and set
> them
> to null if required after checking that the object is of the correct type
> (i.e. it implements your interface)

Nice Idea Andy! You don't even need common methods in the interface, it can just bee a signal

interface DomainModelObject{}

than you can have an interceptor

public class CleanerInterceptor extends AbstractInterceptor {

  public String intercept(ActionInvocation invocation) throws Exception {
    Object action = invocation.getAction();
    if( action instanceof ModelDriven ) {
      Object model = ((ModelDriven)action).getModel();
      //if the model is instance of DomainModelObject clean all of its properties
      if( model instanceof DomainModelObject ) {
        DomainModelObject modelObject = (DomainModelObject)model;
        Collection<DomainObjectModel> properties = getProperties( modelObject );
        for( DomainObjectModel property:properties ) {
          cleanProperty( modelObject ,property );
        }
      }
    }
    invocation.invoke();
  }

  private void cleanProperty( DomainObjectModel parent, DomainObjectModel model )
  {
    if( model==null ) return;

    Collection<DomainObjectModel> properties = getProperties( model );
    
    if( allPropertiesNull( properties ) ) {
      setPropertyNullOnParent( parent,model );
    }else{
      for( DomainObjectModel property:properties ) {
        cleanProperty( model,property );
      }
    }

  private Collection<DomainObjectModel> getProperties( DomainObjectModel model )
  {...
  }

  private void setPropertyNullOnParent( DomainObjectModel parent, DomainObjectModel model
)
  {...
  }

  private boolean allPropertiesNull( Collection<DomainObjectModel> properties )
  {
    for( DomainObjectModel property:properties ) {
      if( property!=null ) return false; 
    }
    return true;
  }
}

getProperties(...) and setPropertyNullOnParent(...) can both be programmed using Java Reflection
and if you don't like DomainObjectModel being an Interface you can make it an annotation.
Then cleaner-Interceptor shoulb probably called last on you interceptorstack.

/Stephan
-- 
GMX Kostenlose Spiele: Einfach online spielen und Spaß haben mit Pastry Passion!
http://games.entertainment.gmx.net/de/entertainment/games/free/puzzle/6169196

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message