Groovy地图<String,List<Map<String,String>>>数据操作
Groovy Map<String,List<Map<String,String>>> data manipulation
我正在尝试使用如下发票错误更新 Map<String,List<Map<String,String>>> invoices
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' :
文件错误:文件格式不受支持])
否则应将错误消息附加到匹配的发票和行号,如下所示
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("|"))}
}
}
}
代码看起来不像 Groovy 并且主要使用传统的 java 代码,我想知道是否可以使用 Groovy 方法
简化代码
我觉得 Groovy 就够了 :-) 除非你想成为超级 groovy。
但是你可以这样写:
def regex = /^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.+/
invLineItems.each {e->
int lineNumber
if (it.errorMessage ==~ regex) {
Matcher m = it.errorMessage =~ /\d+/
if (m.find()) {
lineNumber = m.group() as Integer
}
println "lineNumber $lineNumber"
}
if(e.value['LINE_ITEM_NUMBER'].find{it==lineNumber.toString()}) {
def data = lipErrors.findAll{it.invoiceNumber==e.key && it.errorMessage ==~ Pattern.compile("^Line\s+"+lineNumber+"?\:\s+"+lineNumber+"?.+")}
e.value.each{it['error'] = data.errorMessage.join("|")}
}
}
基本上,我在这里尝试使用一些正则表达式运算符,还专门使用每种形式进行迭代,使用 as 关键字进行类型转换。没什么特别的。是的,我去掉了所有的分号。
我正在尝试使用如下发票错误更新 Map<String,List<Map<String,String>>> invoices
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' :
文件错误:文件格式不受支持])
否则应将错误消息附加到匹配的发票和行号,如下所示
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("|"))}
}
}
}
代码看起来不像 Groovy 并且主要使用传统的 java 代码,我想知道是否可以使用 Groovy 方法
简化代码我觉得 Groovy 就够了 :-) 除非你想成为超级 groovy。
但是你可以这样写:
def regex = /^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.+/
invLineItems.each {e->
int lineNumber
if (it.errorMessage ==~ regex) {
Matcher m = it.errorMessage =~ /\d+/
if (m.find()) {
lineNumber = m.group() as Integer
}
println "lineNumber $lineNumber"
}
if(e.value['LINE_ITEM_NUMBER'].find{it==lineNumber.toString()}) {
def data = lipErrors.findAll{it.invoiceNumber==e.key && it.errorMessage ==~ Pattern.compile("^Line\s+"+lineNumber+"?\:\s+"+lineNumber+"?.+")}
e.value.each{it['error'] = data.errorMessage.join("|")}
}
}
基本上,我在这里尝试使用一些正则表达式运算符,还专门使用每种形式进行迭代,使用 as 关键字进行类型转换。没什么特别的。是的,我去掉了所有的分号。