Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you would like to override security use aipIndexService.queryDocsForObjectOverrideSecurity, if you want permission check with current user use aipIndexService.queryDocsForObject.


Find out how to filter out assets at search

Go to Asset Navigator and perform the search. You may select multiple options to filter assets.


Script

Code Block
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import groovy.transform.Field
import inventoryplugin.service.status.StatusEnum
import inventoryplugin.service.status.SubstatusEnum
import inventoryplugin.dto.portable.InventoryDto
import inventoryplugin.dto.portable.InventoryItemDto
import inventoryplugin.helper.JipUtilityHelper
import inventoryplugin.service.index.AipIndexService
import inventoryplugin.service.index.search.QueryIndexParam
import inventoryplugin.service.index.search.QueryIndexResult
import inventoryplugin.service.index.search.QueryIndexSearchParam
import inventoryplugin.service.rest.dto.SystemFieldDto
import org.apache.log4j.Level
import org.apache.log4j.Logger

@Field Logger logger = Logger.getLogger("snapbytes.inventoryplugin.groovy.script")
logger.setLevel(Level.DEBUG)

@WithPlugin("plugin.jip")

@PluginModule
AipIndexService aipIndexService

try {
    QueryIndexParam queryIndexParam = new QueryIndexParam();
    queryIndexParam.setQuerySource(QueryIndexParam.QuerySource.GENERIC_SEARCH);
    queryIndexParam.setSortField("asset.name");
    queryIndexParam.setSortDirection("asc");
    queryIndexParam.setSortType("STRING");
    queryIndexParam.setSearchType("basic");
    queryIndexParam.setPageNumber(1);
    queryIndexParam.setPageSize(1000);
    queryIndexParam.setQueryIndexSearchParams(new ArrayList<>());

    // filter by asset type
    QueryIndexSearchParam query = new QueryIndexSearchParam();
    query.setField("form.name");
    query.setKeywords(new ArrayList<>());
    query.getKeywords().add("all types");
    query.setRange(true);
    queryIndexParam.getQueryIndexSearchParams().add(query);

    QueryIndexSearchParam query2 = new QueryIndexSearchParam();
    query2.setField("asset.id");
    query2.setMinNum(10494);
    query2.setMaxNum(10495);
    query2.setRange(true);
    queryIndexParam.getQueryIndexSearchParams().add(query2);

    def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    //QueryIndexResult queryIndexResult = aipIndexService.queryDocsForObject(queryIndexParam, user);
    QueryIndexResult queryIndexResult = aipIndexService.queryDocsForObjectOverrideSecurity(queryIndexParam, user);
    def queryIndexResultAsTxt = JipUtilityHelper.convertObjectToJsonStringSafe(queryIndexResult);

    List<InventoryDto> assets = JipUtilityHelper.convertJsonStringToListSafe(queryIndexResult.getAssets(), InventoryDto.class);

    if (assets != null) {
        logger.debug(assets.size() + ' assets found')

        for (InventoryDto oneAsset : assets) {
            logger.debug('----------------------------------------------------')
            logger.debug('----------------------------------------------------')
            logger.debug('Asset id: ' + oneAsset.id)
            logger.debug('Asset name: ' + oneAsset.name)
            logger.debug('Asset type id: ' + oneAsset.formId)
            logger.debug('Asset type name: ' + oneAsset.formName)
            logger.debug('Asset type scheme id: ' + oneAsset.objectSchemaId)
            logger.debug('Asset type scheme name: ' + oneAsset.objectSchemaName)
            logger.debug('Attachments json string: ' + oneAsset.attachments)
            logger.debug('Updated by: ' + oneAsset.updatedBy)
            logger.debug('Updated at: ' + oneAsset.updated)
            logger.debug('Created by: ' + oneAsset.creator)
            logger.debug('Created at: ' + oneAsset.created)

            logger.debug('--- Attributes ----------------------')
            List<InventoryItemDto> inventoryItems = oneAsset.getInventoryItems();
            for (InventoryItemDto assetAttr : inventoryItems) {

                if (assetAttr.attributeType == 'InventoryList' || assetAttr.attributeType == 'InventoryListByForm') {
                    logger.debug(assetAttr.attributeName + ': ');
                    for (def oneVal : assetAttr.inventoryRefs) {
                        logger.debug('       - id:' + oneVal.id + ', name:' + oneVal.name);
                    }
                } else if (assetAttr.attributeType == 'DatePicker' || assetAttr.attributeType == 'DatetimePicker') {
                    logger.debug(assetAttr.attributeName + ': ' + assetAttr.isoDateValue);
                } else if (assetAttr.attributeType == 'JiraMultipleUsers') {
                    logger.debug(assetAttr.attributeName + ': ');
                    if (assetAttr.users != null && assetAttr.users.size() > 0) {
                        for (def oneUser : assetAttr.users) {
                            logger.debug("     ----- " );
                            logger.debug("     - name: " + oneUser.name);
                            logger.debug("     - key: " + oneUser.key);
                            logger.debug("     - display name: " + oneUser.displayName);
                            logger.debug("     - email address: " + oneUser.emailAddress);
                            logger.debug("     - active: " + oneUser.active);
                        }
                    }
                } else if (assetAttr.attributeType == 'JiraSingleUser' || assetAttr.attributeType == 'UserPicker') {
                    logger.debug(assetAttr.attributeName + ': ');
                    if (assetAttr.user != null) {
                        logger.debug("     - name: " + assetAttr.user.name);
                        logger.debug("     - key: " + assetAttr.user.key);
                        logger.debug("     - display name: " + assetAttr.user.displayName);
                        logger.debug("     - email address: " + assetAttr.user.emailAddress);
                        logger.debug("     - active: " + assetAttr.user.active);
                    }
                } else if (assetAttr.attributeType == 'URL') {
                    logger.debug(assetAttr.attributeName + ': ' + assetAttr.value);
                } else if (assetAttr.multiListValues != null && assetAttr.multiListValues.size() > 0) {
                    logger.debug(assetAttr.attributeName + ': ');
                    for (def oneVal : assetAttr.multiListValues) {
                        logger.debug('       - ' + oneVal);
                    }
                } else {
                    String displayValue = (assetAttr.displayValue != null && assetAttr.displayValue != assetAttr.value) ? (", (Display value: " + assetAttr.displayValue + ")") : ""
                    logger.debug(assetAttr.attributeName + ': ' + assetAttr.value + displayValue);
                }
            }

            logger.debug('--- System Fields ----------------------')
            List<SystemFieldDto> systemFields = oneAsset.getSystemFields();
            for (SystemFieldDto systemFieldDto : systemFields) {

                if (systemFieldDto.fieldName == "asset.status") {
                    def status = StatusEnum.getByKey(systemFieldDto.value);
                    String displayValue = status != null ? status.displayName : ""
                    logger.debug(systemFieldDto.fieldName + ': ' + systemFieldDto.value + " (Display value: " + displayValue + ")");
                } else if (systemFieldDto.fieldName == "asset.substatus") {
                    def substatus = SubstatusEnum.getByKey(systemFieldDto.value);
                    String displayValue = substatus != null ? substatus.displayName : ""
                    logger.debug(systemFieldDto.fieldName + ': ' + systemFieldDto.value + " (Display value: " + displayValue + ")");
                } else {
                    logger.debug(systemFieldDto.fieldName + ': ' + systemFieldDto.value);
                }
            }
        }
    }
    return queryIndexResultAsTxt;

} catch (Exception e) {
    logger.debug(e.getMessage())
    return e.getMessage();
}

...