我Map<String,List<Map<String,String>>> invoices
如下Groovy的印刷地圖<字符串,列表<地圖<String,字符串>>>數據
invLineItems = [INV-Error_Test1:[[LINE:1, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:22], [LINE:2, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test1, INVOICE_TOTAL:24]],
INV-Error_Test2:[[LINE:3, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:26], [LINE:4, INVOICE_DATE:20150301, INVOICE_NUMBER:INV-Error_Test2, INVOICE_TOTAL:28,]]]
我想打印出來,如下
println " INV-Error_Test2 Details "
invLineItems.get('INV-Error_Test2').each{
it.each{
print "LINE = "+ it['LINE']
}
}
而且我得到以下錯誤groovy.lang.MissingPropertyException: No such property: LINE for class: java.util.LinkedHashMap$Entry
有人可以幫我打印這些數據嗎?
注:我其實有共24頭,但進行簡單的我剛剛發佈了4頭的問題(Line, Invoice_Date,Invoice-Number,Invoice_Total
),我試圖只打印幾個頭出來24
更新時間:上午試圖更新Map<String,List<Map<String,String>>> invoices
與invoiceErrors如下
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("|"))}
}
}
}
代碼犯規看起來像Groovy和大多采用傳統的Java代碼,我想知道代碼是否可以用Groovy方法簡化
只是要拋出這個:大部分時間當你有一個地圖>你真正想要的是一個Multimap。 http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html –
dnault
2015-04-02 02:38:33