Skip to content

Commit

Permalink
Merge branch 'develop' into update/OP-1351-change-OP_FOR-field
Browse files Browse the repository at this point in the history
  • Loading branch information
mwithi authored Oct 7, 2024
2 parents d5f3ffb + dcd41a5 commit f6828da
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@Component
public class SupplierBrowserManager {

private SupplierOperations ioOperations;
private final SupplierOperations ioOperations;

public SupplierBrowserManager(SupplierOperations supplierOperations) {
this.ioOperations = supplierOperations;
Expand All @@ -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);
}
Expand All @@ -60,7 +64,7 @@ public List<Supplier> 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<Integer, String> getHashMap(boolean all) throws OHServiceException {
List<Supplier> supList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface SupplierIoOperationRepository extends JpaRepository<Supplier, I

@Query(value = "select s from Supplier s where s.supDeleted = 'N'")
List<Supplier> findAllWhereNotDeleted();

Supplier findFirstBySupIdAndSupDeleted(int id, char deleted);
}
27 changes: 19 additions & 8 deletions src/main/java/org/isf/supplier/service/SupplierOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
@TranslateOHServiceException
public class SupplierOperations {

private SupplierIoOperationRepository repository;
private final SupplierIoOperationRepository repository;

public SupplierOperations(SupplierIoOperationRepository supplierIoOperationRepository) {
this.repository = supplierIoOperationRepository;
ExaminationParameters.initialize();
Expand All @@ -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
* <p>This is a soft deletion, we're just setting <code>supDeleted</code> to <code>true</code></p>
* @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<Supplier> getAll() throws OHServiceException {
return repository.findAll();
Expand All @@ -77,7 +88,7 @@ public List<Supplier> 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<Supplier> getList() throws OHServiceException {
return repository.findAllWhereNotDeleted();
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/isf/supplier/TestSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/isf/supplier/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Supplier> 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);
Expand Down

0 comments on commit f6828da

Please sign in to comment.