KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc 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 JavaDoc[] args) {
105         InputStream JavaDoc 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 JavaDoc 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 JavaDoc)
124                     e = ((ResourceException JavaDoc) 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 JavaDoc 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 JavaDoc[] pus;
147             if ((np == null) || (np.length() == 0)) {
148                 pus = new String JavaDoc[0];
149             } else {
150                 int inp = Integer.parseInt(np);
151                 pus = new String JavaDoc[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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
201             System.out.println("\n\tEXCEPTION");
202             e.printStackTrace();
203             System.out.println();
204         }
205     }
206 }
207
Popular Tags