KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > AttributeNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing;
23
24 import oracle.toplink.essentials.descriptors.ClassDescriptor;
25 import oracle.toplink.essentials.exceptions.EJBQLException;
26 import oracle.toplink.essentials.expressions.*;
27 import oracle.toplink.essentials.mappings.DatabaseMapping;
28
29 /**
30  * INTERNAL
31  * <p><b>Purpose</b>: Represent a attribute.
32  *
33  * <p><b>Responsibilities</b>:<ul>
34  * </ul>
35  */

36 public class AttributeNode extends Node {
37
38     /** The attribute name. */
39     private String JavaDoc name;
40
41     /** Flag indicating outer join */
42     private boolean outerJoin;
43
44     /** */
45     private boolean requiresCollectionAttribute;
46
47     /** */
48     private DatabaseMapping mapping;
49
50     /**
51      * Create a new AttributeNode
52      */

53     public AttributeNode() {
54         super();
55     }
56
57     /**
58      * Create a new AttributeNode with the passed name
59      * @param name the attribute name
60      */

61     public AttributeNode(String JavaDoc name) {
62         setAttributeName(name);
63     }
64
65     /**
66      * INTERNAL
67      * Validate the current node and calculates its type.
68      */

69     public void validate(ParseTreeContext context) {
70         // The type is calculated in the parent DotNode.
71
}
72
73     /** */
74     public Expression addToExpression(Expression parentExpression, GenerationContext context) {
75         if (isCollectionAttribute()) {
76             //special case for NOT MEMBER OF
77
if (context.hasMemberOfNode()) {
78                 return parentExpression.noneOf(name, new ExpressionBuilder().equal(context.getMemberOfNode().getLeftExpression()));
79             }
80             return outerJoin ? parentExpression.anyOfAllowingNone(name) :
81                 parentExpression.anyOf(name);
82         } else {
83             // check whether collection attribute is required
84
if (requiresCollectionAttribute()) {
85                 throw EJBQLException.invalidCollectionMemberDecl(name);
86             }
87
88             if (context.shouldUseOuterJoins() || isOuterJoin()) {
89                 return parentExpression.getAllowingNull(name);
90             } else {
91                 return parentExpression.get(name);
92             }
93         }
94     }
95
96     /**
97      * INTERNAL
98      * Is this node an AttributeNode
99      */

100     public boolean isAttributeNode() {
101         return true;
102     }
103
104     /** */
105     public String JavaDoc getAttributeName() {
106         return name;
107     }
108
109     /** */
110     public void setAttributeName(String JavaDoc name) {
111         this.name = name;
112     }
113
114     /** */
115     public boolean isOuterJoin() {
116         return outerJoin;
117     }
118
119     /** */
120     public void setOuterJoin(boolean outerJoin) {
121         this.outerJoin = outerJoin;
122     }
123
124     /** */
125     public boolean requiresCollectionAttribute() {
126         return requiresCollectionAttribute;
127     }
128
129     /** */
130     public void setRequiresCollectionAttribute(boolean requiresCollectionAttribute) {
131         this.requiresCollectionAttribute = requiresCollectionAttribute;
132     }
133
134     /** */
135     public DatabaseMapping getMapping() {
136         return mapping;
137     }
138
139     /** */
140     public void setMapping(DatabaseMapping mapping) {
141         this.mapping = mapping;
142     }
143
144     /** */
145     public boolean isCollectionAttribute() {
146         DatabaseMapping mapping = getMapping();
147         return (mapping != null) && mapping.isCollectionMapping();
148     }
149
150     /**
151      * resolveMapping: Answer the mapping which corresponds to my variableName.
152      */

153     public DatabaseMapping resolveMapping(GenerationContext context, Class JavaDoc ownerClass) {
154         ClassDescriptor descriptor = context.getSession().getDescriptor(ownerClass);
155         return (descriptor==null) ? null : descriptor.getMappingForAttributeName(getAttributeName());
156     }
157
158     /**
159      * resolveClass: Answer the class for the mapping associated with the my variableName in the ownerClass.
160      * Answer null if the node represents a mapping that doesn't exist
161      */

162     public Class JavaDoc resolveClass(GenerationContext context, Class JavaDoc ownerClass) {
163         DatabaseMapping mapping;
164
165         mapping = resolveMapping(context, ownerClass);
166
167         // if we are working with a direct-to-field, or the mapping's null,
168
// return the owner class
169
// Returning the ownerClass when the mapping is null delegates error handling
170
// to the query rather than me
171
if ((mapping == null) || (mapping.isDirectToFieldMapping())) {
172             return ownerClass;
173         }
174
175         ClassDescriptor descriptor = mapping.getReferenceDescriptor();
176         return (descriptor==null) ? null : descriptor.getJavaClass();
177         //return mapping.getReferenceDescriptor().getJavaClass();
178
}
179
180     /**
181      * INTERNAL
182      * Get the string representation of this node.
183      */

184     public String JavaDoc getAsString() {
185         return getAttributeName();
186     }
187 }
188
Popular Tags