Java API Documentation (v 2.6)

Java API Documentation (v 2.6)

Quick Start Guide

Jira Plugin

Download the following files

 

Add the files to your Maven repository

There are two possibilities to install the library files. Either install them in a local Maven repository or deploy them to a hosted Maven repository.

Install in the local Maven repository

mvn install:install-file -Dfile=user-profiles-external-api-1.1.0.jar -DgroupId=de.communardo.atlassian.plugins.userprofile.external.api -DartifactId=user-profiles-external-api -Dversion=1.1.0 -Dpackaging=jar -Djavadoc=user-profiles-external-api-1.1.0-javadoc.jar

Deploy to a hosted Maven repository

mvn deploy:deploy-file -Dfile=user-profiles-external-api-1.1.0.jar -DgroupId=de.communardo.atlassian.plugins.userprofile.external.api -DartifactId=user-profiles-external-api -Dversion=1.1.0 -Dpackaging=jar -Djavadoc=user-profiles-external-api-1.1.0-javadoc.jar -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> -Durl=<url-of-the-repository-to-deploy>

Include the UPJ Java API in your App pom.xml

<dependency> <groupId>de.communardo.atlassian.plugins.userprofile.external.api</groupId> <artifactId>user-profiles-external-api</artifactId> <version>1.1.0</version> <scope>provided</scope> </dependency>

Usage in our own classes (e.g. your components)

To import the services provided by our API as components, using Atlassian Spring Scanner as an example:

