Add comments to Java query code

Add comments and small refactor, ran Jenkins tests here: https://76.230.136.103:8085/job/temp_project/51/testReport/