if (library.getId() == AbstractLibrary.UNSAVED_ID) {
if (getByAlias(library.getAlias()) != null) {
throw new IOException("NEW: A library with this alias already exists in the database");
}
else {
SimpleJdbcInsert insert = new SimpleJdbcInsert(template)
.withTableName(TABLE_NAME)
.usingGeneratedKeyColumns("libraryId");
/*
String name = Library.PREFIX + DbUtils.getAutoIncrement(template, TABLE_NAME);
params.addValue("name", name);
params.addValue("identificationBarcode", name + "::" + library.getAlias());
Number newId = insert.executeAndReturnKey(params);
library.setLibraryId(newId.longValue());
library.setName(name);
*/
try {
library.setId(DbUtils.getAutoIncrement(template, TABLE_NAME));
String name = libraryNamingScheme.generateNameFor("name", library);
library.setName(name);
if (libraryNamingScheme.validateField("name", library.getName()) && libraryNamingScheme.validateField("alias", library.getAlias())) {
String barcode = name + "::" + library.getAlias();
params.addValue("name", name);
params.addValue("identificationBarcode", barcode);
Number newId = insert.executeAndReturnKey(params);
if (newId.longValue() != library.getId()) {
log.error("Expected library ID doesn't match returned value from database insert: rolling back...");
new NamedParameterJdbcTemplate(template).update(LIBRARY_DELETE, new MapSqlParameterSource().addValue("libraryId", newId.longValue()));
throw new IOException("Something bad happened. Expected library ID doesn't match returned value from DB insert");
}
}
else {
throw new IOException("Cannot save library - invalid field:" + library.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save library - issue with naming scheme", e);
}
}
}
else {
try {
if (libraryNamingScheme.validateField("name", library.getName()) && libraryNamingScheme.validateField("alias", library.getAlias())) {
params.addValue("libraryId", library.getId())
.addValue("name", library.getName())
.addValue("identificationBarcode", library.getName() + "::" + library.getAlias());
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
namedTemplate.update(LIBRARY_UPDATE, params);
}
else {
throw new IOException("Cannot save library - invalid field:" + library.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save library - issue with naming scheme", e);
}
}
MapSqlParameterSource libparams = new MapSqlParameterSource();
libparams.addValue("library_libraryId", library.getId());
NamedParameterJdbcTemplate libNamedTemplate = new NamedParameterJdbcTemplate(template);
libNamedTemplate.update(LIBRARY_TAGBARCODE_DELETE_BY_LIBRARY_ID, libparams);
/*
if (library.getTagBarcode() != null) {
SimpleJdbcInsert eInsert = new SimpleJdbcInsert(template)
.withTableName("Library_TagBarcode");
MapSqlParameterSource ltParams = new MapSqlParameterSource();
ltParams.addValue("library_libraryId", library.getLibraryId())
.addValue("barcode_barcodeId", library.getTagBarcode().getTagBarcodeId());
eInsert.execute(ltParams);
}
*/
if (library.getTagBarcodes() != null && !library.getTagBarcodes().isEmpty()) {
SimpleJdbcInsert eInsert = new SimpleJdbcInsert(template)
.withTableName("Library_TagBarcode");
for (TagBarcode t : library.getTagBarcodes().values()) {
MapSqlParameterSource ltParams = new MapSqlParameterSource();
ltParams.addValue("library_libraryId", library.getId())
.addValue("barcode_barcodeId", t.getId());
eInsert.execute(ltParams);
}
}
if (this.cascadeType != null) {
if (this.cascadeType.equals(CascadeType.PERSIST)) {