import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import de.communardo.atlassian.plugins.userprofile.external.api.service.UppProfileElementDataManager; import de.communardo.atlassian.plugins.userprofile.external.api.service.UppProfileElementManager; import org.springframework.stereotype.Component; @Component public class MyComponent { public MyComponent(@ComponentImport UppProfileElementManager uppProfileElementManager, @ComponentImport UppProfileElementDataManager uppProfileElementDataManager) { // do stuff with uppProfileElementManager and/or uppProfileElementDataManager } }

Make sure the User Profiles for Jira (Microsoft Attributes Sync) App is already installed on your Jira system.

Check Installation and Licensing (v 2.6) for further information.

Scriptrunner

General

Our Java API may also be accessed via Scriptrunner. To be able to do that you have to follow the steps which are described in the official Scriptrunner documentation, which can be found here: https://docs.adaptavist.com/sr4js/latest/integrations/other-apps

Make sure the User Profiles for Jira (Microsoft Attributes Sync) App is already installed on your Jira system.

Check Installation and Licensing (v 2.6) for further information.

Examples

To provide you a more easy way to start developing scripts with Scriptrunner, we prepared a minimal example, which provides you our API managers with whom you can access profile elements and their data:

Example 1

Example that prints all profile elements and the available element data for the current user in the log (log-level INFO, which may prevent you from seeing anything per default, so maybe use log-level ERROR instead if you want to directly see something in the log).

import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.atlassian.jira.com.rproxy.goskope.component.ComponentAccessor import com.atlassian.sal.api.user.UserKey import de.communardo.atlassian.plugins.userprofile.external.api.service.UppProfileElementManager import de.communardo.atlassian.plugins.userprofile.external.api.service.UppProfileElementDataManager @WithPlugin("de.communardo.jira.plugins.userprofile") @PluginModule UppProfileElementManager uppProfileElementManager @PluginModule UppProfileElementDataManager uppProfileElementDataManager log.info(uppProfileElementManager.getProfileElements()) log.info(uppProfileElementDataManager.getProfileElementData(new UserKey(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getKey())))

Example 2

Example that shows the data in a given profile element for a given user:

// sample script for app "User Profiles for Jira", showing how to diplay profile data of a user // config variables: def userName = "admin" // or leave empty to use currently logged-in user instead def profileElementId = 1 // ID can be found in URL of profile-element's "Edit"-link // end config // start code import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.atlassian.jira.com.rproxy.goskope.component.ComponentAccessor import com.atlassian.sal.api.user.UserKey import de.communardo.atlassian.plugins.userprofile.external.api.service.UppProfileElementManager import de.communardo.atlassian.plugins.userprofile.external.api.service.UppProfileElementDataManager import de.communardo.atlassian.plugins.userprofile.external.api.model.elements.* @WithPlugin("de.communardo.jira.plugins.userprofile") @PluginModule UppProfileElementManager uppProfileElementManager @PluginModule UppProfileElementDataManager uppProfileElementDataManager // get user def user = userName ? ComponentAccessor.userManager.getUserByName(userName) : ComponentAccessor.jiraAuthenticationContext.loggedInUser // get Profile Element by ID def profileElement = uppProfileElementManager.getProfileElement(profileElementId) // get data object in given profile element of given user def profileData = uppProfileElementDataManager.getProfileElementData(new UserKey(user.getKey()), profileElement) // get human readable version of data object, depending on data type, see docu: https://communardo-products.atlassian.net/wiki/spaces/UPJ/pages/9798347/Java+API+Documentation+v+2.5#Profile-element-and-data-types def humanReadableValue if (profileElement instanceof MultiSelectProfileElement) { humanReadableValue = profileData.getOptions()*.getName() } else if (profileElement instanceof SingleSelectProfileElement) { humanReadableValue = profileData.getOption()?.getName() } else if (profileElement instanceof TextBasedProfileElement) { humanReadableValue = profileData.getText() } else if (profileElement instanceof UserProfileElement) { // value is a userKey -> 1) get referenced user def referencedUser = ComponentAccessor.userManager.getUserByKey(profileData.getUserKey()?.getStringValue()) // 2) get displayName of referenced user humanReadableValue = referencedUser?.getDisplayName() } """ Showing data of user <b>${user.getDisplayName()}</b> in profile element <b>${profileElement.getName()}</b> (ID ${profileElement.getId()}, type ${profileElement.class.simpleName}):<br> <b>${humanReadableValue}</b> """

Internal structure and basic usage

The UPJ Java API consists out of two basic elements: the profile element and the profile element data. Profile elements can be added and removed in the UPJ administration (check Create and Edit User Profile Elements (v 2.6) for further information). Values may afterwards be added for every user via an LDAP sync or by manually editing user profiles (in the administration or the user does it on his own) or even by using our new Java API.

To interact with profile elements, just inject and use the provided UppProfileElementManager. With this manager you will be able to load the available profile elements. Afterwards you may want to access some data which is related to a user. To achieve that, just inject and use the provided UppProfileElementDataManager.

Profile element and data types

There are several profile element and data types which are used to work with the API. To provide you a better start, we are going to list all currently available profile elements and their matching data models in the following table:

Profile Element Models

Profile Element Data Models

getter Method

setter Method(s)

Profile Element Models

Profile Element Data Models

getter Method

setter Method(s)

MultiSelectProfileElement

MultiSelectProfileElementData

Set<OptionedProfileElement.Option> getOptions()

setOptions(Set<OptionedProfileElement.Option> options)

addOption(OptionedProfileElement.Option option)
addOptions(Set<OptionedProfileElement.Option> options)
removeOption(OptionedProfileElement.Option option)
removeOptions(Set<OptionedProfileElement.Option> options)
removeAllOptions()

SingleSelectProfileElement

SingleSelectProfileElementData

OptionedProfileElement.Option getOption()

setOption(OptionedProfileElement.Option option)

removeOption()

TextBasedProfileElement

TextBasedProfileElementData

String getText()

setText(String text)

UserProfileElement

UserProfileElementData

UserKey getUserKey()

setUserKey(UserKey userKey)

You have to use matching profile element and data types to be able to interact with the API. Furthermore, the specialized data objects make an interaction with a specific data type more easy.

Event publication

ProfileElementDataChangedEvent

The User Profiles for Jira App publishes events every minute, which contain all changes that are taken since the last publication. One event contains a maximum of 1000 changes as a collection of updates. Each update contains a key of a user, a profile element and the new data. The following table shows the models of the updates:

Profile Element Update Models

Profile Element Models

Data Models

Profile Element Update Models

Profile Element Models

Data Models

MultiSelectProfileElementDataUpdate

MultiSelectProfileElement

Set<OptionedProfileElement.Option>

SingleSelectProfileElementDataUpdate

SingleSelectProfileElement

OptionedProfileElement.Option

TextBasedProfileElementDataUpdate

TextBasedProfileElement

String

UserProfileElementDataUpdate

UserProfileElement

UserKey

ProfileElementNameChangedEvent

This event is published when the default name of a profile element is changed. It contains the updated profile element.

ProfileElementRemovedEvent

This event is published when a profile element is removed. It contains the removed profile element.

ProfileElementOptionNameChangedEvent

This event is published when the default name of an option of a profile element is changed. It contains the updated profile element and the updated option.

ProfileElementOptionRemovedEvent

This event is published when an option of a profile element is removed. It contains the updated profile element and the removed option.

Examples

To kickstart you a little bit more, we want to provide you with a few examples on what you can currently do with our Java API:

UppProfileElementManager

Loading all profile elements

Collection<ProfileElement> profileElements = uppProfileElementManager.getProfileElements();

Loading a specific profile element with a specific id (for example 1)

ProfileElement profileElement = uppProfileElementManager.getProfileElement(1); //or if you know the type of the profileElement (in this example it is a TextBasedProfileElement) TextBasedProfileElement textbasedProfileElement = uppProfileElementManager.getProfileElement(1);

UppProfileElementDataManager

Loading profile element data for a profile element and a user

ProfileElementData data = uppProfileElementDataManager.getProfileElementData(userKey, profileElement); //or if you know the type of the profileElement or the data (in this example it is a TextBasedProfileElementData) TextBasedProfileElementData data = uppProfileElementDataManager.getProfileElementData(userKey, profileElement);

Loading all profile elements for a user

Map<ProfileElement, ProfileElementData> data = uppProfileElementDataManager.getProfileElementData(userKey);

Storing profile element data for a profile element and a user

uppProfileElementDataManager.storeProfileElementData(userKey, profileElement, data);

To be able to store data, the type of the profile Element must match the type of the data.

ProfileElementDataChangedEvent

Event Listener example

This example describes how to write an event-listener for user profile data changes in your own Jira app.

(Fyi, due to limitations in ScriptRunner these events are not available there; which unfortunately means that it’s not possible to write scripted event-listeners for our events.)

<!-- add the listener as component to atlassian-plugin.xml --> <component key="unique-key-for-your-event-listener" class="path.to.your.event.listener.class.in.this.example.ProfileElementDataChangeEventListener"/> <!-- or use @Component at your listener class if you use the annotation notation in your app -->
public class ProfileElementDataChangeEventListener { public ProfileElementDataChangeEventListener(@ComponentImport EventPublisher eventPublisher) { // register the event listener class in the event publisher eventPublisher.register(this); } @EventListener public void onProfileElementDataChange(ProfileElementDataChangedEvent profileElementDataChangedEvent) { // get all change sets in the event (the number of elements are not limited) Collection<ProfileElementDataUpdate> updates = profileElementDataChangedEvent.getProfileElementDataUpdates(); // iterates over all change sets for(ProfileElementDataUpdate update : updates ) { if (update instanceof TextBasedProfileElementDataUpdate) { // do some stuff for a change of a TextBasedProfileElement } else if (update instanceof MultiSelectProfileElementDataUpdate) { // do some stuff for a change of a MultiSelectProfileElement } else if (update instanceof SingleSelectProfileElementDataUpdate) { // do some stuff for a change of a SingleSelectProfileElement } else if (update instanceof UserProfileElementDataUpdate) { // do some stuff for a change of a UserProfileProfileElement } } } }

You can also follow the instructions on https://developer.atlassian.com/server/jira/platform/writing-jira-event-listeners-with-the-atlassian-event-library/ .