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