How to purge old blackout calendars

  • 7004583
  • 01-Oct-2009
  • 17-Mar-2012

Environment

Business Service Manager 4.0
Business Service Manager 4.5

Situation

You have created a large number blackout calendars and you want to remove calendars that are older than a particular date.

Resolution

Run the script provided on this TID, creating a server side operation is the normal approach.

1. Create a file called "deleteoldbo.fs" in the scripts directory. It must be that exact name.

2. Copy and paste all the code below the line into "deleteoldbo.fs".

3. The script defaults to purge blackout calendars older than six months, you can change this value.

4. Create either a server side operation or job to run the script.

Due to calendars that circularly refer to other calendars, you may need to run the script multiple time, to purge all the old calendars. The script will terminate if it detects a circular reference, but will attempt to break the chain by deleting at least one of the old calendars.

========== Do not include this line in the script ============
//@debug off

function visitor( child ) {
  var tm = Packages.com.mosol.Formula.Performance.TimeManagementConfig.getInstance()

  var bop = tm.getBaseObjPool()

  var master = bop.getCalendarNamed(child.dname)
 
  var seen = new Packages.java.util.HashSet()
 
  walkCalendars(child, bop.getCalendarNamed(child.dname), seen, tm, master)
}

function walkCalendars(child, bsmCal, seen, tm, master) {
  if(bsmCal != null) {
      formula.log.info("Recursing into " + bsmCal)
      if(bsmCal.hasChildren()) {
          var calChildren = bsmCal.getChildren()

          for(i=0; i < calChildren.length; i++) {
              if(!seen.contains(calChildren[i]) && calChildren[i].isFolder()) {
                    seen.add(calChildren[i])
                  walkCalendars(child, calChildren[i], seen, tm, master)
              }
          }
      }
      deleteBlackout(child, bsmCal, tm, master)
  }
}

function deleteBlackout(child, bsmCal, tm, master) {
        var timeDefs = bsmCal.getTimeDefinitions()
    if(timeDefs != null && timeDefs.length > 0) {
        for(i=0; i < timeDefs.length; i++) {
            if( timeDefs[i].getTimeCategory().getCategoryName().equals("Blackout")) {
 
                var r = timeDefs[i].getRecurrence()
                if(r.hasRecurrence()) {
                    if(!r.isNoEndDate() && !r.isEndAfterNTimes()) {
                        var endDate = r.getRecurrenceEnd()
                        var cal = new Packages.java.util.Calendar.getInstance()

                        cal.roll( Packages.java.util.Calendar.MONTH, -6)

                        var purgeDate = cal.getTime()

                        formula.log.info("Purge date has been calculated " + purgeDate)
                        if(endDate.before(purgeDate)) {
                            formula.log.info("This blackout calendar is purgable " + child + " calendar " + timeDefs[i])
                            // Run the script once and double check the results are correct
                            // then uncomment the next two lines and re-run
                            master.removeTimeDefinition(timeDefs[i])
                            tm.save()
                        } else {
                            formula.log.info("This blackout calendar is NOT purgable " + child + " calendar " + timeDefs[i])
                        }   
                    }
                }
            }
        }
    }
}

element.walk( visitor )

// @internal deleteoldbo.fs -7bmcb66