From 800aa2de5d2fcbfff21703f210f8b153b0e72d95 Mon Sep 17 00:00:00 2001 From: SilverD3 Date: Fri, 4 Oct 2024 09:19:04 +0100 Subject: [PATCH] new:OH2-379 | Supplier soft deletion --- .../manager/SupplierBrowserManager.java | 8 +++-- .../SupplierIoOperationRepository.java | 2 ++ .../supplier/service/SupplierOperations.java | 27 ++++++++++----- .../java/org/isf/supplier/TestSupplier.java | 16 ++++----- src/test/java/org/isf/supplier/Tests.java | 33 +++++++++++++++++++ 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/isf/supplier/manager/SupplierBrowserManager.java b/src/main/java/org/isf/supplier/manager/SupplierBrowserManager.java index e00d27bc7..b7d9e5f25 100644 --- a/src/main/java/org/isf/supplier/manager/SupplierBrowserManager.java +++ b/src/main/java/org/isf/supplier/manager/SupplierBrowserManager.java @@ -33,7 +33,7 @@ @Component public class SupplierBrowserManager { - private SupplierOperations ioOperations; + private final SupplierOperations ioOperations; public SupplierBrowserManager(SupplierOperations supplierOperations) { this.ioOperations = supplierOperations; @@ -43,6 +43,10 @@ public Supplier saveOrUpdate(Supplier supplier) throws OHServiceException { return ioOperations.saveOrUpdate(supplier); } + public void delete(Supplier supplier) throws OHServiceException { + ioOperations.delete(supplier); + } + public Supplier getByID(int id) throws OHServiceException { return ioOperations.getByID(id); } @@ -60,7 +64,7 @@ public List getList() throws OHServiceException { * * @param all - if {@code true} it will return deleted ones also * @return the {@link HashMap} of all {@link Supplier}'s ids and names. - * @throws OHServiceException + * @throws OHServiceException When failed to get suppliers */ public Map getHashMap(boolean all) throws OHServiceException { List supList; diff --git a/src/main/java/org/isf/supplier/service/SupplierIoOperationRepository.java b/src/main/java/org/isf/supplier/service/SupplierIoOperationRepository.java index ae1ca877f..4989eb467 100644 --- a/src/main/java/org/isf/supplier/service/SupplierIoOperationRepository.java +++ b/src/main/java/org/isf/supplier/service/SupplierIoOperationRepository.java @@ -33,4 +33,6 @@ public interface SupplierIoOperationRepository extends JpaRepository findAllWhereNotDeleted(); + + Supplier findFirstBySupIdAndSupDeleted(int id, char deleted); } \ No newline at end of file diff --git a/src/main/java/org/isf/supplier/service/SupplierOperations.java b/src/main/java/org/isf/supplier/service/SupplierOperations.java index c30ea9bf0..e80acc550 100644 --- a/src/main/java/org/isf/supplier/service/SupplierOperations.java +++ b/src/main/java/org/isf/supplier/service/SupplierOperations.java @@ -38,8 +38,8 @@ @TranslateOHServiceException public class SupplierOperations { - private SupplierIoOperationRepository repository; - + private final SupplierIoOperationRepository repository; + public SupplierOperations(SupplierIoOperationRepository supplierIoOperationRepository) { this.repository = supplierIoOperationRepository; ExaminationParameters.initialize(); @@ -49,26 +49,37 @@ public SupplierOperations(SupplierIoOperationRepository supplierIoOperationRepos * Save or update a {@link Supplier}. * @param supplier - the {@link Supplier} to save or update * return the recently saved or updated {@link Supplier} object. - * @throws OHServiceException + * @throws OHServiceException When failed to save the supplier */ public Supplier saveOrUpdate(Supplier supplier) throws OHServiceException { return repository.save(supplier); } + /** + * Delete a supplier + *

This is a soft deletion, we're just setting supDeleted to true

+ * @param supplier The Supplier to be deleted + * @throws OHServiceException When failed to delete supplier + */ + public void delete(Supplier supplier) throws OHServiceException { + supplier.setSupDeleted('Y'); + repository.save(supplier); + } + /** * Returns a {@link Supplier} with specified ID * @param id - supplier ID * @return supplier - the {@link Supplier} object with specified ID or {@code null} if not found - * @throws OHServiceException + * @throws OHServiceException When failed to retrieve the supplier */ public Supplier getByID(int id) throws OHServiceException { - return repository.findById(id).orElse(null); + return repository.findFirstBySupIdAndSupDeleted(id, 'N'); } - + /** * Returns the list of all {@link Supplier}s, active and inactive * @return supList - the list of {@link Supplier}s - * @throws OHServiceException + * @throws OHServiceException When failed to retrieve all suppliers */ public List getAll() throws OHServiceException { return repository.findAll(); @@ -77,7 +88,7 @@ public List getAll() throws OHServiceException { /** * Returns the list of active {@link Supplier}s * @return supList - the list of {@link Supplier}s - * @throws OHServiceException + * @throws OHServiceException When failed to retrieve */ public List getList() throws OHServiceException { return repository.findAllWhereNotDeleted(); diff --git a/src/test/java/org/isf/supplier/TestSupplier.java b/src/test/java/org/isf/supplier/TestSupplier.java index eacf7299e..0592e72d2 100644 --- a/src/test/java/org/isf/supplier/TestSupplier.java +++ b/src/test/java/org/isf/supplier/TestSupplier.java @@ -29,14 +29,14 @@ public class TestSupplier { private Integer supId; - private String supName = "TestName"; - private String supAddress = "TestAddress"; - private String supTaxcode = "TestTax"; - private String supPhone = "TestPhone"; - private String supFax = "TestFax"; - private String supEmail = "TestEmail"; - private String supNote = "TestNote"; - private Character supDeleted = 'N'; + private final String supName = "TestName"; + private final String supAddress = "TestAddress"; + private final String supTaxcode = "TestTax"; + private final String supPhone = "TestPhone"; + private final String supFax = "TestFax"; + private final String supEmail = "TestEmail"; + private final String supNote = "TestNote"; + private final Character supDeleted = 'N'; public Supplier setup(boolean usingSet) throws OHException { Supplier supplier; diff --git a/src/test/java/org/isf/supplier/Tests.java b/src/test/java/org/isf/supplier/Tests.java index 19c30c6b7..3f7b7256c 100644 --- a/src/test/java/org/isf/supplier/Tests.java +++ b/src/test/java/org/isf/supplier/Tests.java @@ -25,6 +25,8 @@ import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; import org.isf.OHCoreTestCase; import org.isf.supplier.manager.SupplierBrowserManager; @@ -188,6 +190,37 @@ void testSupplierHashCode() throws Exception { assertThat(supplier.hashCode()).isEqualTo(hashCode); } + @Test + void testSupplierDeletion() throws Exception { + loadSuppliers(); + + Supplier supplier = supplierBrowserManager.getByID(1); + supplierBrowserManager.delete(supplier); + + assertThat(supplierBrowserManager.getByID(1)).isNull(); + } + + private void loadSuppliers() { + AtomicInteger i = new AtomicInteger(); + List suppliers = Stream.of('N','N','Y').map(deleted -> { + i.getAndIncrement(); + + return new Supplier( + null, + "Supplier " + i, + "supAddress " + i, + "supTaxCode " + i, + "supPhone " + i, + "supFax " + i, + "supEmail " + i, + "supNote " + i, + deleted + ); + }).toList(); + + supplierIoOperationRepository.saveAllAndFlush(suppliers); + } + private int setupTestSupplier(boolean usingSet) throws OHException { Supplier supplier = testSupplier.setup(usingSet); supplierIoOperationRepository.saveAndFlush(supplier);