4 Replies Latest reply on Jun 5, 2009 1:06 AM by tarciosaraiva

    Home Objects & EJB3 Interceptors

    tarciosaraiva

      Hi all. I'm starting to use Seam but am a bit lost - or what I'm trying to do is simply not achievable.


      Well, the background scenario is: I've just created a new Seam application on Eclipse. Then, created a few entities and Seam Actions.
      That all works fine and I believe I got what an EntityHome is: a simple object to easy our life on persistence.


      Anyway, as I said, I can insert, update and remove. It works beautifully.


      However, I would like to add an interceptor: when I say interceptor is the EJB3-like interceptor, using the @Interceptors(Clazz.class) annotation, etc; so I can register some information before the actual persistence on the database.


      Well, I was reading through the documentation and got into the stage my code is at the moment.


      Here's my class to be intercepted:



      package com.metrofile.metroasset.session;
      
      import javax.interceptor.Interceptors;
      
      import org.jboss.seam.annotations.Begin;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.web.RequestParameter;
      import org.jboss.seam.ejb.SeamInterceptor;
      import org.jboss.seam.framework.EntityHome;
      
      import com.metrofile.metroasset.entity.Location;
      import com.metrofile.metroasset.interceptors.LocationChange;
      
      @Name("locationHome")
      @LocationChange
      @Interceptors(SeamInterceptor.class)
      public class LocationHome extends EntityHome<Location> {
      
           private static final long serialVersionUID = 260787915456177497L;
      
           @RequestParameter
           Long locationId;
      
           @Override
           public Object getId() {
                if (locationId == null) {
                     return super.getId();
                } else {
                     return locationId;
                }
           }
      
           @Override
           @Begin
           public void create() {
                super.create();
           }
      
      }





      Here's my Interceptor:



      package com.metrofile.metroasset.interceptors;
      
      import javax.interceptor.AroundInvoke;
      import javax.interceptor.InvocationContext;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.jboss.seam.annotations.intercept.Interceptor;
      
      import com.metrofile.metroasset.entity.LocationLog;
      
      @Interceptor
      public class LocationChangeInterceptor {
      
           @PersistenceContext(unitName = "metroAsset")
           private EntityManager em;
      
           @AroundInvoke
           public Object addModificationHistory(InvocationContext invocation)
                     throws Exception {
      
                LocationLog log = new LocationLog();
                log.setDescription(invocation.getMethod().toString());
      
                try {
                     em.persist(log);
                     em.flush();
                } catch (Exception e) {
                     e.printStackTrace();
                     System.out.println(e.getMessage());
                }
      
                return invocation.proceed();
           }
      
      }





      And finally the annotation (as explained at the documentation):



      package com.metrofile.metroasset.interceptors;
      
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      import java.lang.annotation.ElementType;
      
      import javax.interceptor.Interceptors;
      
      /**
       * @author <a href="mailto:support@metrofile.com.au">Tarcio Saraiva</a>
       * @version 04/06/2009
       */
      @Target( { ElementType.TYPE, ElementType.METHOD })
      @Retention(RetentionPolicy.RUNTIME)
      @Interceptors(LocationChangeInterceptor.class)
      public @interface LocationChange {
      
      }




      The main issue is that the EntityManager on the interceptor LocationChangeInterceptor is always null. Doesn't matter what I change - and tags I use - it's always null.


      Am I missing something? Is that the right way to use it?


      Thank you for your answers in advance - slowly I'm building up my Seam knowledge (great framework!)