0 Replies Latest reply on Oct 8, 2011 4:08 PM by Derk Dukker

    Weird injection of GET parameter problem

    Derk Dukker Novice

      Hi all,


      I have a strange problem. I want to inject the categoryId GET parameter into 2 beans but it seems it only does it at one bean. I map it to the CategoryManager bean which has is annotated with @Produces, @Named and with a Qualifier. Then I want to inject it into the TopicManager. I can see in the logs it is showing up correctly in the categoryManager (11), but in the TopicManager it resolves to 1.
      They are both in the same conversation with id 1. I also did this process over again with a conversation id 2 but still the categoryId is 1 in the TopicManager. Can somebody point me the problem?


      Here a snippet from the index page:



      <h:outputLink value="category.jsf">#{_category.title}
                                  <f:param name="categoryId" value="#{_category.id}"/>
                                  <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
                              </h:outputLink>





      here a snippet from the category page:



      <ui:define name="metadata">
              <f:metadata>
                  <!-- there need to be at least one view param when using action, bug in JSF -->
                  <f:viewParam name="categoryId" value="#{categoryManager.categoryId}" /> 
                  
              </f:metadata>
          </ui:define>



      Here the CategoryManager:




      @Stateful
      @ConversationScoped
      @Named
      public class CategoryManager {
      
          /**
           * 
           */
          private static final long serialVersionUID = 9179727796959552384L;
          
          private static final Logger log = Logger.getLogger(CategoryManager.class);
          
          @PersistenceContext(type= PersistenceContextType.EXTENDED)
          private EntityManager entityManager;
          
          private Long categoryId;
          
          @Inject
          private Conversation conversation;
          
          @Produces
          @Named
          public List<Category> getRootCategories() {         
              log.debug("entered getRootCategories");
              if(conversation.isTransient()) {
                  
                  log.debug("transient - conversation id: " + conversation.getId());
                  conversation.begin();
              }
              log.debug("conversation id: " + conversation.getId());
              
              if(getCategoryId() == null) {
                  log.debug("categoryId == null");
                  CriteriaBuilder builder = entityManager.getCriteriaBuilder();
                  CriteriaQuery<Category> cquery = builder.createQuery(Category.class);
                  Root<Category> category = cquery.from(Category.class);
                  cquery.select(category).where(builder.isNull(category.get("previousCategory")));
      
                  List<Category> rootCategories = entityManager.createQuery(cquery).getResultList();
                  
                  return rootCategories;
              }else{
                  log.debug("categoryId != null so return category with selected Id and underlying categoryList");
                  CriteriaBuilder builder = entityManager.getCriteriaBuilder();
                  CriteriaQuery<Category> cquery = builder.createQuery(Category.class);
                  Root<Category> category = cquery.from(Category.class);
                  cquery.select(category).where(builder.equal(category.get(Category_.id), categoryId));
                  Category selectedCategory = entityManager.createQuery(cquery).getSingleResult();
                  List<Category> rootCategories = new ArrayList<Category>(selectedCategory.getCategories());
                  log.debug("size of root categories: " + rootCategories.size());
                  return rootCategories;
              }        
              
          }
          
          @Produces
          @Named("category")
          public Category getCategory() {
              log.debug("entered getCategory");
              if(categoryId != null) {
                  log.info("categoryId: " + categoryId);
                  Category cat = entityManager.find(Category.class, categoryId);
                  log.info("size of categories of the selected category: " + cat.getCategories().size());
                  
                  return cat;
                 
              }else{
                  log.info("categoryId == null");
                  log.info("create new category");
                  return new Category();              
              }
          }
      
          @Produces
          @Named
          @nl.ami.marketplace.market.CategoryQualifier
          public Long getCategoryId() {
              log.debug("getCategoryId: " + categoryId);
              return categoryId;
          }
      
          public void setCategoryId(Long categoryId) {
              log.debug("setCategoryId: " + categoryId);
              this.categoryId = categoryId;
          }
      
          
      }



      Here the TopicManager:




      @Stateful
      @ConversationScoped
      @Named
      public class TopicManager {
      
          /**
           * 
           */
          private static final long serialVersionUID = -3436883531896260369L;
          private static final Logger log = Logger.getLogger(TopicManager.class);
          
          @PersistenceContext(type= PersistenceContextType.EXTENDED)
          private EntityManager entityManager;
      
          private Long topicId;
          
          @Inject
          @CategoryQualifier
          private Long categoryId;
          
          @Inject
          private Conversation conversation;
          
          private List<Bid> bids = new ArrayList<Bid>();
          private SortOrder topicOrder = SortOrder.descending;
          
          @Produces
          @Named
          public Topic getTopic() {
              if(topicId != null) {
                  log.info("topicId: " + topicId);
      
                  Topic topic = entityManager.find(Topic.class, topicId);
                  bids = new ArrayList<Bid>(topic.getBids());
                  return topic;
                 
              }else{
                  log.info("topicId == null");
                  log.info("create new topic");
                  bids = new ArrayList<Bid>();
                  return new Topic();              
              }
          }
          
          @Produces
          @Named
          public List<Topic> getTopics(){
              log.debug("categoryId in getTopics: " + categoryId);
              if(conversation.isTransient()) {
                  conversation.begin();
                  log.debug("not transient - conversation id: " + conversation.getId());
              }
              log.debug("conversation id: " + conversation.getId());
              List<Topic> topics = null;
              if(categoryId != null) {
                  CriteriaBuilder builder = entityManager.getCriteriaBuilder();
                  CriteriaQuery<Topic> tquery = builder.createQuery(Topic.class);
                  Root<Topic> topic = tquery.from(Topic.class);
                  tquery.select(topic).where(builder.equal(topic.get(Topic_.category).get(Category_.id), categoryId));
      
                  topics = entityManager.createQuery(tquery).getResultList();
                  log.debug("size of topics: " + topics.size());
              }
              return topics;
          }
      
          @Produces
          @Named
          @TopicQualifier
          public Long getTopicId() {
              return topicId;
          }
      
          public void setTopicId(Long topicId) {
              this.topicId = topicId;
          }
      
          public List<Bid> getBids() {
              return bids;
          }
      
          public void setBids(List<Bid> bids) {
              this.bids = bids;
          }
          
          public SortOrder getTopicOrder() {
              return topicOrder;
          }
      
          public void setTopicOrder(SortOrder topicOrder) {
              this.topicOrder = topicOrder;
          }
          
      }




      Here the CategoryQualifier:




      @Qualifier
      @Target({TYPE, METHOD, PARAMETER, FIELD})
      @Retention(RUNTIME)
      public @interface CategoryQualifier{
      }