1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import oracle.toplink.essentials.exceptions.EJBQLException; 25 import oracle.toplink.essentials.expressions.*; 26 import oracle.toplink.essentials.internal.expressions.*; 27 import oracle.toplink.essentials.mappings.DatabaseMapping; 28 import oracle.toplink.essentials.mappings.DirectToFieldMapping; 29 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery; 30 import oracle.toplink.essentials.queryframework.ReportQuery; 31 32 41 public class DotNode extends LogicalOperatorNode { 42 43 private Object enumConstant; 44 45 49 public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) { 50 if (theQuery.isReportQuery()){ 51 ReportQuery reportQuery = (ReportQuery)theQuery; 52 reportQuery.addAttribute(resolveAttribute(), generateExpression(context)); 53 reportQuery.dontRetrievePrimaryKeys(); 54 } 55 } 56 57 62 public void validate(ParseTreeContext context) { 63 TypeHelper typeHelper = context.getTypeHelper(); 64 Node leftMost = getLeftMostNode(); 66 if (isDeclaredVariable(leftMost, context)) { 67 Object attributeType = null; 68 left.validate(context); 69 Object leftType = left.getType(); 70 if (!typeHelper.isEntityClass(leftType) && 71 !typeHelper.isEnumType(leftType)) { 72 throw EJBQLException.invalidNavigation( 73 this.getAsString(), left.getAsString(), 74 typeHelper.getTypeName(leftType)); 75 } else { 76 String name = ((AttributeNode)right).getAttributeName(); 77 attributeType = typeHelper.resolveAttribute(leftType, name); 78 if (attributeType == null) { 79 throw EJBQLException.unknownAttribute( 81 name, typeHelper.getTypeName(leftType)); 82 } 83 } 84 setType(attributeType); 85 right.setType(attributeType); 86 } else { 87 String typeName = left.getAsString(); 89 String name = ((AttributeNode)right).getAttributeName(); 90 Object type = resolveEnumTypeName(typeName, typeHelper); 91 if ((type != null) && typeHelper.isEnumType(type)) { 92 enumConstant = typeHelper.resolveEnumConstant(type, name); 93 if (enumConstant == null) { 94 throw EJBQLException.invalidEnumLiteral(typeName, name); 95 } 96 } 97 } 98 } 99 100 101 private boolean isDeclaredVariable(Node node, ParseTreeContext context) { 102 if (node.isVariableNode()) { 103 String name = ((VariableNode)node).getCanonicalVariableName(); 104 return context.isVariable(name); 105 } 106 return false; 107 } 108 109 115 public Expression generateExpression(GenerationContext context) { 116 Node left = getLeft(); 117 Node right = getRight(); 118 119 if (enumConstant != null) { 120 return new ConstantExpression(enumConstant, new ExpressionBuilder()); 122 } else { 123 Expression whereClause = getLeft().generateExpression(context); 125 126 if (right.isAttributeNode()) { 128 ((AttributeNode)right).setMapping(resolveMapping(context)); 129 } 130 131 whereClause = right.addToExpression(whereClause, context); 133 134 return whereClause; 136 } 137 } 138 139 143 public boolean isDotNode() { 144 return true; 145 } 146 147 153 public boolean endsWithDirectToField(GenerationContext context) { 154 DatabaseMapping mapping = resolveMapping(context); 155 return (mapping != null) && mapping.isDirectToFieldMapping(); 156 } 157 158 162 public Class getTypeOfDirectToField(GenerationContext context) { 163 DatabaseMapping mapping = resolveMapping(context); 164 if ((mapping != null) && mapping.isDirectToFieldMapping()) { 165 return ((DirectToFieldMapping)mapping).getAttributeClassification(); 166 } 167 return null; 168 } 169 170 176 public boolean endsWithCollectionField(GenerationContext context) { 177 DatabaseMapping mapping = resolveMapping(context); 178 return (mapping != null) && mapping.isCollectionMapping(); 179 } 180 181 186 public String resolveAttribute() { 187 return ((AttributeNode)getRight()).getAttributeName(); 188 } 189 190 194 public DatabaseMapping resolveMapping(GenerationContext context) { 195 Class leftClass = getLeft().resolveClass(context); 196 return getRight().resolveMapping(context, leftClass); 197 } 198 199 202 public Class resolveClass(GenerationContext context) { 203 Class leftClass = getLeft().resolveClass(context); 204 return getRight().resolveClass(context, leftClass); 205 } 206 207 211 public String getAsString() { 212 return left.getAsString() + "." + right.getAsString(); 213 } 214 215 219 private Node getLeftMostNode() { 220 return left.isDotNode() ? ((DotNode)left).getLeftMostNode() : left; 221 } 222 223 228 private Object resolveEnumTypeName(String name, TypeHelper helper) { 229 Object type = helper.resolveTypeName(name); 230 if (type == null) { 231 int index = name.lastIndexOf('.'); 233 if (index != -1) { 234 name = name.substring(0, index) + '$' + name.substring(index+1); 235 type = helper.resolveTypeName(name); 236 } 237 } 238 return type; 239 } 240 } 241 | Popular Tags |