Access session API from user-defined function

I use a connector mail to send email to a filter population using this script :

import org.bonitasoft.engine.identity.UserMembership
import org.bonitasoft.engine.search.SearchOptionsBuilder
import org.bonitasoft.engine.search.SearchResult
import org.bonitasoft.engine.identity.Group
import org.bonitasoft.engine.identity.GroupSearchDescriptor

final long roleId = apiAccessor.getIdentityAPI().getRoleByName("Agent SAF").getId()
List<UserMembership> userMemberships = apiAccessor.getIdentityAPI().getUserMembershipsByRole(roleId, 0, 1000)

def stringSmtpTo=""

for (UserMembership userMembership: userMemberships) {

    Group group = apiAccessor.getIdentityAPI().getGroup(userMembership.groupId)

    String typeStructure = group.name.split(" ")[0]

    if("SP".equals(typeStructure)) {
        final SearchOptionsBuilder builder = new SearchOptionsBuilder(0,10000)
        builder.filter(GroupSearchDescriptor.PARENT_PATH, group.path)
        final SearchResult<Group> searchResults = apiAccessor.getIdentityAPI().searchGroups(builder.done())
        for (final Group group_fils : searchResults.getResult()){
            if (group_fils.displayName.equals(sesamAchat.directionLibelle.split("-")[1])) {
                if(apiAccessor.getIdentityAPI().getUserContactData(userMembership.userId,true).getEmail()){
                    if(stringSmtpTo!="") stringSmtpTo+=","
                    stringSmtpTo+=apiAccessor.getIdentityAPI().getUserContactData(userMembership.userId,true).getEmail()
                }
            }
        }
    }else {
        if (group.displayName.equals(sesamAchat.directionLibelle.split("-")[1])) {
            if(apiAccessor.getIdentityAPI().getUserContactData(userMembership.userId,true).getEmail()){
                if(stringSmtpTo!="") stringSmtpTo+=","
                stringSmtpTo+=apiAccessor.getIdentityAPI().getUserContactData(userMembership.userId,true).getEmail()
            }
        }
    }

}

if(stringSmtpTo==""){
    final long roleIdAdminF = apiAccessor.getIdentityAPI().getRoleByName("Administrateur fonctionnel").getId()
    List<UserMembership> adminFMemberships = apiAccessor.getIdentityAPI().getUserMembershipsByRole(roleIdAdminF, 0, 1000)
    for (UserMembership adminFMembership: adminFMemberships) {
        if(apiAccessor.getIdentityAPI().getUserContactData(adminFMembership.userId,true).getEmail()){
            if(stringSmtpTo!="") stringSmtpTo+=","
            stringSmtpTo+=apiAccessor.getIdentityAPI().getUserContactData(adminFMembership.userId, true).getEmail()
        }
    }
}

return stringSmtpTo

I'd like to factorize this code in a user-defined groovy script. Can you tell me if it's possible and how process ?
The apiAccessor variable is not present, how mus I process to instantiate it ? to get the identityAPI bound to the process session ?

Thanks
 

 

 

What I would recommend is to create a static method in a Groovy script with the following signature:

static def generateRecipientsList(APIAccessor apiAccessor, String directionLibelle)

And move most of your code in the Groovy script method.