简体   繁体   中英

Groovy method to build <String, List<List>> tokens from String array

I have a string array:

def invoices = [LEDES98BI V2,
LINE|INVOICE_DATE|INVOICE_NUMBER|INVOICE_TOTAL,
1|20150301|INV-Error_Test1|22,
2|20150301|INV-Error_Test1|24,
3|20150301|INV-Error_Test2|26,
4|20150301|INV-Error_Test2|28,]

I tried to convert it to HasMap<String, List<List>> with key as invoice numbers( INV-Error_Test1 , INV-Error_Test2 ) and the values are each invoice line:

[
INV-Error_Test2:[[3,20150301, INV-Error_Test2,26], [4,20150301, INV-Error_Test2,28]], 
INV-Error_Test1:[[1,20150301, INV-Error_Test1,22], [2,20150301, INV-Error_Test1,24]]
]

And this is the method which converts the string array into <String, List<List>> tokens:

def extractInvoiceLineItems(def invoices) {
    Map invLineItems = new HashMap<String, ArrayList<ArrayList>>();
    def lineItems = []
    for(int i = 2; i<invoices.length; i++){
       def tokens =  invoices[i].split('\\|') as List
       if(tokens.size != 1) {
        lineItems.add(tokens)           
      }
    }

    for (int i=0; i< lineItems.size; i++) {
        invNumber = lineItems.get(i).get(1)
         if(invLineItems.keySet().find{it == invNumber}) {
            templineItem = invLineItems.get(invNumber)
            templineItem.add(lineItems.get(i))
            invLineItems.put(invNumber,templineItem)      
         }
        else {
            def list = []
            list.add(lineItems.get(i))
            invLineItems.put(invNumber,list)
        }      
    }
invLineItems
}

I am using lots of traditional for loops and am wondering whether it can be simplified further (using closures or any other way).

UPDATE1: I am trying to print the invoice details per INVOICE_NUMBER as below

def lines = invoices*.split('\\|').findAll{ it.size()>1 }
def heads = lines.first()
def invLineItems = lines.tail().collect{ [heads, it].transpose().collectEntries() }.groupBy{ it.INVOICE_NUMBER }
// => [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{k,v ->
print "LINE = "+ it['LINE']
print " "+" INVOICE_DATE = "+it['INVOICE_DATE']
print " "+" INVOICE_TOTAL  = "+it['INVOICE_TOTAL']
    }
 }

But am seeing all the map values when am trying to print the specific value. can someone help me out?

UPDATE2: Am trying to update the Map<String,List<Map<String,String>>> invoices with the invoiceErrors as below

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

Am trying to achieve below map If the error message doesnt have a line number it need to be appended at the top level as invLineItems.put('error',['INV-Error_Test1' : File Error : The file is in an unsupported format ]) Otherwise errormessage should be appended to the matching INVOICE and linenumber as below

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`]]

I wrote the below method to achieve the above

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("|"))}

     }   

  }
}   

The code doesnt look like Groovy and using traditional java code mostly, am wondering whether the code can be simplified with Groovy approach

What you are after is groupBy

This should do:

def invoices = [
'LEDES98BI V2',
'LINE|INVOICE_DATE|INVOICE_NUMBER|INVOICE_TOTAL',
'1|20150301|INV-Error_Test1|22',
'2|20150301|INV-Error_Test1|24',
'3|20150301|INV-Error_Test2|26',
'4|20150301|INV-Error_Test2|28,']

// get all "valid" lines
def lines = invoices*.split('\\|').findAll{ it.size()>1 }
// remember the keys (headline) for later (optional)
def heads = lines.first()

// as map of maps
println lines.tail().collect{ [heads, it].transpose().collectEntries() }.groupBy{ it.INVOICE_NUMBER }
// => [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,]]]

// or just as you had it (map of string list)
println lines.tail().groupBy{ it[2] }
// => [INV-Error_Test1:[[1, 20150301, INV-Error_Test1, 22], [2, 20150301, INV-Error_Test1, 24]], INV-Error_Test2:[[3, 20150301, INV-Error_Test2, 26], [4, 20150301, INV-Error_Test2, 28,]]]

Edit

Version that prints all the lines for a certain invoice number:

def invLineItems = lines.tail().collect{ [heads, it].transpose().collectEntries() }.groupBy{ it.INVOICE_NUMBER }

This now is aa Map<String,List<Map<String,String>>> . So getting one element gives you the list. The each iterates the list and it there is a map:

invLineItems.get('INV-Error_Test2').each{ 
        print "LINE = "+ it['LINE']
        print " "+" INVOICE_DATE = "+it['INVOICE_DATE']
        print " "+" INVOICE_TOTAL  = "+it['INVOICE_TOTAL']
        println ""
}

Or quick an dirty:

print invLineItems.get('INV-Error_Test2')*.collect{ k, v -> [k,"=",v].join(" ") }*.join(", ").join("\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM