This function is to big and to complex, please introduce dedicated class and add junit tests, use proper java formatting (see dev-buildtools)