KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > prevayler > demo > UCDeleteProduct


1 package org.apache.ojb.broker.prevayler.demo;
2
3 /* Copyright 2003-2005 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.query.Query;
19 import org.apache.ojb.broker.query.QueryByIdentity;
20 import org.apache.ojb.tutorial1.Product;
21
22 /**
23  * Insert the type's description here.
24  * Creation date: (04.03.2001 10:34:15)
25  * @author Thomas Mahler
26  */

27 public class UCDeleteProduct extends AbstractUseCase
28 {
29     /**
30      * UCEnterNewProduct constructor comment.
31      */

32     public UCDeleteProduct(org.apache.ojb.broker.PersistenceBroker b)
33     {
34         super(b);
35     }
36
37     /** perform this use case*/
38     public void apply()
39     {
40         String JavaDoc in = readLineWithMessage("Delete Product with id:");
41         int id = Integer.parseInt(in);
42
43         // We don't have a reference to the selected Product.
44
// So first we have to lookup the object,
45
// we do this by a query by example (QBE):
46
// 1. build an example object with matching primary key values:
47
Product example = new Product();
48         example.setId(id);
49         // 2. build a QueryByIdentity from this sample instance:
50
Query query = new QueryByIdentity(example);
51         try
52         {
53             // start broker transaction
54
broker.beginTransaction();
55             // lookup the product specified by the QBE
56
Product toBeDeleted = (Product) broker.getObjectByQuery(query);
57             // now ask broker to delete the object
58
broker.delete(toBeDeleted);
59             // commit transaction
60
broker.commitTransaction();
61         }
62         catch (Throwable JavaDoc t)
63         {
64             // rollback in case of errors
65
broker.abortTransaction();
66             t.printStackTrace();
67         }
68     }
69
70     /** get descriptive information on use case*/
71     public String JavaDoc getDescription()
72     {
73         return "Delete a product entry";
74     }
75 }
76
Popular Tags