简体   繁体   中英

Find property on Domain class based on Date range

I am working on Grails 2.3.6 app. This app has multiple Domain classes and the task is to find a property in a Domain class based on the Date range provided by user.

I have to navigate through multiple Domain classes to find this property, and that's what makes it tricky for me!

Please see code below:

class GlobalAllSiteData {
     String globalSiteID = ""

     static hasMany = [siteDatas: SiteData]
}

class SiteData {
     static hasMany = [userDatas: UserData]

     static belongsTo = [gd:GlobalAllSiteData]
}

class UserData {
     static hasMany = [userActions: UserAction]

     static belongsTo = [sd:SiteData]
}

class UserAction {
     Date userAddedDate;
     String userStatus = ""; // OK, INACTIVE, REMOVED 
     static belongsTo = [ud:UserData]
}

The 3 parameters that user gives to the app is GlobalAllSiteData as a string and 2 dates that will be used as range in userAddedDate

Below are the steps that am envisioning:

  1. Find all SiteData objects based on GlobalAllSiteData string provided by user.

  2. In each SiteData object, loop through to get all UserData objects.

  3. On each UserData object, loop through to get all UserAction objects.

  4. On each UserAction object, apply the FROM & TO date range and find the number of UserData objects.

There will be multiple UserAction objects for each user depending on how many times the user was added into different systems of the corporate list.

This is the tricky part. So in order to make it easier, i would like to pick the userAddedDate that's closer to Today's date. And then compare that with the Date range that user has provided.

So the final output that i should have based on user provided Date ranges are,

  1. The Total Number of UserData in the database.

  2. The total number of UserData with UserAction userStatus set to OK .

As am new to Grails and Groovy am running out of ideas on this one!

Is there a quicker and efficient way to query for the above condition?

It depends on where you're storing your data. If it's a bunch of text files, then looping like that will be a slow but reasonable approach. If it's in a database, then pulling in a large subset of several tables and looping and filtering client-side would be less than good. Bad even. Crazy.

Try this:

String globalSiteID = ...
Date lower = ...
Date upper = ...

Instead, use the database to do the filtering for you, eg

def userDatas = UserAction.createCriteria().listDistinct {
   between 'userAddedDate', lower, upper
   ud {
      sd {
         gd {
            eq 'globalSiteID', globalSiteID
         }
      }
   }
   eq 'userStatus', 'OK'
   projections {
      property 'ud'
   }
}

int userDataCount = UserAction.createCriteria().get {
   between 'userAddedDate', lower, upper
   ud {
      sd {
         gd {
            eq 'globalSiteID', globalSiteID
         }
      }
   }
   projections {
      countDistinct 'ud'
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM