I would probably do this in some sort of service object.