in src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java [120:255]
static void cleanup(final Calendar removeDate,
final ResourceResolver resolver,
final JobExecutionContext context,
final String basePath,
final String[] topics,
final List<String> stateList)
throws PersistenceException {
final Resource baseResource = resolver.getResource(basePath);
// sanity check - should never be null
if ( baseResource != null ) {
final Iterator<Resource> topicIter = baseResource.listChildren();
while ( !context.isStopped() && topicIter.hasNext() ) {
final Resource topicResource = topicIter.next();
// check topic
boolean found = topics == null;
int index = 0;
while ( !found && index < topics.length ) {
if ( topicResource.getName().equals(topics[index]) ) {
found = true;
}
index++;
}
if ( !found ) {
continue;
}
final int removeYear = removeDate.get(Calendar.YEAR);
final int removeMonth = removeDate.get(Calendar.MONTH) + 1;
final int removeDay = removeDate.get(Calendar.DAY_OF_MONTH);
final int removeHour = removeDate.get(Calendar.HOUR_OF_DAY);
final int removeMinute = removeDate.get(Calendar.MINUTE);
// start with years
final Iterator<Resource> yearIter = topicResource.listChildren();
while ( !context.isStopped() && yearIter.hasNext() ) {
final Resource yearResource = yearIter.next();
final int year = Integer.valueOf(yearResource.getName());
if ( year > removeYear ) {
continue;
}
final boolean oldYear = year < removeYear;
// months
final Iterator<Resource> monthIter = yearResource.listChildren();
while ( !context.isStopped() && monthIter.hasNext() ) {
final Resource monthResource = monthIter.next();
final int month = Integer.valueOf(monthResource.getName());
if ( !oldYear && month > removeMonth) {
continue;
}
final boolean oldMonth = oldYear || month < removeMonth;
// days
final Iterator<Resource> dayIter = monthResource.listChildren();
while ( !context.isStopped() && dayIter.hasNext() ) {
final Resource dayResource = dayIter.next();
final int day = Integer.valueOf(dayResource.getName());
if ( !oldMonth && day > removeDay) {
continue;
}
final boolean oldDay = oldMonth || day < removeDay;
// hours
final Iterator<Resource> hourIter = dayResource.listChildren();
while ( !context.isStopped() && hourIter.hasNext() ) {
final Resource hourResource = hourIter.next();
final int hour = Integer.valueOf(hourResource.getName());
if ( !oldDay && hour > removeHour) {
continue;
}
final boolean oldHour = oldDay || hour < removeHour;
// minutes
final Iterator<Resource> minuteIter = hourResource.listChildren();
while ( !context.isStopped() && minuteIter.hasNext() ) {
final Resource minuteResource = minuteIter.next();
// check if we can delete the minute
final int minute = Integer.valueOf(minuteResource.getName());
final boolean oldMinute = oldHour || minute <= removeMinute;
if ( oldMinute ) {
final Iterator<Resource> jobIter = minuteResource.listChildren();
while ( !context.isStopped() && jobIter.hasNext() ) {
final Resource jobResource = jobIter.next();
boolean remove = stateList == null;
if ( !remove ) {
final ValueMap vm = ResourceUtil.getValueMap(jobResource);
final String state = vm.get(JobImpl.PROPERTY_FINISHED_STATE, String.class);
if ( state != null && stateList.contains(state) ) {
remove = true;
}
}
if ( remove ) {
resolver.delete(jobResource);
resolver.commit();
}
}
// check if we can delete the minute
if ( !context.isStopped() && !minuteResource.listChildren().hasNext()) {
resolver.delete(minuteResource);
resolver.commit();
}
}
}
// check if we can delete the hour
if ( !context.isStopped() && oldHour && !hourResource.listChildren().hasNext()) {
resolver.delete(hourResource);
resolver.commit();
}
}
// check if we can delete the day
if ( !context.isStopped() && oldDay && !dayResource.listChildren().hasNext()) {
resolver.delete(dayResource);
resolver.commit();
}
}
// check if we can delete the month
if ( !context.isStopped() && oldMonth && !monthResource.listChildren().hasNext() ) {
resolver.delete(monthResource);
resolver.commit();
}
}
// check if we can delete the year
if ( !context.isStopped() && oldYear && !yearResource.listChildren().hasNext() ) {
resolver.delete(yearResource);
resolver.commit();
}
}
}
}
}