KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc 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 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 createProduct() {
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, 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
174             System.out.println("\n\tEXCEPTION");
175             e.printStackTrace();
176             System.out.println();
177         }
178     }
179 }
180
Popular Tags