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 EjbManyToManyMethod extends CmpGetter { 50 private static final L10N L = new L10N(EjbManyToManyMethod.class); 51 52 private CmrManyToMany _manyToMany; 53 54 61 public EjbManyToManyMethod(EjbView view, 62 JMethod apiMethod, JMethod implMethod, 63 CmrManyToMany manyToMany) 64 { 65 super(view, apiMethod, implMethod); 66 67 _manyToMany = manyToMany; 68 } 69 70 73 public void assembleBean(BeanAssembler beanAssembler, String fullClassName) 74 throws ConfigException 75 { 76 String listName = _manyToMany.getName() + "_List"; 77 78 CollectionClass list = new CollectionClass(_manyToMany, 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 97 protected void generateCall(JavaWriter out, String []args) 98 throws IOException 99 { 100 out.println("try {"); 101 out.pushDepth(); 102 103 out.println("com.caucho.amber.AmberQuery query;"); 104 105 String abstractSchema = _manyToMany.getBean().getAbstractSchemaName(); 106 107 out.print("String sql = \"SELECT o." + _manyToMany.getName()); 108 out.print(" FROM " + abstractSchema + " o"); 109 out.print(" WHERE "); 110 111 EntityType type = _manyToMany.getBean().getEntityType(); 112 ArrayList <IdField> keys = type.getId().getKeys(); 113 114 for (int i = 0; i < keys.size(); i++) { 115 IdField key = keys.get(i); 116 117 if (i != 0) 118 out.print(" AND "); 119 120 out.print("o." + key.getName() + "=?" + (i + 1)); 121 } 122 123 out.println("\";"); 124 125 out.println("query = _ejb_trans.getAmberConnection().prepareQuery(sql);"); 126 127 EjbConfig config = _manyToMany.getBean().getConfig(); 128 129 out.println("int index = 1;"); 130 for (int i = 0; i < keys.size(); i++) { 131 IdField key = keys.get(i); 132 133 AbstractQueryMethod.generateSetParameter(out, 134 config, 135 key.getJavaType(), 136 "query", 137 key.generateGet("this")); 138 } 139 140 String listName = _manyToMany.getName() + "_List"; 141 142 out.print(listName + " list = "); 143 144 out.println("new " + listName + "(this, query);"); 145 146 out.println("return list;"); 147 148 out.popDepth(); 149 out.println("} catch (RuntimeException e) {"); 150 out.println(" throw e;"); 151 out.println("} catch (java.sql.SQLException e) {"); 152 out.println(" throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e);"); 153 out.println("}"); 154 } 155 } 156 } 157 | Popular Tags |