KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jdo.PersistenceManager;
19 import javax.jdo.PersistenceManagerFactory;
20 import javax.jdo.Transaction;
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 factory The factory for getting persistence managers
31      */

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

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

51     public void apply()
52     {
53         // 1. this will be our new object
54
Product newProduct = new Product();
55
56         // 2. now read in all relevant information and fill the new object:
57
System.out.println("please enter a new product");
58
59         String JavaDoc in = readLineWithMessage("enter name:");
60
61         newProduct.setName(in);
62         in = readLineWithMessage("enter price:");
63         newProduct.setPrice(Double.parseDouble(in));
64         in = readLineWithMessage("enter available stock:");
65         newProduct.setStock(Integer.parseInt(in));
66
67         // now perform persistence operations
68
PersistenceManager manager = factory.getPersistenceManager();
69         Transaction tx = manager.currentTransaction();
70
71         tx.begin();
72         manager.makePersistent(newProduct);
73
74         // 5. commit transaction
75
tx.commit();
76         manager.close();
77     }
78 }
79
Popular Tags