1 29 30 package com.caucho.ejb.cfg; 31 32 import com.caucho.amber.field.IdField; 33 import com.caucho.amber.type.EntityType; 34 import com.caucho.bytecode.JMethod; 35 import com.caucho.config.ConfigException; 36 import com.caucho.ejb.gen.AbstractQueryMethod; 37 import com.caucho.ejb.gen.BeanAssembler; 38 import com.caucho.ejb.gen.CollectionClass; 39 import com.caucho.java.JavaWriter; 40 import com.caucho.java.gen.BaseMethod; 41 import com.caucho.util.L10N; 42 43 import java.io.IOException ; 44 import java.util.ArrayList ; 45 46 49 public class EjbOneToManyMethod extends CmpGetter { 50 private static final L10N L = new L10N(EjbOneToManyMethod.class); 51 52 private CmrOneToMany _oneToMany; 53 54 61 public EjbOneToManyMethod(EjbView view, 62 JMethod apiMethod, JMethod implMethod, 63 CmrOneToMany oneToMany) 64 { 65 super(view, apiMethod, implMethod); 66 67 _oneToMany = oneToMany; 68 } 69 70 73 public void assembleBean(BeanAssembler beanAssembler, String fullClassName) 74 throws ConfigException 75 { 76 String listName = _oneToMany.getName() + "_List"; 77 78 CollectionClass list = new CollectionClass(_oneToMany, listName); 79 80 beanAssembler.addComponent(list); 81 82 beanAssembler.addMethod(new BeanMethod(getImplMethod())); 83 } 84 85 class BeanMethod extends BaseMethod { 86 BeanMethod(JMethod method) 87 { 88 super(method); 89 } 90 91 96 public void generate(JavaWriter out) 97 throws IOException 98 { 99 String listName = _oneToMany.getName() + "_List"; 100 String varName = "__caucho_field_" + _oneToMany.getName(); 101 102 out.println(listName + " " + varName + ";"); 103 out.println(); 104 105 super.generate(out); 106 } 107 108 114 protected void generateCall(JavaWriter out, String []args) 115 throws IOException 116 { 117 String listName = _oneToMany.getName() + "_List"; 118 String varName = "__caucho_field_" + _oneToMany.getName(); 119 120 out.println("if (" + varName + " != null) {"); 121 out.println(" " + varName + ".__caucho_init(_ejb_trans.getAmberConnection());"); 122 out.println(" return " + varName + ";"); 123 out.println("}"); 124 125 out.println(); 126 out.println("try {"); 127 out.pushDepth(); 128 129 out.println("com.caucho.amber.AmberQuery query;"); 130 131 String abstractSchema = _oneToMany.getBean().getAbstractSchemaName(); 132 133 out.print("String sql = \"SELECT o." + _oneToMany.getName()); 134 out.print(" FROM " + abstractSchema + " o"); 135 out.print(" WHERE "); 136 137 EntityType type = _oneToMany.getBean().getEntityType(); 138 ArrayList <IdField> keys = type.getId().getKeys(); 139 140 for (int i = 0; i < keys.size(); i++) { 141 IdField key = keys.get(i); 142 143 if (i != 0) 144 out.print(" AND "); 145 146 out.print("o." + key.getName() + "=?" + (i + 1)); 147 } 148 149 out.println("\";"); 150 151 out.println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);"); 152 153 EjbConfig config = _oneToMany.getBean().getConfig(); 154 155 out.println("int index = 1;"); 156 for (int i = 0; i < keys.size(); i++) { 157 IdField key = keys.get(i); 158 159 AbstractQueryMethod.generateSetParameter(out, 160 config, 161 key.getJavaType(), 162 "query", 163 key.generateGet("super")); 164 } 165 166 out.println(varName + " = new " + listName + "(this, query);"); 167 168 out.println("return " + varName + ";"); 169 170 out.popDepth(); 171 out.println("} catch (RuntimeException e) {"); 172 out.println(" throw e;"); 173 out.println("} catch (java.sql.SQLException e) {"); 174 out.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);"); 175 out.println("}"); 176 } 177 } 178 } 179 | Popular Tags |