Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Issue #214
  • Loading branch information
rsoika committed Sep 25, 2024
1 parent 76848e0 commit 7e4c9f6
Showing 1 changed file with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@
* <pre>
* {@code
<e-invoice name="READ">
<entity>invoice.number=//rsm:CrossIndustryInvoice/rsm:ExchangedDocument/ram:ID</entity>
<entity>invoice.date=//rsm:CrossIndustryInvoice/rsm:ExchangedDocument/ram:IssueDateTime</entity>
</e-invoice>
<e-invoice name="ENTITY">
<name>invoice.date</name>
<type>date</type>
<xpath>//rsm:CrossInvoice/ram:ID</xpath>
</e-invoice>
* }
* </pre>
*
* In 'READ' mode the currently the only supported mode. The adapter expects
* item elements with a item name followed by a xPath expression
* If the type is not set the item value will be treated as a String. Possible
* types are 'double' and 'date'
* <p>
* If the document is not a e-invoice no items and also the einvoice.type
* field will be set.
Expand All @@ -84,7 +86,7 @@
public class EInvoiceAdapter implements SignalAdapter {
private static Logger logger = Logger.getLogger(EInvoiceAdapter.class.getName());

public static final String E_INVOICE_READ = "READ";
public static final String E_INVOICE_ENTITY = "ENTITY";
public static final String FILE_ATTRIBUTE_XML = "xml";
public static final String FILE_ATTRIBUTE_EINVOICE_TYPE = "einvoice.type";

Expand Down Expand Up @@ -132,24 +134,24 @@ public class EInvoiceAdapter implements SignalAdapter {
public ItemCollection execute(ItemCollection workitem, ItemCollection event)
throws AdapterException, PluginException {

List<ItemCollection> readDefinitions = workflowService.evalWorkflowResultXML(event, "e-invoice",
E_INVOICE_READ, workitem, false);
List<ItemCollection> entityDefinitions = workflowService.evalWorkflowResultXML(event, "e-invoice",
E_INVOICE_ENTITY, workitem, false);

// Detect and read E-Invoice Data
if (readDefinitions != null && readDefinitions.size() > 0) {
if (entityDefinitions != null && entityDefinitions.size() > 0) {

FileData eInvoiceFileData = detectEInvoice(workitem);

if (eInvoiceFileData == null) {
logger.info("No e-invoice type detected.");
return workitem;
} else {
String einvoiceType = detectEInvoiceType(eInvoiceFileData);
workitem.setItemValue(FILE_ATTRIBUTE_EINVOICE_TYPE, einvoiceType);
logger.info("Detected e-invoice type: " + einvoiceType);
ItemCollection itemDefinition = readDefinitions.get(0);
List<String> entities = itemDefinition.getItemValueList("entity", String.class);
readEInvoiceContent(eInvoiceFileData, entities, workitem);
// ItemCollection itemDefinition = entityDefinitions.get(0);
// List<String> entities = itemDefinition.getItemValueList("entity",
// String.class);
readEInvoiceContent(eInvoiceFileData, entityDefinitions, workitem);
}

}
Expand Down Expand Up @@ -425,14 +427,14 @@ private byte[] extractFirstXMLFile(Map<String, PDComplexFileSpecification> names
* <p>
* Example:
*
* invoice.number=//rsm:CrossIndustryInvoice/rsm:ExchangedDocument/ram:ID
* invoice.date=//rsm:CrossIndustryInvoice/rsm:ExchangedDocument/ram:IssueDateTime
*
* <entity name=
* "invoice.number">//rsm:CrossIndustryInvoice/rsm:ExchangedDocument/ram:ID</entity>
*
*
* @param xmlData
* @return
*/
private void readEInvoiceContent(FileData eInvoiceFileData, List<String> entityDefinitions,
private void readEInvoiceContent(FileData eInvoiceFileData, List<ItemCollection> entityDefinitions,
ItemCollection workitem) {
byte[] xmlData = readXMLContent(eInvoiceFileData);

Expand Down Expand Up @@ -462,14 +464,16 @@ public Iterator<String> getPrefixes(String uri) {
Map<String, XPathExpression> compiledExpressions = new HashMap<>();

// extract the itemName and the expression from each itemDefinition....
for (String itemDef : entityDefinitions) {
String[] parts = itemDef.split("=", 2);
if (parts.length != 2) {
logger.warning("Invalid item definition: " + itemDef);
for (ItemCollection entityDef : entityDefinitions) {

if (entityDef.getItemValueString("name").isEmpty()
|| entityDef.getItemValueString("xpath").isEmpty()) {
logger.warning("Invalid entity definition: " + entityDef);
continue;
}
String itemName = parts[0].trim();
String xPathExpr = parts[1].trim();
String itemName = entityDef.getItemValueString("name");
String xPathExpr = entityDef.getItemValueString("xpath");
String itemType = entityDef.getItemValueString("type");
XPathExpression expr = compiledExpressions.computeIfAbsent(xPathExpr,
k -> {
try {
Expand All @@ -483,16 +487,20 @@ public Iterator<String> getPrefixes(String uri) {
if (expr != null) {
Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
String itemValue = node != null ? node.getTextContent() : null;
// test if we have a type....

if (itemName.endsWith("date")) {
if ("date".equalsIgnoreCase(itemType)) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
try {
Date invoiceDate = formatter.parse(itemValue);
workitem.setItemValue(itemName, invoiceDate);
} catch (ParseException e) {
e.printStackTrace();
}
} else if ("double".equalsIgnoreCase(itemType)) {
workitem.setItemValue(itemName, Double.parseDouble(itemValue));
} else {
// default...
workitem.setItemValue(itemName, itemValue);
}
}
Expand Down Expand Up @@ -537,4 +545,5 @@ private static PDEmbeddedFile getEmbeddedFile(PDComplexFileSpecification fileSpe
}
return null;
}

}

0 comments on commit 7e4c9f6

Please sign in to comment.