*/
public DatabaseQuery translate(){
//TODO fetch joins
//find and translate subqueries.
ObjectLevelReadQuery query = null;
if (this.queryResult.equals(ResultType.ENTITY)){
query = new ReadAllQuery(this.queryType);
}else if (this.queryResult.equals(ResultType.PARTIAL)){
ReadAllQuery raq = new ReadAllQuery(this.queryType);
for (Selection selection: this.selection.getCompoundSelectionItems()){
raq.addPartialAttribute(((SelectionImpl)selection).currentNode);
}
query = raq;
}else{
if (this.roots == null || this.roots.isEmpty()) throw new IllegalStateException(ExceptionLocalization.buildMessage("CRITERIA_NO_ROOT_FOR_COMPOUND_QUERY"));
ReportQuery reportQuery = null;
if (this.queryResult.equals(ResultType.CONSTRUCTOR)){
reportQuery = new ReportQuery();
reportQuery.addConstructorReportItem(((ConstructorSelectionImpl)this.selection).translate());
reportQuery.setShouldReturnSingleAttribute(true);
}else {
if (this.queryResult.equals(ResultType.TUPLE)){
reportQuery = new TupleQuery(this.selection==null?new ArrayList():this.selection.getCompoundSelectionItems());
}else{
reportQuery = new ReportQuery();
reportQuery.setShouldReturnWithoutReportQueryResult(true);
}
if (this.selection != null){
if (this.selection.isCompoundSelection()){
for (Selection nested: this.selection.getCompoundSelectionItems()){
if (((SelectionImpl)nested).isCompoundSelection()){
reportQuery.addConstructorReportItem(((ConstructorSelectionImpl)nested).translate());
}else{
reportQuery.addItem(nested.getAlias(), ((SelectionImpl)nested).getCurrentNode());
}
}
}else{
reportQuery.addAttribute(this.selection.getAlias(), ((SelectionImpl)this.selection).getCurrentNode(), ((SelectionImpl)this.selection).getJavaType());
}
}
}
if (this.where != null && ((InternalSelection)this.where).getCurrentNode() != null){
reportQuery.setReferenceClass(((InternalSelection)this.where).getCurrentNode().getBuilder().getQueryClass());
reportQuery.setExpressionBuilder(((InternalSelection)this.where).getCurrentNode().getBuilder());
}else{
Root root = this.getRoots().iterator().next();
reportQuery.setReferenceClass(root.getJavaType());
reportQuery.setExpressionBuilder(((RootImpl)root).getCurrentNode().getBuilder());
}
query = reportQuery;
}
if (this.where != null){
query.setSelectionCriteria(((InternalSelection)this.where).getCurrentNode());
}
if (this.distinct){
query.setDistinctState(ObjectLevelReadQuery.USE_DISTINCT);
}
else{
query.setDistinctState(ObjectLevelReadQuery.DONT_USE_DISTINCT);
}
if (this.orderBy!= null && !this.orderBy.isEmpty()){
for (Order order: this.orderBy){
OrderImpl orderImpl = (OrderImpl)order;
org.eclipse.persistence.expressions.Expression orderExp = ((ExpressionImpl)orderImpl.getExpression()).getCurrentNode();
if (orderImpl.isAscending()){
orderExp = orderExp.ascending();
} else{
orderExp = orderExp.descending();
}
query.addOrdering(orderExp);
}
}
return query;
}