Skip to content

Commit

Permalink
remove additional comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sonika-shah committed Sep 30, 2024
1 parent 354ffb3 commit 24dba51
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,14 @@ public static List<String> fieldToExtensionStrings(String field) throws IOExcept
return List.of();
}

// Step 1: Replace semicolons within quoted strings with a placeholder
// Replace semicolons within quoted strings with a placeholder
String preprocessedField =
Pattern.compile("\"([^\"]*)\"") // Matches content inside double quotes
.matcher(field)
.replaceAll(mr -> "\"" + mr.group(1).replace(";", "__SEMICOLON__") + "\"");

// Step 2: Escape newlines and double quotes for CSV parsing
preprocessedField = preprocessedField.replace("\n", "\\n").replace("\"", "\\\"");

// Step 3: Define CSV format with semicolon as the delimiter and proper handling of quotes
CSVFormat format =
CSVFormat.DEFAULT
.withDelimiter(';')
Expand All @@ -143,7 +141,6 @@ public static List<String> fieldToExtensionStrings(String field) throws IOExcept
.withIgnoreEmptyLines(true)
.withEscape('\\'); // Use backslash for escaping special characters

// Step 4: Parse the CSV and process the records
try (CSVParser parser = CSVParser.parse(new StringReader(preprocessedField), format)) {
return parser.getRecords().stream()
.flatMap(CSVRecord::stream)
Expand Down Expand Up @@ -297,17 +294,14 @@ public static List<String> addExtension(List<String> csvRecord, Object extension
}

private static String formatValue(Object value) {
// Handle Map (e.g., entity reference or date interval)
if (value instanceof Map) {
return formatMapValue((Map<String, Object>) value);
}

// Handle List (e.g., Entity Reference List or multi-select Enum List)
if (value instanceof List) {
return formatListValue((List<?>) value);
}

// Fallback for simple types
return value != null ? value.toString() : "";
}

Expand All @@ -318,7 +312,6 @@ private static String formatMapValue(Map<String, Object> valueMap) {
return formatTimeInterval(valueMap);
}

// If no specific format, return the raw map string
return valueMap.toString();
}

Expand All @@ -328,17 +321,14 @@ private static String formatListValue(List<?> list) {
}

if (list.get(0) instanceof Map && isEnumWithDescriptions((Map<String, Object>) list.get(0))) {
// Handle a list of maps with keys and descriptions
return list.stream()
.map(item -> ((Map<String, Object>) item).get("key").toString())
.collect(Collectors.joining(INTERNAL_ARRAY_SEPARATOR));
} else if (list.get(0) instanceof Map) {
// Handle a list of entity references or other maps
return list.stream()
.map(item -> formatMapValue((Map<String, Object>) item))
.collect(Collectors.joining(INTERNAL_ARRAY_SEPARATOR));
} else {
// Handle a simple list of strings or numbers
return list.stream()
.map(Object::toString)
.collect(Collectors.joining(INTERNAL_ARRAY_SEPARATOR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,27 +335,24 @@ public Map<String, Object> getExtension(CSVPrinter printer, CSVRecord csvRecord,
return null;
}

// Parse the extension string into a map of key-value pairs
Map<String, Object> extensionMap = new HashMap<>();

for (String extensions : fieldToExtensionStrings(extensionString)) {
// Split on the first occurrence of ENTITY_TYPE_SEPARATOR to get key-value pair
int separatorIndex = extensions.indexOf(ENTITY_TYPE_SEPARATOR);

if (separatorIndex == -1) {
// No separator found, invalid entry
importFailure(printer, invalidExtension(fieldNumber, extensions, "null"), csvRecord);
continue;
}

String key = extensions.substring(0, separatorIndex); // Get the key part
String value = extensions.substring(separatorIndex + 1); // Get the value part
String key = extensions.substring(0, separatorIndex);
String value = extensions.substring(separatorIndex + 1);

// Validate that the key and value are present
if (key.isEmpty() || value.isEmpty()) {
importFailure(printer, invalidExtension(fieldNumber, key, value), csvRecord);
} else {
extensionMap.put(key, value); // Add to the map
extensionMap.put(key, value);
}
}

Expand All @@ -370,7 +367,6 @@ private void validateExtension(
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();

// Fetch the JSON schema and property type for the given field name
JsonSchema jsonSchema = TypeRegistry.instance().getSchema(entityType, fieldName);
if (jsonSchema == null) {
importFailure(printer, invalidCustomPropertyKey(fieldNumber, fieldName), csvRecord);
Expand All @@ -379,8 +375,6 @@ private void validateExtension(
String customPropertyType = TypeRegistry.getCustomPropertyType(entityType, fieldName);
String propertyConfig = TypeRegistry.getCustomPropertyConfig(entityType, fieldName);

// Validate field based on the custom property type

switch (customPropertyType) {
case "entityReference", "entityReferenceList" -> {
boolean isList = "entityReferenceList".equals(customPropertyType);
Expand Down Expand Up @@ -444,13 +438,11 @@ private Object parseEntityReferences(
throws IOException {
List<EntityReference> entityReferences = new ArrayList<>();

// Split the field into individual references or handle as single entity
List<String> entityRefStrings =
isList
? listOrEmpty(fieldToInternalArray(fieldValue)) // Split by 'INTERNAL_ARRAY_SEPARATOR'
: Collections.singletonList(fieldValue); // Single entity reference
? listOrEmpty(fieldToInternalArray(fieldValue))
: Collections.singletonList(fieldValue);

// Process each entity reference string
for (String entityRefStr : entityRefStrings) {
List<String> entityRefTypeAndValue = listOrEmpty(fieldToEntities(entityRefStr));

Expand Down Expand Up @@ -549,17 +541,14 @@ protected String getFormattedDateTimeField(
case "date-cp" -> {
TemporalAccessor date = formatter.parse(fieldValue);
yield formatter.format(date);
// Parse and format as date
}
case "dateTime-cp" -> {
LocalDateTime dateTime = LocalDateTime.parse(fieldValue, formatter);
yield dateTime.format(formatter);
// Parse and format as LocalDateTime
}
case "time-cp" -> {
LocalTime time = LocalTime.parse(fieldValue, formatter);
yield time.format(formatter);
// Parse and format as LocalTime
}
default -> throw new IllegalStateException("Unexpected value: " + fieldType);
};
Expand Down Expand Up @@ -609,11 +598,9 @@ private void validateAndUpdateExtension(
Map<String, Object> extensionMap,
JsonSchema jsonSchema)
throws IOException {
// Convert the field value into a JsonNode
if (fieldValue != null) {
JsonNode jsonNodeValue = JsonUtils.convertValue(fieldValue, JsonNode.class);

// Validate the field value using the JSON schema
Set<ValidationMessage> validationMessages = jsonSchema.validate(jsonNodeValue);
if (!validationMessages.isEmpty()) {
importFailure(
Expand All @@ -622,7 +609,7 @@ private void validateAndUpdateExtension(
fieldNumber, fieldName, customPropertyType, validationMessages.toString()),
csvRecord);
} else {
extensionMap.put(fieldName, fieldValue); // Add to extensionMap if valid
extensionMap.put(fieldName, fieldValue);
}
}
}
Expand Down

0 comments on commit 24dba51

Please sign in to comment.