register
other register

Friday, August 14, 2009

Database Result to Groovy List, Map

Person --* Order --* LineItem *-- Product


def lineItemList = LineItem.findAll()
def t = []
def q = []
def lineItemMap = []
def orderMap = []
def packs
lineItemList.order.person.unique().each { person ->
def p = []
orders = lineItemList.findAll{it.order.person == person}.order
orders.unique().each { order ->
lineItemMap = lineItemList.findAll{it.order == order}
orderMap = [orderId: order.id, lineItemId: lineItemMap.id]
p.add(orderMap)
// Use the statement below to get all the products
//products = LineItem.getAll(lineItemMap.id).product
}
q = [personId: person.id, order: p]
t.add(q)
}


[[personId:1, order:[[orderId:1, lineItemId:[1, 2]], [orderId:2, lineItemId:[3, 4]]]]]

[[personId:1, order:[[orderId:1, lineItemId:[1, 2]]]], [personId:2, order:[[orderId:2, lineItemId:[3, 4]]]]]

No comments: