Long securityProfileId = experiment.getSecurityProfile().getProfileId();
if (securityProfileId == null || this.cascadeType != null) {// && this.cascadeType.equals(CascadeType.PERSIST)) {
securityProfileId = securityProfileDAO.save(experiment.getSecurityProfile());
}
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("alias", experiment.getAlias())
.addValue("accession", experiment.getAccession())
.addValue("description", experiment.getDescription())
.addValue("title", experiment.getTitle())
.addValue("platform_platformId", experiment.getPlatform().getPlatformId())
.addValue("securityProfile_profileId", securityProfileId)
.addValue("study_studyId", experiment.getStudy().getId());
if (experiment.getId() == AbstractExperiment.UNSAVED_ID) {
SimpleJdbcInsert insert = new SimpleJdbcInsert(template)
.withTableName(TABLE_NAME)
.usingGeneratedKeyColumns("experimentId");
try {
experiment.setId(DbUtils.getAutoIncrement(template, TABLE_NAME));
String name = namingScheme.generateNameFor("name", experiment);
experiment.setName(name);
if (namingScheme.validateField("name", experiment.getName())) {
params.addValue("name", name);
Number newId = insert.executeAndReturnKey(params);
if (newId.longValue() != experiment.getId()) {
log.error("Expected Experiment ID doesn't match returned value from database insert: rolling back...");
new NamedParameterJdbcTemplate(template).update(EXPERIMENT_DELETE, new MapSqlParameterSource().addValue("experimentId", newId.longValue()));
throw new IOException("Something bad happened. Expected Experiment ID doesn't match returned value from DB insert");
}
}
else {
throw new IOException("Cannot save Experiment - invalid field:" + experiment.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save Experiment - issue with naming scheme", e);
}
/*
String name = Experiment.PREFIX + DbUtils.getAutoIncrement(template, TABLE_NAME);
params.addValue("name", name);
Number newId = insert.executeAndReturnKey(params);
experiment.setExperimentId(newId.longValue());
experiment.setName(name);
*/
}
else {
try {
if (namingScheme.validateField("name", experiment.getName())) {
params.addValue("experimentId", experiment.getId())
.addValue("name", experiment.getName());
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
namedTemplate.update(EXPERIMENT_UPDATE, params);
}
else {
throw new IOException("Cannot save Experiment - invalid field:" + experiment.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save Experiment - issue with naming scheme", e);
}
/*
params.addValue("experimentId", experiment.getExperimentId())
.addValue("name", experiment.getName());
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
namedTemplate.update(EXPERIMENT_UPDATE, params);
*/
}
if (this.cascadeType != null) {
MapSqlParameterSource eParams = new MapSqlParameterSource();
eParams.addValue("experiments_experimentId", experiment.getId());
NamedParameterJdbcTemplate eNamedTemplate = new NamedParameterJdbcTemplate(template);
eNamedTemplate.update(POOL_EXPERIMENT_DELETE_BY_EXPERIMENT_ID, eParams);
if (experiment.getPool() != null) {
SimpleJdbcInsert eInsert = new SimpleJdbcInsert(template)
.withTableName("Pool_Experiment");
MapSqlParameterSource esParams = new MapSqlParameterSource();
esParams.addValue("experiments_experimentId", experiment.getId())
.addValue("pool_poolId", experiment.getPool().getId());
eInsert.execute(esParams);
if (this.cascadeType.equals(CascadeType.PERSIST)) {
DbUtils.flushCache(cacheManager, "poolCache");
}
else if (this.cascadeType.equals(CascadeType.REMOVE)) {
//Cache pc = cacheManager.getCache("poolCache");
//pc.remove(DbUtils.hashCodeCacheKeyFor(experiment.getPool().getId()));
DbUtils.updateCaches(cacheManager, experiment.getPool(), Pool.class);
}
}
Study s = experiment.getStudy();
if (this.cascadeType.equals(CascadeType.PERSIST)) {
if (s!=null) studyDAO.save(s);
}
else if (this.cascadeType.equals(CascadeType.REMOVE)) {
if (s != null) {
//Cache pc = cacheManager.getCache("studyCache");
//pc.remove(DbUtils.hashCodeCacheKeyFor(s.getId()));
DbUtils.updateCaches(cacheManager, s, Study.class);
}
}
if (!experiment.getKits().isEmpty()) {
for (Kit k : experiment.getKits()) {
kitDAO.save(k);
SimpleJdbcInsert kInsert = new SimpleJdbcInsert(template)
.withTableName("Experiment_Kit");
MapSqlParameterSource kParams = new MapSqlParameterSource();
kParams.addValue("experiments_experimentId", experiment.getId())
.addValue("kits_kidId", k.getId());
try {
kInsert.execute(kParams);
}
catch(DuplicateKeyException dke) {