Skip to content

Commit

Permalink
JN-1055 import kitrequest data (#911)
Browse files Browse the repository at this point in the history
Co-authored-by: Sampath K. Settipalli <[email protected]>
  • Loading branch information
ssettipalli and Sampath K. Settipalli authored May 28, 2024
1 parent 91678c0 commit 0b0c521
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ public List<Import> findAllByStudyEnv(UUID studyEnvId) {
return findAllByProperty("study_environment_id", studyEnvId);
}

public void deleteByStudyEnvId(UUID studyEnvId) {
deleteByProperty("study_environment_id", studyEnvId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public List<ImportItem> findAllByImport(UUID importId) {
return findAllByProperty("import_id", importId);
}

public void deleteByImportId(UUID importId) {
deleteByProperty("import_id", importId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ public void deleteEnrolleeById(UUID id) {
}
}

public void deleteByImportId(UUID importId) {
dao.deleteByImportId(importId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ public void deleteEnrolleesByImportId(UUID id) {
});
}

@Transactional
public void deleteByStudyEnvId(UUID studyEnvironmentId) {
List<Import> imports = dao.findAllByStudyEnv(studyEnvironmentId);
imports.forEach(dataImport -> importItemService.deleteByImportId(dataImport.getId()));
dao.deleteByStudyEnvId(studyEnvironmentId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import bio.terra.pearl.core.model.dataimport.ImportItemStatus;
import bio.terra.pearl.core.model.dataimport.ImportStatus;
import bio.terra.pearl.core.model.dataimport.ImportType;
import bio.terra.pearl.core.model.audit.ResponsibleEntity;
import bio.terra.pearl.core.model.kit.KitRequest;
import bio.terra.pearl.core.model.kit.KitType;
import bio.terra.pearl.core.model.participant.Enrollee;
import bio.terra.pearl.core.model.participant.ParticipantUser;
import bio.terra.pearl.core.model.participant.PortalParticipantUser;
Expand All @@ -23,9 +24,12 @@
import bio.terra.pearl.core.service.dataimport.ImportService;
import bio.terra.pearl.core.service.exception.internal.InternalServerException;
import bio.terra.pearl.core.service.export.formatters.module.EnrolleeFormatter;
import bio.terra.pearl.core.service.export.formatters.module.KitRequestFormatter;
import bio.terra.pearl.core.service.export.formatters.module.ParticipantUserFormatter;
import bio.terra.pearl.core.service.export.formatters.module.ProfileFormatter;
import bio.terra.pearl.core.service.export.formatters.module.SurveyFormatter;
import bio.terra.pearl.core.service.kit.KitRequestDto;
import bio.terra.pearl.core.service.kit.KitRequestService;
import bio.terra.pearl.core.service.participant.EnrolleeService;
import bio.terra.pearl.core.service.participant.ParticipantUserService;
import bio.terra.pearl.core.service.participant.PortalParticipantUserService;
Expand Down Expand Up @@ -92,6 +96,7 @@ public class EnrolleeImportService {
private final TimeShiftPopulateDao timeShiftPopulateDao;
private final ImportService importService;
private final ImportItemService importItemService;
private final KitRequestService kitRequestService;
private final char CSV_DELIMITER = ',';
private final char TSV_DELIMITER = '\t';

Expand All @@ -100,7 +105,7 @@ public EnrolleeImportService(RegistrationService registrationService, Enrollment
SurveyResponseService surveyResponseService, ParticipantTaskService participantTaskService, PortalService portalService,
ImportService importService, ImportItemService importItemService, SurveyTaskDispatcher surveyTaskDispatcher,
TimeShiftPopulateDao timeShiftPopulateDao, EnrolleeService enrolleeService, ParticipantUserService participantUserService,
PortalParticipantUserService portalParticipantUserService) {
PortalParticipantUserService portalParticipantUserService, KitRequestService kitRequestService) {
this.registrationService = registrationService;
this.enrollmentService = enrollmentService;
this.profileService = profileService;
Expand All @@ -115,6 +120,7 @@ public EnrolleeImportService(RegistrationService registrationService, Enrollment
this.enrolleeService = enrolleeService;
this.participantUserService = participantUserService;
this.portalParticipantUserService = portalParticipantUserService;
this.kitRequestService = kitRequestService;
}

@Transactional
Expand Down Expand Up @@ -144,7 +150,7 @@ public Import importEnrollees(String portalShortcode, String studyShortcode, Stu
Enrollee enrollee = null;
ImportItem importItem;
try {
enrollee = importEnrollee(portalShortcode, studyShortcode, studyEnv, enrolleeMap, exportOptions);
enrollee = importEnrollee(portalShortcode, studyShortcode, studyEnv, enrolleeMap, exportOptions, adminId);
importItem = ImportItem.builder()
.createdEnrolleeId(enrollee.getId())
.importId(dataImport.getId())
Expand Down Expand Up @@ -204,7 +210,7 @@ public List<Map<String, String>> generateImportMaps(InputStream in, ImportFileFo
}
}

public Enrollee importEnrollee(String portalShortcode, String studyShortcode, StudyEnvironment studyEnv, Map<String, String> enrolleeMap, ExportOptions exportOptions) {
public Enrollee importEnrollee(String portalShortcode, String studyShortcode, StudyEnvironment studyEnv, Map<String, String> enrolleeMap, ExportOptions exportOptions, UUID adminId) {
/** while importing handle update for existing import
if same enrolle: update enrollee
if same participant & same portal.. new enrollee & same profile
Expand All @@ -215,7 +221,6 @@ public Enrollee importEnrollee(String portalShortcode, String studyShortcode, St
DataAuditInfo.systemProcessName(getClass(), "importEnrollee")
).build();

//
ParticipantUserFormatter participantUserFormatter = new ParticipantUserFormatter(exportOptions);
final ParticipantUser participantUserInfo = participantUserFormatter.fromStringMap(studyEnv.getId(), enrolleeMap);

Expand All @@ -226,6 +231,9 @@ public Enrollee importEnrollee(String portalShortcode, String studyShortcode, St
/** now update the profile */
Profile profile = importProfile(enrolleeMap, regResult.profile(), exportOptions, studyEnv, auditInfo);

/** populate kit_requests */
importKitRequests(enrolleeMap, adminId, enrollee);

importSurveyResponses(portalShortcode, enrolleeMap, exportOptions, studyEnv, regResult.portalParticipantUser(), enrollee, auditInfo);

/** restore email */
Expand All @@ -234,6 +242,33 @@ public Enrollee importEnrollee(String portalShortcode, String studyShortcode, St
return enrollee;
}

private void importKitRequests(Map<String, String> enrolleeMap, UUID adminId, Enrollee enrollee) {
new KitRequestFormatter().listFromStringMap(enrolleeMap).stream().map(
kitRequestDto -> kitRequestService.create(convertKitRequestDto(adminId, enrollee, kitRequestDto))).toList();
}

private KitRequest convertKitRequestDto(
UUID adminUserId,
Enrollee enrollee,
KitRequestDto kitRequestDto) {

KitType kitType = kitRequestService.lookupKitTypeByName(kitRequestDto.getKitType().getName());
return KitRequest.builder()
.creatingAdminUserId(adminUserId)
.enrolleeId(enrollee.getId())
.status(kitRequestDto.getStatus())
.sentToAddress(kitRequestDto.getSentToAddress())
.skipAddressValidation(kitRequestDto.isSkipAddressValidation())
.kitTypeId(kitType.getId())
.createdAt(kitRequestDto.getCreatedAt())
.labeledAt(kitRequestDto.getLabeledAt())
.sentAt(kitRequestDto.getSentAt())
.receivedAt(kitRequestDto.getReceivedAt())
.trackingNumber(kitRequestDto.getTrackingNumber())
.returnTrackingNumber(kitRequestDto.getReturnTrackingNumber())
.build();
}

private RegistrationService.RegistrationResult registerIfNeeded(String portalShortcode, StudyEnvironment studyEnv, ParticipantUser participantUserInfo) {
return portalParticipantUserService.findOne(participantUserInfo.getUsername(), portalShortcode, studyEnv.getEnvironmentName())
.map(ppUser -> new RegistrationService.RegistrationResult(
Expand Down Expand Up @@ -322,6 +357,6 @@ protected SurveyResponse importSurveyResponse(UUID portalId, SurveyFormatter for
}
// we're not worrying about dating the response yet
return surveyResponseService.updateResponse(response, new ResponsibleEntity(DataAuditInfo.systemProcessName(getClass(), "importSurveyResponse")),
ppUser, enrollee, relatedTask.getId(), portalId).getResponse();
ppUser, enrollee, relatedTask.getId(), portalId).getResponse();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package bio.terra.pearl.core.service.export.formatters.item;

import bio.terra.pearl.core.model.kit.KitType;
import bio.terra.pearl.core.service.export.DataValueExportType;
import bio.terra.pearl.core.service.kit.KitRequestDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;

@Slf4j
public class KitTypeFormatter extends PropertyItemFormatter<KitRequestDto> {
public KitTypeFormatter() {
super("kitType", KitRequestDto.class);
this.baseColumnKey = "kitType";
this.dataType = DataValueExportType.STRING;
}

@Override
public String getExportString(KitRequestDto bean) {
return bean.getKitType().getName();
}

@Override
public void importValueToBean(KitRequestDto bean, String exportString) {
if (exportString != null) {
try {
PropertyUtils.setNestedProperty(bean, getPropertyName(), KitType.builder().name(exportString).build());
} catch (Exception e) {
log.warn("error setting property " + getPropertyName(), e);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package bio.terra.pearl.core.service.export.formatters.module;

import bio.terra.pearl.core.model.kit.KitRequestStatus;
import bio.terra.pearl.core.service.export.EnrolleeExportData;
import bio.terra.pearl.core.service.export.formatters.item.KitRequestTypeFormatter;
import bio.terra.pearl.core.service.export.formatters.item.KitTypeFormatter;
import bio.terra.pearl.core.service.export.formatters.item.PropertyItemFormatter;
import bio.terra.pearl.core.service.kit.KitRequestDto;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand All @@ -14,14 +17,15 @@
public class KitRequestFormatter extends ModuleFormatter<KitRequestDto, PropertyItemFormatter<KitRequestDto>> {
private static final String KIT_REQUEST_MODULE_NAME = "sample_kit";
private static final List<String> KIT_REQUEST_INCLUDED_PROPERTIES =
List.of("status", "sentToAddress", "sentAt", "receivedAt");
List.of("status", "sentToAddress", "sentAt", "receivedAt", "createdAt", "labeledAt",
"trackingNumber", "returnTrackingNumber", "skipAddressValidation");

public KitRequestFormatter() {
itemFormatters = KIT_REQUEST_INCLUDED_PROPERTIES.stream()
.map(propName -> new PropertyItemFormatter<KitRequestDto>(propName, KitRequestDto.class))
.collect(Collectors.toList());
// we have to handle kitType separately because we'll need to match it to the kitType name
itemFormatters.add(new KitRequestTypeFormatter());
itemFormatters.add(new KitTypeFormatter());
moduleName = KIT_REQUEST_MODULE_NAME;
displayName = "Sample kit";
}
Expand All @@ -43,4 +47,38 @@ public Map<String, String> toStringMap(EnrolleeExportData enrolleeData) {
maxNumRepeats = Math.max(maxNumRepeats, sortedKitRequests.size());
return allKitMap;
}

public List<KitRequestDto> listFromStringMap(Map<String, String> enrolleeMap) {
List<KitRequestDto> kitRequests = new ArrayList<>();
int requestNum = 1;
KitRequestDto kitRequestDto = getKitRequestDto(enrolleeMap, requestNum);
while (kitRequestDto != null) {
kitRequests.add(kitRequestDto);
requestNum++;
kitRequestDto = getKitRequestDto(enrolleeMap, requestNum);
}
return kitRequests;
}

private KitRequestDto getKitRequestDto(Map<String, String> enrolleeMap, int requestNum) {
KitRequestDto kitRequestDto = null;
for (PropertyItemFormatter<KitRequestDto> itemFormatter : itemFormatters) {
String columnName = getColumnKey(itemFormatter, false, null, requestNum);
String stringVal = enrolleeMap.get(columnName);
if (StringUtils.isEmpty(stringVal)) {
continue;
}
if (kitRequestDto == null) {
kitRequestDto = new KitRequestDto();
}
if (columnName.contains(".status")) {
//enum lookup
kitRequestDto.setStatus(KitRequestStatus.valueOf(stringVal));
} else {
itemFormatter.importValueToBean(kitRequestDto, stringVal);
}
}
return kitRequestDto;
}

}
Loading

0 comments on commit 0b0c521

Please sign in to comment.