KickJava   Java API By Example, From Geeks To Geeks.

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


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.PersistenceBroker;
19 import org.apache.ojb.broker.PersistenceBrokerException;
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 UCEnterNewProduct extends AbstractUseCase
28 {
29     /**
30      * UCEnterNewProduct constructor comment.
31      */

32     public UCEnterNewProduct(PersistenceBroker broker)
33     {
34         super(broker);
35     }
36
37     /** perform this use case*/
38     public void apply()
39     {
40         // this will be our new object
41
Product newProduct = new Product();
42         
43         // thma: attention, no sequence numbers yet for ojb/prevalyer
44
newProduct.setId((int)System.currentTimeMillis());
45         
46         // now read in all relevant information and fill the new object:
47
System.out.println("please enter a new product");
48         String JavaDoc in = readLineWithMessage("enter name:");
49         newProduct.setName(in);
50         in = readLineWithMessage("enter price:");
51         newProduct.setPrice(Double.parseDouble(in));
52         in = readLineWithMessage("enter available stock:");
53         newProduct.setStock(Integer.parseInt(in));
54
55         // now perform persistence operations
56
try
57         {
58             // 1. open transaction
59
broker.beginTransaction();
60
61             // 2. make the new object persistent
62
broker.store(newProduct);
63             broker.commitTransaction();
64         }
65         catch (PersistenceBrokerException ex)
66         {
67             // if something went wrong: rollback
68
broker.abortTransaction();
69             System.out.println(ex.getMessage());
70             ex.printStackTrace();
71         }
72     }
73
74     /** get descriptive information on use case*/
75     public String JavaDoc getDescription()
76     {
77         return "Enter a new product";
78     }
79 }
80
Popular Tags