-
Notifications
You must be signed in to change notification settings - Fork 0
Improvements for sample app #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,3 +3,4 @@ b1e2c3d4-5f6a-7b8c-9d0e-1f2a3b4c5d6e;Springer Nature;DE;orders@springernature.co | |||||
| c2f3d4e5-6a7b-8c9d-0e1f-2a3b4c5d6e7f;O'Reilly Media;US;invoices@oreilly.com | ||||||
| d3a4b5c6-7d8e-9f0a-1b2c-3d4e5f6a7b8c;Elsevier;NL;ap@elsevier.com | ||||||
| e4b5c6d7-8e9f-0a1b-2c3d-4e5f6a7b8c9d;Pearson Education;GB;finance@pearson.com | ||||||
| e4b5c6d7-8e9f-0a1b-2c3d-4e5f6a7b8c9f;Studio Salford;US;@pearson.com | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Invalid email address for new supplier entry The email value Consider providing a valid email address such as
Suggested change
Double-check suggestion before committing. Edit this comment for amendments. Please provide feedback on the review comment by checking the appropriate box:
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,10 +5,13 @@ | |||||||||||||||
|
|
||||||||||||||||
| import cds.gen.sap.capire.bookshop.SupplierInvoices; | ||||||||||||||||
| import cds.gen.sap.capire.bookshop.SupplierInvoices_; | ||||||||||||||||
| import cds.gen.sap.capire.bookshop.Suppliers; | ||||||||||||||||
| import cds.gen.sap.capire.bookshop.Suppliers_; | ||||||||||||||||
| import com.fasterxml.jackson.databind.JsonNode; | ||||||||||||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||||||||||||||
| import com.sap.cds.feature.documentai.generated.cds4j.sap.document.ai.documentaiservice.DocumentExtractionResult; | ||||||||||||||||
| import com.sap.cds.feature.documentai.generated.cds4j.sap.document.ai.documentaiservice.DocumentExtractionResultContext; | ||||||||||||||||
| import com.sap.cds.ql.Select; | ||||||||||||||||
| import com.sap.cds.ql.Update; | ||||||||||||||||
| import com.sap.cds.services.cds.ApplicationService; | ||||||||||||||||
| import com.sap.cds.services.handler.EventHandler; | ||||||||||||||||
|
|
@@ -17,7 +20,9 @@ | |||||||||||||||
| import com.sap.cds.services.persistence.PersistenceService; | ||||||||||||||||
| import java.math.BigDecimal; | ||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||
| import java.util.Locale; | ||||||||||||||||
| import java.util.Map; | ||||||||||||||||
| import java.util.Optional; | ||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||||||||||||||
|
|
@@ -26,7 +31,7 @@ | |||||||||||||||
| /** | ||||||||||||||||
| * Handles extraction results from the SAP Document AI plugin. Parses the extraction JSON returned | ||||||||||||||||
| * by the DIE service and populates the corresponding SupplierInvoice entity with the extracted | ||||||||||||||||
| * header fields (invoice number, date, total amount, currency). | ||||||||||||||||
| * header fields (invoice number, date, total amount, currency, and matched supplier). | ||||||||||||||||
| */ | ||||||||||||||||
| @Component | ||||||||||||||||
| @ServiceName(value = "*", type = ApplicationService.class) | ||||||||||||||||
|
|
@@ -50,18 +55,15 @@ public void onExtractionCompleted(DocumentExtractionResultContext context) { | |||||||||||||||
| JsonNode root = objectMapper.readTree(data.getExtractionResult()); | ||||||||||||||||
| JsonNode headerFields = root.path("extraction").path("headerFields"); | ||||||||||||||||
|
|
||||||||||||||||
| // Extract relevant header fields from the DIE response | ||||||||||||||||
| String documentNumber = getHeaderFieldValue(headerFields, "documentNumber"); | ||||||||||||||||
| String documentDate = getHeaderFieldValue(headerFields, "documentDate"); | ||||||||||||||||
| String currencyCode = getHeaderFieldValue(headerFields, "currencyCode"); | ||||||||||||||||
| BigDecimal grossAmount = getHeaderFieldNumber(headerFields, "grossAmount"); | ||||||||||||||||
| String senderName = getHeaderFieldValue(headerFields, "senderName"); | ||||||||||||||||
|
|
||||||||||||||||
| logger.info( | ||||||||||||||||
| "[bookshop] Extracted: number={}, date={}, amount={} {}", | ||||||||||||||||
| documentNumber, | ||||||||||||||||
| documentDate, | ||||||||||||||||
| grossAmount, | ||||||||||||||||
| currencyCode); | ||||||||||||||||
| "[bookshop] Extracted: number={}, date={}, amount={} {}, sender={}", | ||||||||||||||||
| documentNumber, documentDate, grossAmount, currencyCode, senderName); | ||||||||||||||||
|
|
||||||||||||||||
| // Update the invoice that is currently in EXTRACTING status. | ||||||||||||||||
| // In a production app you would correlate by a stored jobId; for this sample | ||||||||||||||||
|
|
@@ -73,6 +75,18 @@ public void onExtractionCompleted(DocumentExtractionResultContext context) { | |||||||||||||||
| if (grossAmount != null) updateData.put(SupplierInvoices.TOTAL_AMOUNT, grossAmount); | ||||||||||||||||
| if (currencyCode != null) updateData.put(SupplierInvoices.CURRENCY_CODE, currencyCode); | ||||||||||||||||
|
|
||||||||||||||||
| // Match extracted sender name to a Supplier record (case-insensitive) | ||||||||||||||||
| if (senderName != null) { | ||||||||||||||||
| findSupplierByName(senderName) | ||||||||||||||||
| .ifPresentOrElse( | ||||||||||||||||
| supplier -> { | ||||||||||||||||
| updateData.put(SupplierInvoices.SUPPLIER_ID, supplier.getId()); | ||||||||||||||||
| logger.info( | ||||||||||||||||
| "[bookshop] Matched supplier: {} ({})", supplier.getName(), supplier.getId()); | ||||||||||||||||
| }, | ||||||||||||||||
| () -> logger.warn("[bookshop] No supplier found for sender name: {}", senderName)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| long updated = | ||||||||||||||||
| db.run( | ||||||||||||||||
| Update.entity(SupplierInvoices_.CDS_NAME) | ||||||||||||||||
|
|
@@ -93,6 +107,15 @@ public void onExtractionCompleted(DocumentExtractionResultContext context) { | |||||||||||||||
| context.setCompleted(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| private Optional<Suppliers> findSupplierByName(String name) { | ||||||||||||||||
| String normalized = name.toLowerCase(Locale.ROOT); | ||||||||||||||||
| return db.run(Select.from(Suppliers_.class)) | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance: For even a modest number of suppliers this is wasteful — every extraction event loads all supplier rows. The filter should be pushed down to the database using a return db.run(Select.from(Suppliers_.class)
.where(s -> s.name().eq(name)))
.listOf(Suppliers.class)
.stream()
.filter(s -> s.getName() != null && normalized.equals(s.getName().toLowerCase(Locale.ROOT)))
.findFirst();This at least avoids a full-table scan for exact matches and falls back to the in-memory case-insensitive check only when needed.
Suggested change
Double-check suggestion before committing. Edit this comment for amendments. Please provide feedback on the review comment by checking the appropriate box:
|
||||||||||||||||
| .listOf(Suppliers.class) | ||||||||||||||||
| .stream() | ||||||||||||||||
| .filter(s -> s.getName() != null && normalized.equals(s.getName().toLowerCase(Locale.ROOT))) | ||||||||||||||||
| .findFirst(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| private String getHeaderFieldValue(JsonNode headerFields, String fieldName) { | ||||||||||||||||
| for (JsonNode field : headerFields) { | ||||||||||||||||
| if (fieldName.equals(field.path("name").asText())) { | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best Practices: The new
@titleannotations forinvoiceNumber,supplier, andstatusduplicate labels already defined inline on everyLineItemandFieldGroupentry in the same file.@titleon a property acts as a default label and is useful when no explicitLabelis provided. Since all existingLineItemandFieldGroupdata fields already carry explicitLabelkeys that match these titles, this block adds no functional value and creates a maintenance risk (two places to update labels). Consider removing the block or replacing the per-fieldLabel:properties with this single source of truth instead.Please provide feedback on the review comment by checking the appropriate box: