if (submission.getId() != Submission.UNSAVED_ID) {
try {
if (namingScheme.validateField("name", submission.getName())) {
MapSqlParameterSource delparams = new MapSqlParameterSource();
delparams.addValue("submissionId", submission.getId());
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
log.debug("Deleting Submission elements for " + submission.getId());
namedTemplate.update(SUBMISSION_ELEMENTS_DELETE, delparams);
params.addValue("submissionId", submission.getId())
.addValue("name", submission.getName());
namedTemplate.update(SUBMISSION_UPDATE, params);
}
else {
throw new IOException("Cannot save Submission - invalid field:" + submission.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save Submission - issue with naming scheme", e);
}
/*
params.addValue("submissionId", submission.getSubmissionId())
.addValue("name", submission.getName());
namedTemplate.update(SUBMISSION_UPDATE, params);
*/
}
else {
insert.usingGeneratedKeyColumns("submissionId");
try {
submission.setId(DbUtils.getAutoIncrement(template, TABLE_NAME));
String name = namingScheme.generateNameFor("name", submission);
submission.setName(name);
if (namingScheme.validateField("name", submission.getName())) {
params.addValue("name", name)
.addValue("creationDate", new Date());
Number newId = insert.executeAndReturnKey(params);
if (newId.longValue() != submission.getId()) {
log.error("Expected Submission ID doesn't match returned value from database insert: rolling back...");
new NamedParameterJdbcTemplate(template).update(SUBMISSION_DELETE, new MapSqlParameterSource().addValue("submissionId", newId.longValue()));
throw new IOException("Something bad happened. Expected Submission ID doesn't match returned value from DB insert");
}
}
else {
throw new IOException("Cannot save Submission - invalid field:" + submission.toString());