// First look for "sql-type"
if (datastoreMappingsBySQLType.get(sqlType.toUpperCase()) == null)
{
if (fieldName != null)
{
throw new NucleusException(LOCALISER_RDBMS.msg("054001",
javaType, sqlType, fieldName)).setFatal();
}
else
{
throw new NucleusException(LOCALISER_RDBMS.msg("054000",
javaType, sqlType)).setFatal();
}
}
// Find if this sql-type has been defined for this java-type
Iterator sqlTypeIter = ((Collection) datastoreMappingsBySQLType.get(sqlType.toUpperCase())).iterator();
while (sqlTypeIter.hasNext())
{
RDBMSTypeMapping sqlTypeMapping = (RDBMSTypeMapping)sqlTypeIter.next();
if (sqlTypeMapping.javaType.equals(javaType))
{
datastoreMapping = sqlTypeMapping;
break;
}
}
}
else if (jdbcType != null)
{
// Then look for "jdbc-type"
if (datastoreMappingsByJDBCType.get(jdbcType.toUpperCase()) == null)
{
if (fieldName != null)
{
throw new NucleusException(LOCALISER_RDBMS.msg("054003",
javaType, jdbcType, fieldName)).setFatal();
}
else
{
throw new NucleusException(LOCALISER_RDBMS.msg("054002",
javaType, jdbcType)).setFatal();
}
}
// Find if this jdbc-type has been defined for this java-type
Iterator jdbcTypeIter = ((Collection) datastoreMappingsByJDBCType.get(jdbcType.toUpperCase())).iterator();
while (jdbcTypeIter.hasNext())
{
RDBMSTypeMapping jdbcTypeMapping = (RDBMSTypeMapping)jdbcTypeIter.next();
if (jdbcTypeMapping.javaType.equals(javaType))
{
datastoreMapping = jdbcTypeMapping;
break;
}
}
if (datastoreMapping == null)
{
// This JDBC type is supported but not for persisting this java type
if (fieldName != null)
{
throw new NucleusException(LOCALISER_RDBMS.msg("054003",
javaType, jdbcType, fieldName)).setFatal();
}
else
{
throw new NucleusException(LOCALISER_RDBMS.msg("054002",
javaType, jdbcType)).setFatal();
}
}
}
if (datastoreMapping == null)
{
// No specified type so get the best for this java-type (if primitve then use the wrapper java-type)
// Use wrapper type instead of primitive type since primitives have no java-type entries for datastore mappings
String type = ClassUtils.getWrapperTypeNameForPrimitiveTypeName(javaType);
Collection mappings = (Collection)datastoreMappingsByJavaType.get(type);
if (mappings == null)
{
// This java-type isnt specifically supported so maybe its superclass is
Class javaTypeClass = clr.classForName(type);
Class superClass = javaTypeClass.getSuperclass();
while (superClass != null && !superClass.getName().equals(ClassNameConstants.Object) && mappings == null)
{
mappings = (Collection) datastoreMappingsByJavaType.get(superClass.getName());
superClass = superClass.getSuperclass();
}
}
if (mappings != null)
{
if (mappings.size() == 1)
{
datastoreMapping = (RDBMSTypeMapping) mappings.iterator().next();
}
else
{
// More than 1 so take the default
Iterator mappingsIter = mappings.iterator();
while (mappingsIter.hasNext())
{
RDBMSTypeMapping rdbmsMapping = (RDBMSTypeMapping)mappingsIter.next();
if (rdbmsMapping.isDefault())
{
// default matched so take it
datastoreMapping = rdbmsMapping;
break;
}
}
// No default set, so use the first one
if (datastoreMapping == null && mappings.size() > 0)
{
datastoreMapping = (RDBMSTypeMapping) mappings.iterator().next();
}
}
}
}
if (datastoreMapping == null)
{
if (fieldName != null)
{
throw new NucleusException(LOCALISER_RDBMS.msg("054005",
javaType, jdbcType, sqlType, fieldName)).setFatal();
}
else
{
throw new NucleusException(LOCALISER_RDBMS.msg("054004",
javaType, jdbcType, sqlType)).setFatal();
}
}
return datastoreMapping.getMappingType();
}