@Transactional(readOnly = false, rollbackFor = IOException.class)
public long save(Submission submission) throws IOException {
SimpleJdbcInsert insert = new SimpleJdbcInsert(template)
.withTableName("Submission");
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("alias", submission.getAlias())
.addValue("accession", submission.getAccession())
.addValue("description", submission.getDescription())
.addValue("title", submission.getTitle())
.addValue("creationDate", submission.getCreationDate())
.addValue("submittedDate", submission.getSubmissionDate())
.addValue("verified", submission.isVerified())
.addValue("completed", submission.isCompleted());
//if a submission already exists then delete all the old rows first, and repopulate.
//easier than trying to work out which rows need to be updated and which don't
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());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save Submission - issue with naming scheme", e);
}
/*
String name = "SUB" + DbUtils.getAutoIncrement(template, TABLE_NAME);
params.addValue("creationDate", new Date());
params.addValue("name", name);
Number newId = insert.executeAndReturnKey(params);
submission.setSubmissionId(newId.longValue());
submission.setName(name);
*/
}
if (submission.getSubmissionElements() != null) {
Collection<Submittable<Document>> docs = submission.getSubmissionElements();
for (Submittable s : docs) {
String tableName = "Submission_";
String priKey = null;
Long priValue = null;
boolean process = true;
if (s instanceof Sample) {
tableName += "Sample";
priKey = "samples_sampleId";
priValue = ((Sample) s).getId();
}
else if (s instanceof Study) {
tableName += "Study";
priKey = "studies_studyId";
priValue = ((Study) s).getId();
}
else if (s instanceof Experiment) {
tableName += "Experiment";
priKey = "experiments_experimentId";
priValue = ((Experiment) s).getId();
}
else if (s instanceof SequencerPoolPartition) {
SequencerPoolPartition l = (SequencerPoolPartition) s;
tableName += "Partition_Dilution";
priKey = "partitions_partitionId";
priValue = l.getId();
process = false;
if (l.getPool() != null) {
Collection<Experiment> exps = l.getPool().getExperiments();
for (Experiment experiment : exps) {
SimpleJdbcInsert pInsert = new SimpleJdbcInsert(template)
.withTableName("Submission_Experiment");
try {
MapSqlParameterSource poParams = new MapSqlParameterSource();
poParams.addValue("submission_submissionId", submission.getId())
.addValue("experiments_experimentId", experiment.getId());
pInsert.execute(poParams);
}
catch (DuplicateKeyException dke) {
log.warn("This Submission_Experiment combination already exists - not inserting: " + dke.getMessage());
}
Study study = experiment.getStudy();
SimpleJdbcInsert sInsert = new SimpleJdbcInsert(template)
.withTableName("Submission_Study");
try {
MapSqlParameterSource poParams = new MapSqlParameterSource();
poParams.addValue("submission_submissionId", submission.getId())
.addValue("studies_studyId", study.getId());
sInsert.execute(poParams);
}
catch (DuplicateKeyException dke) {
log.warn("This Submission_Study combination already exists - not inserting: " + dke.getMessage());
}
}
Collection<? extends Dilution> dils = l.getPool().getDilutions();
for (Dilution dil : dils) {
Sample sample = dil.getLibrary().getSample();
SimpleJdbcInsert sInsert = new SimpleJdbcInsert(template)
.withTableName("Submission_Sample");
try {
MapSqlParameterSource poParams = new MapSqlParameterSource();
poParams.addValue("submission_submissionId", submission.getId())
.addValue("samples_sampleId", sample.getId());
sInsert.execute(poParams);
}
catch (DuplicateKeyException dke) {
log.warn("This Submission_Sample combination already exists - not inserting: " + dke.getMessage());
}
//Adds Submission_Partition_Dilution info to DB table.
sInsert = new SimpleJdbcInsert(template).withTableName("Submission_Partition_Dilution");
try {
MapSqlParameterSource poParams = new MapSqlParameterSource();
poParams.addValue("submission_submissionId", submission.getId())
.addValue("partition_partitionId", l.getId())
.addValue("dilution_dilutionId", dil.getId());
sInsert.execute(poParams);
}
catch (DuplicateKeyException dke) {
log.warn("This Submission_Partition_Dilution combination already exists - not inserting: " + dke.getMessage());
}
}
}
}
if (process) {
if (priKey != null && priValue != null) {
SimpleJdbcInsert pInsert = new SimpleJdbcInsert(template)
.withTableName(tableName);
try {
MapSqlParameterSource poParams = new MapSqlParameterSource();
poParams.addValue("submission_submissionId", submission.getId())
.addValue(priKey, priValue);
pInsert.execute(poParams);
}
catch (DuplicateKeyException dke) {
log.warn("This " + tableName + " combination already exists - not inserting: " + dke.getMessage());