use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.
the class Schedule2ServiceTest method getParticipantSchedule.
@Test
public void getParticipantSchedule() {
RequestContext.set(new RequestContext.Builder().withCallerUserId(TEST_USER_ID).withCallerEnrolledStudies(ImmutableSet.of(TEST_STUDY_ID)).build());
Account account = Account.create();
account.setAppId(TEST_APP_ID);
account.setId(TEST_USER_ID);
account.setClientTimeZone("America/Chicago");
Study study = Study.create();
study.setIdentifier(TEST_STUDY_ID);
study.setScheduleGuid(SCHEDULE_GUID);
when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
Schedule2 schedule = Schedule2Test.createValidSchedule();
when(mockDao.getSchedule(TEST_APP_ID, SCHEDULE_GUID)).thenReturn(Optional.of(schedule));
ResourceList<StudyActivityEvent> events = new ResourceList<>(ImmutableList.of());
when(mockStudyActivityEventService.getRecentStudyActivityEvents(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID)).thenReturn(events);
ParticipantSchedule retValue = service.getParticipantSchedule(TEST_APP_ID, TEST_STUDY_ID, account);
assertEquals(retValue.getClientTimeZone(), "America/Chicago");
assertEquals(retValue.getCreatedOn(), CREATED_ON.withZone(DateTimeZone.forID("America/Chicago")));
}
use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.
the class ParticipantScheduleGenerator method generate.
public ParticipantSchedule generate(AdherenceState state, Timeline timeline) {
Multimap<LocalDate, ScheduledSession> chronology = LinkedHashMultimap.create();
LocalDate earliestDate = LATEST_LOCAL_DATE;
LocalDate latestDate = EARLIEST_LOCAL_DATE;
Map<String, DateTime> eventTimestamps = new HashMap<>();
for (ScheduledSession schSession : timeline.getSchedule()) {
String eventId = schSession.getStartEventId();
DateTime eventTimestamp = state.getEventTimestampById(eventId);
if (eventTimestamp == null) {
continue;
}
LocalDate startDate = eventTimestamp.plusDays(schSession.getStartDay()).toLocalDate();
LocalDate endDate = eventTimestamp.plusDays(schSession.getEndDay()).toLocalDate();
eventTimestamps.put(eventId, eventTimestamp);
ScheduledSession.Builder builder = schSession.toBuilder();
builder.withStartDate(startDate);
builder.withEndDate(endDate);
if (startDate.isBefore(earliestDate)) {
earliestDate = startDate;
}
if (endDate.isAfter(latestDate)) {
latestDate = endDate;
}
for (ScheduledAssessment schAssessment : schSession.getAssessments()) {
ScheduledAssessment.Builder asmtBuilder = new ScheduledAssessment.Builder().withRefKey(schAssessment.getRefKey()).withInstanceGuid(schAssessment.getInstanceGuid());
builder.withScheduledAssessment(asmtBuilder.build());
}
// null these out, not useful
schSession.getTimeWindow().setGuid(null);
builder.withStartDay(null);
builder.withEndDay(null);
chronology.put(startDate, builder.build());
}
// chronology.size() is the total number of pairs, not the total number of keys (and thus correct).
List<ScheduledSession> scheduledSessions = Lists.newArrayListWithCapacity(chronology.size());
for (LocalDate date : chronology.keySet()) {
scheduledSessions.addAll(chronology.get(date));
}
scheduledSessions.sort(SCHEDULED_SESSION_COMPARATOR);
DateRange range = null;
if (earliestDate.isBefore(latestDate)) {
range = new DateRange(earliestDate, latestDate);
}
ParticipantSchedule schedule = new ParticipantSchedule();
schedule.setCreatedOn(state.getNow());
schedule.setClientTimeZone(state.getClientTimeZone());
schedule.setDateRange(range);
schedule.setSchedule(scheduledSessions);
schedule.setSessions(timeline.getSessions().stream().map(SessionInfo::createScheduleEntry).collect(toList()));
schedule.setAssessments(timeline.getAssessments());
schedule.setStudyBursts(timeline.getStudyBursts());
schedule.setEventTimestamps(eventTimestamps);
return schedule;
}
use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.
the class StudyParticipantController method getParticipantScheduleForSelf.
@EtagSupport({ // Most recent modification to the schedule
@EtagCacheKey(model = Schedule2.class, keys = { "appId", "studyId" }), // Most recent modification to the participant’s collection of events
@EtagCacheKey(model = StudyActivityEvent.class, keys = { "userId" }), // Most recent modification to the participant’s time zone
@EtagCacheKey(model = DateTimeZone.class, keys = { "userId" }) })
@GetMapping("/v5/studies/{studyId}/participants/self/schedule")
public ParticipantSchedule getParticipantScheduleForSelf(@PathVariable String studyId) {
UserSession session = getAuthenticatedAndConsentedSession();
// This produces the desired error (unauthorized rather than 404)
if (!session.getParticipant().getStudyIds().contains(studyId)) {
throw new UnauthorizedException("Caller is not enrolled in study '" + studyId + "'");
}
AccountId accountId = AccountId.forId(session.getAppId(), session.getId());
Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
// Even if the call fails, we want to know they tried it
DateTime timelineRequestedOn = getDateTime();
RequestInfo requestInfo = getRequestInfoBuilder(session).withTimelineAccessedOn(timelineRequestedOn).build();
requestInfoService.updateRequestInfo(requestInfo);
ParticipantSchedule schedule = scheduleService.getParticipantSchedule(session.getAppId(), studyId, account);
studyActivityEventService.publishEvent(new StudyActivityEvent.Builder().withAppId(session.getAppId()).withStudyId(studyId).withUserId(session.getId()).withObjectType(TIMELINE_RETRIEVED).withTimestamp(timelineRequestedOn).build(), false, true);
return schedule;
}
use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.
the class StudyParticipantControllerTest method getParticipantScheduleForUser.
@Test
public void getParticipantScheduleForUser() {
RequestContext.set(new RequestContext.Builder().withCallerUserId("id").withOrgSponsoredStudies(ImmutableSet.of(TEST_STUDY_ID)).withCallerRoles(ImmutableSet.of(STUDY_COORDINATOR)).build());
mockAccountInStudy();
ParticipantSchedule schedule = new ParticipantSchedule();
when(mockScheduleService.getParticipantSchedule(eq(TEST_APP_ID), eq(TEST_STUDY_ID), any())).thenReturn(schedule);
ParticipantSchedule retValue = controller.getParticipantScheduleForUser(TEST_STUDY_ID, TEST_USER_ID);
assertSame(retValue, schedule);
}
use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.
the class ParticipantScheduleGeneratorTest method test.
@Test
public void test() throws Exception {
Schedule2 schedule = Schedule2Test.createValidSchedule();
Timeline timeline = Scheduler.INSTANCE.calculateTimeline(schedule);
StudyActivityEvent e1 = new StudyActivityEvent.Builder().withEventId("timeline_retrieved").withTimestamp(TIMESTAMP).build();
AdherenceState state = new AdherenceState.Builder().withClientTimeZone(TIME_ZONE.getID()).withNow(TIMESTAMP.withZone(TIMEZONE_MSK)).withEvents(ImmutableList.of(e1)).build();
ParticipantSchedule retValue = INSTANCE.generate(state, timeline);
assertEquals(retValue.getCreatedOn(), TIMESTAMP.withZone(TIME_ZONE));
assertEquals(retValue.getClientTimeZone(), TIME_ZONE.getID());
assertEquals(retValue.getDateRange().getStartDate().toString(), "2015-02-02");
assertEquals(retValue.getDateRange().getEndDate().toString(), "2015-03-16");
// These values are all copied over from the timeline, so they don't need to be
// examined very closely. They are all tested in other tests.
assertEquals(retValue.getSessions().size(), 1);
assertEquals(retValue.getStudyBursts().size(), 1);
assertEquals(retValue.getAssessments().size(), 2);
assertEquals(retValue.getEventTimestamps().size(), 1);
assertEquals(retValue.getEventTimestamps().get("timeline_retrieved"), TIMESTAMP.withZone(TIME_ZONE));
// But the schedule is different
assertEquals(retValue.getSchedule().size(), 7);
assertScheduledSession(retValue.getSchedule().get(0), "NpLGwpRYGr3cYjJvp945zQ", LocalDate.parse("2015-02-02"), LocalDate.parse("2015-02-02"), "MDoRIcMJpAZ3Xqy_uZAbnw", "AzGvv4ph-7Xzi9VRIrFyWw");
assertScheduledSession(retValue.getSchedule().get(1), "0dVupumHdJENCi5rzjA1sQ", LocalDate.parse("2015-02-09"), LocalDate.parse("2015-02-09"), "mZTon_L0lXXErPKme-Ojhg", "okkx1iuHoh6MV8I8I9vYYQ");
assertScheduledSession(retValue.getSchedule().get(2), "JzKooqt2k1rdn7A1E7j1Hg", LocalDate.parse("2015-02-16"), LocalDate.parse("2015-02-16"), "Pp55NkTwC1MWmAhdZWLD7A", "B2hnjJKGkFQ83rLCzkPDmA");
assertScheduledSession(retValue.getSchedule().get(3), "8arBYlLtL93TdSjhVYDjfg", LocalDate.parse("2015-02-23"), LocalDate.parse("2015-02-23"), "4_qpTNQfPsIwVK9m0ok7YQ", "h-0BJQhWuU34QODRfLjRlg");
assertScheduledSession(retValue.getSchedule().get(4), "qUvQ7R3FYbl2j-pEZ42ESQ", LocalDate.parse("2015-03-02"), LocalDate.parse("2015-03-02"), "y0deGrdsyat-i3CHxWzzkg", "9HaDixbXUid_O4Dc5m7Zog");
assertScheduledSession(retValue.getSchedule().get(5), "XuyuOyJF5zs5s4cdqQISCg", LocalDate.parse("2015-03-09"), LocalDate.parse("2015-03-09"), "_IHC7vBzvy62AAOZDXqe6A", "MmMA1dT66qzTSnugAY-reQ");
assertScheduledSession(retValue.getSchedule().get(6), "ym78tZHtMLaEfvx4-f9OGg", LocalDate.parse("2015-03-16"), LocalDate.parse("2015-03-16"), "xkucPnJH3lz_BAJ5X1DaLw", "mBwW2oM-rXEcNO7LIVi5PQ");
}
Aggregations