0
我试图更新Map<String,List<Map<String,String>>> invoices
与invoiceErrors如下Groovy的地图<字符串,列表<地图<String,字符串>>>数据处理
InvoiceError // is an entity with below attributes
{ String errorMessage,
String invoiceNumber
}
ErrorMessage invoiceNumber
------------- -------------------
File Error : The file is in an unsupported format INV-Error_Test1
Line : 1 Invoice does not foot Reported INV-Error_Test1
Line : 2 MATH ERROR INV-Error_Test1
Line : 3 MATH ERROR INV-Error_Test2
Line : 3 Invoice does not foot Reported INV-Error_Test2
想实现如下图 如果错误信息犯规有行号它需要在顶层被附加作为invLineItems.put('error',['INV-Error_Test1' :
文件错误:该文件是不支持的格式])
否则errormessage的应附加到匹配的发票和行号如下
invLineItems = [INV-Error_Test1:[[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:22, error : `Line : 1 Invoice does not foot Reported`],
[LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:24, error : `Line : 2 MATH ERROR`],
INV-Error_Test2:[[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:26, , error : `Line : 3 MATH ERROR | Line : 3 Invoice does not foot Reported`],
[LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:28,]],
error : [[INV-Error_Test1:`File Error : The file is in an unsupported format`]]
我写了下面的方法来实现上述
def regex = "^Line\\s(?:(\\d+)\\s)?\\s*:\\s+(\\d+)?.+";
for (e in invLineItems){
def errors = lipErrors.findAll{it.invoiceNumber==e.key} // finding the error messages with the invoice number
errors.each{ // fetching the line numbre from error message and finding the matching record the invoice number and line number in invLineItems
int lineNumber
if (it.errorMessage.matches(regex)) {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(it.errorMessage);
if (m.find()) {
lineNumber = Integer.parseInt(m.group());
}
println "lineNumber = "+lineNumber
}
if(e.value['LINE_ITEM_NUMBER'].find{it==lineNumber.toString()}) {
def data = lipErrors.findAll{it.invoiceNumber==e.key && it.errorMessage.matches("^Line\\s+"+lineNumber+"?\\:\\s+"+lineNumber+"?.+")}
e.getValue().each{it.put("error", data.errorMessage.join("|"))}
}
}
}
代码does not看起来像Groovy和主要使用传统的Java代码,我想知道是否该代码可以使用Groovy的方法来简化
感谢您的回答。我很感激。你还可以帮助我与这个http://stackoverflow.com/questions/29570648/java-8-find-and-replace-matching-string – RanPaul 2015-04-10 21:41:18
可以帮助我这个http://stackoverflow.com/questions/30789467/groovy-groupby-field-with-white-spaces – RanPaul 2015-06-11 19:16:22
你能帮我解决这个问题吗? https://stackoverflow.com/questions/47717505/groovy-create-a-map-with-jax-b-objects-specific-attributes – RanPaul 2017-12-08 19:41:19