0

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

OTUser
  • 3,788
  • 19
  • 69
  • 127

1 Answers1

1

It looks Groovy enough to me :-) Unless you want to be uber groovy.

However you could write something like this:

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

Basically here I'm trying to use some regex operators, also exclusively iterating using the each form, type casting using the as keyword. Nothing special. Yep, and I got rid of all the semicolons.

defectus
  • 1,947
  • 2
  • 16
  • 21
  • Thanks for the answer. I appreciate it. Can you also help me with this http://stackoverflow.com/questions/29570648/java-8-find-and-replace-matching-strings – OTUser Apr 10 '15 at 21:41
  • can u help me with this http://stackoverflow.com/questions/30789467/groovy-groupby-field-with-white-spaces – OTUser Jun 11 '15 at 19:16
  • can you please help me solving this problem? https://stackoverflow.com/questions/47717505/groovy-create-a-map-with-jax-b-objects-specific-attributes – OTUser Dec 08 '17 at 19:41