KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collection JavaDoc;
19
20 import javax.jdo.PersistenceManagerFactory;
21 import javax.jdo.PersistenceManager;
22 import javax.jdo.Query;
23
24 /**
25  * Use case for editing a product.
26  */

27 public class UCEditProduct extends AbstractUseCase
28 {
29     /**
30      * Creates a new edit use case instance.
31      *
32      * @param factory The factory for getting persistence managers
33      */

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

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

52     public void apply()
53     {
54         // ask user which object should edited
55
String JavaDoc in = readLineWithMessage("Edit Product with id:");
56         int id = Integer.parseInt(in);
57         PersistenceManager manager = null;
58
59         try
60         {
61             // We don't have a reference to the selected Product.
62
// So we have to look it up first,
63

64             // 1. start transaction
65
manager = factory.getPersistenceManager();
66             manager.currentTransaction().begin();
67
68             // 2. Build a query to look up product by the id
69
Query query = manager.newQuery(Product.class, "id == " + id);
70
71             // 3. execute query
72
Collection JavaDoc result = (Collection JavaDoc)query.execute();
73             Product toBeEdited = (Product)result.iterator().next();
74
75             if (toBeEdited == null)
76             {
77                 System.out.println("did not find a matching instance...");
78                 manager.currentTransaction().rollback();
79                 return;
80             }
81
82             // 4. edit the existing entry
83
System.out.println("please edit the product entry");
84             in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):");
85             toBeEdited.setName(in);
86             in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):");
87             toBeEdited.setPrice(Double.parseDouble(in));
88             in = readLineWithMessage("enter available stock (was "+ toBeEdited.getStock()+ "):");
89             toBeEdited.setStock(Integer.parseInt(in));
90
91             // 5. commit changes
92
manager.currentTransaction().commit();
93         }
94         catch (Throwable JavaDoc t)
95         {
96             // rollback in case of errors
97
manager.currentTransaction().rollback();
98             t.printStackTrace();
99         }
100         finally
101         {
102             manager.close();
103         }
104     }
105 }
106
Popular Tags