1 25 package org.objectweb.jonas_ejb.lib; 26 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 import org.objectweb.jonas_ejb.container.TraceEjb; 32 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb2; 33 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc; 34 import org.objectweb.jonas_ejb.deployment.api.MethodCmp2Desc; 35 import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL; 36 import org.objectweb.jorm.api.PMapper; 37 import org.objectweb.jorm.metainfo.api.Manager; 38 import org.objectweb.medor.api.Field; 39 import org.objectweb.medor.eval.api.QueryEvaluator; 40 import org.objectweb.medor.eval.lib.MedorEvaluator; 41 import org.objectweb.medor.expression.api.Expression; 42 import org.objectweb.medor.optim.api.QueryTransformer; 43 import org.objectweb.medor.query.api.OrderField; 44 import org.objectweb.medor.query.api.QueryTree; 45 import org.objectweb.medor.query.api.QueryTreeField; 46 import org.objectweb.medor.query.jorm.api.JormExtent; 47 import org.objectweb.medor.query.jorm.lib.ClassExtent; 48 import org.objectweb.medor.query.jorm.lib.JormQueryTreeHelper; 49 import org.objectweb.medor.query.jorm.lib.QueryBuilder; 50 import org.objectweb.medor.query.lib.QueryTreePrinter; 51 import org.objectweb.medor.query.lib.SelectProject; 52 import org.objectweb.util.monolog.api.BasicLevel; 53 54 55 64 public class EjbqlQueryTreeHolder { 65 66 QueryTree queryTree = null; 67 QueryTransformer queryOptimizer = null; 68 QueryEvaluator optimizedQuery = null; 69 Field resField = null; 70 EjbqlLimiterRange[] limiterRanges = null; 71 72 private String beanName; 73 private String methodName; 74 75 79 private int prefetchIdentifierIndex; 80 81 82 89 public EjbqlQueryTreeHolder(MethodCmp2Desc mDesc, ASTEJBQL ejbql, PMapper mapper) 90 throws Exception { 91 92 EntityCmp2Desc eDesc = (EntityCmp2Desc) mDesc.getBeanDesc(); 93 DeploymentDescEjb2 dd = (DeploymentDescEjb2) eDesc.getDeploymentDesc(); 94 Manager jormManager = dd.getJormManager(); 95 96 QueryBuilder qb = new QueryBuilder(); 98 Map fields = (new EjbqlVariableVisitor(ejbql, dd, qb)).getFields(); 99 100 SelectProject sp = new SelectProject(""); 102 if (mDesc.getPrefetch() && (mapper != null)) { 103 ClassExtent ce = null; 108 QueryTree qt = null; 109 String jormClassName = ((EntityCmp2Desc) mDesc.getBeanDesc()).getJormClassName(); 110 Iterator itFields = fields.values().iterator(); 111 boolean found = false; 112 while (itFields.hasNext() && !found) { 113 QueryTree cqt = ((QueryTreeField) itFields.next()).getQueryTree(); 114 Collection extents = JormQueryTreeHelper.getJormExtents(cqt); 115 for (Iterator it = extents.iterator(); it.hasNext() && !found;) { 116 JormExtent je = (JormExtent) it.next(); 117 if (jormClassName.equals(je.getJormName())) { 118 found = true; 119 ce = (ClassExtent) je; 120 qt = cqt; 121 } 122 } 123 } 124 if ((ce == null) && (qt == null)) { 125 throw new Error ("EjbqlQueryTreeHolder: cannot do the prefetch !!!"); 126 } 127 setMapper(qt, mapper); 128 JormQueryTreeHelper.addPrefetchFields(ce, qt, sp); 131 prefetchIdentifierIndex = sp.getTupleStructure().getSize() + 1; 132 } 133 135 EjbqlSelectVisitor selectVisitor = new EjbqlSelectVisitor(ejbql, fields, sp); 137 138 Expression qf = new EjbqlQueryFilterVisitor 140 (mapper, fields, mDesc.getMethod().getParameterTypes(), ejbql, qb).getQueryFilter(); 141 sp.setQueryFilter(qf); 142 143 OrderField[] ofs = new EjbqlOrderByVisitor(ejbql, fields).getOrderFields(); 145 if (ofs.length > 0) { 146 sp.setOrderBy(ofs); 147 } 148 149 limiterRanges = new EjbqlLimitVisitor(ejbql, mDesc.getMethod().getParameterTypes()).getLimiterRanges(); 151 152 queryTree = selectVisitor.getQueryTree(); 153 154 setMapper(queryTree, mapper); 155 156 resField = queryTree.getTupleStructure().getField(queryTree.getTupleStructure().getSize()); 159 160 beanName = eDesc.getJormClassName(); 162 methodName = mDesc.getMethod().getName(); 163 } 164 165 private void setMapper(QueryTree tree, PMapper mapper) { 166 if (mapper != null) { 168 Collection extents = JormQueryTreeHelper.getJormExtents(tree); 169 for (Iterator it = extents.iterator(); it.hasNext();) { 170 JormExtent je = (JormExtent) it.next(); 171 je.setPMapper(mapper, RdbMappingBuilder.getProjectName()); 172 } 173 } 174 } 175 176 public int getPrefetchIndex() { 177 return prefetchIdentifierIndex; 178 } 179 180 183 public QueryTree getQueryTree() { 184 return queryTree; 185 } 186 187 190 public void setQueryOptimizer(QueryTransformer qtf) { 191 optimizedQuery = null; 192 queryOptimizer = qtf; 193 } 194 195 198 public QueryTransformer getQueryOptimizer() { 199 return queryOptimizer; 200 } 201 202 205 public Field getResField() { 206 return resField; 207 } 208 212 public EjbqlLimiterRange[] getLimiterRanges() { 213 return limiterRanges; 214 } 215 216 221 public QueryEvaluator getOptimizedQueryTree() throws Exception { 222 if (optimizedQuery == null) { 223 if (TraceEjb.isDebugQuery()) { 224 TraceEjb.query.log(BasicLevel.DEBUG, 225 "Initial QueryTree of the method " + methodName 226 + " of the bean " + beanName); 227 QueryTreePrinter.printQueryTree(queryTree, TraceEjb.query, BasicLevel.DEBUG); 228 } 229 QueryTree qt = queryOptimizer.transform(queryTree); 230 if (TraceEjb.isDebugQuery()) { 231 TraceEjb.query.log(BasicLevel.DEBUG, 232 "Optimized QueryTree of the method " + methodName 233 + " of the bean " + beanName); 234 QueryTreePrinter.printQueryTree(qt, TraceEjb.query, BasicLevel.DEBUG); 235 } 236 optimizedQuery = new MedorEvaluator(qt, 0); 237 } 238 return optimizedQuery; 239 } 240 241 } 242 | Popular Tags |