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