Search in sources :

Example 1 with InvalidSnapshotException

use of bio.terra.service.snapshot.exception.InvalidSnapshotException in project jade-data-repo by DataBiosphere.

the class CreateSnapshotMetadataStep method doStep.

@Override
public StepResult doStep(FlightContext context) {
    try {
        Snapshot snapshot = snapshotService.makeSnapshotFromSnapshotRequest(snapshotReq);
        UUID snapshotId = snapshotDao.createAndLock(snapshot, context.getFlightId());
        FlightMap workingMap = context.getWorkingMap();
        workingMap.put(SnapshotWorkingMapKeys.SNAPSHOT_ID, snapshotId);
        SnapshotSummary snapshotSummary = snapshotDao.retrieveSummaryById(snapshot.getId());
        SnapshotSummaryModel response = snapshotService.makeSummaryModelFromSummary(snapshotSummary);
        FlightUtils.setResponse(context, response, HttpStatus.CREATED);
        return StepResult.getStepResultSuccess();
    } catch (InvalidSnapshotException isEx) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, isEx);
    } catch (SnapshotNotFoundException ex) {
        FlightUtils.setErrorResponse(context, ex.toString(), HttpStatus.BAD_REQUEST);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, ex);
    }
}
Also used : Snapshot(bio.terra.service.snapshot.Snapshot) SnapshotSummary(bio.terra.service.snapshot.SnapshotSummary) InvalidSnapshotException(bio.terra.service.snapshot.exception.InvalidSnapshotException) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) SnapshotNotFoundException(bio.terra.service.snapshot.exception.SnapshotNotFoundException) FlightMap(bio.terra.stairway.FlightMap) UUID(java.util.UUID) StepResult(bio.terra.stairway.StepResult)

Example 2 with InvalidSnapshotException

use of bio.terra.service.snapshot.exception.InvalidSnapshotException in project jade-data-repo by DataBiosphere.

the class SnapshotService method makeSnapshotFromSnapshotRequest.

/**
 * Make a Snapshot structure with all of its parts from an incoming snapshot request.
 * Note that the structure does not have UUIDs or created dates filled in. Those are
 * updated by the DAO when it stores the snapshot in the repository metadata.
 *
 * @param snapshotRequestModel
 * @return Snapshot
 */
public Snapshot makeSnapshotFromSnapshotRequest(SnapshotRequestModel snapshotRequestModel) {
    // Make this early so we can hook up back links to it
    Snapshot snapshot = new Snapshot();
    List<SnapshotRequestContentsModel> requestContentsList = snapshotRequestModel.getContents();
    // TODO: for MVM we only allow one source list
    if (requestContentsList.size() > 1) {
        throw new ValidationException("Only a single snapshot contents entry is currently allowed.");
    }
    SnapshotRequestContentsModel requestContents = requestContentsList.get(0);
    Dataset dataset = datasetService.retrieveByName(requestContents.getDatasetName());
    SnapshotSource snapshotSource = new SnapshotSource().snapshot(snapshot).dataset(dataset);
    switch(snapshotRequestModel.getContents().get(0).getMode()) {
        case BYASSET:
            // TODO: When we implement explicit definition of snapshot tables, we will handle that here.
            // For now, we generate the snapshot tables directly from the asset tables of the one source
            // allowed in a snapshot.
            AssetSpecification assetSpecification = getAssetSpecificationFromRequest(requestContents);
            snapshotSource.assetSpecification(assetSpecification);
            conjureSnapshotTablesFromAsset(snapshotSource.getAssetSpecification(), snapshot, snapshotSource);
            break;
        case BYFULLVIEW:
            conjureSnapshotTablesFromDatasetTables(snapshot, snapshotSource);
            break;
        case BYQUERY:
            SnapshotRequestQueryModel queryModel = requestContents.getQuerySpec();
            String assetName = queryModel.getAssetName();
            String snapshotQuery = queryModel.getQuery();
            Query query = Query.parse(snapshotQuery);
            List<String> datasetNames = query.getDatasetNames();
            // TODO this makes the assumption that there is only one dataset
            // (based on the validation flight step that already occurred.)
            // This will change when more than 1 dataset is allowed
            String datasetName = datasetNames.get(0);
            Dataset queryDataset = datasetService.retrieveByName(datasetName);
            AssetSpecification queryAssetSpecification = queryDataset.getAssetSpecificationByName(assetName).orElseThrow(() -> new AssetNotFoundException("This dataset does not have an asset specification with name: " + assetName));
            snapshotSource.assetSpecification(queryAssetSpecification);
            // TODO this is wrong? why dont we just pass the assetSpecification?
            conjureSnapshotTablesFromAsset(snapshotSource.getAssetSpecification(), snapshot, snapshotSource);
            break;
        case BYROWID:
            SnapshotRequestRowIdModel requestRowIdModel = requestContents.getRowIdSpec();
            conjureSnapshotTablesFromRowIds(requestRowIdModel, snapshot, snapshotSource);
            break;
        default:
            throw new InvalidSnapshotException("Snapshot does not have required mode information");
    }
    return snapshot.name(snapshotRequestModel.getName()).description(snapshotRequestModel.getDescription()).snapshotSources(Collections.singletonList(snapshotSource)).profileId(UUID.fromString(snapshotRequestModel.getProfileId())).relationships(createSnapshotRelationships(dataset.getRelationships(), snapshotSource));
}
Also used : ValidationException(bio.terra.app.controller.exception.ValidationException) Query(bio.terra.grammar.Query) SnapshotRequestQueryModel(bio.terra.model.SnapshotRequestQueryModel) Dataset(bio.terra.service.dataset.Dataset) SnapshotRequestRowIdModel(bio.terra.model.SnapshotRequestRowIdModel) SnapshotRequestContentsModel(bio.terra.model.SnapshotRequestContentsModel) AssetSpecification(bio.terra.service.dataset.AssetSpecification) AssetNotFoundException(bio.terra.service.snapshot.exception.AssetNotFoundException) InvalidSnapshotException(bio.terra.service.snapshot.exception.InvalidSnapshotException)

