oracle.toplink.essentials.internal.parsing.ConstructorNode Maven / Gradle / Ivy
The newest version!
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2006, Oracle. All rights reserved.
package oracle.toplink.essentials.internal.parsing;
// Java imports
import java.util.*;
import oracle.toplink.essentials.exceptions.EJBQLException;
import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
import oracle.toplink.essentials.queryframework.ReportQuery;
/**
* INTERNAL
* Purpose: Represent a constructor node (NEW)
*
Responsibilities:
* - Generate the correct expression for a constructor
*
*/
public class ConstructorNode extends Node {
/** The name of the constructor class. */
private String className = null;
/** The list of constructor call argument nodes */
public List constructorItems = new ArrayList();
/**
* Return a new ConstructorNode
*/
public ConstructorNode(String className) {
this.className = className;
}
/**
* INTERNAL
* Apply this node to the passed query
*/
public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) {
if (theQuery instanceof ReportQuery) {
SelectGenerationContext selectContext = (SelectGenerationContext)context;
ReportQuery reportQuery = (ReportQuery)theQuery;
reportQuery.beginAddingConstructorArguments(getConstructorClass());
for (Iterator i = constructorItems.iterator(); i.hasNext();) {
Node node = (Node)i.next();
if (selectingRelationshipField(node, context)) {
selectContext.useOuterJoins();
}
node.applyToQuery(reportQuery, context);
selectContext.dontUseOuterJoins();
}
reportQuery.endAddingToConstructorItem();
}
}
/**
* INTERNAL
* Validate node and calculate its type.
*/
public void validate(ParseTreeContext context) {
for (Iterator i = constructorItems.iterator(); i.hasNext();) {
Node item = (Node)i.next();
item.validate(context);
}
// Resolve constructor class
TypeHelper typeHelper = context.getTypeHelper();
Object type = typeHelper.resolveTypeName(className);
if (type == null) {
String name = className;
// check for inner classes
int index = name.lastIndexOf('.');
if (index != -1) {
name = name.substring(0, index) + '$' + name.substring(index+1);
type = typeHelper.resolveTypeName(name);
}
}
setType(type);
}
/**
* INTERNAL
* Is this node a ConstructorNode
*/
public boolean isConstructorNode() {
return true;
}
/**
* INTERNAL
* Add an Order By Item to this node
*/
public void addConstructorItem(Object theNode) {
constructorItems.add(theNode);
}
/**
* INTERNAL
* Set the list of constructor items of this node.
*/
public void setConstructorItems(List items) {
this.constructorItems = items;
}
/**
* INTERNAL
* Get the list of constructor items of this node.
*/
public List getConstructorItems() {
return this.constructorItems;
}
/**
* Check the specifid constructor class and return its class instance.
* @exception EJBQLException if the specified constructor class could not
* be found.
*/
private Class getConstructorClass() {
Object type = getType();
if (type == null) {
throw EJBQLException.constructorClassNotFound(className);
}
return (Class)type;
}
/**
* INTERNAL
*/
private boolean selectingRelationshipField(Node node, GenerationContext context) {
if ((node == null) || !node.isDotNode()) {
return false;
}
return !((DotNode)node).endsWithDirectToField(context);
}
/**
* INTERNAL
* Get the string representation of this node.
*/
public String getAsString() {
StringBuffer repr = new StringBuffer();
repr.append("NEW ").append(className);
repr.append("(");
for (Iterator i = constructorItems.iterator(); i.hasNext();) {
Node node = (Node)i.next();
repr.append(node.getAsString());
if (i.hasNext()) {
repr.append(", ");
}
}
repr.append(")");
return repr.toString();
}
}