You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Jean-Baptiste Claramonte <je...@gmail.com> on 2004/11/30 10:42:41 UTC

RE: Deep retrieve : a proposal

I do not know if I missed something and if there is a more effective
method to retrieve an object graph (without setting auto-retrieve to
true in the repository) but in any cases, for those who'd like to know
more about it, here is the code of the Java classes for DeepRetrieve.

Please don't hesitate to give feedback about the implementation,
especially if you know a better way to do the job !

Parser.java
Node.java
DeepRetrieve.java

How to use it :
[A]<(*)listeA-----(1)b>[B]<(1)b-----(*)listeC>[C]
We retrieve an instance on A then we execute a deepretrieve of the
object graph :
b(listeA) means that we want to retrieve b and the back reference
claaed listeA from [B] to [A].
We also ask for retrieving listeC and his back reference to [B] called b

<code>
 Criteria crit = new Criteria();
 crit.addEqualTo("idA", new Long(0));

 QueryByCriteria query = QueryFactory.newQuery(A.class, crit);

 A result = (A)broker.getObjectByQuery(query);

 DeepRetrieve deepRetrieve = new DeepRetrieve(broker);
 deepRetrieve.addRetrieveRef("b(listeA).listeC(b)");
 deepRetrieve.retrieve(result);
</code>

************************************************
/*
* Created on 22 nov. 2004
*
*/
package org.test.query.deepretrieve;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
* @author JBClaramonte
*
*/
public class Parser {
       /**
        * contains a collection of String
        */
       private Collection lines;

       /**
        * contains the root Node (initialized with the makeTree method)
        */
       private Node root;

       private Parser() {

       }

       public Parser(Collection lines) {
               this.lines = lines;
       }

       /**
        *
        *
        */
       public Node makeTree4() {
               if (lines==null || lines.size()==0)
                       return null;

               root = new Node("root");
               root.setLevel(0);

               Iterator iter = lines.iterator();
               Node currentNode = root;
               while (iter.hasNext()) {
                       String line = (String)iter.next();

                       Iterator listeRef =
splitNavigationPath2(line).iterator();
                       while (listeRef.hasNext()) {
                               ReferenceToken ref =
(ReferenceToken)listeRef.next();

                               Node nextNode = currentNode.lookup(ref.refName);

                               if (nextNode == null) {
                                       nextNode = new Node(ref.refName);

                                      
nextNode.setLevel(currentNode.getLevel()+1);

                                       currentNode.getChildren().add(nextNode);
                               }

                               if (ref.backRefName!=null)
                                       nextNode.getChildren().add(new
Node(ref.backRefName));

                               currentNode = nextNode;
                       }
                       currentNode = root;
               }
               return root;
       }

       /**
        * If we have the following model :
        * [A]<(*)listeA-----(1)b>[B]<(1)b-----(*)listeC>[C]-----(1)d>[D]
        *                   /\
        *               (*)listeB
        *                   ||
        *               (*)listeE
        *                   \/
        *                  [E]
        * Split a String like : "b(listeA).listeC(b).d"
        * where "b" is a reference from an instance of [A],
        * and "(listeA)" is a back collection reference from [B] to [A],
        * listeC is a collection reference from [B] to [C], ...
        * So splitNavigationPath2 will split the initial string
        * into the following Collection :
        * [@ReferenceToken(ref="b", backRef="listeA"),
        *  @ReferenceToken(ref="listeC", backRef="b"),
        *  @ReferenceToken(ref="d", backRef=null)]
        * @param naviPath
        * @return
        */
       protected Collection splitNavigationPath2(String naviPath) {
               ArrayList result = new ArrayList();
               boolean finished = false;
               int indexFrom = 0;
               while(finished==false) {
                       int indexTo = naviPath.indexOf(".", indexFrom);
                       if (indexTo<0) {
                               String element =
naviPath.substring(indexFrom, naviPath.length());
                               result.add(new ReferenceToken(element));
                               return result;
                       }

                       // Un element c'est une chaine du style : b(listeA)
                       String element = naviPath.substring(indexFrom, indexTo);

                       // Découper element pour le mettre dans ReferenceToken
                       result.add(new ReferenceToken(element));

                       indexFrom = indexTo+1;
               }

               return result;
       }

       class ReferenceToken {
               public ReferenceToken(String element) {
                       int indexA = element.indexOf("(", 0);
                       if (indexA<0) {
                               this.refName = element;
                               this.backRefName = null;
                       } else {
                               this.refName = element.substring(0, indexA);
                               this.backRefName =
element.substring(indexA+1, element.length()-1);
                       }
               }
               public String refName;
               public String backRefName;
       }

       /**
        * @return Returns the lines.
        */
       public Collection getLines() {
               return lines;
       }
       /**
        * @param lines The lines to set.
        */
       public void setLines(Collection lines) {
               this.lines = lines;
       }
}

************************************************

/*
* Created on 22 nov. 2004
*
*/
package org.test.query.deepretrieve;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
* @author JBClaramonte
*
*/
public class Node {

       private String _name;

       private boolean _loadParent = false;

       private Collection _children;

       private int _level=0;

       public Node() {
       }

       public Node(String name) {
               this._name = name;
               this._children = new ArrayList();
       }

       public Node lookup(String name) {
               Iterator iter = this.getChildren().iterator();

               while (iter.hasNext()) {
                       Node n = (Node)iter.next();
                       if (n.getName().equals(name)) {
                               return n;
                       }
               }
               return null;
       }

       /**
        * @return Returns the children.
        */
       public Collection getChildren() {
               return _children;
       }
       /**
        * @param children The children to set.
        */
       public void setChildren(Collection children) {
               this._children = children;
       }
       /**
        * @return Returns the name.
        */
       public String getName() {
               return _name;
       }
       /**
        * @param name The name to set.
        */
       public void setName(String name) {
               this._name = name;
       }
       /**
        * @return Returns the level.
        */
       public int getLevel() {
               return _level;
       }
       /**
        * @param level The level to set.
        */
       public void setLevel(int level) {
               this._level = level;
       }

       /**
        * @return Returns the loadParent.
        */
       public boolean isLoadParent() {
               return _loadParent;
       }
       /**
        * @param loadParent The loadParent to set.
        */
       public void setLoadParent(boolean loadParent) {
               _loadParent = loadParent;
       }

}

************************************************

/*
* Created on 24 nov. 2004
*
*/
package org.test.query.deepretrieve;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.ojb.broker.PersistenceBroker;

/**
* @author JBClaramonte
*
*/
public class DeepRetrieve {
       private Parser _parser;
       private Node _root;
       private PersistenceBroker _broker;
       private Collection _retrieveRef = null;

       public DeepRetrieve() {

       }

       public DeepRetrieve(Collection retrieveRef) {
               this._retrieveRef = retrieveRef;
       }

       public void execute(Object instanceRoot, PersistenceBroker pBroker) {
               if (_retrieveRef!=null) {
                       if (_parser==null) {
                               _parser = new Parser(_retrieveRef);
                               _root = _parser.makeTree4();
                       }
               }
               try {
                       _broker = pBroker;
                       retrieveReferenceForNode(_root, instanceRoot);

               } catch (IllegalAccessException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               } catch (InvocationTargetException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               } catch (NoSuchMethodException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               } finally {
                       _broker = null;
               }
       }

