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.MapClass; 39 import com.caucho.ejb.gen.ViewClass; 40 import com.caucho.java.JavaWriter; 41 import com.caucho.java.gen.BaseMethod; 42 import com.caucho.util.L10N; 43 44 import java.io.IOException ; 45 import java.util.ArrayList ; 46 47 50 public class EjbMapMethod extends CmpGetter { 51 private static final L10N L = new L10N(EjbOneToManyMethod.class); 52 53 private CmrMap _map; 54 55 62 public EjbMapMethod(EjbView view, 63 JMethod apiMethod, JMethod implMethod, 64 CmrMap map) 65 { 66 super(view, apiMethod, implMethod); 67 68 _map = map; 69 } 70 71 74 public BaseMethod assemble(ViewClass viewAssembler, String fullClassName) 75 { 76 BaseMethod method = viewAssembler.createBusinessMethod(this); 77 78 method.setCall(assembleCallChain(method.getCall())); 79 80 return method; 81 } 82 83 86 public void assembleBean(BeanAssembler beanAssembler, String fullClassName) 87 throws ConfigException 88 { 89 String mapName = _map.getName() + "_Map"; 90 91 MapClass map = new MapClass(_map, mapName); 92 93 beanAssembler.addComponent(map); 94 95 beanAssembler.addMethod(new BeanMethod(getImplMethod())); 96 } 97 98 class BeanMethod extends BaseMethod { 99 BeanMethod(JMethod method) 100 { 101 super(method); 102 } 103 104 110 protected void generateCall(JavaWriter out, String []args) 111 throws IOException 112 { 113 out.println("try {"); 114 out.pushDepth(); 115 116 out.println("com.caucho.amber.AmberQuery query;"); 117 118 String abstractSchema = _map.getTargetBean().getAbstractSchemaName(); 119 String id = _map.getIdName(); 120 IdField index = null; 121 122 EntityType sourceType = _map.getBean().getEntityType(); 123 for (IdField key : sourceType.getId().getKeys()) { 124 if (key.getName().equals(id)) { 125 } 126 else { 127 index = key; 128 } 129 } 130 131 out.print("String sql = \"SELECT o." + _map.getIndexName() + ", o"); 132 out.print(" FROM " + abstractSchema + " o"); 133 out.print(" WHERE "); 134 135 EntityType targetType = _map.getBean().getEntityType(); 136 ArrayList <IdField> keys = targetType.getId().getKeys(); 137 138 for (int i = 0; i < keys.size(); i++) { 139 IdField key = keys.get(i); 140 141 if (i != 0) 142 out.print(" AND "); 143 144 out.print("o." + id + "." + key.getName() + "=?" + (i + 1)); 145 } 146 147 out.println("\";"); 148 149 out.println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);"); 150 151 EjbConfig config = _map.getBean().getConfig(); 152 153 out.println("int index = 1;"); 154 for (int i = 0; i < keys.size(); i++) { 155 IdField key = keys.get(i); 156 157 AbstractQueryMethod.generateSetParameter(out, 158 config, 159 key.getJavaType(), 160 "query", 161 key.generateGet("this")); 162 } 163 164 String mapName = _map.getName() + "_Map"; 165 166 out.print(mapName + " map = "); 167 168 out.println("new " + mapName + "(this, query);"); 169 170 out.println("return map;"); 171 172 out.popDepth(); 173 out.println("} catch (RuntimeException e) {"); 174 out.println(" throw e;"); 175 out.println("} catch (java.sql.SQLException e) {"); 176 out.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);"); 177 out.println("}"); 178 } 179 } 180 } 181 | Popular Tags |