Workaround if import is not known in execution environment, but that type is needed as parameter

40 Views Asked by At

I have this line in Groovy: organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId().pagedRequest(new SimplePagedRequest(startRequest,50)).build() Since in the execution environment I cannot import the SimplePageRequest, what can I do as a workaround?

Can I use an anonymous class, or a class loader?

I can get the OrganizationService with a workaround, and thus I can get the OrganizationsQueryBuilder. But the OrganizationsQueryBuilder expects a SimplePagedRequest as parameter, and to provide that it seems I have to import it, which I can't.

The class documentation: https://docs.atlassian.com/jira-servicedesk/4.4.2/com/atlassian/servicedesk/api/organization/OrganizationService.html https://docs.atlassian.com/jira-servicedesk/4.4.2/com/atlassian/servicedesk/api/organization/OrganizationsQuery.Builder.html https://docs.atlassian.com/jira-servicedesk/4.4.2/com/atlassian/servicedesk/api/util/paging/PagedRequest.html https://docs.atlassian.com/jira-servicedesk/4.4.2/com/atlassian/servicedesk/api/util/paging/SimplePagedRequest.html

Background: The line is part of an Insight automation script (or Assets as it is called now) in Jira Datacenter. I try to get a list of all organizations, but it seems like I can only get them via a paged response.
In this environment I have only limited access to Jiras libraries, for example the import of the SimplePagedRequest cannot be resolved, and causes an error:
import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest;

My hope is, that I have overlooked something basic, and there is a simple solution. Anyway, all help is appreciated.

So far I googled it, and I have read the documentation, trying to find a way to use the OrganizationsQueryBuilder without the paged response, or to find a different way to get all organizations. I have tried to call the OrganizationsQueryBuilder with an anonymous class like this (which is an adopted example):

organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId().pagedRequest(new SimplePagedRequest(startRequest, 50) {
    int getStart() {
        return startRequest
    }
    
    int getLimit() {
        return 50
    }
}).build()

But then I also had to import the SimplePagedRequest.

1

There are 1 best solutions below

0
On

For those interested, a colleague of mine pointed out a solution:

First get the class with a classloader (not an instance of the class). It's important, that it's not an instance, because I need to call a new instance within a loop, in each iteration.

Class SimplePagedRequestClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.atlassian.servicedesk.api.util.paging.SimplePagedRequest")

This class has a static method, that returns an instance of the class, it's called ".paged()" (if it did't, it would probably also have worked with newInstance()).

organizationService.newOrganizationsQueryBuilder().serviceDeskId().pagedRequest(SimplePagedRequestClass.paged(startRequest, 50)).build()

That's it.

I'm not sure it it would have worked out in Java as well, but here we are working with Groovy.