Skip to content

Commit

Permalink
avniproject/avni-webapp#1347 | Fix descriptor and sample row content …
Browse files Browse the repository at this point in the history
…for location creat and edit sample files
  • Loading branch information
himeshr committed Oct 7, 2024
1 parent 3874b9d commit e0cfd09
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@

@Service
public class ConceptService implements NonScopeAwareService {
public static final String STRING_CONSTANT_ANY_NUMBER = "Any Number";
public static final String STRING_CONSTANT_ANY_TEXT = "Any Text";
public static final String STRING_CONSTANT_DELIMITER = ", ";
public static final String STRING_CONSTANT_PREFIX = "{";
public static final String STRING_CONSTANT_SUFFIX = "}";
public static final String STRING_CONSTANT_EXAMPLE_NUMBER = "123";
public static final String STRING_CONSTANT_EXAMPLE_TEXT = "ABC";
public static final String EMPTY_STRING = "";

private final Logger logger;
private final ConceptRepository conceptRepository;
private final ConceptAnswerRepository conceptAnswerRepository;
Expand Down Expand Up @@ -374,13 +383,22 @@ public Optional<Concept> findContactNumberConcept() {
}).findFirst();
}

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

public String getExampleValuesForSyncConcept(Concept concept) {
switch (ConceptDataType.valueOf(concept.getDataType())) {
case Numeric: return STRING_CONSTANT_EXAMPLE_NUMBER;
case Text: return STRING_CONSTANT_EXAMPLE_TEXT;
case Coded: return concept.getSortedAnswers().map(sca -> sca.getAnswerConcept().getName()).findAny().get();
default: return EMPTY_STRING;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.avni.server.importer.batch.csv.writer.header.LocationHeaders;

import java.util.Collections;

public interface ImportLocationsConstants {
String STRING_CONSTANT_ONE = "1";
Expand All @@ -22,6 +21,8 @@ public interface ImportLocationsConstants {
String NEW_LOCATION_NAME_EXAMPLE = "Vil C";
String PARENT_LOCATION_WITH_FULL_HIERARCHY_EXAMPLE = "PHC C, Sub C";
String GPS_COORDINATES_EXAMPLE = "Ex: 23.45,43.85";
String ENTER_YOUR_DATA_STARTING_HERE = "Enter your data starting here.";
String PARENT_LOCATION_REQUIRED = "Note: Child locations without parent locations are not allowed.";
String PARENT_LOCATION_REQUIRED = ". Mandatory to mention the parent locations to which the location to be created belongs to.";
String GPS_COORDINATES_SAMPLE = "23.45,43.85";
int STARTING_INDEX = 0;
String[] EXAMPLE_LOCATION_NAMES = {"Jawhar","Sarsun","Dehere","Barso", "Roru"};
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Service
public class ImportService implements ImportLocationsConstants {

public static final Random RANDOM = new Random();
private static final Random RANDOM = new Random();

private final SubjectTypeRepository subjectTypeRepository;
private final FormMappingRepository formMappingRepository;
private static final Logger logger = LoggerFactory.getLogger(ProgramEnrolmentWriter.class);
Expand Down Expand Up @@ -142,27 +144,27 @@ public String getSampleFile(String uploadType) {
return getUsersAndCatchmentsSampleFile();
}

if (uploadSpec[0].equals("Subject")) {
if (uploadSpec[STARTING_INDEX].equals("Subject")) {
SubjectType subjectType = subjectTypeRepository.findByName(uploadSpec[1]);
return getSubjectSampleFile(uploadSpec, response, subjectType);
}

if (uploadSpec[0].equals("ProgramEnrolment")) {
if (uploadSpec[STARTING_INDEX].equals("ProgramEnrolment")) {
Program program = programRepository.findByName(uploadSpec[1]);
return getProgramEnrolmentSampleFile(uploadSpec, response, program);
}

if (uploadSpec[0].equals("ProgramEncounter")) {
if (uploadSpec[STARTING_INDEX].equals("ProgramEncounter")) {
EncounterType encounterType = encounterTypeRepository.findByName(uploadSpec[1]);
return getProgramEncounterSampleFile(uploadSpec, response, encounterType);
}

if (uploadSpec[0].equals("Encounter")) {
if (uploadSpec[STARTING_INDEX].equals("Encounter")) {
EncounterType encounterType = encounterTypeRepository.findByName(uploadSpec[1]);
return getEncounterSampleFile(uploadSpec, response, encounterType);
}

if (uploadSpec[0].equals("GroupMembers")) {
if (uploadSpec[STARTING_INDEX].equals("GroupMembers")) {
return getGroupMembersSampleFile(uploadSpec, response, getSubjectType(uploadSpec[1]));
}

Expand All @@ -183,9 +185,9 @@ private StringBuilder addSampleFileContent(LocationWriter.LocationUploadMode loc
StringBuilder sampleFileBuilder = new StringBuilder();
sampleFileBuilder.append(buildHeaderRowForLocations(locationUploadMode, addressLevelTypes, formElementNamesForLocationTypeFormElements));
sampleFileBuilder.append(STRING_CONSTANT_NEW_LINE);
sampleFileBuilder.append(buildExampleRowForLocations(locationUploadMode, addressLevelTypes, formElementNamesForLocationTypeFormElements));
sampleFileBuilder.append(buildDescriptionRowForLocations(locationUploadMode, addressLevelTypes, formElementNamesForLocationTypeFormElements));
sampleFileBuilder.append(STRING_CONSTANT_NEW_LINE);
sampleFileBuilder.append(buildDescriptionRowForLocations(locationUploadMode));
sampleFileBuilder.append(buildSampleValuesRowForLocations(locationUploadMode, addressLevelTypes, formElementNamesForLocationTypeFormElements));
return sampleFileBuilder;
}

Expand Down Expand Up @@ -223,33 +225,39 @@ private String buildHeaderRowForLocations(LocationWriter.LocationUploadMode loca
return listAsSeparatedString(headers);
}

private String buildExampleRowForLocations(LocationWriter.LocationUploadMode locationUploadMode, List<AddressLevelType> addressLevelTypes,
List<FormElement> formElementNamesForLocationTypeFormElements) {
List<String> examples = new ArrayList<>();
private String buildDescriptionRowForLocations(LocationWriter.LocationUploadMode locationUploadMode, List<AddressLevelType> addressLevelTypes,
List<FormElement> formElementNamesForLocationTypeFormElements) {
List<String> descriptions = new ArrayList<>();
if (LocationWriter.LocationUploadMode.isCreateMode(locationUploadMode)) {
examples.addAll(addressLevelTypes.stream()
descriptions.addAll(addressLevelTypes.stream()
.map(alt -> Example + alt.getName() + STRING_CONSTANT_ONE).collect(Collectors.toList()));
descriptions.set(STARTING_INDEX, descriptions.get(STARTING_INDEX).concat(PARENT_LOCATION_REQUIRED));
} else {
examples.add(LOCATION_WITH_FULL_HIERARCHY_EXAMPLE);
examples.add(NEW_LOCATION_NAME_EXAMPLE);
examples.add(PARENT_LOCATION_WITH_FULL_HIERARCHY_EXAMPLE);
descriptions.add(LOCATION_WITH_FULL_HIERARCHY_DESCRIPTION);
descriptions.add(NEW_LOCATION_NAME_DESCRIPTION);
descriptions.add(PARENT_LOCATION_WITH_FULL_HIERARCHY_DESCRIPTION);
}
examples.add(GPS_COORDINATES_EXAMPLE);
examples.addAll(formElementNamesForLocationTypeFormElements.stream()
.map(fe -> ALLOWED_VALUES + conceptService.getSampleValuesForSyncConcept(fe.getConcept())).collect(Collectors.toList()));
return listAsSeparatedString(examples);
descriptions.add(GPS_COORDINATES_EXAMPLE);
descriptions.addAll(formElementNamesForLocationTypeFormElements.stream()
.map(fe -> ALLOWED_VALUES + conceptService.getAllowedValuesForSyncConcept(fe.getConcept())).collect(Collectors.toList()));
return listAsSeparatedString(descriptions);
}

private String buildDescriptionRowForLocations(LocationWriter.LocationUploadMode locationUploadMode) {
List<String> descriptions = new ArrayList<>();
private String buildSampleValuesRowForLocations(LocationWriter.LocationUploadMode locationUploadMode, List<AddressLevelType> addressLevelTypes,
List<FormElement> formElementNamesForLocationTypeFormElements) {
List<String> sampleValues = new ArrayList<>();
if (LocationWriter.LocationUploadMode.isCreateMode(locationUploadMode)) {
descriptions.add(ENTER_YOUR_DATA_STARTING_HERE + " " + PARENT_LOCATION_REQUIRED);
sampleValues.addAll(IntStream.range(0, addressLevelTypes.size()).mapToObj(idx -> EXAMPLE_LOCATION_NAMES[idx%EXAMPLE_LOCATION_NAMES.length]).collect(Collectors.toList()));
} else {
descriptions.add(LOCATION_WITH_FULL_HIERARCHY_DESCRIPTION);
descriptions.add(NEW_LOCATION_NAME_DESCRIPTION);
descriptions.add(PARENT_LOCATION_WITH_FULL_HIERARCHY_DESCRIPTION);
sampleValues.add(LOCATION_WITH_FULL_HIERARCHY_EXAMPLE);
sampleValues.add(NEW_LOCATION_NAME_EXAMPLE);
sampleValues.add(PARENT_LOCATION_WITH_FULL_HIERARCHY_EXAMPLE);
}
return listAsSeparatedString(descriptions);
sampleValues.add(GPS_COORDINATES_SAMPLE);
sampleValues.addAll(formElementNamesForLocationTypeFormElements.stream()
.map(fe -> conceptService.getExampleValuesForSyncConcept(fe.getConcept()))
.collect(Collectors.toList()));
return listAsSeparatedString(sampleValues);
}

private String getUsersAndCatchmentsSampleFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private List<String> constructSyncAttributeAllowedValuesForSubjectType(SubjectTy

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

Expand Down

0 comments on commit e0cfd09

Please sign in to comment.