01: package org.apache.ojb.broker.prevayler.demo;
02:
03: /* Copyright 2003-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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: * UCEnterNewProduct constructor comment.
30: */
31: public UCEnterNewProduct(PersistenceBroker broker) {
32: super (broker);
33: }
34:
35: /** perform this use case*/
36: public void apply() {
37: // this will be our new object
38: Product newProduct = new Product();
39:
40: // thma: attention, no sequence numbers yet for ojb/prevalyer
41: newProduct.setId((int) System.currentTimeMillis());
42:
43: // now read in all relevant information and fill the new object:
44: System.out.println("please enter a new product");
45: String in = readLineWithMessage("enter name:");
46: newProduct.setName(in);
47: in = readLineWithMessage("enter price:");
48: newProduct.setPrice(Double.parseDouble(in));
49: in = readLineWithMessage("enter available stock:");
50: newProduct.setStock(Integer.parseInt(in));
51:
52: // now perform persistence operations
53: try {
54: // 1. open transaction
55: broker.beginTransaction();
56:
57: // 2. make the new object persistent
58: broker.store(newProduct);
59: broker.commitTransaction();
60: } catch (PersistenceBrokerException ex) {
61: // if something went wrong: rollback
62: broker.abortTransaction();
63: System.out.println(ex.getMessage());
64: ex.printStackTrace();
65: }
66: }
67:
68: /** get descriptive information on use case*/
69: public String getDescription() {
70: return "Enter a new product";
71: }
72: }
|