KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc 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 JavaDoc[] args) {
87         InputStream JavaDoc 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 JavaDoc 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 JavaDoc)
106                     e = ((ResourceException JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
169             System.out.println("\n\tEXCEPTION");
170             e.printStackTrace();
171             System.out.println();
172         }
173     }
174 }
175
Popular Tags