KickJava   Java API By Example, From Geeks To Geeks.

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


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 UCEditProduct extends AbstractUseCase
28 {
29     /**
30      * UCEditProduct constructor comment.
31      */

32     public UCEditProduct(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("Edit 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
50         // 2. build a QueryByIdentity from this sample instance:
51
Query query = new QueryByIdentity(example);
52         try
53         {
54             // 3. start broker transaction
55
broker.beginTransaction();
56
57             // 4. lookup the product specified by the QBE
58
Product toBeEdited = (Product) broker.getObjectByQuery(query);
59
60             // 5. edit the existing entry
61
System.out.println("please edit the product entry");
62             in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):");
63             toBeEdited.setName(in);
64             in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):");
65             toBeEdited.setPrice(Double.parseDouble(in));
66             in = readLineWithMessage("enter available stock (was " + toBeEdited.getStock() + "):");
67             toBeEdited.setStock(Integer.parseInt(in));
68
69
70
71             // 6. now ask broker to store the edited object
72
broker.store(toBeEdited);
73             // 7. commit transaction
74
broker.commitTransaction();
75         }
76         catch (Throwable JavaDoc t)
77         {
78             // rollback in case of errors
79
broker.abortTransaction();
80             t.printStackTrace();
81         }
82     }
83
84     /** get descriptive information on use case*/
85     public String JavaDoc getDescription()
86     {
87         return "Edit a product entry";
88     }
89 }
90
Popular Tags