package org.apache.ojb.tutorial2;
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;
import org.apache.ojb.broker.TestHelper;
import org.apache.ojb.broker.util.ui.AsciiSplash;
import org.apache.ojb.odmg.OJB;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.ODMGException;
/**
* The tutorial application.
*/
public class Application
{
/** The use cases */
private Vector useCases;
/** The name of the database */
private static String databaseName;
static
{
databaseName = TestHelper.DEF_DATABASE_NAME;
}
/**
* Creates a new application object.
*/
public Application()
{
// get facade instance
Implementation odmg = OJB.getInstance();
Database db = odmg.newDatabase();
// open database
try
{
db.open(databaseName, Database.OPEN_READ_WRITE);
}
catch (ODMGException ex)
{
ex.printStackTrace();
}
useCases = new Vector();
useCases.add(new UCListAllProducts(odmg));
useCases.add(new UCEnterNewProduct(odmg));
useCases.add(new UCEditProduct(odmg));
useCases.add(new UCDeleteProduct(odmg));
useCases.add(new UCQuitApplication(odmg));
}
/**
* Disply the available use cases.
*/
public void displayUseCases()
{
System.out.println();
for (int idx = 0; idx < useCases.size(); idx++)
{
System.out.println("[" + idx + "] " + ((UseCase)useCases.get(idx)).getDescription());
}
}
/**
* The main entry point of the tutorial application
*
* @param args The commandline arguments
*/
public static void main(String[] args)
{
Application app = new Application();
app.run();
}
/**
* Reads a single line from stdin and returns it.
*
* @return The user's intput
*/
private String readLine()
{
try
{
BufferedReader rin = new BufferedReader(new InputStreamReader(System.in));
return rin.readLine();
}
catch (Exception e)
{
return "";
}
}
/**
* The application's main loop.
*/
public void run()
{
System.out.println(AsciiSplash.getSplashArt());
System.out.println("Welcome to the OJB ODMG tutorial application");
System.out.println();
// never stop (there is a special use case to quit the application)
while (true)
{
try
{
// select a use case and perform it
UseCase uc = selectUseCase();
uc.apply();
}
catch (Throwable t)
{
System.out.println(t.getMessage());
}
}
}
/**
* Allows the user to select a use case.
*/
public UseCase selectUseCase()
{
displayUseCases();
System.out.println("type in number to select a use case");
String in = readLine();
int idx = Integer.parseInt(in);
return (UseCase)useCases.get(idx);
}
}