Use the sample plugin to learn by example. Download the source bundle from this page and use it with the latest API version.
Script Runner examples
This code demonstrates getting a Gantt chart by structure id, initiating the resource leveling process for this Gantt chart, and then logging the progress until completion.
package examples.docs.structure
import com.almworks.structure.gantt.api.leveling.ResourceLevelingManager
import com.almworks.structure.gantt.api.gantt.GanttChartManager
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.almworks.structure.gantt")
@PluginModule
ResourceLevelingManager resourceLevelingManager
@PluginModule
GanttChartManager ganttChartManager
def structureId = 12;
// Get gantt by structureId
def gantt = ganttChartManager.getGanttChartByStructureId(structureId)
if (gantt) {
def ganttId = gantt.id
def started = resourceLevelingManager.createLevelingRun(ganttId)
.fromProjectStart()
.apply()
def current = started
// Keep checking the status of the resource leveling process until it completes.
// We check that either the process is not running, or it is not the process we started because the node ID or version is different.
while (current != null && current.nodeId == started.nodeId && current.version == started.version) {
log.warn("Leveling is in progress: $current.progress%")
Thread.sleep(1000)
current = resourceLevelingManager.getLevelingInfo(ganttId)
}
"Leveling is finished"
} else {
"Gantt doesn't exists"
}
This example demonstrates how to programmatically manage a Gantt chart and its baselines. Initially, it checks for and deletes an existing Gantt chart for a given structure ID. Then, it creates a new Gantt chart and immediately finds it by name to ensure it's working with the correct chart. Before starting resource leveling, it creates a baseline for comparison purposes. The resource leveling process is then initiated for a specified resource, with enabled "Level resolved tasks" and "Level tasks in progress" options.
package examples.docs.structure
import java.time.LocalDate
import com.almworks.structure.gantt.api.leveling.ResourceLevelingManager
import static com.almworks.structure.gantt.api.leveling.ResourceLevelingOptions.*
import com.almworks.structure.gantt.api.gantt.GanttChartManager
import com.almworks.structure.gantt.api.baseline.BaselineManager
import com.almworks.structure.gantt.api.baseline.BaselineType
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.almworks.structure.gantt")
@PluginModule
ResourceLevelingManager resourceLevelingManager
@PluginModule
GanttChartManager ganttChartManager
@PluginModule
BaselineManager baselineManager
def structureId = 12L;
def gantt = ganttChartManager.getGanttChartByStructureId(structureId)
// Delete existing Gantt chart
if (gantt) {
ganttChartManager.removeGanttChart(gantt.id)
}
// Create new Gantt chart
def created = ganttChartManager.createGanttChart(structureId, "Default", LocalDate.now(), "New Gantt name");
// Find created Gantt chart by name
gantt = ganttChartManager.getGanttChartsByName("New Gantt name")[0]
// Create new baseline before resource leveling
def baseline = baselineManager.createBaseline(gantt.id, "Baseline before leveling", BaselineType.GANTT, [:])
// Run resource leveling for specified user to compare results of resource leveling with Baseline
resourceLevelingManager.createLevelingRun(gantt.id)
.fromProjectStart()
.addUserResource("JIRAUSER10000")
.setOption(LEVEL_RESOLVED, "true")
.setOption(LEVEL_IN_PROGRESS, "true")
.apply()
// User can compare results of resoruce leveling with baseline after leveling completion.