Skip to content

Commit

Permalink
Merge pull request #40 from AAFC-BICoE/35319_Update_to_dina-base_0.132
Browse files Browse the repository at this point in the history
35319 Update to dina-base 0.132
  • Loading branch information
brandonandre authored Dec 18, 2024
2 parents 73fa388 + 40192f6 commit d608251
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<maven-checkstyle-plugin.version>3.4.0</maven-checkstyle-plugin.version>
<checkstyle.version>10.17.0</checkstyle.version>

<dina-base-api.version>0.128</dina-base-api.version>
<dina-base-api.version>0.132</dina-base-api.version>
<spring-boot-maven-plugin.fork>false</spring-boot-maven-plugin.fork>
<zxing.version>3.5.1</zxing.version>
<openhtml.version>1.0.10</openhtml.version>
Expand Down Expand Up @@ -67,6 +67,11 @@
<artifactId>dina-messaging</artifactId>
<version>${dina-base-api.version}</version>
</dependency>
<dependency>
<groupId>io.github.aafc-bicoe</groupId>
<artifactId>dina-json</artifactId>
<version>${dina-base-api.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import ca.gc.aafc.dina.export.api.output.DataOutput;
import ca.gc.aafc.dina.export.api.service.DataExportStatusService;
import ca.gc.aafc.dina.export.api.source.ElasticSearchDataSource;
import ca.gc.aafc.dina.json.JsonHelper;
import ca.gc.aafc.dina.jsonapi.JSONApiDocumentStructure;
import ca.gc.aafc.dina.jsonapi.JsonPathHelper;

import static ca.gc.aafc.dina.export.api.config.JacksonTypeReferences.LIST_MAP_TYPEREF;
import static ca.gc.aafc.dina.export.api.config.JacksonTypeReferences.MAP_TYPEREF;
import static ca.gc.aafc.dina.jsonapi.JSONApiDocumentStructure.atJsonPtr;

import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
Expand Down Expand Up @@ -60,6 +60,7 @@ public class TabularDataExportGenerator extends DataExportGenerator {
private static final String COORDINATES_DD_FORMAT = "%f,%f";
private static final String DEFAULT_CONCAT_SEP = ",";


private final ObjectMapper objectMapper;
private final ElasticSearchDataSource elasticSearchDataSource;
private final Configuration jsonPathConfiguration;
Expand Down Expand Up @@ -87,7 +88,6 @@ public TabularDataExportGenerator(
*/
@Async(DataExportConfig.DINA_THREAD_POOL_BEAN_NAME)
public CompletableFuture<UUID> export(DataExport dinaExport) throws IOException {

DataExport.ExportStatus currStatus = waitForRecord(dinaExport.getUuid());

// Should only work for NEW record at this point
Expand Down Expand Up @@ -192,7 +192,7 @@ private void processRecord(String documentId, JsonNode record,
return;
}

Optional<JsonNode> attributes = atJsonPtr(record, JSONApiDocumentStructure.ATTRIBUTES_PTR);
Optional<JsonNode> attributes = JsonHelper.atJsonPtr(record, JSONApiDocumentStructure.ATTRIBUTES_PTR);
if (attributes.isPresent() && attributes.get() instanceof ObjectNode attributeObjNode) {

attributeObjNode.put(JSONApiDocumentStructure.ID, documentId);
Expand Down Expand Up @@ -251,9 +251,9 @@ private Map<String, Object> extractById(String id, List<Map<String, Object>> doc
private Map<String, Object> flatRelationships(JsonNode jsonApiDocumentRecord) {

Optional<JsonNode> relNodeOpt =
atJsonPtr(jsonApiDocumentRecord, JSONApiDocumentStructure.RELATIONSHIP_PTR);
JsonHelper.atJsonPtr(jsonApiDocumentRecord, JSONApiDocumentStructure.RELATIONSHIP_PTR);
Optional<JsonNode> includedNodeOpt =
atJsonPtr(jsonApiDocumentRecord, JSONApiDocumentStructure.INCLUDED_PTR);
JsonHelper.atJsonPtr(jsonApiDocumentRecord, JSONApiDocumentStructure.INCLUDED_PTR);

if (relNodeOpt.isEmpty() || includedNodeOpt.isEmpty()) {
return Map.of();
Expand All @@ -280,7 +280,7 @@ private Map<String, Object> flatRelationships(JsonNode jsonApiDocumentRecord) {
// pull the nested-document from the included section
flatRelationships.put(relName,
extractById(idValue, includedDoc).get(JSONApiDocumentStructure.ATTRIBUTES));
} else if (jsonNodeHasFieldAndIsArray(currRelNode, JSONApiDocumentStructure.DATA)) {
} else if (JsonHelper.hasFieldAndIsArray(currRelNode, JSONApiDocumentStructure.DATA)) {
// if "data" is an array (to-many)
List<Map<String, Object>> toMerge = new ArrayList<>();
currRelNode.get(JSONApiDocumentStructure.DATA).elements().forEachRemaining(el -> {
Expand All @@ -300,28 +300,6 @@ private Map<String, Object> flatRelationships(JsonNode jsonApiDocumentRecord) {
return flatRelationships;
}

/**
* Checks if a JSON node has a specific field and if that field's value is an array.
*
* @param node The JSON node to check.
* @param fieldName The name of the field to check.
* @return True if the node has the field and its value is an array, false otherwise.
*/
private static boolean jsonNodeHasFieldAndIsArray(JsonNode node, String fieldName) {
return node.has(fieldName) && node.get(fieldName).isArray();
}

/**
* Replace with dina-base function when 0.132 is available.
*
* @param objNode
* @param fieldName
* @return
*/
private static String safeAsText(ObjectNode objNode, String fieldName) {
return objNode.has(fieldName) ? objNode.get(fieldName).asText() : "";
}

/**
* Gets all the text for the "attributes" specified by the columns and concatenate them using
* the default separator.
Expand All @@ -332,7 +310,7 @@ private static String safeAsText(ObjectNode objNode, String fieldName) {
private static String handleConcatFunction(ObjectNode attributeObjNod, List<String> columns) {
List<String> toConcat = new ArrayList<>();
for (String col : columns) {
toConcat.add(safeAsText(attributeObjNod, col));
toConcat.add(JsonHelper.safeAsText(attributeObjNod, col));
}
return String.join(DEFAULT_CONCAT_SEP, toConcat);
}
Expand Down

0 comments on commit d608251

Please sign in to comment.