db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/samples/org/apache/ojb/tutorial1 Application.java
Date Sun, 05 Dec 2004 19:53:37 GMT
tomdz       2004/12/05 11:53:37

  Modified:    src/samples/org/apache/ojb/tutorials OTMExample.java
                        PBExample.java
               src/samples/org/apache/ojb/tutorial5 UCListAllProducts.java
               src/samples/org/apache/ojb/servlet TestServlet.java
               src/samples/org/apache/ojb/tutorial1 Application.java
  Log:
  Updated samples to show the new usage pattern (without the PersistenceBrokerFactory)
  
  Revision  Changes    Path
  1.3       +22 -10    db-ojb/src/samples/org/apache/ojb/tutorials/OTMExample.java
  
  Index: OTMExample.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/samples/org/apache/ojb/tutorials/OTMExample.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- OTMExample.java	22 Jun 2004 22:34:01 -0000	1.2
  +++ OTMExample.java	5 Dec 2004 19:53:37 -0000	1.3
  @@ -20,7 +20,7 @@
   import org.odmg.QueryParameterTypeInvalidException;
   import org.odmg.QueryParameterCountInvalidException;
   
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.broker.OJB;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.otm.OTMConnection;
  @@ -37,12 +37,24 @@
    */
   public class OTMExample
   {
  +    /** The ojb instance */
  +    private OJB ojb;
  +
  +    /**
  +     * Creates an example object.
  +     *
  +     */
  +    public OTMExample()
  +    {
  +        ojb = new OJB();
  +    }
  +
       /**
        * Stores the given product in the database.
        * 
        * @param product The product to store
        */
  -    public static void storeProduct(Product product) throws LockingException
  +    public void storeProduct(Product product) throws LockingException
       {
           OTMKit        kit  = SimpleKit.getInstance();
           OTMConnection conn = null;
  @@ -50,7 +62,7 @@
   
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               tx.begin();
  @@ -78,7 +90,7 @@
        * 
        * @param product The product to remove
        */
  -    public static void removeProduct(Product product) throws LockingException
  +    public void removeProduct(Product product) throws LockingException
       {
           OTMKit        kit  = SimpleKit.getInstance();
           OTMConnection conn = null;
  @@ -86,7 +98,7 @@
   
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               tx.begin();
  @@ -122,7 +134,7 @@
           Transaction   tx   = null;
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               tx.begin();
  @@ -153,7 +165,7 @@
           Transaction   tx   = null;
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               tx.begin();
  @@ -185,7 +197,7 @@
   
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               OQLQuery oql = conn.newOQLQuery();
  @@ -252,7 +264,7 @@
           Transaction   tx   = null;
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               boolean auto = !tx.isInProgress();
  @@ -288,7 +300,7 @@
   
           try
           {
  -            conn = kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            conn = kit.acquireConnection(ojb.getDefaultConfiguration());
               tx   = kit.getTransaction(conn);
   
               tx.begin();
  
  
  
  1.4       +27 -23    db-ojb/src/samples/org/apache/ojb/tutorials/PBExample.java
  
  Index: PBExample.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/samples/org/apache/ojb/tutorials/PBExample.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PBExample.java	27 Aug 2004 20:00:35 -0000	1.3
  +++ PBExample.java	5 Dec 2004 19:53:37 -0000	1.4
  @@ -17,9 +17,9 @@
   
   import java.util.Collection;
   
  +import org.apache.ojb.broker.OJB;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
  @@ -32,18 +32,21 @@
    */
   public class PBExample
   {
  +    /** The OJB instance */
  +    private OJB ojb;
  +
       /**
        * Stores a product in the database.
        * 
        * @param product The product to store
        */
  -    public static void storeProduct(Product product)
  +    public void storeProduct(Product product)
       {
           PersistenceBroker broker = null;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               broker.beginTransaction();
               broker.store(product);
  @@ -71,13 +74,13 @@
        * 
        * @param products The products to store
        */
  -    public static void storeProducts(Product[] products)
  +    public void storeProducts(Product[] products)
       {
           PersistenceBroker broker = null;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               broker.beginTransaction();
               for (int idx = 0; idx < products.length; idx++)
  @@ -109,14 +112,14 @@
        * @param template The product whose equal shall be found
        * @return The found product if any
        */
  -    public static Product findByTemplate(Product template)
  +    public Product findByTemplate(Product template)
       {
           PersistenceBroker broker = null;
           Product result = null;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               Query query = QueryFactory.newQueryByExample(template);
   
  @@ -138,14 +141,14 @@
        * 
        * @return The products if products were found
        */
  -    public static Collection getExpensiveLowStockProducts()
  +    public Collection getExpensiveLowStockProducts()
       {
           PersistenceBroker broker = null;
           Collection results = null;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               Criteria criteria = new Criteria();
   
  @@ -176,14 +179,14 @@
        * @param template The product to sell
        * @return Whether the product was sold
        */
  -    public static boolean sellOneProduct(Product template)
  +    public boolean sellOneProduct(Product template)
       {
           PersistenceBroker broker = null;
           boolean isSold = false;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               Query query = QueryFactory.newQueryByExample(template);
               Product result = (Product) broker.getObjectByQuery(query);
  @@ -225,14 +228,14 @@
        * @param template The product whose equal shall be deleted from the database
        * @return Whether the product was deleted
        */
  -    public static boolean findAndDeleteProduct(Product template)
  +    public boolean findAndDeleteProduct(Product template)
       {
           PersistenceBroker broker = null;
           boolean isDeleted = false;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               Query query = QueryFactory.newQueryByExample(template);
               Product result = (Product) broker.getObjectByQuery(query);
  @@ -268,13 +271,13 @@
        * 
        * @param product The product to delete
        */
  -    public static void deleteProduct(Product product)
  +    public void deleteProduct(Product product)
       {
           PersistenceBroker broker = null;
   
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            broker = ojb.lookupBroker();
   
               broker.beginTransaction();
               broker.delete(product);
  @@ -304,7 +307,8 @@
        */
       public static void main(String[] args)
       {
  -        Product product = new Product();
  +        PBExample appObj  = new PBExample();
  +        Product   product = new Product();
   
           product.setName("Sprocket");
           product.setPrice(1.99);
  @@ -312,7 +316,7 @@
   
           System.out.println("1a. Store product: " + product);
   
  -        storeProduct(product);
  +        appObj.storeProduct(product);
   
           System.out.println("1b. Product stored: " + product);
           System.out.println();
  @@ -323,27 +327,27 @@
   
           System.out.println("2a. Find product by template, used template: " + template);
   
  -        Product aProduct = findByTemplate(template);
  +        Product aProduct = appObj.findByTemplate(template);
   
           System.out.println("2b. Found product: " + aProduct);
           System.out.println();
   
           System.out.println("3a. Sell one product, stock before was " + aProduct.getStock());
   
  -        sellOneProduct(template);
  -        aProduct = findByTemplate(template);
  +        appObj.sellOneProduct(template);
  +        aProduct = appObj.findByTemplate(template);
   
           System.out.println("3b. Product sold, current stock is " + aProduct.getStock());
           System.out.println();
   
           System.out.println("4a. Delete a product object");
   
  -        deleteProduct(aProduct);
  +        appObj.deleteProduct(aProduct);
   
           System.out.println("4b. Product deleted");
           System.out.println("4c. Try to lookup deleted product");
   
  -        Product newProduct = findByTemplate(aProduct);
  +        Product newProduct = appObj.findByTemplate(aProduct);
   
           System.out.println("4d. Found product: " + newProduct);
   
  
  
  
  1.3       +3 -3      db-ojb/src/samples/org/apache/ojb/tutorial5/UCListAllProducts.java
  
  Index: UCListAllProducts.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/samples/org/apache/ojb/tutorial5/UCListAllProducts.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UCListAllProducts.java	22 Jun 2004 22:34:01 -0000	1.2
  +++ UCListAllProducts.java	5 Dec 2004 19:53:37 -0000	1.3
  @@ -19,7 +19,7 @@
   import javax.jdo.PersistenceManagerFactory;
   import javax.jdo.Query;
   
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.jdori.sql.OjbStorePMF;
   
   import java.util.Collection;
   import java.util.Iterator;
  @@ -60,8 +60,8 @@
   
           try
           {
  -        	// JDO does not like old instances...
  -        	PersistenceBrokerFactory.defaultPersistenceBroker().clearCache();
  +            // JDO does not like old instances...
  +            ((OjbStorePMF)manager.getPersistenceManagerFactory()).clearCache();
   
           	manager.currentTransaction().begin();
   
  
  
  
  1.4       +3 -13     db-ojb/src/samples/org/apache/ojb/servlet/TestServlet.java
  
  Index: TestServlet.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/samples/org/apache/ojb/servlet/TestServlet.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestServlet.java	24 Jun 2004 19:50:37 -0000	1.3
  +++ TestServlet.java	5 Dec 2004 19:53:37 -0000	1.4
  @@ -20,9 +20,8 @@
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   
  -import org.apache.ojb.broker.PBFactoryException;
  +import org.apache.ojb.broker.OJB;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryFactory;
  @@ -54,11 +53,12 @@
       protected void doGet(HttpServletRequest req, HttpServletResponse response)
           throws ServletException, IOException
       {
  +        OJB               ojb    = new OJB();
           PersistenceBroker broker = null;
   
           try
           {
  -            broker = getBroker();
  +            broker = ojb.lookupBroker();
           }
           catch (Exception ex)
           {
  @@ -134,16 +134,6 @@
           Query      query     = QueryFactory.newQuery(Product.class, selectAll);
   
           return broker.getCollectionByQuery(query);
  -    }
  -
  -    /**
  -     * Returns the persistence broker to use for database operations.
  -     * 
  -     * @return The persistence broker
  -     */
  -    protected PersistenceBroker getBroker() throws PBFactoryException
  -    {
  -     	return PersistenceBrokerFactory.defaultPersistenceBroker();
       }
   }
   
  
  
  
  1.3       +3 -2      db-ojb/src/samples/org/apache/ojb/tutorial1/Application.java
  
  Index: Application.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/samples/org/apache/ojb/tutorial1/Application.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Application.java	22 Jun 2004 22:34:00 -0000	1.2
  +++ Application.java	5 Dec 2004 19:53:37 -0000	1.3
  @@ -15,8 +15,8 @@
    * limitations under the License.
    */
   
  +import org.apache.ojb.broker.OJB;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.util.ui.AsciiSplash;
   
   import java.io.BufferedReader;
  @@ -41,7 +41,8 @@
           broker = null;
           try
           {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            OJB ojb = new OJB();
  +            broker = ojb.lookupBroker();
           }
           catch (Throwable t)
           {
  
  
  

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


Mime
View raw message