1 28 29 package com.caucho.ejb.gen; 30 31 import com.caucho.bytecode.JClass; 32 import com.caucho.bytecode.JMethod; 33 import com.caucho.config.ConfigException; 34 import com.caucho.ejb.cfg.EjbEntityBean; 35 import com.caucho.ejb.ql.EjbSelectQuery; 36 import com.caucho.java.JavaWriter; 37 import com.caucho.util.L10N; 38 39 import java.io.IOException ; 40 import java.util.Collection ; 41 42 45 public class AmberQueryMethod extends AbstractQueryMethod { 46 private static final L10N L = new L10N(AmberQueryMethod.class); 47 48 private EjbEntityBean _returnType; 49 private JMethod _method; 50 private String _contextClassName; 51 private String _prefix; 52 53 public AmberQueryMethod(EjbEntityBean type, 54 JMethod method, 55 String contextClassName, 56 String prefix, 57 EjbSelectQuery query) 58 throws ConfigException 59 { 60 super(type, method, query); 61 62 _returnType = type; 63 _method = method; 64 _contextClassName = contextClassName; 65 _prefix = prefix; 66 } 67 68 71 public JClass []getParameterTypes() 72 { 73 return _method.getParameterTypes(); 74 } 75 76 79 public JClass getReturnType() 80 { 81 return _method.getReturnType(); 82 } 83 84 89 public void generateCall(JavaWriter out, String []args) 90 throws IOException 91 { 92 out.print("com.caucho.ejb.xa.TransactionContext trans"); 93 out.println(" = _xaManager.beginSupports();"); 94 95 out.println("try {"); 96 out.pushDepth(); 97 98 generatePrepareQuery(out, args); 99 100 out.println("com.caucho.amber.query.ResultSetImpl rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();"); 101 out.println("java.util.ArrayList list = new java.util.ArrayList();"); 102 103 boolean isCollection = 104 _method.getReturnType().isAssignableTo(Collection .class); 105 106 if (isCollection) { 107 out.println("while (rs.next()) {"); 108 out.pushDepth(); 109 110 String beanClass = _returnType.getFullImplName(); 111 112 out.println("com.caucho.ejb.entity.EntityObject item = (com.caucho.ejb.entity.EntityObject) rs.getObject(1);"); 113 if ("RemoteHome".equals(_prefix)) 114 out.println("list.add(item.getEJBObject());"); 115 else if ("LocalHome".equals(_prefix)) 116 out.println("list.add(item);"); 117 else 118 out.println("list.add(item);"); 119 120 out.popDepth(); 121 out.println("}"); 122 out.println("rs.close();"); 123 out.println(); 124 out.println("return list;"); 125 } 126 else { 127 out.println("if (rs.next()) {"); 128 out.pushDepth(); 129 130 String beanClass = _returnType.getFullImplName(); 131 132 out.println("com.caucho.ejb.entity.EntityObject entity = (com.caucho.ejb.entity.EntityObject) rs.getObject(1);"); 133 out.println("rs.close();"); 134 out.println(); 135 136 String retType = getReturnType().getName(); 137 138 if ("RemoteHome".equals(_prefix)) 139 out.println("return (" + retType + ") entity.getEJBObject();"); 140 else if ("LocalHome".equals(_prefix)) 141 out.println("return (" + retType + ") entity;"); 142 else 143 throw new IllegalStateException (L.l("'{0}' is an unknown type", 144 _prefix)); 145 146 158 159 out.popDepth(); 160 out.println("}"); 161 out.println(); 162 out.println("throw new ObjectNotFoundException(\"no matching object found\");"); 163 } 164 165 176 177 out.popDepth(); 178 out.println("} catch (java.sql.SQLException e) {"); 179 out.println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);"); 180 out.println("} finally {"); 181 out.println(" trans.commit();"); 182 out.println("}"); 183 } 184 } 185 | Popular Tags |