File: ReportItem.java

package info (click to toggle)
eclipselink 2.7.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,820 kB
  • sloc: java: 477,843; xml: 503; makefile: 21
file content (222 lines) | stat: -rw-r--r-- 8,621 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2019 IBM Corporation. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.internal.queries;

import org.eclipse.persistence.exceptions.*;
import org.eclipse.persistence.expressions.*;
import org.eclipse.persistence.mappings.*;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.queries.*;
import org.eclipse.persistence.internal.expressions.FunctionExpression;
import org.eclipse.persistence.internal.expressions.QueryKeyExpression;

/**
 * <b>Purpose</b>: represents an item requested (i.e. field for SELECT)
 *
 * @author Doug Clarke
 * @since 2.0
 */
public class ReportItem implements Cloneable, java.io.Serializable {

    /** Expression (partial) describing the attribute wanted */
    protected Expression attributeExpression;

    /** Name given for item, can be used to retrieve value from result. Useful if same field retrieved multiple times */
    protected String name;

    /** Mapping which relates field to attribute, used to convert value and determine reference descriptor */
    protected DatabaseMapping mapping;

    /** Descriptor for object result that is not based on an expression */
    protected ClassDescriptor descriptor;

    /** Result type for this report item. */
    protected Class resultType;
    /** Stores the Join information for this item */
    protected JoinedAttributeManager joinedAttributeManager;

    /** Stores the row index for this item, given multiple results and joins */
    protected int resultIndex;

    public ReportItem() {
        super();
    }

    public ReportItem(String name, Expression attributeExpression) {
        this();
        this.name = name;
        this.attributeExpression = attributeExpression;
    }

    public Object clone(){
        try{
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    public Expression getAttributeExpression() {
        return attributeExpression;
    }

    public void setAttributeExpression(Expression attributeExpression) {
        this.attributeExpression = attributeExpression;
    }

    public ClassDescriptor getDescriptor(){
        return this.descriptor;
    }

    /**
     * INTERNAL:
     * Set the list of expressions that represent elements that are joined because of their
     * mapping for this query.
     */
    public JoinedAttributeManager getJoinedAttributeManager() {
        if (this.joinedAttributeManager == null) {
            this.joinedAttributeManager = new JoinedAttributeManager();
        }
        return this.joinedAttributeManager;
    }

    public JoinedAttributeManager getJoinedAttributeManagerInternal(){
        return joinedAttributeManager;
    }

    /**
     * INTERNAL:
     * Return if any attributes are joined.
     * To avoid the initialization of the JoinedAttributeManager this should be first checked before accessing.
     */
    public boolean hasJoining() {
        return this.joinedAttributeManager != null;
    }

    public DatabaseMapping getMapping() {
        return mapping;
    }

    public String getName() {
        return name;
    }

    public int getResultIndex() {
        return resultIndex;
    }

    public Class getResultType() {
        return resultType;
    }

    /**
     * INTERNAL:
     * Looks up mapping for attribute during preExecute of ReportQuery
     */
    public void initialize(ReportQuery query) throws QueryException {
        if (this.mapping == null) {
            if (this.attributeExpression != null) {
                DatabaseMapping mapping = this.attributeExpression.getLeafMapping(query, query.getDescriptor(), query.getSession());
                if (mapping == null) {
                    //TODO: This feels like it should be set earlier during ReportItemBuild.visit. Should investigate.
                    if (this.attributeExpression.isExpressionBuilder()) {
                        Class resultClass = ((ExpressionBuilder)this.attributeExpression).getQueryClass();
                        if (resultClass == null) {
                            resultClass = query.getReferenceClass();
                            ((ExpressionBuilder)this.attributeExpression).setQueryClass(resultClass);
                        }
                        setDescriptor(query.getSession().getDescriptor(resultClass));
                        if (getDescriptor().hasInheritance()) {
                            ((ExpressionBuilder)this.attributeExpression).setShouldUseOuterJoinForMultitableInheritance(true);
                        }
                    }
                } else {
                    if (mapping.isAbstractDirectMapping() || this.attributeExpression.isMapEntryExpression() || mapping.isAggregateObjectMapping() || mapping.isDirectCollectionMapping()){
                            setMapping(mapping);
                    //Bug4942640  Widen the check to support collection mapping too
                    } else if (mapping.isForeignReferenceMapping()) {
                        // DirectCollectionMapping doesn't have reference descriptor
                        if(mapping.getReferenceDescriptor() != null) {
                            setDescriptor(mapping.getReferenceDescriptor());
                            if (getDescriptor().hasInheritance()){
                                ((QueryKeyExpression)this.attributeExpression).setShouldUseOuterJoinForMultitableInheritance(true);
                            }
                        }
                    } else {
                        throw QueryException.invalidExpressionForQueryItem(this.attributeExpression, query);
                    }
                }
                if (hasJoining()) {
                    this.joinedAttributeManager.setDescriptor(this.descriptor);
                    this.joinedAttributeManager.setBaseQuery(query);
                }
                if (getAttributeExpression() != null){
                    if (getAttributeExpression().getBuilder().wasQueryClassSetInternally()){
                        //rebuild if class was not set by user this ensures the query has the same base
                        this.attributeExpression = this.attributeExpression.rebuildOn(query.getExpressionBuilder());
                    }
                    if (hasJoining()) {
                        this.joinedAttributeManager.setBaseExpressionBuilder(this.attributeExpression.getBuilder());
                    }
                } else if (hasJoining()) {
                    this.joinedAttributeManager.setBaseExpressionBuilder(query.getExpressionBuilder());
                }
                if (hasJoining()) {
                    this.joinedAttributeManager.prepareJoinExpressions(query.getSession());
                    this.joinedAttributeManager.processJoinedMappings(query.getSession());
                    this.joinedAttributeManager.computeJoiningMappingQueries(query.getSession());
                }
            }
        }
    }

    public boolean isConstructorItem(){
        return false;
    }

    public void setDescriptor(ClassDescriptor descriptor){
        this.descriptor = descriptor;
    }


    public void setJoinedAttributeManager(JoinedAttributeManager joinManager){
        this.joinedAttributeManager = joinManager;
    }

    public void setMapping(DatabaseMapping mapping) {
        this.mapping = mapping;
    }

    public void setResultIndex(int resultIndex) {
        this.resultIndex = resultIndex;
        if (hasJoining()) {
            getJoinedAttributeManager().setParentResultIndex(resultIndex);
        }
    }

    public void setResultType(Class resultType) {
        this.resultType = resultType;

        // Set it on the attribute expression as well if it is a function.
        if (getAttributeExpression()!=null && getAttributeExpression().isFunctionExpression()) {
            ((FunctionExpression) getAttributeExpression()).setResultType(resultType);
        }
    }

    public String toString() {
        return "ReportQueryItem(" + getName() + " -> " + getAttributeExpression() + ")";
    }
}