class TrackedEntityServiceImpl implements TrackedEntityService, LocalDhisRepositoryPersistCallback<TrackedEntityInstance> {

    @HystrixCommand( ignoreExceptions = UnauthorizedException.class )
    @Nonnull
    protected TrackedEntityInstance create( @Nonnull TrackedEntityInstance trackedEntityInstance )
    {
        final DhisSyncGroup syncGroup = storedItemService.findSyncGroupById( DhisSyncGroup.DEFAULT_ID )
            .orElseThrow( () -> new DhisResourceException( "Could not load default DHIS2 sync group." ) );
        final ResponseEntity<ImportSummariesWebMessage> response;

        if ( trackedEntityInstance.getId() == null )
        {
            trackedEntityInstance.setId( CodeGenerator.generateUid() );
        }

        try
        {
            clear( trackedEntityInstance );
            response = restTemplate.exchange( CREATE_URI, HttpMethod.POST, new HttpEntity<>( trackedEntityInstance ), ImportSummariesWebMessage.class );
        }
        catch ( HttpClientErrorException e )
        {
            if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) )
            {
                throw new DhisConflictException( "Tracked tracked entity instance could not be created: " + e.getResponseBodyAsString(), e );
            }

            throw e;
        }

        final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

        if ( result.isNotSuccessful() )
        {
            throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful import of tracked entity instance." );
        }

        trackedEntityInstance.setId( result.getResponse().getImportSummaries().get( 0 ).getReference() );
        trackedEntityInstance.resetNewResource();
        trackedEntityInstance.setLocal( false );

        storeItem( syncGroup, trackedEntityInstance.getId(), response );
        return trackedEntityInstance;
    }

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

}