package org.jboss.ejb.plugins.cmp.jdbc.metadata;
import java.lang.reflect.Method;
import org.w3c.dom.Element;
import org.jboss.deployment.DeploymentException;
import org.jboss.metadata.MetaData;
public final class JDBCDeclaredQueryMetaData implements JDBCQueryMetaData
{
private final Method method;
private final String additionalColumns;
private final String from;
private final String where;
private final String order;
private final String other;
private final boolean distinct;
private final String ejbName;
private final String fieldName;
private final String alias;
private final JDBCReadAheadMetaData readAhead;
private final boolean resultTypeMappingLocal;
private final Class compiler;
private final boolean lazyResultSetLoading;
public JDBCDeclaredQueryMetaData(JDBCDeclaredQueryMetaData defaults,
JDBCReadAheadMetaData readAhead,
Class compiler,
boolean lazyResultSetLoading)
throws DeploymentException
{
this.method = defaults.getMethod();
this.readAhead = readAhead;
this.from = defaults.getFrom();
this.where = defaults.getWhere();
this.order = defaults.getOrder();
this.other = defaults.getOther();
this.resultTypeMappingLocal = defaults.isResultTypeMappingLocal();
this.distinct = defaults.isSelectDistinct();
this.ejbName = defaults.getEJBName();
this.fieldName = defaults.getFieldName();
this.alias = defaults.getAlias();
this.additionalColumns = defaults.getAdditionalColumns();
this.compiler = compiler;
this.lazyResultSetLoading = lazyResultSetLoading;
}
public JDBCDeclaredQueryMetaData(boolean isResultTypeMappingLocal,
Element queryElement,
Method method,
JDBCReadAheadMetaData readAhead,
Class compiler,
boolean lazyResultSetLoading)
throws DeploymentException
{
this.compiler = compiler;
this.lazyResultSetLoading = lazyResultSetLoading;
this.method = method;
this.readAhead = readAhead;
from = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "from"));
where = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "where"));
order = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "order"));
other = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "other"));
this.resultTypeMappingLocal = isResultTypeMappingLocal;
Element selectElement =
MetaData.getOptionalChild(queryElement, "select");
if(selectElement != null)
{
distinct =
(MetaData.getOptionalChild(selectElement, "distinct") != null);
if(method.getName().startsWith("ejbSelect"))
{
ejbName = MetaData.getUniqueChildContent(selectElement, "ejb-name");
fieldName = nullIfEmpty(MetaData.getOptionalChildContent(selectElement, "field-name"));
}
else
{
if(MetaData.getOptionalChild(selectElement, "ejb-name") != null)
{
throw new DeploymentException(
"The ejb-name element of declared-sql select is only " +
"allowed for ejbSelect queries."
);
}
if(MetaData.getOptionalChild(selectElement, "field-name") != null)
{
throw new DeploymentException(
"The field-name element of declared-sql select is only " +
"allowed for ejbSelect queries."
);
}
ejbName = null;
fieldName = null;
}
alias = nullIfEmpty(MetaData.getOptionalChildContent(selectElement, "alias"));
additionalColumns = nullIfEmpty(MetaData.getOptionalChildContent(selectElement, "additional-columns"));
}
else
{
if(method.getName().startsWith("ejbSelect"))
{
throw new DeploymentException(
"The select element of " +
"declared-sql is required for ejbSelect queries."
);
}
distinct = false;
ejbName = null;
fieldName = null;
alias = null;
additionalColumns = null;
}
}
public Method getMethod()
{
return method;
}
public boolean isResultTypeMappingLocal()
{
return resultTypeMappingLocal;
}
public JDBCReadAheadMetaData getReadAhead()
{
return readAhead;
}
public Class getQLCompilerClass()
{
return compiler;
}
public String getFrom()
{
return from;
}
public String getWhere()
{
return where;
}
public String getOrder()
{
return order;
}
public String getOther()
{
return other;
}
public boolean isSelectDistinct()
{
return distinct;
}
public String getEJBName()
{
return ejbName;
}
public String getFieldName()
{
return fieldName;
}
public String getAlias()
{
return alias;
}
public String getAdditionalColumns()
{
return additionalColumns;
}
public boolean isLazyResultSetLoading()
{
return lazyResultSetLoading;
}
public boolean equals(Object o)
{
if(o instanceof JDBCDeclaredQueryMetaData)
{
return ((JDBCDeclaredQueryMetaData) o).method.equals(method);
}
return false;
}
public int hashCode()
{
return method.hashCode();
}
public String toString()
{
return "[JDBCDeclaredQueryMetaData : method=" + method + "]";
}
private static String nullIfEmpty(String s)
{
if(s != null && s.trim().length() == 0)
{
s = null;
}
return s;
}
}