KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedReader JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import javax.jdo.PersistenceManager;
23 import javax.jdo.PersistenceManagerFactory;
24
25 import org.apache.ojb.broker.util.ui.AsciiSplash;
26 import org.apache.ojb.jdori.sql.OjbStorePMF;
27
28 /**
29  * The tutorial application.
30  */

31 public class Application
32 {
33     /** The use cases */
34     private Vector JavaDoc useCases;
35     /** The persistence manager factory used to acquire persistence managers */
36     private PersistenceManagerFactory factory;
37     /** The persistence manager used for database operations */
38     private PersistenceManager manager;
39
40     /**
41      * Creates a new application object.
42      */

43     public Application()
44     {
45         factory = null;
46         manager = null;
47         try
48         {
49             factory = new OjbStorePMF();
50         }
51         catch (Throwable JavaDoc t)
52         {
53             System.out.println("ERROR: " + t.getMessage());
54             t.printStackTrace();
55         }
56
57         useCases = new Vector JavaDoc();
58         useCases.add(new UCListAllProducts(factory));
59         useCases.add(new UCEnterNewProduct(factory));
60         useCases.add(new UCEditProduct(factory));
61         useCases.add(new UCDeleteProduct(factory));
62         useCases.add(new UCQuitApplication(factory));
63     }
64
65     /**
66      * Disply the available use cases.
67      */

68     public void displayUseCases()
69     {
70         System.out.println();
71         for (int idx = 0; idx < useCases.size(); idx++)
72         {
73             System.out.println("[" + idx + "] " + ((UseCase)useCases.get(idx)).getDescription());
74         }
75     }
76
77     /**
78      * The main entry point of the tutorial application
79      *
80      * @param args The commandline arguments
81      */

82     public static void main(String JavaDoc[] args)
83     {
84         Application app = new Application();
85
86         app.run();
87     }
88
89     /**
90      * Reads a single line from stdin and returns it.
91      *
92      * @return The user's intput
93      */

94     private String JavaDoc readLine()
95     {
96         try
97         {
98             BufferedReader JavaDoc rin = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
99
100             return rin.readLine();
101         }
102         catch (Exception JavaDoc e)
103         {
104             return "";
105         }
106     }
107
108     /**
109      * The application's main loop.
110      */

111     public void run()
112     {
113         System.out.println(AsciiSplash.getSplashArt());
114         System.out.println("Welcome to the OJB JDO RI tutorial application");
115         System.out.println();
116
117         // never stop (there is a special use case to quit the application)
118
while (true)
119         {
120             try
121             {
122                 // select a use case and perform it
123
UseCase uc = selectUseCase();
124
125                 uc.apply();
126             }
127             catch (Throwable JavaDoc t)
128             {
129                 if (manager != null)
130                 {
131                     manager.close();
132                 }
133                 System.out.println(t.getMessage());
134             }
135         }
136     }
137
138     /**
139      * Allows the user to select a use case.
140      */

141     public UseCase selectUseCase()
142     {
143         displayUseCases();
144         System.out.println("type in number to select a use case");
145
146         String JavaDoc in = readLine();
147         int idx = Integer.parseInt(in);
148
149         return (UseCase)useCases.get(idx);
150     }
151 }
152
Popular Tags