1 package org.apache.ojb.tutorial5; 2 3 17 18 import java.io.BufferedReader ; 19 import java.io.InputStreamReader ; 20 import java.util.Vector ; 21 22 import javax.jdo.PersistenceManager; 23 import javax.jdo.PersistenceManagerFactory; 24 25 import org.apache.ojb.broker.util.ui.AsciiSplash; 26 import org.apache.ojb.jdori.sql.OjbStorePMF; 27 28 31 public class Application 32 { 33 34 private Vector useCases; 35 36 private PersistenceManagerFactory factory; 37 38 private PersistenceManager manager; 39 40 43 public Application() 44 { 45 factory = null; 46 manager = null; 47 try 48 { 49 factory = new OjbStorePMF(); 50 } 51 catch (Throwable t) 52 { 53 System.out.println("ERROR: " + t.getMessage()); 54 t.printStackTrace(); 55 } 56 57 useCases = new Vector (); 58 useCases.add(new UCListAllProducts(factory)); 59 useCases.add(new UCEnterNewProduct(factory)); 60 useCases.add(new UCEditProduct(factory)); 61 useCases.add(new UCDeleteProduct(factory)); 62 useCases.add(new UCQuitApplication(factory)); 63 } 64 65 68 public void displayUseCases() 69 { 70 System.out.println(); 71 for (int idx = 0; idx < useCases.size(); idx++) 72 { 73 System.out.println("[" + idx + "] " + ((UseCase)useCases.get(idx)).getDescription()); 74 } 75 } 76 77 82 public static void main(String [] args) 83 { 84 Application app = new Application(); 85 86 app.run(); 87 } 88 89 94 private String readLine() 95 { 96 try 97 { 98 BufferedReader rin = new BufferedReader (new InputStreamReader (System.in)); 99 100 return rin.readLine(); 101 } 102 catch (Exception e) 103 { 104 return ""; 105 } 106 } 107 108 111 public void run() 112 { 113 System.out.println(AsciiSplash.getSplashArt()); 114 System.out.println("Welcome to the OJB JDO RI tutorial application"); 115 System.out.println(); 116 117 while (true) 119 { 120 try 121 { 122 UseCase uc = selectUseCase(); 124 125 uc.apply(); 126 } 127 catch (Throwable t) 128 { 129 if (manager != null) 130 { 131 manager.close(); 132 } 133 System.out.println(t.getMessage()); 134 } 135 } 136 } 137 138 141 public UseCase selectUseCase() 142 { 143 displayUseCases(); 144 System.out.println("type in number to select a use case"); 145 146 String in = readLine(); 147 int idx = Integer.parseInt(in); 148 149 return (UseCase)useCases.get(idx); 150 } 151 } 152 | Popular Tags |