// Using a dynamic finder Author.findAllByNameLike("John%",[sort:'name',order:'asc',fetch:[location:'join']])importorg.hibernate.FetchModeasFMdefresults=Airport.withCriteria{eq"region","EMEA"fetchMode"flights",FM.SELECT}
WHERE QUERY WITH DETACHED CRITERIA AND PROP PROJECTION JOINS
//subquery find users with managers whose name is like the search string //query use list from subquery to find all users and create map to match filtered users search defqueryByManagerName(StringsearchString,List<Long>filteredUsers,sort,order,_clientSetupId,statusFilters,unassignedManagerFilter,unassignedAssessmentFilter){def(firstNameSearch,lastNameSearch,rest)=searchString?.tokenize()defstringToRegex={string->string?"^${string}.*":null}defmanagerQuery=UserRelationship.whereAny{manager{firstName==~~/${stringToRegex(searchString)}/||lastName==~~/${stringToRegex(searchString)}/}manager{firstName==~~/${stringToRegex(firstNameSearch)}/&&lastName==~~/${stringToRegex(lastNameSearch)}/}if(filteredUsers){not{'in'('user.id',filteredUsers.collect{(Long)it})}}}.join('user').distinct('user.id')DetachedCriteria<User>detachedUserQuery=User.where{idinmanagerQueryprojections{distinct('id')property('username')property('clientSetupId')property('email')property('firstName')property('lastName')property('userMetadata')property('lastLogin')property('pictureUrl')}}if(_clientSetupId){detachedUserQuery.eq('clientSetupId',_clientSetupId)}if(unassignedManagerFilter||unassignedAssessmentFilter){defuserIdsWithManager=getClientUserIdsWithManagers(_clientSetupId)detachedUserQuery.or{if(unassignedManagerFilter){if(userIdsWithManager){not{'in'('id',userIdsWithManager)}}}if(unassignedAssessmentFilter){defuserIdsWithAssessment=getClientUserIdsLinkedToAssessments(_clientSetupId)if(userIdsWithAssessment){not{'in'('id',userIdsWithAssessment)}}}}}detachedUserQuery.sort(sort,order).collect{[id:it[0],username:it[1],clientSetupId:it[2],email:it[3],firstName:it[4],lastName:it[5],userMetadata:it[6],lastLogin:it[7],pictureUrl:it[8]]}}def(firstNameSearch,lastNameSearch,rest)=searchString?.tokenize()defstringToRegex={string->string?"^${string}.*":null}