Search in sources :

Example 21 with BadRequestException

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

the class StudyActivityEventService method publishEvent.

/**
 * Publish an event. If the event is being created for the first time and it is the originating (triggering)
 * event for a study burst, the set of study burst events will be created. Editing of both the event
 * and any study burst events is thereafter governed by the update type of the event and study bursts. If
 * both are mutable or future_only, then updating the originating event will update all the study burst
 * events, unless the <code>updateBursts</code> flag is set to false.
 *
 * @param event
 *      the event to publish
 * @param showError
 *      if false (the default), this method returns quietly regardless of the outcome of publishing the event.
 *      If true, it will throw a BadRequestException if any event cannot be published.
 * @param updateBursts
 *      if true (the default), this method will update study burst events based on their update type. If false,
 *      study burst events will not be updated if the update would be an edit of existing events (if the origin
 *      event is being published for the first time, then this flag has to be ignored so the study bursts are
 *      published).
 */
public void publishEvent(StudyActivityEvent event, boolean showError, boolean updateBursts) {
    checkNotNull(event);
    event.setCreatedOn(getCreatedOn());
    Validate.entityThrowingException(CREATE_INSTANCE, event);
    StudyActivityEvent mostRecent = dao.getRecentStudyActivityEvent(event.getUserId(), event.getStudyId(), event.getEventId());
    if (mostRecent != null) {
        event.setOriginEventId(mostRecent.getOriginEventId());
        event.setStudyBurstId(mostRecent.getStudyBurstId());
        event.setPeriodFromOrigin(mostRecent.getPeriodFromOrigin());
        event.setUpdateType(mostRecent.getUpdateType());
    } else {
        // we will honor this, unless the origin event has never been published, then it *has* to
        // trigger creating of the study bursts.
        updateBursts = true;
    }
    // Throwing exceptions will prevent study burst updates from happening if
    // an error occurs in earlier order...so we collect errors and only show
    // them at the end if we want to throw an exception.
    List<String> failedEventIds = new ArrayList<>();
    if (event.getUpdateType().canUpdate(mostRecent, event)) {
        dao.publishEvent(event);
        CacheKey cacheKey = CacheKey.etag(StudyActivityEvent.class, event.getUserId());
        cacheProvider.setObject(cacheKey, event.getCreatedOn());
    } else {
        failedEventIds.add(event.getEventId());
    }
    if (updateBursts) {
        Study study = studyService.getStudy(event.getAppId(), event.getStudyId(), true);
        Schedule2 schedule = scheduleService.getScheduleForStudy(study.getAppId(), study).orElse(null);
        if (schedule != null) {
            createStudyBurstEvents(schedule, event, failedEventIds);
        }
    }
    if (!failedEventIds.isEmpty()) {
        String eventNames = COMMA_SPACE_JOINER.join(failedEventIds);
        if (LOG.isDebugEnabled()) {
            LOG.debug("User " + event.getUserId() + " failed to publish study event(s): " + eventNames);
        }
        if (showError) {
            throw new BadRequestException("Study event(s) failed to publish: " + eventNames + ".");
        }
    }
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) ArrayList(java.util.ArrayList) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) CacheKey(org.sagebionetworks.bridge.cache.CacheKey)

Example 22 with BadRequestException

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

the class ReportService method getParticipantReportV4.

/**
 * Return set of participant report records based on the provided datetime range. Study memberships are enforced.
 */
public ForwardCursorPagedResourceList<ReportData> getParticipantReportV4(final String appId, final String userId, final String identifier, final String healthCode, final DateTime startTime, final DateTime endTime, final String offsetKey, final int pageSize) {
    if (pageSize < API_MINIMUM_PAGE_SIZE || pageSize > API_MAXIMUM_PAGE_SIZE) {
        throw new BadRequestException(BridgeConstants.PAGE_SIZE_ERROR);
    }
    RangeTuple<DateTime> finalTimes = validateDateTimeRange(startTime, endTime);
    ReportDataKey key = new ReportDataKey.Builder().withHealthCode(healthCode).withReportType(ReportType.PARTICIPANT).withIdentifier(identifier).withAppId(appId).build();
    Validate.entityThrowingException(ReportDataKeyValidator.INSTANCE, key);
    ReportIndex index = reportIndexDao.getIndex(key);
    checkParticipantReportAccess(userId, index);
    return reportDataDao.getReportDataV4(key, finalTimes.getStart(), finalTimes.getEnd(), offsetKey, pageSize);
}
Also used : ReportIndex(org.sagebionetworks.bridge.models.reports.ReportIndex) ReportDataKey(org.sagebionetworks.bridge.models.reports.ReportDataKey) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) DateTime(org.joda.time.DateTime)

