Looking up Insight Objects programmatically, PermissionInsightException

I am trying to programmatically replicate a Custom Field from one Jira site to another. The custom field itself is okay, but if the custom field is an Insight field with a default Insight object value, then I need to obtain the Insight object.

I can obtain the CustomField’s DefaultValueOperation, which gives me a ObjectBean instance. The ObjectBean instance contains its properties:
key (e.g. TEST-2)
id (e.g. 2)
Schema ID (e.g. 3)
Object Type ID (e.g. 25)

But when Insight Schema/Object are exported and imported to the other Jira site, the object ID, Schema ID and Object Type ID change.

So I need to obtain the Schema key and Object Type name, so I can try to find them in the other Jira site.

I can obtain Insight’s Facade objects via OSGI, e.g.:

			OBJECT_FACADE = 
					(ObjectFacade) ComponentAccessor.getOSGiComponentInstanceOfType(
					ObjectFacade.class);
			logger.debug("OBJECT_FACADE: " + OBJECT_FACADE);
			OBJECT_TYPE_FACADE =
					(ObjectTypeFacade) ComponentAccessor.getOSGiComponentInstanceOfType(
					ObjectTypeFacade.class);
			logger.debug("OBJECT_TYPE_FACADE: " + OBJECT_TYPE_FACADE);
			IQL_FACADE = 
					(IQLFacade) ComponentAccessor.getOSGiComponentInstanceOfType(IQLFacade.class);
			logger.debug("IQL_FACADE: " + IQL_FACADE);
			SCHEMA_FACADE = 
					(ObjectSchemaFacade) ComponentAccessor.getOSGiComponentInstanceOfType(
					ObjectSchemaFacade.class);
			logger.debug("SCHEMA_FACADE: " + SCHEMA_FACADE);

Then I invoke:

		for (ObjectSchemaBean schema : SCHEMA_FACADE.findObjectSchemaBeans()) {
			for (ObjectTypeBean type : 
				OBJECT_TYPE_FACADE.findObjectTypeBeans(schema.getId())) {

However ObjectTypeFacade.findObjectTypeBeans(schemaId) fails:

2023-02-10 16:15:17,774 http-nio-2990-exec-6 DEBUG admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [i.r.i.index.model.ObjectIndexImpl] Try to fetch object with object key: TEST-8
2023-02-10 16:15:17,774 http-nio-2990-exec-6 DEBUG admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [c.i.configmigration.defaultvalueoperations.DefaultValueOperationsDTO] parseValueHelper: com.riadalabs.jira.plugins.insight.services.model.ObjectBean
2023-02-10 16:15:17,774 http-nio-2990-exec-6 DEBUG admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [c.i.configmigration.insight.ObjectBeanDTO] Looking for ObjectTypeId: 25
2023-02-10 16:15:17,774 http-nio-2990-exec-6 DEBUG admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [c.r.j.p.i.services.permission.InsightPermissionServiceImpl] hasInsightObjectSchemaViewPermission (InsightUser{key=admin}, 3)
2023-02-10 16:15:17,774 http-nio-2990-exec-6 DEBUG admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [c.r.j.p.i.services.permission.InsightPermissionServiceImpl] hasInsightPermission (InsightUser{key=admin}, 1, null)
2023-02-10 16:15:17,774 http-nio-2990-exec-6 DEBUG admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [c.r.j.p.i.services.permission.InsightPermissionServiceImpl] hasInsightObjectTypeModifyPermission (InsightUser{key=admin}, 3)
2023-02-10 16:15:17,775 http-nio-2990-exec-6 INFO admin 975x104639x1 hta0vi 0:0:0:0:0:0:0:1 /secure/admin/plugins/handler/ExportAction.jspa [c.r.j.p.i.services.core.ObjectTypeServiceImpl] InsightException:PermissionInsightException: User admin didn't have correct permission (view) for object type: 3
com.riadalabs.jira.plugins.insight.common.exception.PermissionInsightException: PermissionInsightException: User admin didn't have correct permission (view) for object type: 3

I checked the Roles on ObjectTypeId 25 (which is Employee). They are by default empty. So I added admin:


But that makes no difference.

The schema is granted to admin by default:

As admin using the UI, I can access any and all Insight objects. Why do I not have permission, and how can I forcibly grant the access?

I also found a number of issues with PermissionInsightExcpetion:
https://jira.atlassian.com/browse/JSDSERVER-12199
It seems there are some issues with the Insight API? And none of the workaround are for the APIs.

The source code seems to be looking for object types 6, 7 or 5 for view and 5 for modify:

And from H2 console, if I am not interpreting the database incorrectly, I think I do have access: