KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > ASTDbPath


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.exp.parser;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Map JavaDoc;
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 /**
31  * Path expression traversing DB relationships and attributes.
32  *
33  * @since 1.1
34  * @author Andrus Adamchik
35  */

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 JavaDoc value) {
46         super(ExpressionParserTreeConstants.JJTDBPATH);
47         setPath(value);
48     }
49
50     protected Object JavaDoc evaluateNode(Object JavaDoc o) throws Exception JavaDoc {
51         // TODO: implement resolving DB_PATH for DataObjects
52

53         if (o instanceof Entity) {
54             return evaluateEntityNode((Entity) o);
55         }
56
57         Map JavaDoc map = toMap(o);
58         return (map != null) ? map.get(path) : null;
59     }
60
61     protected Map JavaDoc toMap(Object JavaDoc o) {
62         if (o instanceof Map JavaDoc) {
63             return (Map JavaDoc) 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             // TODO: returns ObjectId snapshot for now.. should probably
72
// retrieve full snapshot...
73
ObjectId oid = persistent.getObjectId();
74             return (oid != null) ? oid.getIdSnapshot() : null;
75         }
76         else {
77             return null;
78         }
79     }
80
81     /**
82      * Creates a copy of this expression node, without copying children.
83      */

84     public Expression shallowCopy() {
85         ASTDbPath copy = new ASTDbPath(id);
86         copy.path = path;
87         return copy;
88     }
89
90     public void encodeAsString(PrintWriter JavaDoc 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