1 package org.apache.ojb.tutorial2; 2 3 17 18 import java.io.BufferedReader ; 19 import java.io.InputStreamReader ; 20 import java.util.Vector ; 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 31 public class Application 32 { 33 34 private static final String DATABASE_NAME = "default#sa#"; 35 36 37 private Vector useCases; 38 39 42 public Application() 43 { 44 Implementation odmg = OJB.getInstance(); 49 Database db = odmg.newDatabase(); 50 51 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 (); 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 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 86 public static void main(String [] args) 87 { 88 Application app = new Application(); 89 90 app.run(); 91 } 92 93 98 private String readLine() 99 { 100 try 101 { 102 BufferedReader rin = new BufferedReader (new InputStreamReader (System.in)); 103 104 return rin.readLine(); 105 } 106 catch (Exception e) 107 { 108 return ""; 109 } 110 } 111 112 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 while (true) 123 { 124 try 125 { 126 UseCase uc = selectUseCase(); 128 129 uc.apply(); 130 } 131 catch (Throwable t) 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 |