KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tutorial5 > UCListAllProducts


1 package org.apache.ojb.tutorial5;
2
3 /* Copyright 2002-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import javax.jdo.PersistenceManager;
19 import javax.jdo.PersistenceManagerFactory;
20 import javax.jdo.Query;
21
22 import org.apache.ojb.broker.PersistenceBrokerFactory;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * Use case for listing all products in the database.
29  */

30 public class UCListAllProducts extends AbstractUseCase
31 {
32     /**
33      * Creates a new list-all use case instance.
34      *
35      * @param factory The factory for getting persistence managers
36      */

37     public UCListAllProducts(PersistenceManagerFactory factory)
38     {
39         super(factory);
40     }
41
42     /**
43      * Returns a description of this use case.
44      *
45      * @return A description of the use case
46      */

47     public String JavaDoc getDescription()
48     {
49         return "List all product entries";
50     }
51
52     /**
53      * Performs this use case.
54      */

55     public void apply()
56     {
57         System.out.println("The list of available products:");
58
59         PersistenceManager manager = factory.getPersistenceManager();
60
61         try
62         {
63             // JDO does not like old instances...
64
PersistenceBrokerFactory.defaultPersistenceBroker().clearCache();
65
66             manager.currentTransaction().begin();
67
68             Query query = manager.newQuery(Product.class);
69             Collection JavaDoc allProducts = (Collection JavaDoc)query.execute();
70
71             // now iterate over the result to print each product
72
for (Iterator JavaDoc iter = allProducts.iterator(); iter.hasNext();)
73             {
74                 System.out.println(iter.next());
75             }
76             manager.currentTransaction().commit();
77         }
78         catch (Throwable JavaDoc t)
79         {
80             t.printStackTrace();
81         }
82         finally
83         {
84             manager.close();
85         }
86     }
87 }
88
Popular Tags