Example 3 with InvalidSnapshotException

use of bio.terra.service.snapshot.exception.InvalidSnapshotException in project jade-data-repo by DataBiosphere.

the class SnapshotDao method createAndLock.

/**
 * Create a new snapshot object and lock it. An exception is thrown if the snapshot already exists.
 * The correct order to call the SnapshotDao methods when creating a snapshot is: createAndLock, unlock.
 * @param snapshot the snapshot object to create
 * @return the id of the new snapshot
 * @throws InvalidSnapshotException if a row already exists with this snapshot name
 */
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public UUID createAndLock(Snapshot snapshot, String flightId) {
    logger.debug("createAndLock snapshot " + snapshot.getName());
    String sql = "INSERT INTO snapshot (name, description, profile_id, flightid) " + "VALUES (:name, :description, :profile_id, :flightid) ";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("name", snapshot.getName()).addValue("description", snapshot.getDescription()).addValue("profile_id", snapshot.getProfileId()).addValue("flightid", flightId);
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    try {
        jdbcTemplate.update(sql, params, keyHolder);
    } catch (DuplicateKeyException dkEx) {
        throw new InvalidSnapshotException("Snapshot name already exists: " + snapshot.getName(), dkEx);
    }
    UUID snapshotId = keyHolder.getId();
    snapshot.id(snapshotId).createdDate(keyHolder.getCreatedDate());
    snapshotTableDao.createTables(snapshotId, snapshot.getTables());
    for (SnapshotSource snapshotSource : snapshot.getSnapshotSources()) {
        createSnapshotSource(snapshotSource);
    }
    return snapshotId;
}
Also used : InvalidSnapshotException(bio.terra.service.snapshot.exception.InvalidSnapshotException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DaoKeyHolder(bio.terra.common.DaoKeyHolder) UUID(java.util.UUID) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

InvalidSnapshotException (bio.terra.service.snapshot.exception.InvalidSnapshotException)3 UUID (java.util.UUID)2 ValidationException (bio.terra.app.controller.exception.ValidationException)1 DaoKeyHolder (bio.terra.common.DaoKeyHolder)1 Query (bio.terra.grammar.Query)1 SnapshotRequestContentsModel (bio.terra.model.SnapshotRequestContentsModel)1 SnapshotRequestQueryModel (bio.terra.model.SnapshotRequestQueryModel)1 SnapshotRequestRowIdModel (bio.terra.model.SnapshotRequestRowIdModel)1 SnapshotSummaryModel (bio.terra.model.SnapshotSummaryModel)1 AssetSpecification (bio.terra.service.dataset.AssetSpecification)1 Dataset (bio.terra.service.dataset.Dataset)1 Snapshot (bio.terra.service.snapshot.Snapshot)1 SnapshotSummary (bio.terra.service.snapshot.SnapshotSummary)1 AssetNotFoundException (bio.terra.service.snapshot.exception.AssetNotFoundException)1 SnapshotNotFoundException (bio.terra.service.snapshot.exception.SnapshotNotFoundException)1 FlightMap (bio.terra.stairway.FlightMap)1 StepResult (bio.terra.stairway.StepResult)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1