2015-04-02 101 views
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的方法來簡化

回答

1

它看起來足夠的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("|")} 
    } 
} 

基本上在這裏我想用一些regex操作符,使用使用關鍵字作爲各形式,類型轉換還專門迭代。沒什麼特別的。是的,我擺脫了所有的分號。

+0

感謝您的回答。我很感激。你還可以幫助我與這個http://stackoverflow.com/questions/29570648/java-8-find-and-replace-matching-string – RanPaul 2015-04-10 21:41:18

+0

可以幫助我這個http://stackoverflow.com/questions/30789467/groovy-groupby-field-with-white-spaces – RanPaul 2015-06-11 19:16:22

+0

你能幫我解決這個問題嗎? https://stackoverflow.com/questions/47717505/groovy-create-a-map-with-jax-b-objects-specific-attributes – RanPaul 2017-12-08 19:41:19

相關問題