KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collection JavaDoc;
19
20 import javax.jdo.PersistenceManager;
21 import javax.jdo.PersistenceManagerFactory;
22 import javax.jdo.Query;
23 import javax.jdo.Transaction;
24
25 /**
26  * Use case for deleting a product from the database.
27  */

28 public class UCDeleteProduct extends AbstractUseCase
29 {
30     /**
31      * Creates a new delete use case instance.
32      *
33      * @param factory The factory for getting persistence managers
34      */

35     public UCDeleteProduct(PersistenceManagerFactory factory)
36     {
37         super(factory);
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 "Delete a product entry";
48     }
49
50     /**
51      * Performs this use case.
52      */

53     public void apply()
54     {
55         String JavaDoc in = readLineWithMessage("Delete Product with id:");
56         int id = Integer.parseInt(in);
57         Transaction tx = null;
58
59         try
60         {
61             // 1. start transaction
62
PersistenceManager manager = factory.getPersistenceManager();
63
64             tx = manager.currentTransaction();
65             tx.begin();
66
67             // 2. Build a query to look up product by the id
68
Query query = manager.newQuery(Product.class, "id == " + id);
69             
70             // 3. execute query
71
Collection JavaDoc result = (Collection JavaDoc) query.execute();
72             
73             // 4. if no matching product was found, print a message
74
if (result.size() == 0)
75             {
76                 System.out.println("did not find a Product with id=" + id);
77                 tx.rollback();
78                 manager.close();
79                 return;
80             }
81             // 5. if a matching product was found, delete it
82
else
83             {
84                 Product toBeDeleted = (Product) result.iterator().next();
85
86                 manager.deletePersistent(toBeDeleted);
87                 tx.commit();
88                 manager.close();
89             }
90         }
91         catch (Throwable JavaDoc t)
92         {
93             // rollback in case of errors
94
tx.rollback();
95             t.printStackTrace();
96         }
97     }
98 }
99
Popular Tags