1

so i require something like the dynamic finder should change as per the condition, let me explain by code what i mean

the below code find all employee by status and lastdateattended

    def employee = Employee.findAllByStatusAndLastDateAttended("INA",dateObject)

now i have two fields in Employee LastDateAttended and LastDateSigned , and now i want that if a record does not have LastDateAttended, then it should find by LastDateSigned , otherwise LastDateAttended, so another condition is like

      def employee = Employee.findAllByStatusAndLastDateAttended("INA",dateObject)

and i somehow wants to join and use both the query based on a condition , can it be achieved ?, if possible please help

  • other than such a line of code would be unreadable long, i'd expect this not to work. why not use one of the many ways grails provide to write a nice (understandable) query that does that? – cfrick Oct 19 '15 at 07:08

3 Answers3

2

I think criteria query make sense here, something like following

Employee.createCriteria().list{
 and{
     eq('status','INA')
     or {
        eq('lastDateAttended',dateObject)
        eq('lastDateSigned',dateObject)
     }
  }    
}
Uday
  • 619
  • 3
  • 7
0
Employee.list().findAll { emp-> 
     emp.status == "INA" && ( emp.lastDateAttended!=null?
         emp.lastDateAttended.equals(dateObject):emp.lastDateSigned.equals(dateObject)
       ) 
}
Neoryder
  • 897
  • 2
  • 13
  • 26
roanjain
  • 1,252
  • 4
  • 14
  • 32
-1
Employee.findAllByStatusOrLastDateAttendedOrStatusAndLastDateAttended("INA",dateObject)
Develop4Life
  • 7,581
  • 8
  • 58
  • 76