fix infinite recursion on case hierarchy generation + test

http://manage.dimagi.com/default.asp?124211