1 23 24 package examples.invoice.applications; 25 26 import examples.invoice.persistclass.ProdUnits; 27 import examples.invoice.persistclass.ProdUnitsHelper; 28 import examples.invoice.persistclass.MapHelper; 29 import examples.invoice.persistclass.ProductHelper; 30 import org.objectweb.jorm.api.PException; 31 import org.objectweb.jorm.naming.api.PName; 32 33 import java.util.Iterator ; 34 import java.io.InputStream ; 35 import javax.resource.ResourceException ; 36 37 40 public class ProdUnitsAppli implements ShellExtent { 41 private static ProdUnitsAppli singleton = null; 42 private static ProductAppli productAppli; 43 private ProductHelper productHelper; 44 private ProdUnitsHelper mapHelper; 45 private BasicShell basicShell; 46 47 public static ProdUnitsAppli getInstance() { 48 if (singleton == null) { 49 productAppli = ProductAppli.getInstance(); 50 singleton = new ProdUnitsAppli(); 51 } 52 return singleton; 53 } 54 55 private ProdUnitsAppli() { 56 productHelper = (ProductHelper) productAppli.getHelper(); 57 mapHelper = new ProdUnitsHelper(productHelper); 58 basicShell = BasicShell.getInstance(); 59 } 60 61 public boolean interpCmd(String cmd) { 62 if (cmd.equals("cu") || cmd.equals("createprodunits")) 63 createProdUnits(); 64 else if (cmd.equals("lu") || cmd.equals("listprodunits")) 65 listProdUnits(); 66 else if (cmd.equals("du") || cmd.equals("deleteprodunits")) 67 deleteProdUnits(); 68 else if (productAppli.interpCmd(cmd)) 69 return true; 70 else 71 return false; 72 return true; 73 } 74 75 public void help() { 76 productAppli.help(); 77 System.out.println("\t\t- createprodunits | cu : creates a new ProdUnits using the active mapper."); 78 System.out.println("\t\t- listprodunits | lu : lists existing ProdUnitses using the active mapper."); 79 System.out.println("\t\t- deleteprodunits | du : deletes an existing ProdUnits using the active mapper."); 80 } 81 82 public MapHelper getHelper() { 83 return mapHelper; 84 } 85 86 public static void main(String [] args) { 87 InputStream in = System.in; 88 try { 89 switch (args.length) { 90 case 2: 91 in = ObsInputStream.observes(args[1]); 92 case 1: 93 BasicShell.getInstance().shellLoop(args[0], getInstance(), in); 94 break; 95 default: 96 System.err.println("Usage: AddressAppli prop_file_name [jormexsh_cmdfile]"); 97 System.exit(-1); 98 } 99 } catch (Exception e) { 100 System.err.println("Exit with exception: " + e.getMessage()); 101 do { 102 e.printStackTrace(); 103 if (e instanceof PException) 104 e = ((PException) e).getNestedException(); 105 if (e instanceof ResourceException ) 106 e = ((ResourceException ) e).getLinkedException(); 107 else 108 e = null; 109 } while (e != null); 110 } 111 } 112 113 private void createProdUnits() { 114 if (basicShell.activeMapper == null) { 115 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 116 return; 117 } 118 try { 119 String p, q; 120 p = basicShell.promptString("\tProduct: ", 200); 121 q = basicShell.promptString("\tQuantity: ", 10); 122 if ((p == null) || (q == null) || (p.length() == 0) || (q.length() == 0)) { 123 basicShell.errorMsg("wrong product or quantity (mandatory)"); 124 return; 125 } 126 ProdUnits ad = mapHelper.createProdUnits(basicShell.activeMapper, 127 p, Integer.parseInt(q)); 128 System.out.println("\tCreated ProdUnits:\n\t\t" + ad.toPrintable()); 129 } catch (Exception e) { 130 System.out.println("\n\tEXCEPTION"); 131 e.printStackTrace(); 132 System.out.println(); 133 } 134 } 135 136 private void listProdUnits() { 137 if (basicShell.activeMapper == null) { 138 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 139 return; 140 } 141 try { 142 Iterator adit = mapHelper.listProdUnits(basicShell.activeMapper); 143 System.out.println("\tExisting ProdUnits:"); 144 while (adit.hasNext()) { 145 PName pn = (PName) adit.next(); 146 ProdUnits ad = mapHelper.getProdUnits(basicShell.activeMapper, pn); 147 System.out.println("\t\t" + ad.toPrintable()); 148 } 149 } catch (Exception e) { 150 System.out.println("\n\tEXCEPTION"); 151 e.printStackTrace(); 152 System.out.println(); 153 } 154 } 155 156 private void deleteProdUnits() { 157 if (basicShell.activeMapper == null) { 158 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 159 return; 160 } 161 String sid; 162 sid = basicShell.promptString("\tID: ", 20); 163 try { 164 if (mapHelper.deleteProdUnits(basicShell.activeMapper, Long.parseLong(sid))) 165 System.out.println("ProdUnits deleted."); 166 else 167 basicShell.errorMsg("cannot delete ProdUnits"); 168 } catch (Exception e) { 169 System.out.println("\n\tEXCEPTION"); 170 e.printStackTrace(); 171 System.out.println(); 172 } 173 } 174 } 175 | Popular Tags |