Skip to content

Commit

Permalink
avniproject#762 | StringUtils shifted to S.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Aug 28, 2024
1 parent 08f3705 commit 7dba7f8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.avni.server.dao.metabase.DatabaseRepository;
import org.avni.server.domain.metabase.*;
import org.avni.server.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import org.avni.server.util.S;

@Service
public class DatabaseService {
Expand Down Expand Up @@ -91,7 +91,7 @@ private String createRequestBodyForDataset(int sourceTableId) {

public int getFieldIdByTableNameAndFieldName(String tableName, String fieldName) {
JsonNode fieldsArray = databaseRepository.getFields(getDatabaseId());
String snakeCaseTableName = StringUtils.toSnakeCase(tableName);
String snakeCaseTableName = S.toSnakeCase(tableName);
for (JsonNode fieldNode : fieldsArray) {
if (snakeCaseTableName.equals(fieldNode.path("table_name").asText()) && fieldName.equals(fieldNode.path("name").asText())) {
return fieldNode.path("id").asInt();
Expand All @@ -112,19 +112,6 @@ private JsonNode getTableMetadata() {
return databaseRepository.getDataset(requestBody);
}

private String formatName(String rawName) {
String[] parts = rawName.split("_");
StringBuilder formattedName = new StringBuilder();

for (String part : parts) {
formattedName.append(part.substring(0, 1).toUpperCase())
.append(part.substring(1))
.append(" ");
}

return formattedName.toString().trim();
}

public List<String> getSubjectTypeNames() {
JsonNode tableMetadata = getTableMetadata();
List<String> subjectTypeNames = new ArrayList<>();
Expand All @@ -134,7 +121,7 @@ public List<String> getSubjectTypeNames() {
String type = row.get(2).asText();
if (Arrays.asList(TableType.INDIVIDUAL.getTypeName(), TableType.HOUSEHOLD.getTypeName(), TableType.GROUP.getTypeName(), TableType.PERSON.getTypeName()).contains(type)) {
String rawName = row.get(1).asText();
subjectTypeNames.add(formatName(rawName));
subjectTypeNames.add(S.formatName(rawName));
}
}
System.out.println("The subject type names::" + subjectTypeNames);
Expand All @@ -151,7 +138,7 @@ public List<String> getProgramAndEncounterNames() {
String type = row.get(2).asText();
if (Arrays.asList(TableType.PROGRAM_ENCOUNTER.getTypeName(), TableType.PROGRAM_ENROLMENT.getTypeName()).contains(type)) {
String rawName = row.get(1).asText();
programNames.add(formatName(rawName));
programNames.add(S.formatName(rawName));
}
}
System.out.println("The program and encounter::" + programNames);
Expand Down
20 changes: 20 additions & 0 deletions avni-server-api/src/main/java/org/avni/server/util/S.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,24 @@ public static String[] splitMultiSelectAnswer(String answerValue) {
.toArray(String[]::new);
}

public static String toSnakeCase(String input) {
if (input == null) {
return null;
}
return input.trim().replaceAll(" +", "_").toLowerCase();
}

public static String formatName(String rawName) {
String[] parts = rawName.split("_");
StringBuilder formattedName = new StringBuilder();

for (String part : parts) {
formattedName.append(part.substring(0, 1).toUpperCase())
.append(part.substring(1))
.append(" ");
}

return formattedName.toString().trim();
}

}

This file was deleted.

0 comments on commit 7dba7f8

Please sign in to comment.