use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.
the class AdherenceService method updateSessionState.
protected void updateSessionState(String appId, MetadataContainer container, AdherenceRecord asmt) {
TimelineMetadata asmtMeta = container.getMetadata(asmt.getInstanceGuid());
String sessionInstanceGuid = asmtMeta.getSessionInstanceGuid();
List<TimelineMetadata> asmtMetas = scheduleService.getSessionAssessmentMetadata(sessionInstanceGuid);
Set<String> instanceGuids = asmtMetas.stream().map(TimelineMetadata::getAssessmentInstanceGuid).collect(toSet());
instanceGuids.add(sessionInstanceGuid);
PagedResourceList<AdherenceRecord> allRecords = recordDao.getAdherenceRecords(new AdherenceRecordsSearch.Builder().withUserId(asmt.getUserId()).withStudyId(asmt.getStudyId()).withEventTimestamps(ImmutableMap.of(asmtMeta.getSessionStartEventId(), asmt.getEventTimestamp())).withInstanceGuids(instanceGuids).build());
SessionState state = new SessionState(asmtMetas.size());
// The session record may have been submitted, it may be persisted, or
// it may not yet exist, and we take the records in that order.
AdherenceRecord sessionRecord = container.getRecord(sessionInstanceGuid);
for (AdherenceRecord oneRecord : allRecords.getItems()) {
if (sessionInstanceGuid.equals(oneRecord.getInstanceGuid())) {
// The record was persisted
if (sessionRecord == null) {
sessionRecord = oneRecord;
}
} else {
state.add(oneRecord);
}
}
// The record is new and needs to be created
if (sessionRecord == null) {
sessionRecord = new AdherenceRecord();
sessionRecord.setAppId(asmt.getAppId());
sessionRecord.setUserId(asmt.getUserId());
sessionRecord.setStudyId(asmt.getStudyId());
sessionRecord.setInstanceGuid(sessionInstanceGuid);
sessionRecord.setEventTimestamp(asmt.getEventTimestamp());
}
if (state.updateSessionRecord(sessionRecord)) {
container.addRecord(sessionRecord);
}
}
use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.
the class AdherenceService method generateReport.
private <T> T generateReport(String appId, String studyId, String userId, DateTime createdOn, String clientTimeZone, Function<AdherenceState, T> func) {
AdherenceState.Builder builder = new AdherenceState.Builder();
builder.withNow(createdOn);
builder.withClientTimeZone(clientTimeZone);
Study study = studyService.getStudy(appId, studyId, true);
if (study.getScheduleGuid() == null) {
return func.apply(builder.build());
}
List<TimelineMetadata> metadata = scheduleService.getScheduleMetadata(study.getScheduleGuid());
List<StudyActivityEvent> events = studyActivityEventService.getRecentStudyActivityEvents(appId, studyId, userId).getItems();
List<AdherenceRecord> adherenceRecords = getAdherenceRecords(appId, new AdherenceRecordsSearch.Builder().withCurrentTimestampsOnly(true).withIncludeRepeats(true).withAdherenceRecordType(AdherenceRecordType.SESSION).withStudyId(studyId).withUserId(userId).build()).getItems();
builder.withMetadata(metadata);
builder.withEvents(events);
builder.withAdherenceRecords(adherenceRecords);
builder.withStudyStartEventId(study.getStudyStartEventId());
return func.apply(builder.build());
}
use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.
the class MetadataContainerTest method addRecord_addPersistentSessionAdherenceRecord.
@Test
public void addRecord_addPersistentSessionAdherenceRecord() {
AdherenceRecord record = new AdherenceRecord();
record.setStartedOn(TIMESTAMP);
record.setInstanceGuid("instanceGuid");
TimelineMetadata meta = new TimelineMetadata();
meta.setTimeWindowPersistent(true);
meta.setGuid("guid");
when(mockScheduleService.getTimelineMetadata("instanceGuid")).thenReturn(Optional.of(meta));
MetadataContainer container = createContainer(ImmutableList.of(record));
container.addRecord(record);
assertEquals(record, container.getRecord("instanceGuid"));
assertEquals(meta, container.getMetadata("instanceGuid"));
assertEquals(record.getInstanceTimestamp(), TIMESTAMP);
assertTrue(container.getAssessments().isEmpty());
assertFalse(container.getSessionUpdates().isEmpty());
}
use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.
the class MetadataContainerTest method ar.
private AdherenceRecord ar(String asmtInstanceGuid, String sessionInstanceGuid, DateTime startedOn, DateTime finishedOn) {
AdherenceRecord record = new AdherenceRecord();
record.setAppId(TEST_APP_ID);
record.setUserId(TEST_USER_ID);
record.setStudyId(TEST_STUDY_ID);
record.setStartedOn(startedOn);
record.setFinishedOn(finishedOn);
record.setEventTimestamp(TIMESTAMP);
TimelineMetadata meta = new TimelineMetadata();
meta.setAppId(TEST_APP_ID);
meta.setSessionStartEventId("enrollment");
if (asmtInstanceGuid == null) {
// Session
record.setInstanceGuid(sessionInstanceGuid);
meta.setGuid(sessionInstanceGuid);
meta.setSessionInstanceGuid(sessionInstanceGuid);
when(mockScheduleService.getTimelineMetadata(sessionInstanceGuid)).thenReturn(Optional.of(meta));
} else {
// Assessment
record.setInstanceGuid(asmtInstanceGuid);
meta.setGuid(asmtInstanceGuid);
meta.setSessionInstanceGuid(sessionInstanceGuid);
meta.setAssessmentInstanceGuid(asmtInstanceGuid);
when(mockScheduleService.getTimelineMetadata(asmtInstanceGuid)).thenReturn(Optional.of(meta));
}
return record;
}
use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.
the class AdherenceServiceTest method deleteAdherenceRecord_persistentTimeWindow.
@Test
public void deleteAdherenceRecord_persistentTimeWindow() {
AdherenceRecord record = ar(STARTED_ON, FINISHED_ON, "fake-guid", false);
RequestContext.set(new RequestContext.Builder().withCallerRoles(ImmutableSet.of(RESEARCHER)).build());
TimelineMetadata timelineMetadata = new TimelineMetadata();
timelineMetadata.setTimeWindowPersistent(true);
when(mockScheduleService.getTimelineMetadata(any())).thenReturn(Optional.of(timelineMetadata));
service.deleteAdherenceRecord(record);
verify(mockRecordDao).deleteAdherenceRecordPermanently(eq(record));
assertEquals(record.getInstanceTimestamp(), record.getStartedOn());
}
Aggregations