assertIsOpen();
// Throw exception on incomplete input
if (queryName == null)
{
throw new JDOUserException(LOCALISER_JDO.msg("011005", queryName, cls));
}
// Find the Query for the specified class
ClassLoaderResolver clr = om.getClassLoaderResolver();
QueryMetaData qmd = om.getMetaDataManager().getMetaDataForQuery(cls, clr, queryName);
if (qmd == null)
{
throw new JDOUserException(LOCALISER_JDO.msg("011005", queryName, cls));
}
// Create the Query
Query query = newQuery(qmd.getLanguage(), qmd.getQuery());
if (cls != null)
{
query.setClass(cls);
if (!om.getStoreManager().managesClass(cls.getName()))
{
// Load the candidate class since not yet managed
om.getStoreManager().addClass(cls.getName(), clr);
}
}
// Optional args that should only be used with SQL
if (qmd.getLanguage().equals(QueryLanguage.JDOQL.toString()) &&
(qmd.isUnique() || qmd.getResultClass() != null))
{
throw new JDOUserException(LOCALISER_JDO.msg("011007", queryName));
}
if (qmd.isUnique())
{
query.setUnique(true);
}
if (qmd.getResultClass() != null)
{
// Set the result class, allowing for it being in the same package as the candidate
Class resultCls = null;
try
{
resultCls = clr.classForName(qmd.getResultClass());
}
catch (ClassNotResolvedException cnre)
{
try
{
String resultClassName = cls.getPackage().getName() + "." + qmd.getResultClass();
resultCls = clr.classForName(resultClassName);
}
catch (ClassNotResolvedException cnre2)
{
throw new JDOUserException(LOCALISER_JDO.msg("011008", queryName, qmd.getResultClass()));
}
}
query.setResultClass(resultCls);
}