7 Replies Latest reply on Nov 22, 2007 12:16 PM by Maykell Frometa

    @Factory is not working

    Maykell Frometa Newbie

      Hi all, i annoatated a method with a @Factory, it is supposed to be executed first of all, but it doesn't .
      I put a break point to debug at this point but nothing, it never enter to the annotated method.


      There is any kind of configuration i have to do in order to use @Factory properly, i mean, set some Seam xml file or something like that?

      Thank you,
      Maykell.

        • 1. Re: @Factory is not working
          whafrog Newbie

          Can you post your code?

          • 2. Re: @Factory is not working
            Tim Evers Master

            Make sure you have a seam.properties file in your classes directory.

            Are all other SEAM operations working?

            • 3. Re: @Factory is not working
              Damian Harvey Master

              @Factory doesn't guarantee that the method is called first. Maybe you are thinking of @Create?

              @Factory will be called when you call it from a page (or wherever) and Seam will place the value into the context variable so that it isn't called again for the life of the context.

              Cheers,

              Damian

              • 4. Re: @Factory is not working
                Maykell Frometa Newbie

                Hi all


                Make sure you have a seam.properties file in your classes directory.
                Are all other SEAM operations working?


                Yes, seam.properties is rigth and other Seam stuff work well, i'm sure of this.


                @Factory will be called when you call it from a page (or wherever) and Seam will place the value into the context variable so that it isn't called again for the life of the context.

                This is exactly my idea, that's whay i don't understand whay my code is not executing the @Factory annotated method.

                this is the jsf page that is calling a variable which is refrenced by the @Factory, the variable is "tipoAcusadoList":


                <f:subview id="kk" >
                <h:form id="turnarExpForm">
                <h:outputLabel value="Esta es la pagina de Registro" />
                <h:panelGrid columns="2" border="2" >
                <h:outputLabel value="Número de Expediente"/>
                <h:inputText value="#{expediente.nroExpediente}" id="lblNroExpediente"/>
                <h:outputLabel value="Tipo de acusado"/>
                <h:selectOneMenu value="#{expedienteFacadeStateless.tipoAcusadoSelected}">
                <f:selectItems value="#{expedienteFacadeStateless.tipoAcusadoList}"/>
                </h:selectOneMenu>
                <h:outputLabel value="Fecha de turnado" rendered=""/>
                <h:inputText value="#{expediente.fechaTurnado}" id="lblFechaTurnado" rendered=""/>
                </h:panelGrid>

                <a4j:commandButton action="#{expedienteFacadeStateless.turnarExpediente}" value="Ir a ...." />
                </h:form>
                </f:subview>



                This is my stateless session bean:

                package com.facade.impl;

                import java.util.List;

                import javax.ejb.Stateless;
                import javax.faces.model.SelectItem;
                import javax.persistence.EntityManager;
                import javax.persistence.PersistenceContext;
                import javax.persistence.Query;

                import org.jboss.seam.annotations.Factory;
                import org.jboss.seam.annotations.In;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.Out;
                import org.jboss.seam.core.PersistenceContexts;

                import com.domain.Expediente;
                import com.domain.nomenclador.NTipoAcusado;
                import com.facade.ExpedienteFacadeStateless;

                @Stateless
                @Name("expedienteFacadeStateless")
                public class ExpedienteFacadeStatelessImpl implements ExpedienteFacadeStateless {

                @In @Out
                private Expediente expediente;

                @PersistenceContext
                private EntityManager em;


                private SelectItem[] tipoAcusadoList;

                private NTipoAcusado tipoAcusadoSelected;


                @Factory("tipoAcusadoList")
                public void loadTipoAcusadoList()
                {
                Query query = em.createQuery("Select f from NTipoAcusado f");
                List<NTipoAcusado> tempList = query.getResultList();
                int size = tempList.size();
                tipoAcusadoList = new SelectItem[size];
                for (int i=0;i<size;i++)
                tipoAcusadoList = new SelectItem(tempList.get(i),tempList.get(i).getValor());
                }

                public EntityManager getEm() {
                return em;
                }

                public void setEm(EntityManager em) {
                this.em = em;
                }

                public Expediente getExpediente() {
                return expediente;
                }

                public void setExpediente(Expediente expediente) {
                this.expediente = expediente;
                }

                public String turnarExpediente(Expediente Exp) {
                // TODO Auto-generated method stub
                return null;
                }

                public SelectItem[] getTipoAcusadoList() {
                return tipoAcusadoList;
                }

                public void setTipoAcusadoList(SelectItem[] tipoAcusadoList) {
                this.tipoAcusadoList = tipoAcusadoList;
                }

                public NTipoAcusado getTipoAcusadoSelected() {
                return tipoAcusadoSelected;
                }

                public void setTipoAcusadoSelected(NTipoAcusado tipoAcusadoSelected) {
                this.tipoAcusadoSelected = tipoAcusadoSelected;
                }

                }



                and this is the interface:

                package com.facade;

                import javax.faces.model.SelectItem;

                import com.domain.Expediente;
                import com.domain.nomenclador.NTipoAcusado;

                public interface ExpedienteFacadeStateless {

                public String turnarExpediente(Expediente Exp);
                public SelectItem[] getTipoAcusadoList();
                public NTipoAcusado getTipoAcusadoSelected();
                public void loadTipoAcusadoList();
                }


                • 5. Re: @Factory is not working
                  Edwin van der Elst Newbie

                  From the docs:

                  A factory method will be called when a context variable is referenced but has no value bound to it.


                  You are however not referencing the context variable, but a getter method on another context variable. (if you had an @Out at the list, the factory would have been called).

                  Also, for selectitems, you can use the s:convertEntity tag and return the results from the query instead. Much simpler!



                  • 6. Re: @Factory is not working
                    Damian Harvey Master

                    Yep. Or change your page to :

                    <f:selectItems value="#{tipoAcusadoList}"/>

                    Cheers,

                    Damian.

                    • 7. Re: @Factory is not working
                      Maykell Frometa Newbie

                       


                      You are however not referencing the context variable, but a getter method on another context variable.

                      Please clarify me, what is exactly a "context variable" ?

                      1- You mean a variable annotated with an Out?
                      2- A Session bean annotated with a @Name it is not a context variable?
                      3- In my example i have "expedienteFacadeStateless" that is a stateless session bean annotated with a @Name seam annotation, now i thougth that the property tipoAcusadoRs implicitly become a context variable. i'm wrong?


                      (if you had an @Out at the list, the factory would have been called).

                      I did that an i get the following exepction:

                      java.lang.IllegalArgumentException: Expected a child component type of UISelectItem/UISelectItems for component type javax.faces.SelectOne(j_id_jsp_912580932_7pc2). Found null.

                      Any suggestion will be very appreciated.Thank you ,
                      Maykell