final long numToSkip,
final long numToReturn) {
// Validate the parameters.
if(owner == null) {
throw new OmhException("The data is null.");
}
else if(schemaId == null) {
throw new OmhException("The schema ID is null.");
}
// Retrieve the list of results.
List<Data> list;
try {
list =
SqlDao
.getInstance()
.getJdbcTemplate()
.query(
// Get the required columns to build the object.
"SELECT " +
User.JSON_KEY_USERNAME + ", " +
Schema.JSON_KEY_ID + ", " +
Schema.JSON_KEY_VERSION + ", " +
Data.JSON_KEY_METADATA + "_" +
MetaData.JSON_KEY_ID + ", " +
Data.JSON_KEY_METADATA + "_" +
MetaData.JSON_KEY_TIMESTAMP + ", " +
Data.JSON_KEY_DATA + " " +
// Include all of the required tables.
"FROM " +
UserBin.DB_NAME + ", " +
Registry.DB_NAME + ", " +
DataSet.DB_NAME + " " +
// Link the user table to the data table.
"WHERE " +
UserBin.DB_NAME +
"." +
SqlDao.KEY_DATABASE_ID +
" = " +
DataSet.DB_NAME +
"." +
UserBin.DB_NAME + "_id " +
// Limit the results based on the required
// username.
"AND " + User.JSON_KEY_USERNAME + " = ? " +
// Link the registry table to the data table.
"AND " +
Registry.DB_NAME +
"." +
SqlDao.KEY_DATABASE_ID +
" = " +
DataSet.DB_NAME +
"." +
Registry.DB_NAME + "_id " +
// Limit the results based on the required schema
// ID and version.
"AND " + Schema.JSON_KEY_ID + " = ? " +
"AND " + Schema.JSON_KEY_VERSION + " = ? " +
"LIMIT ?, ?",
new Object[] {
owner,
schemaId,
version,
numToSkip,
numToReturn },
new RowMapper<Data>() {
/**
* Maps the row to a {@link Data} object.
*/
@Override
public Data mapRow(
final ResultSet resultSet,
final int rowNum)
throws SQLException {
// Get the username.
String username =
resultSet
.getString(User.JSON_KEY_USERNAME);
// Get the schema's ID.
String id =
resultSet.getString(Schema.JSON_KEY_ID);
// Get the stream's version.
long version =
resultSet.getLong(Schema.JSON_KEY_VERSION);
// Build the meta-data.
MetaData.Builder metaDataBuilder =
new MetaData.Builder();
// Get and set the ID, even if it is null.
metaDataBuilder
.setId(
resultSet
.getString(
Data.JSON_KEY_METADATA +
"_" +
MetaData.JSON_KEY_ID));
// Get the timestamp.
String metaDataTimestampString =
resultSet
.getString(
Data.JSON_KEY_METADATA +
"_" +
MetaData.JSON_KEY_TIMESTAMP);
// If the timestamp is not null, decode it and
// set it.
if(metaDataTimestampString != null) {
metaDataBuilder
.setTimestamp(
ISOW3CDateTimeFormat
.any()
.parseDateTime(
metaDataTimestampString));
}
// If the builder has no non-null members,
// create a MetaData object; otherwise, just
// leave it as null.
MetaData metaData =
((metaDataBuilder.isNull()) ?
null :
metaDataBuilder.build());
// Get the data.
JsonNode data;
try {
data =
JSON_MAPPER
.readTree(
resultSet
.getString(
Data.JSON_KEY_DATA));
}
catch(IOException e) {
throw
new SQLException(
"Error decoding the data.",
e);
}
// FIXME: Apply the column list.
// Create a Data object and return it.
return
new Data(
username,
id,
version,
metaData,
data);
}
});
}
// For all issues, we simply propagate the exception.
catch(DataAccessException e) {
throw
new OmhException(
"There was an error querying for schemas.",
e);
}
// Retrieve the total count of results.
int count;
try {
count =
SqlDao
.getInstance()
.getJdbcTemplate()
.queryForInt(
"SELECT COUNT(1) " +
// Include all of the required tables.
"FROM " +
UserBin.DB_NAME + ", " +
Registry.DB_NAME + ", " +
DataSet.DB_NAME + " " +
// Link the user table to the data table.
"WHERE " +
UserBin.DB_NAME +
"." +
SqlDao.KEY_DATABASE_ID +
" = " +
DataSet.DB_NAME +
"." +
UserBin.DB_NAME + "_id " +
// Limit the results based on the required
// username.
"AND " + User.JSON_KEY_USERNAME + " = ? " +
// Link the registry table to the data table.
"AND " +
Registry.DB_NAME +
"." +
SqlDao.KEY_DATABASE_ID +
" = " +
DataSet.DB_NAME +
"." +
Registry.DB_NAME + "_id " +
// Limit the results based on the required schema
// ID and version.
"AND " + Schema.JSON_KEY_ID + " = ? " +
"AND " + Schema.JSON_KEY_VERSION + " = ? ",
new Object[] { owner, schemaId, version });
}
// For all issues, we simply propagate the exception.
catch(DataAccessException e) {
throw
new OmhException(
"There was an error querying for schemas count.",
e);
}
return new SqlMultiValueResult<Data>(list, count);