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