1 23 24 package examples.invoice.applications; 25 26 import examples.invoice.persistclass.Address; 27 import examples.invoice.persistclass.AddressHelper; 28 import examples.invoice.persistclass.MapHelper; 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 AddressAppli implements ShellExtent { 40 private static AddressAppli singleton = null; 41 private AddressHelper mapHelper; 42 private BasicShell basicShell; 43 44 public static AddressAppli getInstance() { 45 if (singleton == null) { 46 singleton = new AddressAppli(); 47 } 48 return singleton; 49 } 50 51 private AddressAppli() { 52 mapHelper = new AddressHelper(); 53 basicShell = BasicShell.getInstance(); 54 } 55 56 public boolean interpCmd(String cmd) { 57 if (cmd.equals("ca") || cmd.equals("createaddress")) 58 createAddress(); 59 else if (cmd.equals("la") || cmd.equals("listaddress")) 60 listAddress(); 61 else if (cmd.equals("da") || cmd.equals("deleteaddress")) 62 deleteAddress(); 63 else 64 return false; 65 return true; 66 } 67 68 public void help() { 69 System.out.println("\t\t- createaddress | ca : creates a new Address using the active mapper."); 70 System.out.println("\t\t- listaddress | la : lists existing Addresses using the active mapper."); 71 System.out.println("\t\t- deleteaddress | da : deletes an existing Address 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 createAddress() { 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, z, t, c; 112 n = basicShell.promptString("\tName: ", 50); 113 s = basicShell.promptString("\tStreet: ", 50); 114 z = basicShell.promptString("\tZip code: ", 50); 115 t = basicShell.promptString("\tTown: ", 50); 116 c = basicShell.promptString("\tCountry: ", 50); 117 if ((n == null) || (s == null) || (n.length() == 0) || (s.length() == 0)) { 118 basicShell.errorMsg("wrong name or street (mandatory)"); 119 return; 120 } 121 if (z.length() == 0) 122 z = null; 123 if (t.length() == 0) 124 t = null; 125 if (c.length() == 0) 126 c = null; 127 Address ad = mapHelper.createAddress(basicShell.activeMapper, 128 n, s, z, t, c); 129 System.out.println("\tCreated Address:\n\t\t" + ad.toPrintable()); 130 } catch (Exception e) { 131 System.err.println("EXCEPTION: " + e.getMessage()); 132 do { 133 e.printStackTrace(); 134 if (e instanceof PException) 135 e = ((PException) e).getNestedException(); 136 if (e instanceof ResourceException ) 137 e = ((ResourceException ) e).getLinkedException(); 138 else 139 e = null; 140 } while (e != null); 141 System.out.println(); 142 } 143 } 144 145 private void listAddress() { 146 if (basicShell.activeMapper == null) { 147 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 148 return; 149 } 150 try { 151 Iterator adit = mapHelper.listAddress(basicShell.activeMapper); 152 System.out.println("\tExisting Addresses:"); 153 while (adit.hasNext()) { 154 PName pn = (PName) adit.next(); 155 Address ad = mapHelper.getAddress(basicShell.activeMapper, pn); 156 System.out.println("\t\t" + ad.toPrintable()); 157 } 158 } catch (Exception e) { 159 System.err.println("EXCEPTION: " + e.getMessage()); 160 do { 161 e.printStackTrace(); 162 if (e instanceof PException) 163 e = ((PException) e).getNestedException(); 164 if (e instanceof ResourceException ) 165 e = ((ResourceException ) e).getLinkedException(); 166 else 167 e = null; 168 } while (e != null); 169 System.out.println(); 170 } 171 } 172 173 private void deleteAddress() { 174 if (basicShell.activeMapper == null) { 175 basicShell.errorMsg("requires an active mapper (run <activatemapper> command before)"); 176 return; 177 } 178 String sid = basicShell.promptString("\tID: ", 256); 179 try { 180 if (mapHelper.deleteAddress(basicShell.activeMapper, sid)) 181 System.out.println("Address deleted."); 182 else 183 basicShell.errorMsg("cannot delete Address"); 184 } catch (Exception e) { 185 System.err.println("EXCEPTION: " + e.getMessage()); 186 do { 187 e.printStackTrace(); 188 if (e instanceof PException) 189 e = ((PException) e).getNestedException(); 190 if (e instanceof ResourceException ) 191 e = ((ResourceException ) e).getLinkedException(); 192 else 193 e = null; 194 } while (e != null); 195 System.out.println(); 196 } 197 } 198 } 199 | Popular Tags |