return run.getId();
}
public synchronized int[] saveAll(Collection<Run> runs) throws IOException {
log.debug(">>> Entering saveAll with " + runs.size() + " runs");
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
List<SqlParameterSource> batch = new ArrayList<SqlParameterSource>();
long autoIncrement = DbUtils.getAutoIncrement(template, TABLE_NAME);
for (Run run : runs) {
Long securityProfileId = run.getSecurityProfile().getProfileId();
if (securityProfileId == null || (this.cascadeType != null)) {// && this.cascadeType.equals(CascadeType.PERSIST))) {
securityProfileId = securityProfileDAO.save(run.getSecurityProfile());
}
Long statusId = null;
if (run.getStatus() != null) {
Status s = run.getStatus();
statusId = s.getStatusId();
//if no status has ever been saved to the database for this run
//we want to create one, cascading or not
if (statusId == StatusImpl.UNSAVED_ID || (this.cascadeType != null && this.cascadeType.equals(CascadeType.PERSIST))) {
if (s.getRunName() == null) {
s.setRunName(run.getAlias());
}
if (s.getInstrumentName() == null && run.getSequencerReference() != null) {
s.setInstrumentName(run.getSequencerReference().getName());
}
}
statusId = statusDAO.save(s);
run.setStatus(s);
}
else {
log.warn("No status available to save for run: " + run.getAlias());
}
try {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("accession", run.getAccession())
.addValue("alias", run.getAlias())
.addValue("description", run.getDescription())
.addValue("platformRunId", run.getPlatformRunId())
.addValue("pairedEnd", run.getPairedEnd())
.addValue("cycles", run.getCycles())
.addValue("filePath", run.getFilePath())
.addValue("platformType", run.getPlatformType().getKey())
.addValue("securityProfile_profileId", securityProfileId)
.addValue("status_statusId", statusId)
.addValue("sequencerReference_sequencerReferenceId", run.getSequencerReference().getId());
if (run.getId() == AbstractRun.UNSAVED_ID) {
SimpleJdbcInsert insert = new SimpleJdbcInsert(template)
.withTableName(TABLE_NAME)
.usingGeneratedKeyColumns("runId");
try {
run.setId(autoIncrement);
String name = namingScheme.generateNameFor("name", run);
run.setName(name);
if (namingScheme.validateField("name", run.getName())) {
params.addValue("name", name);
Number newId = insert.executeAndReturnKey(params);
if (newId.longValue() != run.getId()) {
log.error("Expected Run ID doesn't match returned value from database insert: rolling back...");
new NamedParameterJdbcTemplate(template).update(RUN_DELETE, new MapSqlParameterSource().addValue("runId", newId.longValue()));
throw new IOException("Something bad happened. Expected Run ID doesn't match returned value from DB insert");
}
autoIncrement = newId.longValue() + 1;
log.debug(run.getName() + ":: Inserted as ID " + run.getId());
}
else {
throw new IOException("Cannot save Run - invalid field:" + run.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save Run - issue with naming scheme", e);
}
/*
String name = "RUN" + autoIncrement;
params.addValue("name", name);
Number newId = insert.executeAndReturnKey(params);
run.setRunId(newId.longValue());
run.setName(name);
autoIncrement = newId.longValue() + 1;
log.debug(run.getName() + ":: Inserted as ID " + run.getRunId());
*/
}
else {
try {
if (namingScheme.validateField("name", run.getName())) {
params.addValue("runId", run.getId())
.addValue("name", run.getName());
log.debug(run.getName() + ":: Updating as ID " + run.getId());
batch.add(params);
}
else {
throw new IOException("Cannot save Run - invalid field:" + run.toString());
}
}
catch (MisoNamingException e) {
throw new IOException("Cannot save Run - issue with naming scheme", e);
}
/*
params.addValue("runId", run.getRunId())
.addValue("name", run.getName());
log.debug(run.getName() + ":: Updating as ID " + run.getRunId());
batch.add(params);
*/
}
if (this.cascadeType != null) {
if (this.cascadeType.equals(CascadeType.PERSIST)) {
for (SequencerPartitionContainer<SequencerPoolPartition> l : ((RunImpl)run).getSequencerPartitionContainers()) {
l.setSecurityProfile(run.getSecurityProfile());
if (l.getPlatformType() == null) {
l.setPlatformType(run.getPlatformType());
}
long containerId = sequencerPartitionContainerDAO.save(l);
SimpleJdbcInsert fInsert = new SimpleJdbcInsert(template).withTableName("Run_SequencerPartitionContainer");
MapSqlParameterSource fcParams = new MapSqlParameterSource();
fcParams.addValue("Run_runId", run.getId())
.addValue("containers_containerId", containerId);
try {
fInsert.execute(fcParams);
}
catch(DuplicateKeyException dke) {
log.debug("This Run/SequencerPartitionContainer combination already exists - not inserting: " + dke.getMessage());
}
}
}
if (!run.getNotes().isEmpty()) {
for (Note n : run.getNotes()) {
noteDAO.saveRunNote(run, n);
}
}
//if this is saved by a user, and not automatically saved by the notification system
User user = securityManager.getUserByLoginName(SecurityContextHolder.getContext().getAuthentication().getName());
watcherDAO.removeWatchedEntityByUser(run, user);
for (User u : run.getWatchers()) {
watcherDAO.saveWatchedEntityUser(run, u);
}
}
}
catch (IOException e) {
log.error("Cannot batch save run: " + run.getName());
e.printStackTrace();
}
}
int[] rows = namedTemplate.batchUpdate(RUN_UPDATE, batch.toArray(new SqlParameterSource[batch.size()]));
//flush caches
purgeCaches(runs);
log.debug("<<< Exiting saveAll");