001: package org.apache.ojb.broker.prevayler.demo;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.PersistenceBroker;
019: import org.apache.ojb.broker.PersistenceBrokerFactory;
020: import org.apache.ojb.broker.util.ui.AsciiSplash;
021:
022: import java.io.BufferedReader;
023: import java.io.InputStreamReader;
024: import java.util.Vector;
025:
026: /**
027: * The tutorial application.
028: * @author Thomas Mahler
029: */
030: public class Application {
031: private Vector useCases;
032: private PersistenceBroker broker;
033:
034: /**
035: * Application constructor comment.
036: */
037: public Application() {
038: broker = null;
039: try {
040: broker = PersistenceBrokerFactory
041: .defaultPersistenceBroker();
042: } catch (Throwable t) {
043: t.printStackTrace();
044: }
045: useCases = new Vector();
046: useCases.add(new UCListAllProducts(broker));
047: useCases.add(new UCEnterNewProduct(broker));
048: useCases.add(new UCEditProduct(broker));
049: useCases.add(new UCDeleteProduct(broker));
050: useCases.add(new UCQuitApplication(broker));
051: }
052:
053: /**
054: * Disply available use cases.
055: */
056: public void displayUseCases() {
057: System.out.println();
058: for (int i = 0; i < useCases.size(); i++) {
059: System.out.println("[" + i + "] "
060: + ((UseCase) useCases.get(i)).getDescription());
061: }
062: }
063:
064: /**
065: * Insert the method's description here.
066: * Creation date: (04.03.2001 10:40:25)
067: * @param args java.lang.String[]
068: */
069: public static void main(String[] args) {
070: Application app = new Application();
071: app.run();
072: }
073:
074: /**
075: * read a single line from stdin and return as String
076: */
077: private String readLine() {
078: try {
079: BufferedReader rin = new BufferedReader(
080: new InputStreamReader(System.in));
081: return rin.readLine();
082: } catch (Exception e) {
083: return "";
084: }
085: }
086:
087: /**
088: * the applications main loop.
089: */
090: public void run() {
091: System.out.println(AsciiSplash.getSplashArt());
092: System.out
093: .println("Welcome to the OJB PB tutorial application");
094: System.out.println();
095: // never stop (there is a special use case to quit the application)
096: while (true) {
097: try {
098: // select a use case and perform it
099: UseCase uc = selectUseCase();
100: uc.apply();
101: } catch (Throwable t) {
102: broker.close();
103: System.out.println(t.getMessage());
104: }
105: }
106: }
107:
108: /**
109: * select a use case.
110: */
111: public UseCase selectUseCase() {
112: displayUseCases();
113: System.out.println("type in number to select a use case");
114: String in = readLine();
115: int index = Integer.parseInt(in);
116: return (UseCase) useCases.get(index);
117: }
118:
119: }
|