Skip to content

Commit

Permalink
avniproject/avni-webapp#1146 | Improve "Users & Catchments" sample CS…
Browse files Browse the repository at this point in the history
…V file by adding Description row
  • Loading branch information
himeshr committed May 23, 2024
1 parent 6b5a124 commit 35fceb0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,23 @@ private String getUsersAndCatchmentsSampleFile() {
try (InputStream csvFileResourceStream = this.getClass().getResourceAsStream("/usersAndCatchments.csv")) {
BufferedReader csvReader = new BufferedReader(new InputStreamReader(csvFileResourceStream));
List<String> headersForSubjectTypesWithSyncAttributes = appendHeaderRow(sampleFileBuilder, csvReader);
appendDescription(sampleFileBuilder, csvReader);
appendSampleValues(sampleFileBuilder, csvReader, headersForSubjectTypesWithSyncAttributes);
} catch (IOException e) {
throw new RuntimeException(e);
}
return sampleFileBuilder.toString();
}

private void appendDescription(StringBuilder sampleFileBuilder, BufferedReader csvReader) throws IOException {
String descriptionRow = csvReader.readLine();
List<String> allowedValuesForSubjectTypesWithSyncAttributes = subjectTypeService.constructSyncAttributeAllowedValuesForSubjectTypes();
String syncAttributesSampleValues = String.join(",", allowedValuesForSubjectTypesWithSyncAttributes);

descriptionRow = allowedValuesForSubjectTypesWithSyncAttributes.isEmpty() ? descriptionRow : String.format("%s,%s", descriptionRow ,syncAttributesSampleValues);
sampleFileBuilder.append("\n").append(descriptionRow);
}

private void appendSampleValues(StringBuilder sampleFileBuilder, BufferedReader csvReader, List<String> headersForSubjectTypesWithSyncAttributes) throws IOException {
String toBeAppendedValuesForSyncAttributeConcepts = constructSampleSyncAttributeConceptValues(headersForSubjectTypesWithSyncAttributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ public List<String> constructSyncAttributeHeadersForSubjectTypes() {
Predicate<SubjectType> subjectTypeHasSyncAttributes = subjectType ->
Objects.nonNull(subjectType.getSyncRegistrationConcept1()) ||
Objects.nonNull(subjectType.getSyncRegistrationConcept2());

return subjectTypes.stream().
return subjectTypes.stream().sorted((a,b) -> (int) (a.getId() - b.getId())).
filter(subjectTypeHasSyncAttributes).
map(this::constructSyncAttributeHeadersForSubjectType).
flatMap(Collection::stream).
Expand All @@ -217,11 +216,43 @@ private List<String> constructSyncAttributeHeadersForSubjectType(SubjectType sub
subjectTypeWithSyncAttribute.getSyncRegistrationConcept2()};

return Arrays.stream(syncAttributes).
filter(Objects::nonNull).
filter(Objects::nonNull).sorted().
map(sa -> String.format("%s->%s", subjectTypeWithSyncAttribute.getName(), conceptService.get(sa).getName())).
collect(Collectors.toList());
}

public List<String> constructSyncAttributeAllowedValuesForSubjectTypes() {
List<SubjectType> subjectTypes = subjectTypeRepository.findByIsVoidedFalse();
Predicate<SubjectType> subjectTypeHasSyncAttributes = subjectType ->
Objects.nonNull(subjectType.getSyncRegistrationConcept1()) ||
Objects.nonNull(subjectType.getSyncRegistrationConcept2());
return subjectTypes.stream().sorted((a,b) -> (int) (a.getId() - b.getId())).
filter(subjectTypeHasSyncAttributes).
map(this::constructSyncAttributeAllowedValuesForSubjectType).
flatMap(Collection::stream).
collect(Collectors.toList());
}

private List<String> constructSyncAttributeAllowedValuesForSubjectType(SubjectType subjectTypeWithSyncAttribute) {
String[] syncAttributes = new String[]{subjectTypeWithSyncAttribute.getSyncRegistrationConcept1(),
subjectTypeWithSyncAttribute.getSyncRegistrationConcept2()};

return Arrays.stream(syncAttributes).
filter(Objects::nonNull).sorted().
map(sa -> String.format("Allowed values: %s", getSampleValuesForSyncConcept(conceptService.get(sa))))
.collect(Collectors.toList());
}

private String getSampleValuesForSyncConcept(Concept concept) {
switch (ConceptDataType.valueOf(concept.getDataType())) {
case Numeric: return "Any Number";
case Text: return "Any Text";
case Coded: return concept.getSortedAnswers().map(sca -> sca.getConcept().getName())
.collect(Collectors.joining(", ", "{", "}"));
default: return String.format("Appropriate value for a %s type concept", concept.getDataType());
}
}

public JsonObject getDefaultSettings() {
JsonObject defaultSettings = new JsonObject();
defaultSettings.put(String.valueOf(SubjectTypeSettingKey.displayPlannedEncounters), true);
Expand Down
9 changes: 5 additions & 4 deletions avni-server-api/src/main/resources/usersAndCatchments.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Location with full hierarchy,Catchment Name,Username,Full Name of User,Email,Phone,Language,Track Location,Date picker mode,Enable Beneficiary mode,Beneficiary ID Prefix,User Groups
"PHC B, Sub B, Vil B",Org X Cat A,user29@orgx,user29,[email protected],+919876543210,English,yes,calendar,no,U29,"Group 1|Group 2"
"PHC B, Sub B, Vil C",Org X Cat A,user29@orgx,user29,[email protected],+919876543210,English,yes,calendar,no,U29,"Group 1|Group 2
"PHC B, Sub C, Vil C",Org X Cat A,user29@orgx,user29,[email protected],+919876543210,English,yes,calendar,no,U29,"Group 1|Group 2
Location with full hierarchy,Catchment Name,Username,Full Name of User,Email Address,Mobile Number,Preferred Language,Track Location,Date picker mode,Enable Beneficiary mode,Identifier Prefix,User Groups
"- mandatory field\n- Can be found from Admin -> Locations -> Click Export\n",- mandatory field,- mandatory field,- mandatory field,- mandatory field,"- mandatory field\n- prepend the mobile number with country code","- Allowed values: English, Marathi\n- Only single value allowed\n- default: English","- Allowed values: yes, no\n- default: no","- Allowed values: calendar, spinner \n- default: calendar","- Allowed values: yes, no\n- default: no",,"- Allowed values: Administrator, POSHAN Sathi, QRT Users"
"PHC B, Sub B, Vil B",Org X Cat A,user29@orgx,user29,[email protected],+919876543210,English,yes,calendar,no,U29,"Group 1,Group 2"
"PHC B, Sub B, Vil C",Org X Cat A,user29@orgx,user29,[email protected],+919876543210,English,yes,calendar,no,U29,"Group 1,Group 2"
"PHC B, Sub C, Vil C",Org X Cat A,user29@orgx,user29,[email protected],+919876543210,English,yes,calendar,no,U29,"Group 1,Group 2"
"PHC B, Sub B, Vil C",Org X Cat B,user49@orgx,user49,[email protected],+919876543210,English,yes,,no,U49,"Group 1"
"PHC B, Sub C, Vil C",Org X Cat B,user49@orgx,user49,[email protected],+919876543210,English,yes,,no,U49,"Group 1"
"PHC C, Sub C, Vil C",Org X Cat B,user49@orgx,user49,[email protected],+919876543210,English,yes,,no,U49,"Group 1"
Expand Down

0 comments on commit 35fceb0

Please sign in to comment.