Java API - Get Elements Connect field display value


Table of contents


Use case

Display Elements Connect public (visible) values (e.g: in another addon).

Using the service

OSGi import

You need to add the following package 'com.valiantys.nfeed.api' to your OSGi dependencies instruction in the pom file. Like this:



<plugins>
    <plugin>
        <groupId>com.atlassian.maven.plugins</groupId>
        <artifactId>maven-jira-plugin</artifactId>
        ...
        <configuration>
            ...
            <instructions>
                <DynamicImport-Package>
					...,
    				com.valiantys.nfeed.api;resolution:="optional"
                </DynamicImport-Package>
            </instructions>
...


Import-Package vs. DynamicImport-Package

The package can be added in the Import-Package section instead of the DynamicImport-Package. The issue with Import-Package is that is does not detect classes after install (if Elements Connect is installed or enabled after your add-on, API classes won't be loaded)

Accessing the service

This feature is provided by the IFieldDisplayService:



IFieldDisplayService example
import com.valiantys.nfeed.api.FieldDisplayResult;
import com.valiantys.nfeed.api.IFieldDisplayService;
import com.valiantys.nfeed.api.View;
import org.apache.felix.framework.FilterImpl;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

public class ConnectAPIService implements InitializingBean, DisposableBean {

    @Autowired
    private BundleContext bundleContext;

    private ServiceTracker serviceTracker;

    /**
     * Gets the Connect field Issue rendering
     */ 
    public String getDisplayResult(long issueId, String customFieldId) throws ExocetException {
        IFieldDisplayService fieldDisplayService = getFieldDisplayService();
        FieldDisplayResult displayResult = fieldDisplayService.getDisplayResult(issueId, customFieldId);
        if (displayResult == null) {
            return null;
        }
        if (displayResult.hasError()) {
            // Throw exception or process error
			throw new RuntimeException(displayResult.getError());
        }
        return displayResult.getDisplay();
    }

    /**
     * Allows to chose the Connect field Issue rendering
     */ 
    public String getDisplayResult(long issueId, String customFieldId, View view) throws ExocetException {
        IFieldDisplayService fieldDisplayService = getFieldDisplayService();
        FieldDisplayResult displayResult = fieldDisplayService.getDisplayResult(issueId, customFieldId, view);
        if (displayResult == null) {
            return null;
        }
        if (displayResult.hasError()) {
            // Throw exception or process error
			throw new RuntimeException(displayResult.getError());
        }
        return displayResult.getDisplay();
    }

    public IFieldDisplayService getFieldDisplayService() {
        return (IFieldDisplayService) serviceTracker.getService();
    }

    @Override
    public void destroy() throws Exception {
        serviceTracker.close();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        final String filter = "(objectClass=" + IFieldDisplayService.class.getName() + ")";
        serviceTracker = new ServiceTracker(bundleContext, new FilterImpl(filter), null);
        serviceTracker.open();
    }
}


OSGi Bundle - Important Note

Please note that referencing the bundle itself is not recommended, do not do this. You can get the service by its class as in the example above.

In case you still need to reference Elements Connect OSGi bundle directly by its name, it has changed as of version 5.13.22.

VersionBundle name
5.13.22 or latercom.valiantys.jira.plugins.elements-connect
before 5.13.22com.valiantys.jira.plugins.nFeed

ClassNotFoundException

The service above makes reference to classes provided by Elements Connect. You must make sure that Elements Connect is installed before accessing the service or even trying to reference it. This check must be done at runtime. If Elements Connect is not present and you try to use the service, your code might fail with a ClassNotFoundException.