1 19 20 package org.apache.cayenne.exp.parser; 21 22 import java.io.PrintWriter ; 23 import java.util.Map ; 24 25 import org.apache.cayenne.ObjectId; 26 import org.apache.cayenne.Persistent; 27 import org.apache.cayenne.exp.Expression; 28 import org.apache.cayenne.map.Entity; 29 30 36 public class ASTDbPath extends ASTPath { 37 ASTDbPath(int id) { 38 super(id); 39 } 40 41 public ASTDbPath() { 42 super(ExpressionParserTreeConstants.JJTDBPATH); 43 } 44 45 public ASTDbPath(Object value) { 46 super(ExpressionParserTreeConstants.JJTDBPATH); 47 setPath(value); 48 } 49 50 protected Object evaluateNode(Object o) throws Exception { 51 53 if (o instanceof Entity) { 54 return evaluateEntityNode((Entity) o); 55 } 56 57 Map map = toMap(o); 58 return (map != null) ? map.get(path) : null; 59 } 60 61 protected Map toMap(Object o) { 62 if (o instanceof Map ) { 63 return (Map ) o; 64 } 65 else if (o instanceof ObjectId) { 66 return ((ObjectId) o).getIdSnapshot(); 67 } 68 else if (o instanceof Persistent) { 69 Persistent persistent = (Persistent) o; 70 71 ObjectId oid = persistent.getObjectId(); 74 return (oid != null) ? oid.getIdSnapshot() : null; 75 } 76 else { 77 return null; 78 } 79 } 80 81 84 public Expression shallowCopy() { 85 ASTDbPath copy = new ASTDbPath(id); 86 copy.path = path; 87 return copy; 88 } 89 90 public void encodeAsString(PrintWriter pw) { 91 pw.print("db:"); 92 pw.print(path); 93 } 94 95 public int getType() { 96 return Expression.DB_PATH; 97 } 98 } 99 | Popular Tags |