Long securityProfileId = dilution.getSecurityProfile().getProfileId();
if (securityProfileId == null || (this.cascadeType != null)) { // && this.cascadeType.equals(CascadeType.PERSIST))) {
securityProfileId = securityProfileDAO.save(dilution.getSecurityProfile());
}
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("concentration", dilution.getConcentration())
.addValue("library_libraryId", dilution.getLibrary().getId())
.addValue("creationDate", dilution.getCreationDate())
.addValue("securityProfile_profileId", securityProfileId)
.addValue("dilutionUserName", dilution.getDilutionCreator());
if (dilution.getId() == AbstractDilution.UNSAVED_ID) {
SimpleJdbcInsert insert = new SimpleJdbcInsert(template)
.withTableName("LibraryDilution")
.usingGeneratedKeyColumns("dilutionId");
try {
dilution.setId(DbUtils.getAutoIncrement(template, "LibraryDilution"));
String name = namingScheme.generateNameFor("name", dilution);
dilution.setName(name);
if (namingScheme.validateField("name", dilution.getName())) {
String barcode = name + "::" + dilution.getLibrary().getAlias();
params.addValue("name", name);
params.addValue("identificationBarcode", barcode);
Number newId = insert.executeAndReturnKey(params);
if (newId.longValue() != dilution.getId()) {
log.error("Expected LibraryDilution ID doesn't match returned value from database insert: rolling back...");
new NamedParameterJdbcTemplate(template).update(LIBRARY_DILUTION_DELETE, new MapSqlParameterSource().addValue("dilutionId", newId.longValue()));
throw new IOException("Something bad happened. Expected LibraryDilution ID doesn't match returned value from DB insert");
}
}
else {
throw new IOException("Cannot save LibraryDilution - invalid field:" + dilution.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save LibraryDilution - issue with naming scheme", e);
}
/*
String name = "LDI"+ DbUtils.getAutoIncrement(template, "LibraryDilution");
params.addValue("name", name);
params.addValue("identificationBarcode", name + "::" + dilution.getLibrary().getAlias());
Number newId = insert.executeAndReturnKey(params);
dilution.setDilutionId(newId.longValue());
dilution.setName(name);
*/
}
else {
try {
if (namingScheme.validateField("name", dilution.getName())) {
params.addValue("dilutionId", dilution.getId())
.addValue("name", dilution.getName())
.addValue("identificationBarcode", dilution.getName() + "::" + dilution.getLibrary().getAlias());
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
namedTemplate.update(LIBRARY_DILUTION_UPDATE, params);
}