Refactor Controller to remove duplicate code

OpenMRS Code Jam.