8 Replies Latest reply on Oct 16, 2007 4:09 AM by w17chm4n

    DataModel and DataModelSelection question

    w17chm4n

      First of all, the code..

      @Stateless
      @Name("QuestionCategoryController")
      public class QuestionCategoryControllerBean implements QuestionCategoryController {
      
       @Logger
       private Log log;
      
       @In(create = true)
       private QuestionCategoryManager questionCategoryManager;
      
       @In(required = false)
       private QuestionCategory questionCategory;
      
       @DataModel(value = "categories")
       private List<QuestionCategory> categoryList;
      
       @DataModelSelection(value = "categories")
       private List<Question> questionList;
      
       public void addCategory() {
       questionCategory.setCreated(new Date());
       questionCategoryManager.addCategory(this.questionCategory);
       }
      
       public void removeCategory(QuestionCategory questionCategory) { //todo
       log.info("Trying to remove #{questionCategory.categoryName}");
       if(questionCategory == null) {
       log.error("questionCategory is NULL !");
       } else {
       questionCategoryManager.removeCategory(questionCategory);
       }
       }
      
       @Factory("categories")
       public void getAllQuestionsCategories() {
       setCategoryList(questionCategoryManager.getAllQuestionCategories());
       }
      
       public List<QuestionCategory> getCategoryList() {
       return categoryList;
       }
      
       public void setCategoryList(List<QuestionCategory> categoryList) {
       this.categoryList = categoryList;
       }
      
       public List<Question> getQuestionList() {
       return questionList;
       }
      
       public void setQuestionList(List<Question> questionList) {
       this.questionList = questionList;
       }
      
      }
      


      and

      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:s="http://jboss.com/products/seam/taglib">
       <body>
      
       <h:form>
       <h:inputText value="#{questionCategory.categoryName}" required="true"/>
       <h:commandButton type="submit" value="Add new category" action="#{QuestionCategoryController.addCategory}"></h:commandButton><br/>
       <h:outputText value="Categories rowCount is null" rendered="#{categories.rowCount == null}"/><br/>
       <h:outputText value="Categories rowCount = 0" rendered="#{categories.rowCount == 0}"/><br/>
       <h:outputText value="Available categories: " rendered="#{categories.rowCount > 0}"/><br/>
       <h:dataTable var="cat" value="#{categories}">
       <h:column>
       <h:outputText value="[ #{cat.categoryName} ]"/>
       </h:column>
       <h:column>
       <h:outputText value=" - [ #{cat.created} ]"><f:convertDateTime pattern="dd-MMM-yyyy H:mm"/></h:outputText>
       </h:column>
       <h:column>
       <s:link value="remove" action="#{QuestionCategoryController.removeCategory(cat)}"/>
       </h:column>
       </h:dataTable>
       </h:form>
      
       <ui:debug hotkey="d"/>
      
       </body>
      </html>
      


      Whats the problem is that QuestionCategoryController.removeCategory(cat) doesn`t work. I know it`s something about the annotations but I can`t find any answer.

      As this controller should be used for adding new QuestionCategory, returning the existing categories and remove them i`ve also tried sth like that:

      @Stateless
      @Name("QuestionCategoryController")
      public class QuestionCategoryControllerBean implements QuestionCategoryController {
      
       @Logger
       private Log log;
      
       @In(create = true)
       private QuestionCategoryManager questionCategoryManager;
      
       @DataModelSelection(value = "categories") @In(required = false) @Out(required = false)
       private QuestionCategory questionCategory;
      
       @DataModel(value = "categories")
       private List<QuestionCategory> categoryList;
      
       public void addCategory() {
       questionCategory.setCreated(new Date());
       questionCategoryManager.addCategory(this.questionCategory);
       }
      
       public void removeCategory(QuestionCategory questionCategory) { //todo
       log.info("Trying to remove #{questionCategory.categoryName}");
       if(questionCategory == null) {
       log.error("questionCategory is NULL !");
       } else {
       questionCategoryManager.removeCategory(questionCategory);
       }
       }
      
       @Factory("categories")
       public void getAllQuestionsCategories() {
       setCategoryList(questionCategoryManager.getAllQuestionCategories());
       }
      
       public List<QuestionCategory> getCategoryList() {
       return categoryList;
       }
      
       public void setCategoryList(List<QuestionCategory> categoryList) {
       this.categoryList = categoryList;
       }
      
      }
      


      Oh and the exception, I simply get an NullPointerException (or in logs "questionCategory is NULL !") so as I think, the QuestionCategory object isn`t injected into the removeQuestion method.

      Any suggestions how to deal with this problem ?

        • 1. Re: DataModel and DataModelSelection question

          You are using Stateless Beans, How do you expect state to be saved? Try using Stateful EJBs

          • 2. Re: DataModel and DataModelSelection question
            w17chm4n

            I`ve changed to stateful EJB but still it doesn`t inject the questionCategory.

            @Stateful
            @Name("QuestionCategoryController")
            public class QuestionCategoryControllerBean implements QuestionCategoryController {
            
             @Logger
             private Log log;
            
             @In(create = true)
             private QuestionCategoryManager questionCategoryManager;
            
             @DataModelSelection
             @In(required = false)
             @Out(required = false)
             private QuestionCategory questionCategory;
            
             @DataModel
             private List<QuestionCategory> categoryList;
            
             @Out(required = false)
             private List<Question> questionList;
            
             public void addCategory() {
             questionCategory.setCreated(new Date());
             questionCategoryManager.addCategory(this.questionCategory);
             }
            
             public void removeCategory(QuestionCategory questionCategory) { //todo
             log.info("Trying to remove #{questionCategory.categoryName}");
             if(questionCategory == null) {
             log.fatal("questionCategory is NULL !");
             } else {
             questionCategoryManager.removeCategory(questionCategory);
             }
             }
            
             @Factory("categoryList")
             public void getAllQuestionsCategories() {
             setCategoryList(questionCategoryManager.getAllQuestionCategories());
             }
            
             public List<QuestionCategory> getCategoryList() {
             return categoryList;
             }
            
             public void setCategoryList(List<QuestionCategory> categoryList) {
             this.categoryList = categoryList;
             }
            
             public List<Question> getQuestionList() {
             return questionList;
             }
            
             public void setQuestionList(List<Question> questionList) {
             this.questionList = questionList;
             }
            
             @Destroy @Remove
             public void destroy() {}
            }
            


            • 3. Re: DataModel and DataModelSelection question
              dhinojosa

              What was the result after you changed to stateful? Was it still:
              questionCategory is NULL !

              • 4. Re: DataModel and DataModelSelection question

                Try defining the DataModel in your DataModelSelection

                @DataModelSelection("categoryList")

                With only one DataModel in your bean, it should not be necessary but I always put it in to be safe.

                • 5. Re: DataModel and DataModelSelection question
                  nickarls

                  Try putting the bean and the datamodel to page scope and if it works, start narrowing down...

                  • 6. Re: DataModel and DataModelSelection question
                    w17chm4n

                    First of all, thanks for all Yours replays !

                    "dhinojosa" wrote:
                    What was the result after you changed to stateful? Was it still:
                    questionCategory is NULL !

                    Yes, still it striked me with "questionCategory is NULL !

                    "smithbstl" wrote:
                    Try defining the DataModel in your DataModelSelection

                    @DataModelSelection("categoryList")

                    With only one DataModel in your bean, it should not be necessary but I always put it in to be safe.

                    I did that

                    Try putting the bean and the datamodel to page scope and if it works, start narrowing down...

                    and that, but still no success.

                    Any more ideas ???

                    • 7. Re: DataModel and DataModelSelection question
                      dhinojosa

                      You don't have a scope on your "controller". If you do, sounds like you have some new changes to your code. Can you put em up here?

                      • 8. Re: DataModel and DataModelSelection question
                        w17chm4n

                        I found the sollution.

                        @Stateful
                        @Scope(ScopeType.SESSION)
                        @Name("QuestionCategoryController")
                        public class QuestionCategoryControllerBean implements QuestionCategoryController {
                        
                         @Logger
                         private Log log;
                        
                         @In(create = true)
                         private QuestionCategoryManager questionCategoryManager;
                        
                         @DataModel
                         private List<QuestionCategory> questionCategoryList;
                        
                         @DataModelSelection
                         @In(required = false)
                         private QuestionCategory questionCategory;
                        
                         public void addCategory() {
                         questionCategoryManager.addCategory(questionCategory);
                         getAllQuestionsCategories();
                         }
                        
                         public void removeCategory() {
                         questionCategoryList.remove(questionCategory);
                         questionCategoryManager.removeCategory(questionCategory);
                         questionCategory = null;
                         getAllQuestionsCategories();
                         }
                        
                         @Factory(value="questionCategoryList")
                         public void getAllQuestionsCategories() {
                         questionCategoryList = questionCategoryManager.getAllQuestionCategories();
                         }
                        
                         @Remove @Destroy
                         public void destroy() {}
                        
                        }
                        


                        @Stateful
                        @Scope(ScopeType.APPLICATION)
                        @Name("questionCategoryManager")
                        public class QuestionCategoryManagerBean implements QuestionCategoryManager {
                        
                         @Logger
                         Log log;
                        
                         @PersistenceContext(type = PersistenceContextType.EXTENDED)
                         private EntityManager entityManager;
                        
                         public void addCategory(QuestionCategory category) {
                         category.setCreated(new Date());
                         entityManager.persist(category);
                         }
                        
                         public void removeCategory(QuestionCategory category) {
                         entityManager.remove(category);
                         }
                        
                         public List<QuestionCategory> getAllQuestionCategories() {
                         return entityManager.createQuery("from QuestionCategory qc").getResultList();
                         }
                        
                         @Destroy @Remove
                         public void destroy() {
                         }
                        }
                        


                        @Entity
                        @Scope(ScopeType.EVENT)
                        @Name("questionCategory")
                        @Table(name="QuestionCategories")
                        public class QuestionCategory implements Serializable {
                        
                         @Id @GeneratedValue
                         private Long id;
                        
                         @NotNull @Length(min=3, max=100)
                         private String categoryName;
                        
                         @OneToMany
                         private List<Question> questionList;
                        
                         @Temporal(value = TemporalType.TIMESTAMP)
                         private Date created;
                        
                         /** Creates a new instance of QuestionCategory */
                         public QuestionCategory() {
                         this.questionList = new ArrayList<Question>();
                         }
                        
                         public QuestionCategory(String categoryName) {
                         this.categoryName = categoryName;
                         this.questionList = new ArrayList<Question>();
                         }
                        
                         public Long getId() {
                         return id;
                         }
                        
                         public void setId(Long id) {
                         this.id = id;
                         }
                        
                         public String getCategoryName() {
                         return categoryName;
                         }
                        
                         public void setCategoryName(String categoryName) {
                         this.categoryName = categoryName;
                         }
                        
                         public List<Question> getQuestionList() {
                         return questionList;
                         }
                        
                         public void setQuestionList(List<Question> questionList) {
                         this.questionList = questionList;
                         }
                        
                         public Date getCreated() {
                         return created;
                         }
                        
                         public void setCreated(Date created) {
                         this.created = created;
                         }
                        }
                        


                        It was all about setting correct scopes ! Notice that entity bean has to have set Scope to event, without it, it won`t work.

                        Thx dhinojosa for all your support !