KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > invoice > applications > AddressAppli


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import javax.resource.ResourceException JavaDoc;
35
36 /**
37  * @author P. Dechamboux
38  */

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 JavaDoc 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 JavaDoc[] args) {
79         InputStream JavaDoc 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 JavaDoc 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 JavaDoc)
98                     e = ((ResourceException JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc)
137                     e = ((ResourceException JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc)
165                     e = ((ResourceException JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc)
191                     e = ((ResourceException JavaDoc) e).getLinkedException();
192                 else
193                     e = null;
194             } while (e != null);
195             System.out.println();
196         }
197     }
198 }
199
Popular Tags