Chapter 13. Further Examples

13. Further Examples

In this chapter we show some useful scripting examples. These examples aim to show how to achieve different functionalities with help of scripting.

13.1. Administration
13.1.1. Reports in TeamSpaces

reportsinteamspace.groovy, Type: Script datasource

Lists all reports contained in TeamSpaces and prints useful information about them.

import net.datenwerke.rs.base.service.reportengines.table.output.object.RSTableModel
import net.datenwerke.rs.base.service.reportengines.table.output.object.RSTableRow
import net.datenwerke.rs.base.service.reportengines.table.output.object.TableDefinition
import net.datenwerke.rs.core.service.reportmanager.interfaces.ReportVariant
import net.datenwerke.rs.teamspace.service.teamspace.entities.TeamSpace
import net.datenwerke.rs.tsreportarea.service.tsreportarea.TsDiskService
import net.datenwerke.rs.tsreportarea.service.tsreportarea.entities.TsDiskReportReference

import org.apache.commons.lang.time.DateUtils

/*
 * reportsinteamspace.groovy: Script datasource
 * Lists all reports contained in TeamSpaces and prints useful information about them.
 */
 
/* check registry */
def last = GLOBALS.services['registry'].get('_report_reports_in_teamspace_last')
if(null != last && last instanceof Date && DateUtils.addMinutes(last.clone(), 10).after(new Date()) )
    return GLOBALS.services['registry'].get('_report_reports_in_teamspace_data')

 
/* load services */
TsDiskService tsDiskService = GLOBALS.getRsService(TsDiskService.class)

/* prepare  result */
TableDefinition tableDefinition = new TableDefinition(
    ['TEAMSPACE_ID', 'TEAMSPACE_NAME', 'TEAMSPACE_DESCRIPTION', 'REFERENCE_ID', 'REFERENCE_NAME','REFERENCE_PATH', 'REPORT_ID',  'REPORT_NAME', 'REPORT_DESCRIPTION', 'REPORT_TYPE', 'BASE_REPORT_ID', 'BASE_REPORT_NAME'],
    [Long.class, String.class, String.class, Long.class, String.class, String.class, Long.class, String.class, String.class, String.class, Long.class, String.class]
)

def result = new RSTableModel(tableDefinition)

GLOBALS.getEntitiesByType(TeamSpace.class).each{
    def ts = it
    
    tsDiskService.getReferencesFor(ts).each{
        if(! (it instanceof TsDiskReportReference))
            return
        
        def reportRef = it
        def report = reportRef.report
        def baseReport = (null != report && report instanceof ReportVariant) ? report.parent : null
        
        def referencePath = reportRef.rootLine.collect({it.name}).reverse().join("/")
        
        def resultLine = [ts.id, ts.name?.replaceAll('\'',""), ts.description?.replaceAll('\'',""),
            reportRef.id, reportRef.name?.replaceAll('\'',""),
            referencePath.replaceAll('\'',""),
            report == null ? null : report?.id, report?.name?.replaceAll('\'',""), report?.description?.replaceAll('\'',""), report?.class?.simpleName,
            baseReport == null ? null : baseReport?.id, baseReport?.name?.replaceAll('\'',"")]

        /* add to result */
        result.addDataRow(new RSTableRow(tableDefinition, resultLine.toArray()))
    }
}

GLOBALS.services['registry'].put('_report_reports_in_teamspace_last', new Date())
GLOBALS.services['registry'].put('_report_reports_in_teamspace_data', result)

return result