Long securityProfileId = library.getSecurityProfile().getProfileId();
if (this.cascadeType != null) { // && this.cascadeType.equals(CascadeType.PERSIST)) {
securityProfileId = securityProfileDAO.save(library.getSecurityProfile());
}
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("alias", library.getAlias())
.addValue("accession", library.getAccession())
.addValue("description", library.getDescription())
.addValue("locationBarcode", library.getLocationBarcode())
.addValue("paired", library.getPaired())
.addValue("sample_sampleId", library.getSample().getId())
.addValue("securityProfile_profileId", securityProfileId)
.addValue("libraryType", library.getLibraryType().getLibraryTypeId())
.addValue("librarySelectionType", library.getLibrarySelectionType().getLibrarySelectionTypeId())
.addValue("libraryStrategyType", library.getLibraryStrategyType().getLibraryStrategyTypeId())
.addValue("platformName", library.getPlatformName())
.addValue("concentration", library.getInitialConcentration())
.addValue("creationDate", library.getCreationDate());
//.addValue("qcPassed", library.getQcPassed());
if (library.getQcPassed() != null) {
params.addValue("qcPassed", library.getQcPassed().toString());
}
else {
params.addValue("qcPassed", library.getQcPassed());
}
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);
}
}