Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tnine
Created September 6, 2011 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnine/395c8b9814e089602e9c to your computer and use it in GitHub Desktop.
Save tnine/395c8b9814e089602e9c to your computer and use it in GitHub Desktop.
/**
* Get a list of UserSumRange objects that should be used in calculating data. Note that any returned UserSumRange could already be persistent
*
* @param customer The customer to use
* @param startTime The time this process started in epoch milliseconds
* @param zone The Time
* @return
*/
private List<UserSumRange> getRanges(Customer customer, long startTime) {
// the start date of our first registration range
// get all open ranges for this customer
List<UserSumRange> ranges = new ArrayList<UserSumRange>(userSumRangeDao.getOpenRanges(customer.getId(), 1000));
//truncate our created date to be at exactly 00:00 on the day it was created
DateTime createdDate = new DateTime(customer.getCreatedDate());
DateTime startDateRange = new DateTime(createdDate.getYear(),createdDate.getMonthOfYear(), createdDate.getDayOfMonth(), 0, 0, 0, 0, customer.getTimeZone().getTimeZone());
UserSumRange latest = getLast(ranges);
// if there are no sum ranges, we've never generated anything for this
// customer.
// The customer MUST always have at least 1 open sum range. Otherwise this process has never been run for them
if (latest == null) {
DateTime startDateEnd = startDateRange.plusMonths(1);
latest = new UserSumRange();
latest.setStart(startDateRange);
latest.setEnd(startDateEnd);
latest.setUserId(customer.getId());
ranges.add(latest);
}
// add all sums required to bring us up to "current"
while (latest.getEnd().isBefore(startTime)) {
Months months = Months.monthsBetween(startDateRange, latest.getEnd());
DateTime startDate = startDateRange.plus(months);
DateTime endDate = startDateRange.plus(months.plus(1));
UserSumRange newRange = new UserSumRange();
newRange.setStart(startDate);
newRange.setEnd(endDate);
newRange.setUserId(customer.getId());
ranges.add(newRange);
latest = newRange;
}
return ranges;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment