2 Replies Latest reply on Aug 16, 2007 11:14 AM by laksu

    inject into a non-component?

    laksu

      I have a wrapper class called Node implementing TreeNode from RichFaces. I try to avoid recursively populating the whole tree and so have a mechanism in there to populate each node as they are referred. And in conjunction with the "ajax" property of the tree component, I expect the individual population of the nodes happening in the ajax calls.
      The class "Node" basically keeps a Map (required to be a Map by the tree) of my Category objects that are children of the wrapped Category object when populated.
      I provide these details because I could not figure out what kind of a component the Node should be.
      My proof of concept does not even start by populating the root node. I try to inject the session (I use Hibernate/session instead of JPA/em) but it is injected null or not injected.
      So the question is if my "Node" should be a Seam component to have the Hibernate session injected and if so what kind of a scope I should put it in?

      Here follows my Node class:

      public class Node<E extends Treeable<E>> implements TreeNode {
      
       @In(create=true)
       private Session session;
      
       private E value;
       Node parent;
      
       public Node(E aValue, Node<E> parent) {
       value=aValue;
       this.parent=parent;
       System.out.println("initing Node with Treeable="+aValue.getName());
       }
      
       private Map<Long,Node<E>> dataMap;
      
       public Node(String className) {
       List<E> rooties;
       System.out.println("Expanding root. session is"+session);
       rooties=session.createQuery("from "+className+" where parent"+className+" is null")
       .list();
      
       System.out.println("Root has "+rooties.size()+" rooties");
       dataMap=new HashMap<Long,Node<E>>();
       for(E rootie:rooties){
       Node node=new Node(rootie,this);
       dataMap.put(rootie.getId(),node);
       System.out.println("Rootie :"+rootie.getName()+" was added to roots");
       }
      
       System.out.println("initing rooties done");
       }
      
      
       public Map<Long, Node<E>> getDataMap() {
       if (dataMap==null){
       List<E> children;
       System.out.println("Expanding Node:"+value);
       String className=value.getClass().getName();
       children=session.createQuery("from "+className+" where parent"+className+"= :parent")
       .setEntity("parent",value)
       .list();
      
       dataMap=new HashMap<Long,Node<E>>();
       for(E child:value.getChildren()){
       Node node=new Node(child,this);
       dataMap.put(child.getId(),node);
       }
       }
       return dataMap;
       }
      
       public Object getData() {
       return this;
       }
      
       public void setData(Object data) {
       }
      
       public boolean isLeaf() {
       return getDataMap().isEmpty();
       }
      
       public Iterator getChildren() {
       System.out.println("getChildren is called for:"+toString());
       return getDataMap().entrySet().iterator();
       }
      
       public TreeNode getChild(Object id) {
       return getDataMap().get(id);
       }
      
       public void addChild(Object identifier, TreeNode child) {
       }
      
       public void removeChild(Object id) {
       }
      
       public TreeNode getParent() {
       System.out.println("getParent is called for:"+toString());
       return parent;
       }
      
      
       public void setParent(TreeNode treeNode) {
       System.out.println("setParent is called with:"+treeNode);
       }
      
       public String getType(){
       return (value==null)?"root":value.getType();
       }
      
       public String toString(){
      ...
       }
      
       public String getName(){
       return value.getName();
       }
      
       public E getValue() {
       return value;
       }
      
      }
      




        • 1. Re: inject into a non-component?
          pmuir

          Use Component.getInstance("session");

          • 2. Re: inject into a non-component?
            laksu

            Great, thank you. I knew it would have a lean solution.

            So, would you say this is not a workaround but what it should have been?
            In other words, one cannot inject a Seam component into an ordinary class which is not a Seam component.
            And that would clarify for me the phenomena that I cannot use @Logger with some of my classes.