KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tutorial2 > UCEditProduct


1 package org.apache.ojb.tutorial2;
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.List JavaDoc;
19
20 import org.odmg.Implementation;
21 import org.odmg.Transaction;
22 import org.odmg.OQLQuery;
23
24 /**
25  * Use case for editing a product.
26  */

27 public class UCEditProduct extends AbstractUseCase
28 {
29     /**
30      * Creates a new edit use case instance.
31      *
32      * @param odmg The odmg implementation to use
33      */

34     public UCEditProduct(Implementation odmg)
35     {
36         super(odmg);
37     }
38
39     /**
40      * Returns a description of this use case.
41      *
42      * @return A description of the use case
43      */

44     public String JavaDoc getDescription()
45     {
46         return "Edit a product entry";
47     }
48
49     /**
50      * Performs this use case.
51      */

52     public void apply()
53     {
54         String JavaDoc in = readLineWithMessage("Edit Product with id:");
55         int id = Integer.parseInt(in);
56
57         // We don't have a reference to the selected Product.
58
// So first we have to lookup the object.
59

60         // 1. build oql query to select product by id:
61
String JavaDoc oqlQuery = "select del from " + Product.class.getName() + " where id = " + id;
62         Transaction tx = null;
63
64         try
65         {
66             // 2. start transaction
67
tx = odmg.newTransaction();
68             tx.begin();
69
70             // 3. lookup the product specified by query
71
OQLQuery query = odmg.newOQLQuery();
72
73             query.create(oqlQuery);
74
75             List JavaDoc result = (List JavaDoc)query.execute();
76             Product toBeEdited = (Product)result.get(0);
77
78             // 4. lock the product for write access
79
tx.lock(toBeEdited, Transaction.WRITE);
80
81             // 5. Edit the product entry
82
System.out.println("please edit existing product");
83             in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):");
84             toBeEdited.setName(in);
85             in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):");
86             toBeEdited.setPrice(Double.parseDouble(in));
87             in = readLineWithMessage("enter available stock (was " + toBeEdited.getStock() + "):");
88             toBeEdited.setStock(Integer.parseInt(in));
89
90             // 6. commit transaction
91
tx.commit();
92         }
93         catch (Throwable JavaDoc t)
94         {
95             // rollback in case of errors
96
tx.abort();
97             t.printStackTrace();
98         }
99     }
100 }
101
Popular Tags