Groovy web console

subscribe to the feed Subscribe
to this
site

groupBy plus swtich

Published 2 months ago by aeischeid with tags groupBy each switch
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def rows = [
  [account_key:5781, account_name:"Some Vet Center", name: "Hank Johnsen", issueing_license: "8969 - IA", species: "Swine", count: 1],
  [account_key:5781, account_name:"Some Vet Center", name: "Chris Savers DVM", issueing_license: "8921 - IA", species: "Swine", count: 6],
  [account_key:5781, account_name:"Some Vet Center", name: "Juan Schwirtz", issueing_license: "8761 - IA", species: "Swine", count: 1],
  [account_key:5781, account_name:"Some Vet Center", name: "Jeff Fader DVM", issueing_license: "6512 - IA", species: "Swine", count: 1],
  [account_key:5781, account_name:"Some Vet Center", name: "Jorde Graham DVM", issueing_license: "8059 - IA", species: "Swine", count: 10],
  [account_key:5781, account_name:"Some Vet Center", name: "Pete E. Yeske DVM", issueing_license: "5418 - IA", species: "Swine", count: 13],
  [account_key:5781, account_name:"Some Vet Center", name: "Ryan Kiehne DVM", issueing_license: "6782 - IA", species: "Swine", count: 36],
  [account_key:5781, account_name:"Some Vet Center", name: "Ross Strobel DVM",, issueing_license: "8485 - IA", species: "Swine", count: 11],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Greg Yagar DVM", issueing_license: "7659 - IA", species: "Bovine", count:6],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Greg Yagar DVM", issueing_license: "7659 - IA", species: "Caprine", count:3],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Greg Yagar DVM", issueing_license: "7659 - IA", species: "Swine", count:2],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Julie Pettit DVM", issueing_license: "6848 - IA", species: "Bovine", count:2],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Julie Pettit DVM", issueing_license: "6848 - IA", species: "Equine", count:4],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Julie Pettit DVM", issueing_license: "6848 - IA", species: "Swine", count:2],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Jake A. Graving DVM", issueing_license: "7284 - IA", species: "Bovine", count:2],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Pual Ellingson DVM", issueing_license: "7718 - IA", species: "Swine", count:3],
  [account_key:7501, account_name:"Other Veterinary Clinic", name: "Jake Thomas DVM", issueing_license: "7795 - IA", species: "Swine", count:3]
]
// take rows which has a entry for each species and group by vet and account
def grouped = rows.groupBy({ it.account_key }, { it.name })

Integer count = 0
Map consolidatedRows = [:]

// now consolidate the groups into one row per vet putting all species counts into that one row 
// this would be useful for building a spreadsheet or csv report of how much each individual did with various species.
grouped.each{k,v->
  //println v
  println "accountId:" + k
  v.each{name, speciesEntries ->
    println name
    def newRou = [
      speciesEntries[0].account_key,
      speciesEntries[0].account_name,
      name,
      speciesEntries[0].issueing_license,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ]
    speciesEntries.each{
      switch(it.species) {
        case "Equine":
          newRou[4] = it.count
          break
        case "Swine":
          newRou[5] = it.count
          break
        case "Bovine":
          newRou[6] = it.count
          break
        case "Camelid":
          newRou[7] = it.count
          break
        case "Caprine":
          newRou[8] = it.count
          break
        case "Cervidae":
          newRou[9] = it.count
          break
        case "Ovine":
          newRou[10] = it.count
          break
        case "Poultry":
          newRou[11] = it.count
          break
        default:
          println 'unknown species'
      }
    }
    consolidatedRows[count] = newRou
    count++
  }
}

return consolidatedRows