       /**
        *
        * @param node
        * @param currentInstance
        * @throws IllegalAccessException
        * @throws InvocationTargetException
        * @throws NoSuchMethodException
        */
       protected void retrieveReferenceForNode(Node node, Object
currentInstance) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {

               if (currentInstance==null)
                       return;

               Object retrievedRefInstance = null;
               Iterator iter = node.getChildren().iterator();
               while (iter.hasNext()) {
                       Node n = (Node)iter.next();
                       if ((currentInstance instanceof java.util.Collection)) {
                               Collection listeCurrentInstance =
(Collection)currentInstance;
                               Iterator iterListeInstance =
listeCurrentInstance.iterator();
                               while(iterListeInstance.hasNext()) {
                                       Object elementListe =
iterListeInstance.next();
                                       retrieveReferenceForNode(node,
elementListe);
                               }
                       }
                       else {
                               System.out.println("retrieving :
"+node.getName()+"."+n.getName());
                              
_broker.retrieveReference(currentInstance, n.getName());
                               retrievedRefInstance =
PropertyUtils.getProperty(currentInstance,
n.getName());
                       }

                       if (n.getChildren().size()>0) {
                               retrieveReferenceForNode(n,
retrievedRefInstance);
                       }

               }

       }

       public void addRetrieveRef(String path) {
               if(_retrieveRef==null)
                       _retrieveRef = new ArrayList();

               _retrieveRef.add(path);
       }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Fwd: Deep retrieve : a proposal

Posted by Jean-Baptiste Claramonte <je...@gmail.com>.
I do not know if I missed something and if there is a more effective
method to retrieve an object graph (without setting auto-retrieve to
true in the repository) but in any cases, for those who'd like to know
more about it, here is the code of the Java classes for DeepRetrieve.

Please don't hesitate to give feedback about the implementation,
especially if you know a better way to do the job !

Parser.java
Node.java
DeepRetrieve.java

How to use it :
[A]<(*)listeA-----(1)b>[B]<(1)b-----(*)listeC>[C]
We retrieve an instance on A then we execute a deepretrieve of the
object graph :
b(listeA) means that we want to retrieve b and the back reference
claaed listeA from [B] to [A].
We also ask for retrieving listeC and his back reference to [B] called b

<code>
Criteria crit = new Criteria();
crit.addEqualTo("idA", new Long(0));

QueryByCriteria query = QueryFactory.newQuery(A.class, crit);

A result = (A)broker.getObjectByQuery(query);

DeepRetrieve deepRetrieve = new DeepRetrieve();
deepRetrieve.addRetrieveRef("b(listeA).listeC(b)");
deepRetrieve.execute(result, broker);
</code>

************************************************
/*
* Created on 22 nov. 2004
*
*/
package org.test.query.deepretrieve;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
* @author JBClaramonte
*
*/
public class Parser {
      /**
       * contains a collection of String
       */
      private Collection lines;

      /**
       * contains the root Node (initialized with the makeTree method)
       */
      private Node root;

      private Parser() {

      }

      public Parser(Collection lines) {
              this.lines = lines;
      }

      /**
       *
       *
       */
      public Node makeTree4() {
              if (lines==null || lines.size()==0)
                      return null;

              root = new Node("root");
              root.setLevel(0);

              Iterator iter = lines.iterator();
              Node currentNode = root;
              while (iter.hasNext()) {
                      String line = (String)iter.next();

                      Iterator listeRef =
splitNavigationPath2(line).iterator();
                      while (listeRef.hasNext()) {
                              ReferenceToken ref =
(ReferenceToken)listeRef.next();

                              Node nextNode = currentNode.lookup(ref.refName);

                              if (nextNode == null) {
                                      nextNode = new Node(ref.refName);

nextNode.setLevel(currentNode.getLevel()+1);

                                      currentNode.getChildren().add(nextNode);
                              }

                              if (ref.backRefName!=null)
                                      nextNode.getChildren().add(new
Node(ref.backRefName));

                              currentNode = nextNode;
                      }
                      currentNode = root;
              }
              return root;
      }

      /**
       * If we have the following model :
       * [A]<(*)listeA-----(1)b>[B]<(1)b-----(*)listeC>[C]-----(1)d>[D]
       *                   /\
       *               (*)listeB
       *                   ||
       *               (*)listeE
       *                   \/
       *                  [E]
       * Split a String like : "b(listeA).listeC(b).d"
       * where "b" is a reference from an instance of [A],
       * and "(listeA)" is a back collection reference from [B] to [A],
       * listeC is a collection reference from [B] to [C], ...
       * So splitNavigationPath2 will split the initial string
       * into the following Collection :
       * [@ReferenceToken(ref="b", backRef="listeA"),
       *  @ReferenceToken(ref="listeC", backRef="b"),
       *  @ReferenceToken(ref="d", backRef=null)]
       * @param naviPath
       * @return
       */
      protected Collection splitNavigationPath2(String naviPath) {
              ArrayList result = new ArrayList();
              boolean finished = false;
              int indexFrom = 0;
              while(finished==false) {
                      int indexTo = naviPath.indexOf(".", indexFrom);
                      if (indexTo<0) {
                              String element =
naviPath.substring(indexFrom, naviPath.length());
                              result.add(new ReferenceToken(element));
                              return result;
                      }

                      // Un element c'est une chaine du style : b(listeA)
                      String element = naviPath.substring(indexFrom, indexTo);

                      // Découper element pour le mettre dans ReferenceToken
                      result.add(new ReferenceToken(element));

                      indexFrom = indexTo+1;
              }

              return result;
      }

      class ReferenceToken {
              public ReferenceToken(String element) {
                      int indexA = element.indexOf("(", 0);
                      if (indexA<0) {
                              this.refName = element;
                              this.backRefName = null;
                      } else {
                              this.refName = element.substring(0, indexA);
                              this.backRefName =
element.substring(indexA+1, element.length()-1);
                      }
              }
              public String refName;
              public String backRefName;
      }

      /**
       * @return Returns the lines.
       */
      public Collection getLines() {
              return lines;
      }
      /**
       * @param lines The lines to set.
       */
      public void setLines(Collection lines) {
              this.lines = lines;
      }
}

************************************************

/*
* Created on 22 nov. 2004
*
*/
package org.test.query.deepretrieve;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
* @author JBClaramonte
*
*/
public class Node {

      private String _name;

      private boolean _loadParent = false;

      private Collection _children;

      private int _level=0;

      public Node() {
      }

      public Node(String name) {
              this._name = name;
              this._children = new ArrayList();
      }

      public Node lookup(String name) {
              Iterator iter = this.getChildren().iterator();

              while (iter.hasNext()) {
                      Node n = (Node)iter.next();
                      if (n.getName().equals(name)) {
                              return n;
                      }
              }
              return null;
      }

      /**
       * @return Returns the children.
       */
      public Collection getChildren() {
              return _children;
      }
      /**
       * @param children The children to set.
       */
      public void setChildren(Collection children) {
              this._children = children;
      }
      /**
       * @return Returns the name.
       */
      public String getName() {
              return _name;
      }
      /**
       * @param name The name to set.
       */
      public void setName(String name) {
              this._name = name;
      }
      /**
       * @return Returns the level.
       */
      public int getLevel() {
              return _level;
      }
      /**
       * @param level The level to set.
       */
      public void setLevel(int level) {
              this._level = level;
      }

      /**
       * @return Returns the loadParent.
       */
      public boolean isLoadParent() {
              return _loadParent;
      }
      /**
       * @param loadParent The loadParent to set.
       */
      public void setLoadParent(boolean loadParent) {
              _loadParent = loadParent;
      }

}

************************************************

/*
* Created on 24 nov. 2004
*
*/
package org.test.query.deepretrieve;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.ojb.broker.PersistenceBroker;

/**
* @author JBClaramonte
*
*/
public class DeepRetrieve {
      private Parser _parser;
      private Node _root;
      private PersistenceBroker _broker;
      private Collection _retrieveRef = null;

      public DeepRetrieve() {

      }

      public DeepRetrieve(Collection retrieveRef) {
              this._retrieveRef = retrieveRef;
      }

      public void execute(Object instanceRoot, PersistenceBroker pBroker) {
              if (_retrieveRef!=null) {
                      if (_parser==null) {
                              _parser = new Parser(_retrieveRef);
                              _root = _parser.makeTree4();
                      }
              }
              try {
                      _broker = pBroker;
                      retrieveReferenceForNode(_root, instanceRoot);

              } catch (IllegalAccessException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
              } catch (InvocationTargetException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
              } catch (NoSuchMethodException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
              } finally {
                      _broker = null;
              }
      }

      /**
       *
       * @param node
       * @param currentInstance
       * @throws IllegalAccessException
       * @throws InvocationTargetException
       * @throws NoSuchMethodException
       */
      protected void retrieveReferenceForNode(Node node, Object
currentInstance) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {

              if (currentInstance==null)
                      return;

              Object retrievedRefInstance = null;
              Iterator iter = node.getChildren().iterator();
              while (iter.hasNext()) {
                      Node n = (Node)iter.next();
                      if ((currentInstance instanceof java.util.Collection)) {
                              Collection listeCurrentInstance =
(Collection)currentInstance;
                              Iterator iterListeInstance =
listeCurrentInstance.iterator();
                              while(iterListeInstance.hasNext()) {
                                      Object elementListe =
iterListeInstance.next();
                                      retrieveReferenceForNode(node,
elementListe);
                              }
                      }
                      else {
                              System.out.println("retrieving :
"+node.getName()+"."+n.getName());

_broker.retrieveReference(currentInstance, n.getName());
                              retrievedRefInstance =
PropertyUtils.getProperty(currentInstance,
n.getName());
                      }

                      if (n.getChildren().size()>0) {
                              retrieveReferenceForNode(n,
retrievedRefInstance);
                      }

              }

      }

      public void addRetrieveRef(String path) {
              if(_retrieveRef==null)
                      _retrieveRef = new ArrayList();

              _retrieveRef.add(path);
      }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org