KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > invoice > queries > AddressQuery


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2002 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package examples.invoice.queries;
25
26 import examples.invoice.applications.AddressAppli;
27 import examples.invoice.applications.JormInit;
28 import org.objectweb.jorm.api.PClassMapping;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PMapper;
31 import org.objectweb.jorm.comp.cmdline.api.CmdLineParser;
32 import org.objectweb.jorm.comp.cmdline.rdb.RdbMapperOption;
33 import org.objectweb.jorm.comp.compiler.api.CompilerParameter;
34 import org.objectweb.jorm.comp.compiler.lib.BasicCompiler;
35 import org.objectweb.jorm.metainfo.api.Class;
36 import org.objectweb.jorm.metainfo.api.MetaObject;
37 import org.objectweb.jorm.metainfo.api.Schema;
38 import org.objectweb.util.io.api.PathExplorer;
39 import org.objectweb.util.io.lib.DirJavaExplorer;
40 import org.objectweb.medor.query.jorm.lib.JormExtent;
41 import org.objectweb.medor.query.api.QueryLeaf;
42 import org.objectweb.medor.api.Field;
43
44 import java.io.FileInputStream JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Properties JavaDoc;
48 import javax.resource.ResourceException JavaDoc;
49 import javax.resource.cci.ConnectionFactory JavaDoc;
50
51 /**
52  *
53  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
54  * Mourad Alia
55  * </b></A>
56  * <A HREF="mailto:alexandre.lefebvre@rd.francetelecom.com><b>
57  * Alexandre Lefebvre
58  </b></A>
59  */

