KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > queryframework > ReportItem


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.queryframework;
23
24 import java.util.List JavaDoc;
25
26 import oracle.toplink.essentials.exceptions.*;
27 import oracle.toplink.essentials.expressions.*;
28 import oracle.toplink.essentials.mappings.*;
29 import oracle.toplink.essentials.descriptors.ClassDescriptor;
30 import oracle.toplink.essentials.queryframework.*;
31 import oracle.toplink.essentials.internal.expressions.FunctionExpression;
32 import oracle.toplink.essentials.internal.expressions.QueryKeyExpression;
33
34 /**
35  * <b>Purpose</b>: represents an item requested (i.e. field for SELECT)
36  *
37  * @author Doug Clarke
38  * @since 2.0
39  */

40 public class ReportItem implements java.io.Serializable JavaDoc {
41
42     /** Expression (partial) describing the attribute wanted */
43     protected Expression attributeExpression;
44
45     /** Name given for item, can be used to retieve value from result. Useful if same field retrieved multipe times */
46     protected String JavaDoc name;
47
48     /** Mapping which relates field to attribute, used to convert value and determine reference descriptor */
49     protected DatabaseMapping mapping;
50     
51     /** Desriptor for object result that is not based on an expression */
52     protected ClassDescriptor descriptor;
53     
54     /** Result type for this report item. */
55     protected Class JavaDoc resultType;
56     
57     /** Stores the Join information for this item */
58     protected JoinedAttributeManager joinManager;
59     
60     /** Stores the row index for this item, given multiple results and joins */
61     protected int resultIndex;
62     
63     public ReportItem() {
64         super();
65     }
66
67     public ReportItem(String JavaDoc name, Expression attributeExpression) {
68         this.name = name;
69         this.attributeExpression = attributeExpression;
70         this.joinManager = new JoinedAttributeManager();
71     }
72     
73     public Expression getAttributeExpression() {
74         return attributeExpression;
75     }
76
77     public ClassDescriptor getDescriptor(){
78         return this.descriptor;
79     }
80     
81     /**
82      * INTERNAL:
83      * Set the list of expressions that represent elements that are joined because of their
84      * mapping for this query.
85      */

86     public JoinedAttributeManager getJoinedAttributeManager() {
87         return this.joinManager;
88     }
89
90     public DatabaseMapping getMapping() {
91         return mapping;
92     }
93
94     public String JavaDoc getName() {
95         return name;
96     }
97     
98     public int getResultIndex() {
99         return resultIndex;
100     }
101
102     public Class JavaDoc getResultType() {
103         return resultType;
104     }
105
106     /**
107      * INTERNAL:
108      * Looks up mapping for attribute during preExecute of ReportQuery
109      */

110     public void initialize(ReportQuery query) throws QueryException {
111         if (getMapping() == null) {
112             DatabaseMapping mapping = query.getLeafMappingFor(getAttributeExpression(), query.getDescriptor());
113             if (mapping == null){
114                 if (getAttributeExpression() != null){
115                     if (getAttributeExpression().isExpressionBuilder()) {
116                         Class JavaDoc resultClass = ((ExpressionBuilder)getAttributeExpression()).getQueryClass();
117                         if (resultClass == null){
118                             resultClass = query.getReferenceClass();
119                         }
120                         setDescriptor(query.getSession().getDescriptor(resultClass));
121                         if (getDescriptor().hasInheritance()){
122                             ((ExpressionBuilder)getAttributeExpression()).setShouldUseOuterJoinForMultitableInheritance(true);
123                         }
124                     }
125                 }
126             }else{
127                 //Bug4942640 Widen the check to support collection mapping too
128
if (mapping.isForeignReferenceMapping()){
129                     setDescriptor(mapping.getReferenceDescriptor());
130                     if (getDescriptor().hasInheritance()){
131                         ((QueryKeyExpression)getAttributeExpression()).setShouldUseOuterJoinForMultitableInheritance(true);
132                     }
133                 } else if (mapping.isAbstractDirectMapping()){
134                     setMapping((DatabaseMapping)mapping);
135                 } else {
136                     throw QueryException.invalidExpressionForQueryItem(getAttributeExpression(), query);
137                 }
138             }
139             this.joinManager.setDescriptor(this.descriptor);
140             this.joinManager.setBaseQuery(query);
141             if (getAttributeExpression() != null){
142                 if (getAttributeExpression().getBuilder().wasQueryClassSetInternally()){
143                     //rebuild if class was not set by user this ensures the query has the same base
144
this.attributeExpression = getAttributeExpression().rebuildOn(query.getExpressionBuilder());
145                 }
146                 this.joinManager.setBaseExpressionBuilder(this.attributeExpression.getBuilder());
147             }else{
148                 this.joinManager.setBaseExpressionBuilder(query.getExpressionBuilder());
149             }
150             this.joinManager.prepareJoinExpressions(query.getSession());
151         }
152     }
153
154     public boolean isContructorItem(){
155         return false;
156     }
157
158     /**
159      * @return true if there is no expression (null)
160      */

161     public boolean isPlaceHolder() {
162         return getAttributeExpression() == null;
163     }
164
165     public void setDescriptor(ClassDescriptor descriptor){
166         this.descriptor = descriptor;
167     }
168     
169     public void setMapping(DatabaseMapping mapping) {
170         this.mapping = mapping;
171     }
172     
173     public void setResultIndex(int resultIndex) {
174         this.resultIndex = resultIndex;
175         this.joinManager.setParentResultIndex(resultIndex);
176     }
177
178     public void setResultType(Class JavaDoc resultType) {
179         this.resultType = resultType;
180     
181         // Set it on the attribute expression as well if it is a function.
182
if (getAttributeExpression()!=null && getAttributeExpression().isFunctionExpression()) {
183             ((FunctionExpression) getAttributeExpression()).setResultType(resultType);
184         }
185     }
186
187     public String JavaDoc toString() {
188         return "ReportQueryItem(" + getName() + " -> " + getAttributeExpression() + ")";
189     }
190
191 }
192
Popular Tags