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 + ".");
}
}
}
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);
}
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);
}
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;
}
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);
}
Aggregations