Generate random resultset from actual resultset

/*
* Show defined number of random entries in current resultset
*
*
* Usage:
* 1. Edit the number of entries to show randomly
* 2. Run button script
* 3. Show all in Query Mode to be able to use the button script again
*
* @author David Pech <[email protected]>
*/
 
 
import com.im.df.api.dml.*
import com.im.df.api.support.SelectionDescription
import com.im.ijc.core.api.util.IJCCoreUtils
 
 
init = { widget ->
}
 
destroy = { widget ->
}
 
// evaluate is called when the button is clicked to perform action
//
// widget is IJCWidget instance
evaluate = { widget ->
 
// get the needed environmental variables
def rs = widget.form.resultSet // result set
def rootVS = rs.rootVertexState // vertex state of the resultset
// set amount of entries to generate from your resultset
def randomSeriesLength = 20;
println "Showing random entries of total count of " + randomSeriesLength
 
// get all the IDs of your current resultset and
// create a copy of the immutable List to work with
List listOfIDs = new ArrayList(rootVS.getIds());
// randlomly shuffle the list
Collections.shuffle(listOfIDs);
// select n values from the list and sort
// n of values defined in randomSeriesLength
def randomSeries = listOfIDs.subList(0, randomSeriesLength).sort();
// convert the generated sublist type to ArrayList type
// to be able to load it
List randomList = new ArrayList(randomSeries)
// apply the list of IDs to the resultset
def rsLock = rs.lockable.withLock('applying random list of IDs') { rsEnv ->
rs.applyList(randomList, rsEnv)
}
}
 
 
on_change = { widget, button ->
}