class TrackedEntityServiceImpl implements TrackedEntityService, LocalDhisRepositoryPersistCallback<TrackedEntityInstance> {

    @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } )
    @Nonnull
    @Override
    public DhisResourceResult<TrackedEntityInstance> find( @Nonnull String trackedEntityTypeId, @Nonnull UriFilterApplier uriFilterApplier, int from, int max )
    {
        final DhisPagingQuery pagingQuery = DhisPagingUtils.createPagingQuery( from, max );
        final List<String> variables = new ArrayList<>();
        final String uri = uriFilterApplier.add( UriComponentsBuilder.newInstance(), variables ).path( "/trackedEntityInstances.json" )
            .queryParam( "skipPaging", "false" ).queryParam( "page", pagingQuery.getPage() ).queryParam( "pageSize", pagingQuery.getPageSize() )
            .queryParam( "trackedEntityType", trackedEntityTypeId ).queryParam( "ouMode", uriFilterApplier.containsQueryParam( "ou" ) ? "SELECTED" : "ACCESSIBLE" )
            .queryParam( "fields", TEI_FIELDS ).build().toString();
        final ResponseEntity<TrackedEntityInstances> result;
        try
        {
            result = restTemplate.getForEntity( uri, TrackedEntityInstances.class, variables.toArray() );
        }
        catch ( HttpClientErrorException e )
        {
            if ( e.getStatusCode() == HttpStatus.BAD_REQUEST || e.getStatusCode() == HttpStatus.CONFLICT )
            {
                throw new DhisFindException( e.getMessage(), e );
            }
            throw e;
        }
        final TrackedEntityInstances instances = Objects.requireNonNull( result.getBody() );

        return new DhisResourceResult<>( (instances.getTrackedEntityInstances().size() > pagingQuery.getResultOffset()) ?
            instances.getTrackedEntityInstances().subList( pagingQuery.getResultOffset(), instances.getTrackedEntityInstances().size() ) : Collections.emptyList(),
            (instances.getTrackedEntityInstances().size() >= pagingQuery.getPageSize()) );
    }

    @Autowired public  TrackedEntityServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" ) RestTemplate restTemplate, @Nonnull RequestCacheService requestCacheService,
        @Nonnull TrackedEntityMetadataService metadataService, @Nonnull StoredDhisResourceService storedItemService );

}