Search in sources :

Example 1 with StudyActivityEventIdsMap

use of org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap in project BridgeServer2 by Sage-Bionetworks.

the class ActivityEventController method deleteActivityEventForSelf.

@DeleteMapping("/v5/studies/{studyId}/participants/self/activityevents/{eventId}")
public StatusMessage deleteActivityEventForSelf(@PathVariable String studyId, @PathVariable String eventId, @RequestParam(required = false) String showError) {
    UserSession session = getAuthenticatedAndConsentedSession();
    if (!session.getParticipant().getStudyIds().contains(studyId)) {
        throw new EntityNotFoundException(Account.class);
    }
    StudyActivityEventRequest request = new StudyActivityEventRequest(eventId, null, null, null);
    StudyActivityEventIdsMap eventMap = studyService.getStudyActivityEventIdsMap(session.getAppId(), studyId);
    boolean showErrorBool = "true".equals(showError);
    studyActivityEventService.deleteEvent(request.parse(eventMap).withAppId(session.getAppId()).withStudyId(studyId).withUserId(session.getId()).build(), showErrorBool);
    return EVENT_DELETED_MSG;
}
Also used : StudyActivityEventRequest(org.sagebionetworks.bridge.models.activities.StudyActivityEventRequest) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 2 with StudyActivityEventIdsMap

use of org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceService method cleanupSearch.

protected AdherenceRecordsSearch cleanupSearch(String appId, AdherenceRecordsSearch search) {
    checkNotNull(appId);
    checkNotNull(search);
    // optimization: skip all this if not relevant to the search
    boolean skipFixes = search.getEventTimestamps().isEmpty() && search.getInstanceGuids().isEmpty() && Boolean.FALSE.equals(search.getCurrentTimestampsOnly());
    if (skipFixes) {
        return search;
    }
    AdherenceRecordsSearch.Builder builder = search.toBuilder();
    if (TRUE.equals(search.getCurrentTimestampsOnly()) || !search.getEventTimestamps().isEmpty()) {
        StudyActivityEventIdsMap eventMap = studyService.getStudyActivityEventIdsMap(appId, search.getStudyId());
        Map<String, DateTime> fixedMap = new HashMap<>();
        if (TRUE.equals(search.getCurrentTimestampsOnly())) {
            // This adds current server timestamps to the search filters
            Map<String, DateTime> events = studyActivityEventService.getRecentStudyActivityEvents(appId, search.getStudyId(), search.getUserId()).getItems().stream().collect(toMap(StudyActivityEvent::getEventId, StudyActivityEvent::getTimestamp));
            addToMap(events, eventMap, fixedMap);
        }
        if (!search.getEventTimestamps().isEmpty()) {
            // This fixes things like failing to put a "custom:" prefix on a custom event.
            addToMap(search.getEventTimestamps(), eventMap, fixedMap);
        }
        builder.withEventTimestamps(fixedMap);
    }
    // to a different map.
    if (!search.getInstanceGuids().isEmpty()) {
        Map<String, DateTime> map = new HashMap<>();
        Set<String> instanceGuids = new HashSet<>();
        for (String instanceGuid : search.getInstanceGuids()) {
            String[] split = instanceGuid.split("@", 2);
            if (split.length == 2) {
                map.put(split[0], DateTime.parse(split[1]));
            } else {
                instanceGuids.add(instanceGuid);
            }
        }
        builder.withInstanceGuidStartedOnMap(map);
        builder.withInstanceGuids(instanceGuids);
    }
    return builder.build();
}
Also used : HashMap(java.util.HashMap) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) AdherenceRecordsSearch(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecordsSearch) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet)

Example 3 with StudyActivityEventIdsMap

use of org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap in project BridgeServer2 by Sage-Bionetworks.

the class StudyService method getStudyActivityEventIdsMap.

public StudyActivityEventIdsMap getStudyActivityEventIdsMap(String appId, String studyId) {
    StudyActivityEventIdsMap map = new StudyActivityEventIdsMap();
    Study study = getStudy(appId, studyId, true);
    map.addCustomEvents(study.getCustomEvents());
    Schedule2 schedule = scheduleService.getScheduleForStudy(appId, study).orElse(null);
    if (schedule != null) {
        map.addStudyBursts(schedule.getStudyBursts());
    }
    return map;
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap)

Example 4 with StudyActivityEventIdsMap

use of org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap in project BridgeServer2 by Sage-Bionetworks.

the class Schedule2Service method preValidationCleanup.

/**
 * Set GUIDs on objects that don't have them; clean up event keys or set
 * them to null if they're not valid, so they will fail validation.
 */
