1 29 30 package com.caucho.ejb.cfg; 31 32 import com.caucho.amber.manager.AmberPersistenceUnit; 33 import com.caucho.amber.type.Type; 34 import com.caucho.bytecode.JClass; 35 import com.caucho.bytecode.JMethod; 36 import com.caucho.config.ConfigException; 37 import com.caucho.ejb.EjbServerManager; 38 import com.caucho.ejb.gen.AbstractQueryMethod; 39 import com.caucho.ejb.gen.AmberSelectCollectionMethod; 40 import com.caucho.ejb.gen.AmberSelectMethod; 41 import com.caucho.ejb.gen.BeanAssembler; 42 import com.caucho.ejb.ql.EjbSelectQuery; 43 import com.caucho.ejb.ql.QLParser; 44 import com.caucho.java.gen.BaseMethod; 45 import com.caucho.util.L10N; 46 47 import javax.ejb.EJBLocalObject ; 48 import java.util.Collection ; 49 50 53 public class EjbAmberSelectMethod extends EjbBaseMethod { 54 private static final L10N L = new L10N(EjbAmberSelectMethod.class); 55 56 private String _query; 57 private String _location; 58 59 private boolean _queryLoadsBean; 60 61 68 public EjbAmberSelectMethod(EjbEntityBean bean, JMethod method, 69 String query, String location) 70 throws ConfigException 71 { 72 super(bean, method); 73 74 _query = query; 75 _location = location; 76 } 77 78 81 public void setQueryLoadsBean(boolean queryLoadsBean) 82 { 83 _queryLoadsBean = queryLoadsBean; 84 } 85 86 89 public BaseMethod assemble(BeanAssembler assembler, String fullClassName) 90 throws ConfigException 91 { 92 JMethod method = getMethod(); 93 94 QLParser parser = new QLParser((EjbEntityBean) getBean(), 95 method.getName(), method, 96 method.getReturnType()); 97 98 parser.setLocation(_location); 99 100 EjbSelectQuery query = (EjbSelectQuery) parser.parseQuery(_query); 101 102 String returnEJB = parser.getReturnEJB(); 103 JClass retType = method.getReturnType(); 104 105 EjbConfig ejbConfig = getBean().getConfig(); 106 EjbServerManager ejbManager = ejbConfig.getEJBManager(); 107 EjbEntityBean retBean = (EjbEntityBean) ejbConfig.getBeanConfig(returnEJB); 108 109 AmberPersistenceUnit amberPersistenceUnit = ejbManager.getAmberManager(); 110 111 Type amberType = null; 112 113 if (returnEJB != null) { 114 amberType = amberPersistenceUnit.getEntity(retBean.getAbstractSchemaName()); 115 116 if (amberType == null) 117 throw new NullPointerException ("No amber entity for " + returnEJB); 118 } 119 else if (retType.isAssignableTo(EJBLocalObject .class)) { 120 EjbEntityBean targetBean = getBean().getConfig().findEntityByLocal(retType); 121 122 JClass queryRetType = parser.getSelectExpr().getJavaType(); 123 124 if (queryRetType != null && 125 ! queryRetType.getName().equals("java.lang.Object") && 126 ! queryRetType.isAssignableTo(EJBLocalObject .class)) { 127 throw new ConfigException(L.l("Mismatched return type '{0}' in\n{1}", 128 retType.getName(), _query)); 129 } 130 131 amberType = amberPersistenceUnit.getEntity(targetBean.getAbstractSchemaName()); 132 } 133 else if (! retType.isAssignableTo(Collection .class)) 134 amberType = amberPersistenceUnit.createType(retType); 135 136 AbstractQueryMethod queryMethod; 137 138 if (retType.isAssignableTo(Collection .class)) 139 queryMethod = new AmberSelectCollectionMethod((EjbEntityBean) getBean(), 140 getMethod(), 141 fullClassName, 142 query); 143 else 144 queryMethod = new AmberSelectMethod((EjbEntityBean) getBean(), 145 method, 146 fullClassName, 147 query, 148 amberType); 149 150 queryMethod.setQueryLoadsBean(_queryLoadsBean); 151 152 return queryMethod; 153 } 154 } 155 | Popular Tags |