DTO - find it by its identifier

AbstractDto can be identified by uuid identifier and if dto implements Codeable interface, then by it's string code too. Codeable interface is typically used for identity, role, system etc. ⇒ for dtos with some unique code. In this tutorial we will describe how to simple find single dto (or entity) by an identifier.

If we know dto's uuid identifier, we can use:

// by uuid
IdmIdentityDto identity = (IdmIdentityDto) lookupService.lookupDto(IdmIdentityDto.class, UUID.fromString("e963dd20-0a10-4052-8355-1425d021299a"));
// or more simpler by string uuid representation
IdmIdentityDto identity = (IdmIdentityDto) lookupService.lookupDto(IdmIdentityDto.class, "e963dd20-0a10-4052-8355-1425d021299a");

If we know dto's string code, we can use:

// by string code
IdmIdentityDto identity = (IdmIdentityDto) lookupService.lookupDto(IdmIdentityDto.class, "username");
As you can see, the method is still the same - is not important, what identifier we know - we can use him the same way for find dto instance.

Since IdmTreeNodeDto is not Codeable (tree node doesn't have unique code, because more tree types can exist), we can't use the previous example with lookupService for searching tree nodes by their code.

But if we don't have multiple tree types, the following script works for searching the tree node with the code "123456":

import eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto;
import eu.bcvsolutions.idm.core.api.dto.filter.IdmTreeNodeFilter;

IdmTreeNodeFilter filter = new IdmTreeNodeFilter();
filter.setCode("123456");
List<IdmTreeNodeDto> trees = treeNodeService.find(filter , null).getContent();
IdmTreeNodeDto org = null;
if (!trees.isEmpty()) {
    org = trees.get(0);
}

If you use this in the Groovy script, you must add following authorizations:

Classes:

  • java.util.Collections$UnmodifiableRandomAccessList
  • org.springframework.data.domain.PageImpl
  • eu.bcvsolutions.idm.core.api.dto.filter.IdmTreeNodeFilter
  • eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto

Services:

  • treeNodeService