60 public class AddressQuery extends AddressAppli {
61
62     private static AddressQuery singleQuery =
63         new AddressQuery();
64
65     public static void main(String JavaDoc[] args) {
66         if (args.length != 1) {
67             System.err.println("Usage: AddressQuery prop_file_name");
68             System.exit(-1);
69         }
70         singleQuery.runAddressQuery(args[0]);
71     }
72
73     /**
74      * @param propfilename is the filename for the runtime.properties file in the
75      * etc directory of the invoice example in the JORM dist.
76      */

77     public void runAddressQuery(String JavaDoc addPropFile) {
78         Properties JavaDoc addProp = new Properties JavaDoc();
79         try {
80             addProp.load(new FileInputStream JavaDoc(addPropFile));
81             String JavaDoc propfilename = addProp.getProperty("runtime", null);
82             //links to the properties file
83
Properties JavaDoc prop = new Properties JavaDoc();
84             System.out.println(propfilename);
85             prop.load(new FileInputStream JavaDoc(propfilename));
86             JormInit.getInstance().initLogSystem(prop);
87             JormInit.getInstance().initResourceAdapter(prop);
88             prop = null;
89
90             mapClasses();
91             if (activeMapper == null) {
92                 System.out.println("null active mapper");
93             }
94             else {
95                 System.out.println("Active mapper is " + activeMapper.getMapperName());
96             }
97
98             //Compiles the source pd file
99
//Initilization of the compiler
100
BasicCompiler comp = new BasicCompiler();
101             CompilerParameter compPar = comp.getCompilerParameter();
102             PathExplorer pathex = new DirJavaExplorer();
103             pathex.addPath(addProp.getProperty("pddir", null));
104             compPar.setClasspath(pathex);
105             compPar.setLogConfFile(addProp.getProperty("logconf", null));
106
107             compPar.setMapperName("rdb");
108             compPar.setMappingFactory("org.objectweb.jorm.metainfo.lib.rdb.BasicRDBMappingFactory");
109             compPar.setMappingName("INVOICE_RDB");
110             compPar.setParserName("org.objectweb.jorm.parser.lib.rdb.RDBParser");
111             compPar.setVerifierName("org.objectweb.jorm.verifier.lib.rdb.RdbVerifier");
112
113             String JavaDoc pddir = addProp.getProperty("pddir");
114             compPar.addInputFileName(pddir + java.io.File.separator + "Address.pd");
115             /*
116             compPar.addInputFileName(pddir + java.io.File.separator + "Invoice.pd");
117             compPar.addInputFileName(pddir + java.io.File.separator + "Product.pd");
118             compPar.addInputFileName(pddir + java.io.File.separator + "ProdId.pd");
119             compPar.addInputFileName(pddir + java.io.File.separator + "PUId.pd");
120             compPar.addInputFileName(pddir + java.io.File.separator + "ProdUnits.pd");
121             */

122             RdbMapperOption rop = new RdbMapperOption();
123             //need to get the logger.
124
rop.setLogger(JormInit.getInstance().getLoggerFactory().getLogger("examples.invoice.applications.JormInit"));
125             ((CmdLineParser) rop).parseProperty("DBName", "postgres");
126             compPar.addCPExtension(
127                 "org.objectweb.jorm.comp.cmdline.rdb.RdbMapperOption",
128                 rop);
129
130             System.out.println("Compiler initialized");
131
132             //call the parser
133
Collection JavaDoc metaobjs = comp.parseFiles(compPar.getInputFileNames());
134
135             System.out.println("Pasing finished");
136
137             Iterator JavaDoc metaIt = metaobjs.iterator();
138
139             while (metaIt.hasNext()) {
140                 MetaObject mo = (MetaObject) metaIt.next();
141                 if (mo instanceof Class JavaDoc) {
142                     System.out.println("Initializing class name " +
143                                         ((Class JavaDoc) mo).getName());
144                     String JavaDoc fullClass =
145                         ((Schema) ((Class JavaDoc) mo).getParent()).getName() + "." +
146                         ((Class JavaDoc) mo).getName();
147                     PClassMapping pm =
148                         activeMapper.lookup(fullClass);
149                     //add the meta objects from the parsing
150
pm.init(activeMapper, mo);
151                 }
152             }
153             //build a JORM extent for class Address
154
PClassMapping pm =
155                 activeMapper.lookup("examples.invoice.persistclass.Address");
156             QueryLeaf ql = new JormExtent(((Class JavaDoc)pm.getMetaInfo()),
157                 activeMapper, "Address", false);
158             System.out.println("Created new extent (all fields)");
159
160             //looking at field names
161
Field[] fields = ql.getTupleStructure().getFields();
162             for (int i = 0; i < fields.length; i++) {
163                 System.out.println("Field name "+fields[i].getName());
164             }
165
166             String JavaDoc[] selected = {"name", "zip"};
167             QueryLeaf q2 = new JormExtent(((Class JavaDoc)pm.getMetaInfo()),
168                         activeMapper, selected, true, "Address");
169             System.out.println("Created new extent (selected fields)");
170             //looking at field names
171
Field[] fields2 = q2.getTupleStructure().getFields();
172             for (int i = 0; i < fields2.length; i++) {
173                 System.out.println("Field name "+fields2[i].getName());
174             }
175
176         }
177         catch (Exception JavaDoc e) {
178             System.err.println("Exit with exception: " + e.getMessage());
179             do {
180                 e.printStackTrace();
181                 if (e instanceof PException)
182                     e = ((PException) e).getNestedException();
183                 if (e instanceof ResourceException JavaDoc)
184                     e = ((ResourceException JavaDoc) e).getLinkedException();
185                 else
186                     e = null;
187             } while (e != null);
188         }
189
190     }
191
192     private void mapClasses() {
193         String JavaDoc name = "jdbc.default";
194         PMapper m;
195         ConnectionFactory JavaDoc cf;
196
197         m = JormInit.getInstance().getMapper(name, null);
198         activeMapper = m;
199         if (m == null) {
200             errorMsg("no associated mapper");
201             return;
202         }
203         if (m.getMapperName().equals("rdb")) {
204             cf = JormInit.getInstance().getJdbcCF(name, null, null, null);
205         }
206         else if (m.getMapperName().equals("fos")) {
207             cf = JormInit.getInstance().getFosCF(name, null);
208         }
209         else {
210             errorMsg("unsupported mapper type");
211             return;
212         }
213         if (cf == null) {
214             errorMsg("no associated connection factory");
215             return;
216         }
217
218         if (JormInit.getInstance().isMapped(name)) {
219             errorMsg("map has already been performed");
220             return;
221         }
222         if (!JormInit.getInstance().canMap(name)) {
223             errorMsg("cannot map with this mapper");
224             return;
225         }
226         try {
227             getHelper().map(m, cf, JormInit.getInstance().getLoggerFactory());
228             JormInit.getInstance().setMapped(name);
229             System.out.println("Persistent classes mapped.");
230         }
231         catch (Exception JavaDoc e) {
232             System.out.println("\n\tEXCEPTION");
233             e.printStackTrace();
234             System.out.println();
235         }
236     }
237
238 }
239
Popular Tags