1 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 ; 45 import java.util.Collection ; 46 import java.util.Iterator ; 47 import java.util.Properties ; 48 import javax.resource.ResourceException ; 49 import javax.resource.cci.ConnectionFactory ; 50 51 60 public class AddressQuery extends AddressAppli { 61 62 private static AddressQuery singleQuery = 63 new AddressQuery(); 64 65 public static void main(String [] 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 77 public void runAddressQuery(String addPropFile) { 78 Properties addProp = new Properties (); 79 try { 80 addProp.load(new FileInputStream (addPropFile)); 81 String propfilename = addProp.getProperty("runtime", null); 82 Properties prop = new Properties (); 84 System.out.println(propfilename); 85 prop.load(new FileInputStream (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 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 pddir = addProp.getProperty("pddir"); 114 compPar.addInputFileName(pddir + java.io.File.separator + "Address.pd"); 115 122 RdbMapperOption rop = new RdbMapperOption(); 123 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 Collection metaobjs = comp.parseFiles(compPar.getInputFileNames()); 134 135 System.out.println("Pasing finished"); 136 137 Iterator metaIt = metaobjs.iterator(); 138 139 while (metaIt.hasNext()) { 140 MetaObject mo = (MetaObject) metaIt.next(); 141 if (mo instanceof Class ) { 142 System.out.println("Initializing class name " + 143 ((Class ) mo).getName()); 144 String fullClass = 145 ((Schema) ((Class ) mo).getParent()).getName() + "." + 146 ((Class ) mo).getName(); 147 PClassMapping pm = 148 activeMapper.lookup(fullClass); 149 pm.init(activeMapper, mo); 151 } 152 } 153 PClassMapping pm = 155 activeMapper.lookup("examples.invoice.persistclass.Address"); 156 QueryLeaf ql = new JormExtent(((Class )pm.getMetaInfo()), 157 activeMapper, "Address", false); 158 System.out.println("Created new extent (all fields)"); 159 160 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 [] selected = {"name", "zip"}; 167 QueryLeaf q2 = new JormExtent(((Class )pm.getMetaInfo()), 168 activeMapper, selected, true, "Address"); 169 System.out.println("Created new extent (selected fields)"); 170 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 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 ) 184 e = ((ResourceException ) e).getLinkedException(); 185 else 186 e = null; 187 } while (e != null); 188 } 189 190 } 191 192 private void mapClasses() { 193 String name = "jdbc.default"; 194 PMapper m; 195 ConnectionFactory 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 e) { 232 System.out.println("\n\tEXCEPTION"); 233 e.printStackTrace(); 234 System.out.println(); 235 } 236 } 237 238 } 239 | Popular Tags |