Feature/query refactoring and validations 2395

https://www.openproject.org/work_packages/2395