Example 23 with BadRequestException

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

the class StudyService method deleteStudyPermanently.

public void deleteStudyPermanently(String appId, String studyId) {
    checkNotNull(appId);
    checkNotNull(studyId);
    Study existing = getStudy(appId, studyId, true);
    RequestContext context = RequestContext.get();
    if (!CAN_DELETE_STUDY.contains(existing.getPhase()) && !context.isInRole(ADMIN)) {
        throw new BadRequestException("Study cannot be deleted during phase " + existing.getPhase().label());
    }
    String scheduleGuid = existing.getScheduleGuid();
    studyDao.deleteStudyPermanently(appId, studyId);
    if (scheduleGuid != null) {
        scheduleService.deleteSchedulePermanently(appId, scheduleGuid);
    }
    CacheKey cacheKey = CacheKey.publicStudy(appId, studyId);
    cacheProvider.removeObject(cacheKey);
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) RequestContext(org.sagebionetworks.bridge.RequestContext) CacheKey(org.sagebionetworks.bridge.cache.CacheKey)

Example 24 with BadRequestException

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

the class FileService method getFileRevisions.

public PagedResourceList<FileRevision> getFileRevisions(String appId, String guid, int offset, int pageSize) {
    // Will throw if the file doesn't exist in the caller's app
    getFile(appId, guid);
    if (pageSize < API_MINIMUM_PAGE_SIZE || pageSize > API_MAXIMUM_PAGE_SIZE) {
        throw new BadRequestException(PAGE_SIZE_ERROR);
    }
    PagedResourceList<FileRevision> revisions = fileRevisionDao.getFileRevisions(guid, offset, pageSize);
    for (FileRevision rev : revisions.getItems()) {
        rev.setDownloadURL(getDownloadURL(rev));
    }
    return revisions;
}
Also used : BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) FileRevision(org.sagebionetworks.bridge.models.files.FileRevision)

Example 25 with BadRequestException

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

the class OrganizationService method addMember.

/**
 * Once assigned, only admins can re-assign accounts.
 */
public void addMember(String appId, String identifier, String userId) {
    checkArgument(isNotBlank(appId));
    checkArgument(isNotBlank(identifier));
    checkArgument(isNotBlank(userId));
    AccountId accountId = AccountId.forId(appId, userId);
    accountService.editAccount(accountId, (acct) -> {
        RequestContext context = RequestContext.get();
        if (!context.isInRole(ADMIN) && acct.getOrgMembership() != null) {
            throw new BadRequestException("Account already assigned to an organization.");
        }
        acct.setOrgMembership(identifier);
    });
    sessionUpdateService.updateOrgMembership(userId, identifier);
}
Also used : AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) RequestContext(org.sagebionetworks.bridge.RequestContext)

Aggregations

BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)104 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)23 Test (org.testng.annotations.Test)19 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)17 App (org.sagebionetworks.bridge.models.apps.App)17 DateTime (org.joda.time.DateTime)13 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)10 Study (org.sagebionetworks.bridge.models.studies.Study)10 ArrayList (java.util.ArrayList)9 Account (org.sagebionetworks.bridge.models.accounts.Account)9 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)9 PostMapping (org.springframework.web.bind.annotation.PostMapping)9 UploadSchema (org.sagebionetworks.bridge.models.upload.UploadSchema)8 CacheKey (org.sagebionetworks.bridge.cache.CacheKey)7 AccountId (org.sagebionetworks.bridge.models.accounts.AccountId)7 ScheduleContext (org.sagebionetworks.bridge.models.schedules.ScheduleContext)7 List (java.util.List)5 Survey (org.sagebionetworks.bridge.models.surveys.Survey)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 Map (java.util.Map)4