KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.tutorial2;
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 org.apache.ojb.broker.util.ui.AsciiSplash;
23 import org.apache.ojb.odmg.OJB;
24 import org.odmg.Database;
25 import org.odmg.Implementation;
26 import org.odmg.ODMGException;
27
28 /**
29  * The tutorial application.
30  */

31 public class Application
32 {
33     /** The name of the database, i.e. the jcd-alias (including username and password) */
34     private static final String JavaDoc DATABASE_NAME = "default#sa#";
35
36     /** The use cases */
37     private Vector JavaDoc useCases;
38
39     /**
40      * Creates a new application object.
41      */

42     public Application()
43     {
44         // get facade instance
45
// use one Implementation instance per database
46
// it's recommended to share the Implementation and Database instances
47
// in the application, instead of always instantiate and open it.
48
Implementation odmg = OJB.getInstance();
49         Database db = odmg.newDatabase();
50
51         // open database
52
try
53         {
54             db.open(DATABASE_NAME, Database.OPEN_READ_WRITE);
55         }
56         catch (ODMGException ex)
57         {
58             ex.printStackTrace();
59         }
60
61         useCases = new Vector JavaDoc();
62         useCases.add(new UCListAllProducts(odmg));
63         useCases.add(new UCEnterNewProduct(odmg));
64         useCases.add(new UCEditProduct(odmg));
65         useCases.add(new UCDeleteProduct(odmg));
66         useCases.add(new UCQuitApplication(odmg));
67     }
68
69     /**
70      * Disply the available use cases.
71      */

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

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

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

115     public void run()
116     {
117         System.out.println(AsciiSplash.getSplashArt());
118         System.out.println("Welcome to the OJB ODMG tutorial application");
119         System.out.println();
120
121         // never stop (there is a special use case to quit the application)
122
while (true)
123         {
124             try
125             {
126                 // select a use case and perform it
127
UseCase uc = selectUseCase();
128
129                 uc.apply();
130             }
131             catch (Throwable JavaDoc t)
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