This is one knarly big method. I'd say ideally you'd have a HasMany class and a HasOne class, with smaller methods there to handle the building of this stuff.