Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1358|Improve Subject - sample CSV file #823

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Service
@@ -203,8 +204,8 @@ public List<AddressLevelType> getAddressLevelTypesForCreateModeSingleHierarchy(S

private String listAsSeparatedString(List<String> rowItems) {
return rowItems.stream()
.map(rowItem -> String.format(STRING_PLACEHOLDER_BLOCK, rowItem))
.collect(Collectors.joining(STRING_CONSTANT_SEPARATOR));
.map(rowItem -> String.format("\"%s\"", rowItem.replace("\"", "")))
.collect(Collectors.joining(","));
}

private String buildHeaderRowForLocations(LocationWriter.LocationUploadMode locationUploadMode, List<AddressLevelType> addressLevelTypes,
@@ -324,13 +325,128 @@ private String getEncounterSampleFile(String[] uploadSpec, String response, Enco
return addToResponse(response, formMapping);
}

private String getSubjectSampleFile(String[] uploadSpec, String response, SubjectType subjectType) {
SubjectHeaders subjectHeaders = new SubjectHeaders(subjectType);
response = addToResponse(response, Arrays.asList(subjectHeaders.getAllHeaders()));
response = addToResponse(response, addressLevelTypeRepository.getAllNames());
FormMapping formMapping = formMappingRepository.getRequiredFormMapping(getSubjectType(uploadSpec[1]).getUuid(), null, null, FormType.IndividualProfile);
return addToResponse(response, formMapping);
private String getSubjectSampleFile(String[] uploadSpec, String response, SubjectType subjectType) {
StringBuilder sampleFileBuilder = new StringBuilder();

try (InputStream csvFileResourceStream = this.getClass().getResourceAsStream("/bulkuploads/sample/subjects.csv")) {
BufferedReader csvReader = new BufferedReader(new InputStreamReader(csvFileResourceStream));

List<String> formElementHeaders = appendHeaderRow(sampleFileBuilder, csvReader, subjectType);

appendDescriptionRow(sampleFileBuilder, csvReader, formElementHeaders,
formMappingRepository.getRequiredFormMapping(subjectType.getUuid(), null, null, FormType.IndividualProfile));

appendSampleRows(sampleFileBuilder, csvReader, formElementHeaders,
formMappingRepository.getRequiredFormMapping(subjectType.getUuid(), null, null, FormType.IndividualProfile));

appendEntryRow(sampleFileBuilder, csvReader, formElementHeaders);

} catch (IOException e) {
throw new RuntimeException(e);
}

return sampleFileBuilder.toString();
}

private List<String> appendHeaderRow(StringBuilder sampleFileBuilder, BufferedReader csvReader,
SubjectType subjectType) throws IOException {
String headerRow = csvReader.readLine();

FormMapping formMapping = formMappingRepository.getRequiredFormMapping(
subjectType.getUuid(),
null,
null,
FormType.IndividualProfile
);

List<String> formElementHeaders = formMapping.getForm().getApplicableFormElements().stream()
.filter(formElement -> !ConceptDataType.isQuestionGroup(formElement.getConcept().getDataType()))
.map(this::getHeaderName)
.collect(Collectors.toList());

String fullHeaderRow = formElementHeaders.isEmpty() ? headerRow :
headerRow + "," + String.join(",", formElementHeaders);
sampleFileBuilder.append(fullHeaderRow);

return formElementHeaders;
}

private void appendDescriptionRow(StringBuilder sampleFileBuilder, BufferedReader csvReader,
List<String> formElementHeaders, FormMapping formMapping) throws IOException {
String descriptionRow = csvReader.readLine();

if (!formElementHeaders.isEmpty()) {
String formElementDescriptions = formElementHeaders.stream()
.map((String h) -> {
String headerName = h.replace("\"", "");
FormElement formElement = formMapping.getForm().getApplicableFormElements().stream()
.filter(fe -> getHeaderName(fe).replace("\"", "").equals(headerName))
.findFirst()
.orElse(null);
return String.format("\"%s\"", ALLOWED_VALUES +
(formElement != null ? conceptService.getSampleValuesForSyncConcept(formElement.getConcept()) : ""));
})
.collect(Collectors.joining(","));
descriptionRow = descriptionRow + "," + formElementDescriptions;
}

sampleFileBuilder.append(STRING_CONSTANT_NEW_LINE).append(descriptionRow);
}

private void appendSampleRows(StringBuilder sampleFileBuilder, BufferedReader csvReader,
List<String> formElementHeaders, FormMapping formMapping) throws IOException {
IntStream.range(0, 3)
.mapToObj(i -> {
try {
return csvReader.readLine();
} catch (IOException e) {
return null;
}
})
.filter(Objects::nonNull)
.forEach(sampleRow -> {
if (!formElementHeaders.isEmpty()) {
String formElementSamples = formElementHeaders.stream()
.map((String h) -> {
String headerName = h.replace("\"", "");
FormElement formElement = formMapping.getForm().getApplicableFormElements().stream()
.filter(fe -> getHeaderName(fe).replace("\"", "").equals(headerName))
.findFirst()
.orElse(null);

if (formElement != null) {
String conceptValues = conceptService.getSampleValuesForSyncConcept(formElement.getConcept())
.replaceAll("[{}]", "");
String[] values = conceptValues.split(",");
if (values.length > 0) {
return String.format("\"%s\"", values[0].trim());
}
}

return "\"Any Text\"";
})
.collect(Collectors.joining(","));

sampleRow = sampleRow + "," + formElementSamples;
}

sampleFileBuilder.append(STRING_CONSTANT_NEW_LINE).append(sampleRow);
});
}

private void appendEntryRow(StringBuilder sampleFileBuilder, BufferedReader csvReader,
List<String> formElementHeaders) throws IOException {
String entryRow = csvReader.readLine();

if (!formElementHeaders.isEmpty()) {
String formElementEntries = formElementHeaders.stream()
.map(header -> "\"\"")
.collect(Collectors.joining(","));
entryRow = entryRow + "," + formElementEntries;
}

sampleFileBuilder.append(STRING_CONSTANT_NEW_LINE).append(entryRow);
}

private String getProgramEnrolmentSampleFile(String[] uploadSpec, String response, Program program) {
response = addToResponse(response, Arrays.asList(new ProgramEnrolmentHeaders(program).getAllHeaders()));
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Id","Subject Type","First Name","Last Name","Profile Picture","Date Of Birth","Date Of Birth Verified","Date Of Registration","Registration Location","Gender"
"Identifier for this row. Optional. If not provided, this will create a new item. You can use either the uuid for this row if you want to update it, or an assigned identifier you have from a previous system you are trying to import.","Allowed values:{Person, Household, Group, Individual}","Mandatory field","Optional","Url/link that points to the picture","Date format: yyyy-mm-dd","Allowed values: {yes, no, y, n, 1, 0}","Date format: yyyy-mm-dd","Allowed values: {latitude, longitude}","Allowed values: {female, male, other Gender}"
"123e4567-e89b-12d3-a456-426614174000","Individual","user","dummy","https://example.com/profile1.jpg","2024-12-12","yes","2024-12-12","7.123","female"
"123e4567-e89b-12d3-a456-426614174000","Individual","user","dummy","https://example.com/profile1.jpg","2024-01-12","n","2024-12-12","9.09","female"
"123e4567-e89b-12d3-a456-426614174000","Individual","user","dummy","https://example.com/profile1.jpg","2024-12-12","yes","2024-12-12","8.21","female"
"",