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.query.Query;
19: import org.apache.ojb.broker.query.QueryByIdentity;
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 UCEditProduct extends AbstractUseCase {
28: /**
29: * UCEditProduct constructor comment.
30: */
31: public UCEditProduct(org.apache.ojb.broker.PersistenceBroker b) {
32: super (b);
33: }
34:
35: /** perform this use case*/
36: public void apply() {
37: String in = readLineWithMessage("Edit Product with id:");
38: int id = Integer.parseInt(in);
39:
40: // We don't have a reference to the selected Product.
41: // So first we have to lookup the object,
42: // we do this by a query by example (QBE):
43: // 1. build an example object with matching primary key values:
44: Product example = new Product();
45: example.setId(id);
46:
47: // 2. build a QueryByIdentity from this sample instance:
48: Query query = new QueryByIdentity(example);
49: try {
50: // 3. start broker transaction
51: broker.beginTransaction();
52:
53: // 4. lookup the product specified by the QBE
54: Product toBeEdited = (Product) broker
55: .getObjectByQuery(query);
56:
57: // 5. edit the existing entry
58: System.out.println("please edit the product entry");
59: in = readLineWithMessage("enter name (was "
60: + toBeEdited.getName() + "):");
61: toBeEdited.setName(in);
62: in = readLineWithMessage("enter price (was "
63: + toBeEdited.getPrice() + "):");
64: toBeEdited.setPrice(Double.parseDouble(in));
65: in = readLineWithMessage("enter available stock (was "
66: + toBeEdited.getStock() + "):");
67: toBeEdited.setStock(Integer.parseInt(in));
68:
69: // 6. now ask broker to store the edited object
70: broker.store(toBeEdited);
71: // 7. commit transaction
72: broker.commitTransaction();
73: } catch (Throwable t) {
74: // rollback in case of errors
75: broker.abortTransaction();
76: t.printStackTrace();
77: }
78: }
79:
80: /** get descriptive information on use case*/
81: public String getDescription() {
82: return "Edit a product entry";
83: }
84: }
|