Skip to content

Commit

Permalink
OLMIS-7443 GET /lots endpoint can filter by orderableId
Browse files Browse the repository at this point in the history
  • Loading branch information
hdsldv committed Nov 29, 2021
1 parent da32d38 commit 59f5561
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 20 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
build/
out/
.idea/
gradle/
.env
.tx/
node_modules/
Expand Down
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public void shouldFindLotsWithSimilarCode() {
null,
expected.getLotCode(),
null,
null,
null,
pageRequest
);

Expand All @@ -107,6 +109,8 @@ public void shouldEnableAddingLotsWithSimilarCodeForDifferentTradeItems() {
null,
lotOne.getLotCode(),
null,
null,
null,
pageRequest
);

Expand All @@ -121,7 +125,7 @@ public void shouldFindLotsByExpirationDate() {
expected.setExpirationDate(now);
expected = lotRepository.save(expected);

Page<Lot> lotPage = lotRepository.search(null, now, null, null, pageRequest);
Page<Lot> lotPage = lotRepository.search(null, now, null, null, null, null, pageRequest);

assertEquals(1, lotPage.getNumberOfElements());
assertEquals(expected, lotPage.getContent().get(0));
Expand All @@ -136,6 +140,8 @@ public void shouldFindLotsByTradeItem() {
null,
null,
null,
null,
null,
pageRequest
);

Expand All @@ -153,6 +159,8 @@ public void shouldFindLotsByMultipleTradeItems() {
null,
null,
null,
null,
null,
pageRequest
);

Expand All @@ -168,6 +176,8 @@ public void shouldFindAllLotsIfSearchByEmptyTradeItemList() {
null,
null,
null,
null,
null,
pageRequest
);

Expand All @@ -183,6 +193,8 @@ public void shouldFindLotsByAllParameters() {
expected.getExpirationDate(),
expected.getLotCode(),
Collections.singletonList(expected.getId()),
null,
null,
pageRequest
);

Expand All @@ -203,6 +215,8 @@ public void shouldFindLotsByIds() {
null,
null,
Arrays.asList(instanceOne.getId(), instanceTwo.getId()),
null,
null,
pageRequest
);

Expand All @@ -212,7 +226,7 @@ public void shouldFindLotsByIds() {

@Test
public void shouldReturnAllIfNoParamIsGiven() {
Page<Lot> lotPage = lotRepository.search(null, null, null, null, pageRequest);
Page<Lot> lotPage = lotRepository.search(null, null, null, null, null, null, pageRequest);

assertEquals(5, lotPage.getNumberOfElements());
}
Expand Down Expand Up @@ -257,6 +271,8 @@ public void shouldReturnCorrectDate() {
null,
entity.getLotCode(),
null,
null,
null,
pageRequest
);

Expand All @@ -273,6 +289,8 @@ public void shouldRespectPaginationParameters() {
null,
null,
null,
null,
null,
pageable
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void setUp() {
public void shouldCreateNewLot() {
mockUserHasRight(LOTS_MANAGE);

given(lotRepository.search(null, null, lot.getLotCode(), null, null))
given(lotRepository.search(null, null, lot.getLotCode(), null, null, null, null))
.willReturn(Pagination.getPage(Collections.emptyList(), PageRequest.of(0, 10)));

LotDto response = restAssured
Expand Down Expand Up @@ -161,7 +161,7 @@ public void shouldUpdateLot() {
mockUserHasRight(LOTS_MANAGE);
when(lotRepository.findById(lotId)).thenReturn(Optional.of(lot));

given(lotRepository.search(null, null, lot.getLotCode(), null, null))
given(lotRepository.search(null, null, lot.getLotCode(), null, null, null, null))
.willReturn(Pagination.getPage(Collections.singletonList(lot), PageRequest.of(0, 10)));

LotDto response = restAssured
Expand Down Expand Up @@ -226,7 +226,7 @@ public void shouldRejectUpdateLotIfUserHasNoRight() {
@Test
public void shouldFindLots() {
given(lotRepository.search(anyList(), any(LocalDate.class), anyString(),
nullable(List.class), any(Pageable.class)))
nullable(List.class), null, null, any(Pageable.class)))
.willReturn(Pagination.getPage(singletonList(lot), pageable));
when(tradeItemRepository.findAllById(anyList()))
.thenReturn(Collections.singletonList(new TradeItem()));
Expand Down Expand Up @@ -383,6 +383,8 @@ public void shouldFindByUuids() {
eq(null),
eq(null),
eq(Arrays.asList(lots.get(0).getId(), lots.get(1).getId())),
eq(null),
eq(null),
any(Pageable.class)
)).willReturn(Pagination.getPage(lots, PageRequest.of(0, 10)));

Expand Down Expand Up @@ -413,6 +415,8 @@ public void shouldRespectPaginationParams() {
eq(null),
eq(null),
eq(null),
eq(null),
eq(null),
eq(PageRequest.of(0, 2))
)).willReturn(Pagination.getPage(lots, PageRequest.of(0, 2)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@

public interface LotRepositoryCustom {

Page<Lot> search(Collection<TradeItem> tradeItems, LocalDate expirationDate, String code,
List<UUID> ids, Pageable pageable);

Page<Lot> search(
Collection<TradeItem> tradeItems,
LocalDate expirationDate,
String code,
List<UUID> ids,
LocalDate expirationDateFrom,
LocalDate expirationDateTo,
Pageable pageable
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,40 @@ public class LotRepositoryImpl implements LotRepositoryCustom {
* @param lotCode Part of wanted code.
* @return List of Facilities matching the parameters.
*/
public Page<Lot> search(Collection<TradeItem> tradeItems, LocalDate expirationDate,
String lotCode, List<UUID> ids, Pageable pageable) {

public Page<Lot> search(
Collection<TradeItem> tradeItems,
LocalDate expirationDate,
String lotCode,
List<UUID> ids,
LocalDate expirationDateFrom,
LocalDate expirationDateTo,
Pageable pageable
) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();

CriteriaQuery<Lot> lotQuery = builder.createQuery(Lot.class);
lotQuery = prepareQuery(lotQuery, tradeItems, expirationDate, lotCode, ids, false);
lotQuery = prepareQuery(
lotQuery,
tradeItems,
expirationDate,
lotCode,
ids,
expirationDateFrom,
expirationDateTo,
false
);

CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
countQuery = prepareQuery(countQuery, tradeItems, expirationDate, lotCode, ids, true);
countQuery = prepareQuery(
countQuery,
tradeItems,
expirationDate,
lotCode,
ids,
expirationDateFrom,
expirationDateTo,
true
);

Long count = entityManager.createQuery(countQuery).getSingleResult();

Expand Down Expand Up @@ -103,4 +127,54 @@ private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, Collection<Tra
query.where(predicate);
return query;
}

private <T> CriteriaQuery<T> prepareQuery(
CriteriaQuery<T> query,
Collection<TradeItem>
tradeItems,
LocalDate expirationDate,
String lotCode,
List<UUID> ids,
LocalDate expirationDateFrom,
LocalDate expirationDateTo,
boolean count
) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
Root<Lot> root = query.from(Lot.class);

if (count) {
CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
}

Predicate predicate = builder.conjunction();

if (!isEmpty(tradeItems)) {
predicate = builder.and(predicate, root.get("tradeItem").in(tradeItems));
}

if (lotCode != null) {
predicate = builder.and(predicate,
builder.like(builder.upper(root.get("lotCode")), "%" + lotCode.toUpperCase() + "%"));
}

if (expirationDate != null) {
predicate = builder.and(predicate, builder.equal(root.get("expirationDate"), expirationDate));
}

if (ids != null && ids.size() > 0) {
predicate = builder.and(predicate, root.get("id").in(ids));
}

if(expirationDateFrom != null) {
predicate = builder.and(predicate, builder.greaterThanOrEqualTo(root.get("expirationDate"), expirationDateFrom));
}

if(expirationDateTo != null) {
predicate = builder.and(predicate, builder.lessThanOrEqualTo(root.get("expirationDate"), expirationDateTo));
}

query.where(predicate);
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ public class LotSearchParams {
private String lotCode;
private List<UUID> id;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate expirationDateFrom;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate expirationDateTo;

private List<UUID> orderableId;
private boolean isTradeItemIdIgnored = false;
}
41 changes: 38 additions & 3 deletions src/main/java/org/openlmis/referencedata/service/LotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.validation.constraints.NotNull;
import org.openlmis.referencedata.domain.Lot;
import org.openlmis.referencedata.domain.Orderable;
import org.openlmis.referencedata.domain.TradeItem;
import org.openlmis.referencedata.exception.ValidationMessageException;
import org.openlmis.referencedata.repository.LotRepository;
import org.openlmis.referencedata.repository.OrderableRepository;
import org.openlmis.referencedata.repository.TradeItemRepository;
import org.openlmis.referencedata.util.Pagination;
import org.openlmis.referencedata.util.messagekeys.LotMessageKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -39,6 +47,9 @@ public class LotService {
@Autowired
private TradeItemRepository tradeItemRepository;

@Autowired
private OrderableRepository orderableRepository;

/**
* Returns page of lots matching the given request parameters.
*
Expand All @@ -47,13 +58,35 @@ public class LotService {
* @return the Page of lots found, or an empty page.
*/
public Page<Lot> search(@NotNull LotSearchParams requestParams, Pageable pageable) {
List<UUID> tradeItemId = Optional.ofNullable(requestParams.getTradeItemId()).orElse(Collections.emptyList());
List<UUID> orderableId = Optional.ofNullable(requestParams.getOrderableId()).orElse(Collections.emptyList());

if (!tradeItemId.isEmpty() && !orderableId.isEmpty()) {
throw new ValidationMessageException(LotMessageKeys.ERROR_ORDERABLE_ID_AND_TRADE_ITEM_ID_USED_TOGETHER);
}

List<TradeItem> tradeItems = Collections.emptyList();

if (!isEmpty(requestParams.getTradeItemId())) {
tradeItems = tradeItemRepository.findAllById(requestParams.getTradeItemId());
if (!tradeItemId.isEmpty() && !requestParams.isTradeItemIdIgnored()) {
tradeItems = tradeItemRepository.findAllById(tradeItemId);

if (tradeItems.isEmpty()) {
return Pagination.getEmptyPage(pageable);
}
}

if(!orderableId.isEmpty()) {
Page<Orderable> orderables = orderableRepository.findAllLatestByIds(requestParams.getOrderableId(), null);

tradeItemId = orderables.getContent().stream()
.filter(o -> Objects.nonNull(o.getTradeItemIdentifier()))
.map(o -> UUID.fromString(o.getTradeItemIdentifier()))
.collect(Collectors.toList());

tradeItems = tradeItemRepository.findAllById(tradeItemId);

if (tradeItems.isEmpty()) {
return Pagination.getPage(Collections.emptyList(), pageable, 0);
return Pagination.getEmptyPage(pageable);
}
}

Expand All @@ -62,6 +95,8 @@ public Page<Lot> search(@NotNull LotSearchParams requestParams, Pageable pageabl
requestParams.getExpirationDate(),
requestParams.getLotCode(),
requestParams.getId(),
requestParams.getExpirationDateFrom(),
requestParams.getExpirationDateTo(),
pageable
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/openlmis/referencedata/util/Pagination.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -145,4 +146,8 @@ public static <T> void handlePage(Function<Pageable, Page<T>> data,
pageable = pageable.next();
}
}

public static <T> Page<T> getEmptyPage(Pageable pageable) {
return Pagination.getPage(Collections.emptyList(), pageable, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public abstract class LotMessageKeys extends MessageKeys {
public static final String ERROR_TRADE_ITEM_REQUIRED = join(ERROR, TRADE_ITEM, REQUIRED);
public static final String ERROR_NOT_FOUND = join(ERROR, NOT_FOUND);
public static final String ERROR_LOT_CODE_MUST_BE_UNIQUE = join(ERROR, LOT_CODE, MUST_BE_UNIQUE);
public static final String ERROR_ORDERABLE_ID_AND_TRADE_ITEM_ID_USED_TOGETHER = join(ERROR, NOT_TOGETHER, "orderableIdAndtradeItemdId");
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public abstract class MessageKeys {
protected static final String MISSING = "missing";
protected static final String PROVIDED = "provided";
protected static final String ASSIGNED = "isAlreadyAssigned";
protected static final String NOT_TOGETHER = "notTogether";

// Entities
protected static final String LOT = "lot";
Expand Down
Loading

0 comments on commit 59f5561

Please sign in to comment.