KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.tutorial1;
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 org.apache.ojb.broker.PersistenceBroker;
19 import org.apache.ojb.broker.query.Query;
20 import org.apache.ojb.broker.query.QueryByCriteria;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * Use case for listing all products in the database.
27  */

28 public class UCListAllProducts extends AbstractUseCase
29 {
30     /**
31      * Creates a new list-all use case instance.
32      *
33      * @param broker The broker to use for database operations
34      */

35     public UCListAllProducts(PersistenceBroker broker)
36     {
37         super(broker);
38     }
39
40     /**
41      * Returns a description of this use case.
42      *
43      * @return A description of the use case
44      */

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

53     public void apply()
54     {
55         System.out.println("The list of available products:");
56
57         // 1. build a query that select all objects of Class Product, without any further criteria
58
// according to ODMG the Collection containing all instances of a persistent class is called "Extent"
59
Query query = new QueryByCriteria(Product.class, null);
60
61         try
62         {
63             // 2. ask the broker to retrieve the Extent collection
64
Collection JavaDoc allProducts = broker.getCollectionByQuery(query);
65
66             // 3. now iterate over the result to print each product
67
for (Iterator JavaDoc iter = allProducts.iterator(); iter.hasNext();)
68             {
69                 System.out.println(iter.next());
70             }
71         }
72         catch (Throwable JavaDoc t)
73         {
74             t.printStackTrace();
75         }
76     }
77 }
78
Popular Tags