void preValidationCleanup(Study study, Schedule2 schedule, Consumer<HasGuid> consumer) {
    checkNotNull(study);
    checkNotNull(schedule);
    StudyActivityEventIdsMap map = new StudyActivityEventIdsMap();
    map.addCustomEvents(study.getCustomEvents());
    map.addStudyBursts(schedule.getStudyBursts());
    for (Session session : schedule.getSessions()) {
        consumer.accept(session);
        session.setSchedule(schedule);
        for (TimeWindow window : session.getTimeWindows()) {
            consumer.accept(window);
        }
        List<String> events = session.getStartEventIds().stream().map(s -> formatActivityEventId(map, s)).collect(toList());
        session.setStartEventIds(events);
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) Study(org.sagebionetworks.bridge.models.studies.Study) CAN_EDIT_SCHEDULES(org.sagebionetworks.bridge.AuthUtils.CAN_EDIT_SCHEDULES) INCLUDE_DELETED(org.sagebionetworks.bridge.models.ResourceList.INCLUDE_DELETED) OFFSET_BY(org.sagebionetworks.bridge.models.ResourceList.OFFSET_BY) LoggerFactory(org.slf4j.LoggerFactory) NEGATIVE_OFFSET_ERROR(org.sagebionetworks.bridge.BridgeConstants.NEGATIVE_OFFSET_ERROR) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestContext(org.sagebionetworks.bridge.RequestContext) StringUtils(org.apache.commons.lang3.StringUtils) Timeline(org.sagebionetworks.bridge.models.schedules2.timelines.Timeline) ParticipantScheduleGenerator(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantScheduleGenerator) Session(org.sagebionetworks.bridge.models.schedules2.Session) ORG_ID(org.sagebionetworks.bridge.AuthEvaluatorField.ORG_ID) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) PAGE_SIZE(org.sagebionetworks.bridge.models.ResourceList.PAGE_SIZE) API_MAXIMUM_PAGE_SIZE(org.sagebionetworks.bridge.BridgeConstants.API_MAXIMUM_PAGE_SIZE) Account(org.sagebionetworks.bridge.models.accounts.Account) ADMIN(org.sagebionetworks.bridge.Roles.ADMIN) List(java.util.List) Schedule2Dao(org.sagebionetworks.bridge.dao.Schedule2Dao) Optional(java.util.Optional) BridgeUtils(org.sagebionetworks.bridge.BridgeUtils) API_MINIMUM_PAGE_SIZE(org.sagebionetworks.bridge.BridgeConstants.API_MINIMUM_PAGE_SIZE) CAN_READ_STUDIES(org.sagebionetworks.bridge.AuthUtils.CAN_READ_STUDIES) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Stopwatch(com.google.common.base.Stopwatch) Validate(org.sagebionetworks.bridge.validators.Validate) Scheduler(org.sagebionetworks.bridge.models.schedules2.timelines.Scheduler) INSTANCE(org.sagebionetworks.bridge.validators.Schedule2Validator.INSTANCE) TimelineMetadata(org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata) ParticipantSchedule(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) PAGE_SIZE_ERROR(org.sagebionetworks.bridge.BridgeConstants.PAGE_SIZE_ERROR) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) AdherenceState(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceState) PublishedEntityException(org.sagebionetworks.bridge.exceptions.PublishedEntityException) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SUPERADMIN(org.sagebionetworks.bridge.Roles.SUPERADMIN) DateTime(org.joda.time.DateTime) CAN_READ_SCHEDULES(org.sagebionetworks.bridge.AuthUtils.CAN_READ_SCHEDULES) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) STUDY_ID(org.sagebionetworks.bridge.AuthEvaluatorField.STUDY_ID) BridgeUtils.formatActivityEventId(org.sagebionetworks.bridge.BridgeUtils.formatActivityEventId) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) CAN_CREATE_SCHEDULES(org.sagebionetworks.bridge.AuthUtils.CAN_CREATE_SCHEDULES) HasGuid(org.sagebionetworks.bridge.models.schedules2.HasGuid) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Session(org.sagebionetworks.bridge.models.schedules2.Session)

Example 5 with StudyActivityEventIdsMap

use of org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap in project BridgeServer2 by Sage-Bionetworks.

the class StudyActivityEventServiceTest method getStudyActivityEventHistory.

@Test
public void getStudyActivityEventHistory() {
    List<StudyActivityEvent> list = new ArrayList<>();
    PagedResourceList<StudyActivityEvent> page = new PagedResourceList<>(list, 100);
    when(mockDao.getStudyActivityEventHistory(TEST_USER_ID, TEST_STUDY_ID, "custom:event1", 10, 100)).thenReturn(page);
    Account account = Account.create();
    account.setId(TEST_USER_ID);
    when(mockAccountService.getAccount(ACCOUNT_ID)).thenReturn(Optional.of(account));
    StudyActivityEventIdsMap eventMap = new StudyActivityEventIdsMap();
    eventMap.addCustomEvents(ImmutableList.of(new StudyCustomEvent("event1", MUTABLE)));
    when(mockStudyService.getStudyActivityEventIdsMap(TEST_APP_ID, TEST_STUDY_ID)).thenReturn(eventMap);
    PagedResourceList<StudyActivityEvent> retValue = service.getStudyActivityEventHistory(ACCOUNT_ID, TEST_STUDY_ID, "custom:event1", 10, 100);
    assertSame(retValue, page);
    assertEquals(retValue.getRequestParams().get("offsetBy"), Integer.valueOf(10));
    assertEquals(retValue.getRequestParams().get("pageSize"), Integer.valueOf(100));
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) StudyCustomEvent(org.sagebionetworks.bridge.models.studies.StudyCustomEvent) ArrayList(java.util.ArrayList) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) Test(org.testng.annotations.Test)

Aggregations

StudyActivityEventIdsMap (org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap)30 Test (org.testng.annotations.Test)21 StudyCustomEvent (org.sagebionetworks.bridge.models.studies.StudyCustomEvent)18 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)11 Account (org.sagebionetworks.bridge.models.accounts.Account)9 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)7 App (org.sagebionetworks.bridge.models.apps.App)6 PagedResourceList (org.sagebionetworks.bridge.models.PagedResourceList)5 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)4 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)4 StudyActivityEventRequest (org.sagebionetworks.bridge.models.activities.StudyActivityEventRequest)4 AdherenceRecordsSearch (org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecordsSearch)4 StudyAdherenceReportGeneratorTest (org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)4 Study (org.sagebionetworks.bridge.models.studies.Study)4 DateTime (org.joda.time.DateTime)3 StatusMessage (org.sagebionetworks.bridge.models.StatusMessage)3 Schedule2 (org.sagebionetworks.bridge.models.schedules2.Schedule2)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 RequestContext (org.sagebionetworks.bridge.RequestContext)2