KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.odmg.Implementation;
19 import org.odmg.Transaction;
20 import org.odmg.Database;
21
22 /**
23  * Use case for adding a new product to the database.
24  */

25 public class UCEnterNewProduct extends AbstractUseCase
26 {
27     /**
28      * Creates a new enter-new use case instance.
29      *
30      * @param odmg The odmg implementation to use
31      */

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

42     public String JavaDoc getDescription()
43     {
44         return "Enter a new product";
45     }
46
47     /**
48      * Performs this use case.
49      */

50     public void apply()
51     {
52         // 1. this will be our new object
53
Product newProduct = new Product();
54
55         // 2. now read in all relevant information and fill the new object:
56
System.out.println("please enter a new product");
57
58         String JavaDoc in = readLineWithMessage("enter name:");
59
60         newProduct.setName(in);
61         in = readLineWithMessage("enter price:");
62         newProduct.setPrice(Double.parseDouble(in));
63         in = readLineWithMessage("enter available stock:");
64         newProduct.setStock(Integer.parseInt(in));
65
66         // now perform persistence operations
67

68         // 3. open transaction
69
Database db = odmg.getDatabase(null); // the current DB
70
Transaction tx = odmg.newTransaction();
71
72         tx.begin();
73
74         // 4. make persistent new object
75
db.makePersistent(newProduct);
76
77         // 5. commit transaction
78
tx.commit();
79     }
80 }
81
Popular Tags