BZ-6840: Refactoring code - moving some logic from service to controller because it is not generic enough to warrant reuse.