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 UCDeleteProduct extends AbstractUseCase {
28: /**
29: * UCEnterNewProduct constructor comment.
30: */
31: public UCDeleteProduct(org.apache.ojb.broker.PersistenceBroker b) {
32: super (b);
33: }
34:
35: /** perform this use case*/
36: public void apply() {
37: String in = readLineWithMessage("Delete 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: // 2. build a QueryByIdentity from this sample instance:
47: Query query = new QueryByIdentity(example);
48: try {
49: // start broker transaction
50: broker.beginTransaction();
51: // lookup the product specified by the QBE
52: Product toBeDeleted = (Product) broker
53: .getObjectByQuery(query);
54: // now ask broker to delete the object
55: broker.delete(toBeDeleted);
56: // commit transaction
57: broker.commitTransaction();
58: } catch (Throwable t) {
59: // rollback in case of errors
60: broker.abortTransaction();
61: t.printStackTrace();
62: }
63: }
64:
65: /** get descriptive information on use case*/
66: public String getDescription() {
67: return "Delete a product entry";
68: }
69: }
|