Search in sources :

Example 76 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class DynamoSurveyDao method saveSurvey.

private Survey saveSurvey(Survey survey) {
    deleteAllElements(survey.getGuid(), survey.getCreatedOn());
    List<DynamoSurveyElement> dynamoElements = Lists.newArrayList();
    for (int i = 0; i < survey.getElements().size(); i++) {
        SurveyElement element = survey.getElements().get(i);
        element.setSurveyKeyComponents(survey.getGuid(), survey.getCreatedOn());
        element.setOrder(i);
        if (element.getGuid() == null) {
            element.setGuid(generateGuid());
        }
        reconcileRules(element);
        dynamoElements.add((DynamoSurveyElement) element);
    }
    List<FailedBatch> failures = surveyElementMapper.batchSave(dynamoElements);
    BridgeUtils.ifFailuresThrowException(failures);
    try {
        surveyMapper.save(survey);
    } catch (ConditionalCheckFailedException throwable) {
        // At this point, delete the elements you just created... compensating transaction
        deleteAllElements(survey.getGuid(), survey.getCreatedOn());
        throw new ConcurrentModificationException(survey);
    } catch (Throwable t) {
        throw new BridgeServiceException(t);
    }
    return survey;
}
Also used : ConcurrentModificationException(org.sagebionetworks.bridge.exceptions.ConcurrentModificationException) SurveyElement(org.sagebionetworks.bridge.models.surveys.SurveyElement) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) FailedBatch(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.FailedBatch) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 77 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class HibernateTemplateRevisionDao method getTemplateRevision.

@Override
public Optional<TemplateRevision> getTemplateRevision(String templateGuid, DateTime createdOn) {
    checkNotNull(templateGuid);
    checkNotNull(createdOn);
    TemplateRevisionId revisionId = new TemplateRevisionId(templateGuid, createdOn);
    HibernateTemplateRevision revision = hibernateHelper.getById(HibernateTemplateRevision.class, revisionId);
    if (revision == null) {
        return Optional.empty();
    }
    // Load document content
    try {
        String storagePath = getStoragePath(templateGuid, createdOn);
        String documentContent = s3Helper.readS3FileAsString(publicationsBucket, storagePath);
        revision.setDocumentContent(documentContent);
    } catch (IOException ioe) {
        throw new BridgeServiceException("Error loading template revision document", ioe);
    }
    return Optional.of(revision);
}
Also used : TemplateRevisionId(org.sagebionetworks.bridge.models.templates.TemplateRevisionId) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) IOException(java.io.IOException)

Example 78 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class ViewCacheTest method nothingWasCachedAndThereIsAnException.

@Test
public void nothingWasCachedAndThereIsAnException() {
    ViewCache cache = new ViewCache();
    cache.setObjectMapper(BridgeObjectMapper.get());
    cache.setCachePeriod(BridgeConstants.BRIDGE_VIEW_EXPIRE_IN_SECONDS);
    CacheKey cacheKey = cache.getCacheKey(App.class, app.getIdentifier());
    CacheProvider provider = mock(CacheProvider.class);
    when(provider.getObject(cacheKey, String.class)).thenReturn(null);
    cache.setCacheProvider(provider);
    // It doesn't get wrapped or transformed or anything
    try {
        cache.getView(cacheKey, new Supplier<App>() {

            @Override
            public App get() {
                throw new BridgeServiceException("There has been a problem retrieving the app");
            }
        });
        fail("This should have thrown an exception");
    } catch (BridgeServiceException e) {
        assertEquals(e.getMessage(), "There has been a problem retrieving the app");
    }
}
Also used : DynamoApp(org.sagebionetworks.bridge.dynamodb.DynamoApp) App(org.sagebionetworks.bridge.models.apps.App) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) Test(org.testng.annotations.Test)

Aggregations

BridgeServiceException (org.sagebionetworks.bridge.exceptions.BridgeServiceException)78 IOException (java.io.IOException)25 Test (org.testng.annotations.Test)21 PersistenceException (javax.persistence.PersistenceException)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 CacheKey (org.sagebionetworks.bridge.cache.CacheKey)5 App (org.sagebionetworks.bridge.models.apps.App)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Stopwatch (com.google.common.base.Stopwatch)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 List (java.util.List)3 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)3 FailedBatch (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.FailedBatch)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 SendMessageResult (com.amazonaws.services.sqs.model.SendMessageResult)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2