1 23 24 package examples.invoice.applications; 25 26 import examples.invoice.persistclass.AddressHelper; 27 import examples.invoice.persistclass.Invoice; 28 import examples.invoice.persistclass.InvoiceHelper; 29 import examples.invoice.persistclass.MapHelper; 30 import examples.invoice.persistclass.ProductHelper; 31 import examples.invoice.persistclass.ProdUnitsHelper; 32 import org.objectweb.jorm.api.PException; 33 import org.objectweb.jorm.naming.api.PName; 34 35 import java.util.Iterator ; 36 import java.io.InputStream ; 37 import javax.resource.ResourceException ; 38 39 42 public class InvoiceAppli implements ShellExtent { 43 private static InvoiceAppli singleton = null; 44 private static AddressAppli addressAppli; 45 private static ProductAppli productAppli; 46 private static ProdUnitsAppli prodUnitsAppli; 47 private AddressHelper addressHelper; 48 private ProductHelper productHelper; 49 private ProdUnitsHelper prodUnitsHelper; 50 private InvoiceHelper mapHelper; 51 private BasicShell basicShell; 52 53 public static InvoiceAppli getInstance() { 54 if (singleton == null) { 55 addressAppli = AddressAppli.getInstance(); 56 productAppli = ProductAppli.getInstance(); 57 prodUnitsAppli = ProdUnitsAppli.getInstance(); 58 singleton = new InvoiceAppli(); 59 } 60 return singleton; 61 } 62 63 private InvoiceAppli() { 64 addressHelper = (AddressHelper) addressAppli.getHelper(); 65 productHelper = (ProductHelper) productAppli.getHelper(); 66 prodUnitsHelper = (ProdUnitsHelper) prodUnitsAppli.getHelper(); 67 mapHelper = new InvoiceHelper(addressHelper, 68 productHelper, 69 prodUnitsHelper); 70 basicShell = BasicShell.getInstance(); 71 } 72 73 public boolean interpCmd(String cmd) { 74 if (cmd.equals("ci") || cmd.equals("createinvoice")) 75 createInvoice(); 76 else if (cmd.equals("li") || cmd.equals("listinvoice")) 77 listInvoice(); 78 else if (cmd.equals("di") || cmd.equals("deleteinvoice")) 79 deleteInvoice(); 80 else if (addressAppli.interpCmd(cmd)) 81 return true; 82 else if (productAppli.interpCmd(cmd)) 83 return true; 84 else if (prodUnitsAppli.interpCmd(cmd)) 85 return true; 86 else 87 return false; 88 return true; 89 } 90 91 public void help() { 92 addressAppli.help(); 93 productAppli.help(); 94 prodUnitsAppli.help(); 95 System.out.println("\t\t- createinvoice | ci : creates a new Invoice using the active mapper."); 96 System.out.println("\t\t- listinvoice | li : lists existing Invoices using the active mapper."); 97 System.out.println("\t\t- deleteinvoice | di : deletes an existing Invoice using the active mapper."); 98 } 99 100 public MapHelper getHelper() { 101 return mapHelper; 102 } 103 104 public static void main(String [] args) { 105 InputStream in = System.in; 106 try { 107 switch (args.length) { 108 case 2: 109 in = ObsInputStream.observes(args[1]); 110 case 1: 111 BasicShell.getInstance().shellLoop(args[0], getInstance(), in); 112 break; 113 default: 114 System.err.println("Usage: AddressAppli prop_file_name [jormexsh_cmdfile]"); 115 System.exit(-1); 116 } 117 } catch (Exception e) { 118 System.err.println("Exit with exception: " + e.getMessage()); 119 do { 120 e.printStackTrace(); 121 if (e instanceof PException) 122 e = ((PException) e).getNestedException(); 123 if (e instanceof ResourceException ) 124 e = ((ResourceException ) e).getLinkedException(); 125 else 126 e = null; 127 } while (e != null); 128 } 129 } 130 131 private void createInvoice() { 132 if (basicShell.activeMapper == null) { 133 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 134 return; 135 } 136 try { 137 String n, a, np; 138 n = basicShell.promptString("\tNumber: ", 20); 139 a = basicShell.promptString("\tAddress ID: ", 200); 140 np = basicShell.promptString("\tNumber of ProdUnits: ", 20); 141 if ((n == null) || (a == null) || 142 (n.length() == 0) || (a.length() == 0)) { 143 basicShell.errorMsg("wrong number or address ID (mandatory)"); 144 return; 145 } 146 String [] pus; 147 if ((np == null) || (np.length() == 0)) { 148 pus = new String [0]; 149 } else { 150 int inp = Integer.parseInt(np); 151 pus = new String [inp]; 152 for (int i = 0; i < inp; i++) { 153 pus[i] = basicShell.promptString("\tProdUnits ID-" + i + ": ", 20); 154 } 155 } 156 Invoice pr = mapHelper.createInvoice(basicShell.activeMapper, 157 Integer.parseInt(n), a, pus); 158 System.out.println("\tCreated Invoice:\n\t\t" + pr.toPrintable()); 159 } catch (Exception e) { 160 System.out.println("\n\tEXCEPTION"); 161 e.printStackTrace(); 162 System.out.println(); 163 } 164 } 165 166 private void listInvoice() { 167 if (basicShell.activeMapper == null) { 168 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 169 return; 170 } 171 try { 172 Iterator pit = mapHelper.listInvoices(basicShell.activeMapper); 173 System.out.println("\tExisting Invoices:"); 174 while (pit.hasNext()) { 175 PName pn = (PName) pit.next(); 176 Invoice p = mapHelper.getInvoice(basicShell.activeMapper, pn); 177 System.out.println("\t\t" + p.toPrintable()); 178 } 179 } catch (Exception e) { 180 if (e instanceof PException) 181 ((PException) e).getNestedException().printStackTrace(); 182 System.out.println("\n\tEXCEPTION"); 183 e.printStackTrace(); 184 System.out.println(); 185 } 186 } 187 188 private void deleteInvoice() { 189 if (basicShell.activeMapper == null) { 190 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 191 return; 192 } 193 String sid; 194 sid = basicShell.promptString("\tID: ", 200); 195 try { 196 if (mapHelper.deleteInvoice(basicShell.activeMapper, sid)) 197 System.out.println("Invoice deleted."); 198 else 199 basicShell.errorMsg("cannot delete Invoice"); 200 } catch (Exception e) { 201 System.out.println("\n\tEXCEPTION"); 202 e.printStackTrace(); 203 System.out.println(); 204 } 205 } 206 } 207 | Popular Tags |