use of bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException in project terra-workspace-manager by DataBiosphere.
the class WorkspaceDao method createWorkspace.
/**
* Persists a workspace to DB. Returns ID of persisted workspace on success.
*
* @param workspace all properties of the workspace to create
* @return workspace id
*/
@WriteTransaction
public UUID createWorkspace(Workspace workspace) {
final String sql = "INSERT INTO workspace (workspace_id, display_name, description, spend_profile, properties, workspace_stage) " + "values (:workspace_id, :display_name, :description, :spend_profile," + " cast(:properties AS jsonb), :workspace_stage)";
final String workspaceId = workspace.getWorkspaceId().toString();
MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId).addValue("display_name", workspace.getDisplayName().orElse(null)).addValue("description", workspace.getDescription().orElse(null)).addValue("spend_profile", workspace.getSpendProfileId().map(SpendProfileId::getId).orElse(null)).addValue("properties", DbSerDes.propertiesToJson(workspace.getProperties())).addValue("workspace_stage", workspace.getWorkspaceStage().toString());
try {
jdbcTemplate.update(sql, params);
logger.info("Inserted record for workspace {}", workspaceId);
} catch (DuplicateKeyException e) {
throw new DuplicateWorkspaceException(String.format("Workspace with id %s already exists - display name %s stage %s", workspaceId, workspace.getDisplayName().toString(), workspace.getWorkspaceStage().toString()), e);
}
return workspace.getWorkspaceId();
}
use of bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException in project terra-workspace-manager by DataBiosphere.
the class CreateWorkspaceStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws RetryException, InterruptedException {
UUID workspaceId = workspace.getWorkspaceId();
try {
workspaceDao.createWorkspace(workspace);
} catch (DuplicateWorkspaceException ex) {
// This might be the result of a step re-running, or it might be an ID conflict. We can ignore
// this if the existing workspace matches the one we were about to create, otherwise rethrow.
Workspace existingWorkspace = workspaceDao.getWorkspace(workspaceId);
if (!workspace.equals(existingWorkspace)) {
throw ex;
}
}
FlightUtils.setResponse(flightContext, workspaceId, HttpStatus.OK);
logger.info("Workspace created with id {}", workspaceId);
return StepResult.getStepResultSuccess();
}
Aggregations