Package org.apache.ojb.tutorial5

Source Code of org.apache.ojb.tutorial5.Application

package org.apache.ojb.tutorial5;

/* Copyright 2002-2005 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 javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import org.apache.ojb.broker.util.ui.AsciiSplash;
import org.apache.ojb.jdori.sql.OjbStorePMF;

/**
* The tutorial application.
*/
public class Application
{
    /** The use cases */
    private Vector useCases;
    /** The persistence manager factory used to acquire persistence managers */
    private PersistenceManagerFactory factory;
    /** The persistence manager used for database operations */
  private PersistenceManager manager;

    /**
     * Creates a new application object.
     */
    public Application()
    {
      factory = null;
        manager = null;
        try
        {
      factory = new OjbStorePMF();
        }
        catch (Throwable t)
        {
      System.out.println("ERROR: " + t.getMessage());
            t.printStackTrace();
        }

        useCases = new Vector();
        useCases.add(new UCListAllProducts(factory));
        useCases.add(new UCEnterNewProduct(factory));
        useCases.add(new UCEditProduct(factory));
        useCases.add(new UCDeleteProduct(factory));
        useCases.add(new UCQuitApplication(factory));
    }

    /**
     * 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 JDO RI 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)
            {
        if (manager != null)
                {
            manager.close();
                }
                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);
    }
}
TOP

Related Classes of org.apache.ojb.tutorial5.Application

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.