1.1.3. Groovy Dataset

Groovy dataset is produced as result of a Groovy script execution. The script must return an object of the List<Map<String, Object>> type. Each element of this list - an object of the Map<String, Object> type - corresponds to one dataset record.

The following objects are passed into the script:

  • dataManager - an object of the com.haulmont.cuba.core.global.DataManager type that provides CRUD functionality. For example:

    LoadContext<Book> loadContext = LoadContext.create(Book.class)
            .setId(bookId)
            .setView("book.edit")
    def book = dataManager.load(loadContext)
  • metadata - an object of the com.haulmont.cuba.core.global.Metadata type, providing access the application metadata. For example:

    def metaClass = metadata.getClassNN('sec$User')
  • params - external report parameters map. Below is an example to get a parameter value:

    def active = params['active']
  • parentBand - parent band as an object of the com.haulmont.yarg.structure.BandData type. This object allows you to get a parent band field value by invoking the getParameterValue() method, for example:

    def groupId = parentBand.getParameterValue('groupId')
  • persistence - an object of the com.haulmont.cuba.core.Persistence type that allows you to manage transactions and get the EntityManager, for example:

    def tx = persistence.createTransaction()
    try {
        def em = persistence.getEntityManager()
        def query = em.createQuery('select g from sec$Group g')
    ...
        tx.commit()
    } finally {
        tx.end()
    }

    For working with an additional data store, pass its name as a parameter to createTransaction() and getEntityManager() methods. By default, the main database is used.

    def tx = persistence.createTransaction('myStore')
    try {
        def em = persistence.getEntityManager('myStore')
    ...
        tx.commit()
    } finally {
        tx.end()
    }
  • security - an object of the com.haulmont.cuba.core.global.Security type used to check user access rights to different objects in the system. For example:

    if (security.isEntityOpPermitted(Book.class, EntityOp.READ) {
        ...
    }
  • timeSource - an object of the com.haulmont.cuba.core.global.TimeSource type used to obtain the current time. For example:

    def currentDate = timeSource.currentTimestamp()
  • transactional - a method that takes a closure, which should be executed in a new transaction, as parameter. The current EntityManager becomes the closure parameter. For example:

    transactional { em ->
        def query = em.createQuery('select g from sec$Group g')
        ...
    }

    Below is an example of the Groovy script which extracts users by the group which is output in the parent band and by the active external parameter:

    def result = []
    transactional { em ->
        def query = em.createQuery('select u from sec$User u where u.group.id = ?1 and u.active = ?2')
        query.setParameter(1, parentBand.getParameterValue('groupId'))
        query.setParameter(2, params['active'])
        query.resultList.each { user ->
            result.add(['userLogin': user.login, 'userName': user.name])
        }
    }
    return result
  • userSession - an object of the com.haulmont.cuba.security.global.UserSession type associated with the currently authenticated user. For example:

    def user = userSession.currentOrSubstitutedUser
  • userSessionSource - an object of the com.haulmont.cuba.core.global.UserSessionSource type which is used to obtain current user session object. For example:

    def locale = userSessionSource.locale
Tip

You can use static methods of the AppBeans class to access any Spring beans of the middleware tier, for example:

def myService = com.haulmont.cuba.core.global.AppBeans.get('sample_MyService')