KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.tutorial1;
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 org.apache.ojb.broker.PersistenceBroker;
19 import org.apache.ojb.broker.PersistenceBrokerFactory;
20 import org.apache.ojb.broker.util.ui.AsciiSplash;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * The tutorial application.
28  */

29 public class Application
30 {
31     /** The use cases */
32     private Vector JavaDoc useCases;
33     /** The broker to use for database operations */
34     private PersistenceBroker broker;
35
36     /**
37      * Creates a new application object.
38      */

39     public Application()
40     {
41         broker = null;
42         try
43         {
44             broker = PersistenceBrokerFactory.defaultPersistenceBroker();
45         }
46         catch (Throwable JavaDoc t)
47         {
48             t.printStackTrace();
49         }
50
51         useCases = new Vector JavaDoc();
52         useCases.add(new UCListAllProducts(broker));
53         useCases.add(new UCEnterNewProduct(broker));
54         useCases.add(new UCEditProduct(broker));
55         useCases.add(new UCDeleteProduct(broker));
56         useCases.add(new UCQuitApplication(broker));
57     }
58
59     /**
60      * Disply the available use cases.
61      */

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

76     public static void main(String JavaDoc[] args)
77     {
78         Application app = new Application();
79
80         app.run();
81     }
82
83     /**
84      * Reads a single line from stdin and returns it.
85      *
86      * @return The user's intput
87      */

88     private String JavaDoc readLine()
89     {
90         try
91         {
92             BufferedReader JavaDoc rin = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
93
94             return rin.readLine();
95         }
96         catch (Exception JavaDoc e)
97         {
98             return "";
99         }
100     }
101
102     /**
103      * The application's main loop.
104      */

105     public void run()
106     {
107         System.out.println(AsciiSplash.getSplashArt());
108         System.out.println("Welcome to the OJB PB tutorial application");
109         System.out.println();
110
111         // never stop (there is a special use case to quit the application)
112
while (true)
113         {
114             try
115             {
116                 // select a use case and perform it
117
UseCase uc = selectUseCase();
118
119                 uc.apply();
120             }
121             catch (Throwable JavaDoc t)
122             {
123                 broker.close();
124                 System.out.println(t.getMessage());
125             }
126         }
127     }
128
129     /**
130      * Allows the user to select a use case.
131      */

132     public UseCase selectUseCase()
133     {
134         displayUseCases();
135         System.out.println("type in number to select a use case");
136
137         String JavaDoc in = readLine();
138         int idx = Integer.parseInt(in);
139
140         return (UseCase)useCases.get(idx);
141     }
142     
143
144 }
145
Popular Tags