@Test public void applySaves() { final LocalDhisResourceRepositoryImpl<TrackedEntityInstance> resourceRepository = new LocalDhisResourceRepositoryImpl<>( TrackedEntityInstance.class ); final WritableTrackedEntityType trackedEntityType = new WritableTrackedEntityType(); trackedEntityType.setAttributes( Collections.emptyList() ); final TrackedEntityInstance trackedEntityInstance1 = new TrackedEntityInstance( trackedEntityType, "a1234567890", true ); final Object resourceKey1 = new Object(); final TrackedEntityInstance trackedEntityInstance2 = new TrackedEntityInstance( trackedEntityType, "a1234567891", true ); final Object resourceKey2 = new Object(); final TrackedEntityInstance trackedEntityInstance3 = new TrackedEntityInstance( trackedEntityType, "a1234567892", false ); final Object resourceKey3 = new Object(); final TrackedEntityInstance trackedEntityInstance4 = new TrackedEntityInstance( trackedEntityType, "a1234567893", true ); final Object resourceKey4 = new Object(); final Object resourceKey4_2 = new Object(); final TrackedEntityInstance trackedEntityInstance5 = new TrackedEntityInstance( trackedEntityType, "a1234567894", false ); final Object resourceKey5 = new Object(); final TrackedEntityInstance trackedEntityInstance6 = new TrackedEntityInstance( trackedEntityType, "a1234567896", true ); final Object resourceKey6 = new Object(); final Object resourceKey6_2 = new Object(); resourceRepository.save( trackedEntityInstance1, resourceKey1 ); resourceRepository.save( trackedEntityInstance2, resourceKey2 ); resourceRepository.save( trackedEntityInstance3, resourceKey3 ); resourceRepository.save( trackedEntityInstance4, resourceKey4 ); resourceRepository.save( trackedEntityInstance5, resourceKey5 ); resourceRepository.save( trackedEntityInstance6, resourceKey6 ); resourceRepository.deleteById( "a1234567893", resourceKey4_2, TrackedEntityInstance::new ); resourceRepository.deleteById( "a1234567894", null, TrackedEntityInstance::new ); resourceRepository.deleteById( "a1234567895", resourceKey5, TrackedEntityInstance::new ); resourceRepository.deleteById( "a1234567896", null, TrackedEntityInstance::new ); resourceRepository.save( trackedEntityInstance6, resourceKey6_2 ); final List<DhisResource> persistedResources = new ArrayList<>(); final List<Object> persistedResourceKeys = new ArrayList<>(); resourceRepository.applySaves( new LocalDhisRepositoryPersistCallback<TrackedEntityInstance>() { @Override public void persistSave( @Nonnull Collection<TrackedEntityInstance> resources, boolean create, @Nullable Consumer<LocalDhisRepositoryPersistResult> resultConsumer ) { if ( create ) { Assert.assertThat( resources, Matchers.containsInAnyOrder( trackedEntityInstance1, trackedEntityInstance2, trackedEntityInstance6 ) ); } else { Assert.assertThat( resources, Matchers.containsInAnyOrder( trackedEntityInstance3 ) ); } resources.forEach( resource -> { assert resultConsumer != null; resultConsumer.accept( new LocalDhisRepositoryPersistResult( LocalDhisRepositoryPersistStatus.SUCCESS, resource.getId() ) ); } ); } @Nonnull @Override public TrackedEntityInstance persistSave( @Nonnull TrackedEntityInstance resource ) { throw new UnsupportedOperationException(); } @Override public void persistDeleteById( @Nonnull Collection<String> ids, @Nullable Consumer<LocalDhisRepositoryPersistResult> resultConsumer ) { throw new UnsupportedOperationException(); } @Override public boolean persistDeleteById( @Nonnull String id ) { throw new UnsupportedOperationException(); } }, ( resource, resourceKey, result ) -> { Assert.assertEquals( LocalDhisRepositoryPersistStatus.SUCCESS, result.getStatus() ); persistedResources.add( resource ); persistedResourceKeys.add( resourceKey ); } ); Assert.assertThat( persistedResources, Matchers.containsInAnyOrder( trackedEntityInstance1, trackedEntityInstance2, trackedEntityInstance3, trackedEntityInstance6 ) ); Assert.assertThat( persistedResourceKeys, Matchers.containsInAnyOrder( resourceKey1, resourceKey2, resourceKey3, resourceKey6_2 ) ); }
@Override public void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_NEW ), true ); applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_EXISTING ), false ); }
LocalDhisResourceRepositoryImpl implements LocalDhisResourceRepository<T> { @Override public void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_NEW ), true ); applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_EXISTING ), false ); } }
LocalDhisResourceRepositoryImpl implements LocalDhisResourceRepository<T> { @Override public void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_NEW ), true ); applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_EXISTING ), false ); } LocalDhisResourceRepositoryImpl( @Nonnull Class<T> resourceClass ); }
LocalDhisResourceRepositoryImpl implements LocalDhisResourceRepository<T> { @Override public void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_NEW ), true ); applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_EXISTING ), false ); } LocalDhisResourceRepositoryImpl( @Nonnull Class<T> resourceClass ); @Override @Nonnull T save( @Nonnull T resource, @Nullable Object resourceKey ); @Override boolean deleteById( @Nonnull String id, @Nullable Object resourceKey, @Nonnull Function<String, T> prototypeFunction ); @Override boolean containsCollectionKey( @Nonnull String key ); @Override @Nonnull Collection<T> found( @Nonnull Collection<T> resources, @Nonnull String key ); @Override @Nonnull Optional<T> findOneById( @Nonnull String id ); @Override @Nonnull Collection<T> find( @Nonnull Predicate<T> filter ); @Override @Nonnull Collection<T> find( @Nonnull String trackedEntityInstanceId, @Nonnull Predicate<T> filter ); @Override void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ); @Override void applyDeletes( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ); }
LocalDhisResourceRepositoryImpl implements LocalDhisResourceRepository<T> { @Override public void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ) { applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_NEW ), true ); applySaves( persistCallback, resultCallback, Collections.singleton( LocalDhisResourceState.SAVED_EXISTING ), false ); } LocalDhisResourceRepositoryImpl( @Nonnull Class<T> resourceClass ); @Override @Nonnull T save( @Nonnull T resource, @Nullable Object resourceKey ); @Override boolean deleteById( @Nonnull String id, @Nullable Object resourceKey, @Nonnull Function<String, T> prototypeFunction ); @Override boolean containsCollectionKey( @Nonnull String key ); @Override @Nonnull Collection<T> found( @Nonnull Collection<T> resources, @Nonnull String key ); @Override @Nonnull Optional<T> findOneById( @Nonnull String id ); @Override @Nonnull Collection<T> find( @Nonnull Predicate<T> filter ); @Override @Nonnull Collection<T> find( @Nonnull String trackedEntityInstanceId, @Nonnull Predicate<T> filter ); @Override void applySaves( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback ); @Override void applyDeletes( @Nonnull LocalDhisRepositoryPersistCallback<T> persistCallback, @Nonnull LocalDhisRepositoryResultCallback resultCallback );  }