Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update jooq repository to improve reusability (0.115.0) #9535

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

public record EntityIdRangeParameter(RangeOperator operator, EntityId value) implements RangeParameter<EntityId> {
public record EntityIdRangeParameter(RangeOperator operator, Long value) implements RangeParameter<Long> {

public static final EntityIdRangeParameter EMPTY = new EntityIdRangeParameter(null, null);
public static final EntityIdRangeParameter EMPTY = new EntityIdRangeParameter(null, EntityId.EMPTY);

public EntityIdRangeParameter(RangeOperator operator, EntityId entityId) {
this(operator, entityId.getId());
}

public static EntityIdRangeParameter valueOf(String entityIdRangeParam) {
if (StringUtils.isBlank(entityIdRangeParam)) {
Expand All @@ -43,7 +47,6 @@ public static EntityIdRangeParameter valueOf(String entityIdRangeParam) {
}

private static EntityId getEntityId(String entityId) {

List<Long> parts = Splitter.on('.')
.splitToStream(Objects.requireNonNullElse(entityId, ""))
.map(Long::valueOf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public enum RangeOperator {
private final String operator;
private final BiFunction<Field, Object, Condition> function;

public boolean isInclusive() {
return this == RangeOperator.EQ || this == RangeOperator.LTE || this == RangeOperator.GTE;
}

@Override
public String toString() {
return name().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ default boolean hasLowerBound() {
default boolean hasUpperBound() {
return operator() == RangeOperator.LT || operator() == RangeOperator.LTE;
}

default boolean isEmpty() {
return operator() == null && value() == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.hedera.mirror.restjava.common.Constants.DEFAULT_LIMIT;
import static com.hedera.mirror.restjava.common.Constants.MAX_LIMIT;
import static com.hedera.mirror.restjava.common.Constants.TOKEN_ID;
import static com.hedera.mirror.restjava.jooq.domain.Tables.NFT_ALLOWANCE;

import com.google.common.collect.ImmutableSortedMap;
import com.hedera.mirror.rest.model.NftAllowance;
Expand All @@ -34,7 +35,6 @@
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import lombok.CustomLog;
Expand Down Expand Up @@ -70,19 +70,19 @@ public class AllowancesController {
@GetMapping(value = "/nfts")
NftAllowancesResponse getNftAllowances(
@PathVariable EntityIdParameter id,
@RequestParam(name = ACCOUNT_ID, required = false) @Size(max = 2) List<EntityIdRangeParameter> accountIds,
@RequestParam(name = ACCOUNT_ID, required = false) @Size(max = 2) EntityIdRangeParameter[] accountIds,
@RequestParam(defaultValue = DEFAULT_LIMIT) @Positive @Max(MAX_LIMIT) int limit,
@RequestParam(defaultValue = "asc") Sort.Direction order,
@RequestParam(defaultValue = "true") boolean owner,
@RequestParam(name = TOKEN_ID, required = false) @Size(max = 2) List<EntityIdRangeParameter> tokenIds) {

@RequestParam(name = TOKEN_ID, required = false) @Size(max = 2) EntityIdRangeParameter[] tokenIds) {
var field = owner ? NFT_ALLOWANCE.SPENDER : NFT_ALLOWANCE.OWNER;
var request = NftAllowanceRequest.builder()
.accountId(id)
.isOwner(owner)
.limit(limit)
.order(order)
.ownerOrSpenderIds(new Bound(accountIds, true, ACCOUNT_ID))
.tokenIds(new Bound(tokenIds, false, TOKEN_ID))
.ownerOrSpenderIds(new Bound(accountIds, true, ACCOUNT_ID, field))
.tokenIds(new Bound(tokenIds, false, TOKEN_ID, NFT_ALLOWANCE.TOKEN_ID))
.build();

var serviceResponse = service.getNftAllowances(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
import static com.hedera.mirror.restjava.common.Constants.MAX_LIMIT;
import static com.hedera.mirror.restjava.common.Constants.RECEIVER_ID;
import static com.hedera.mirror.restjava.common.Constants.SENDER_ID;
import static com.hedera.mirror.restjava.common.Constants.SERIAL_NUMBER;
import static com.hedera.mirror.restjava.common.Constants.TOKEN_ID;
import static com.hedera.mirror.restjava.dto.TokenAirdropRequest.AirdropRequestType.OUTSTANDING;
import static com.hedera.mirror.restjava.dto.TokenAirdropRequest.AirdropRequestType.PENDING;
import static com.hedera.mirror.restjava.jooq.domain.Tables.TOKEN_AIRDROP;

import com.google.common.collect.ImmutableSortedMap;
import com.hedera.mirror.rest.model.TokenAirdrop;
import com.hedera.mirror.rest.model.TokenAirdropsResponse;
import com.hedera.mirror.restjava.common.EntityIdParameter;
import com.hedera.mirror.restjava.common.EntityIdRangeParameter;
import com.hedera.mirror.restjava.common.LinkFactory;
import com.hedera.mirror.restjava.common.NumberRangeParameter;
import com.hedera.mirror.restjava.dto.TokenAirdropRequest;
import com.hedera.mirror.restjava.dto.TokenAirdropRequest.AirdropRequestType;
import com.hedera.mirror.restjava.mapper.TokenAirdropMapper;
Expand All @@ -39,7 +42,6 @@
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import lombok.CustomLog;
Expand Down Expand Up @@ -71,43 +73,46 @@ TokenAirdropsResponse getOutstandingAirdrops(
@PathVariable EntityIdParameter id,
@RequestParam(defaultValue = DEFAULT_LIMIT) @Positive @Max(MAX_LIMIT) int limit,
@RequestParam(defaultValue = "asc") Sort.Direction order,
@RequestParam(name = RECEIVER_ID, required = false) @Size(max = 2) List<EntityIdRangeParameter> receiverIds,
@RequestParam(name = TOKEN_ID, required = false) @Size(max = 2) List<EntityIdRangeParameter> tokenIds) {
var entityIdsBound = new Bound(receiverIds, true, ACCOUNT_ID);
return processRequest(id, entityIdsBound, limit, order, tokenIds, OUTSTANDING, RECEIVER_ID);
@RequestParam(name = RECEIVER_ID, required = false) @Size(max = 2) EntityIdRangeParameter[] receiverIds,
@RequestParam(name = SERIAL_NUMBER, required = false) @Size(max = 2) NumberRangeParameter[] serialNumbers,
@RequestParam(name = TOKEN_ID, required = false) @Size(max = 2) EntityIdRangeParameter[] tokenIds) {
return processRequest(id, receiverIds, limit, order, serialNumbers, tokenIds, OUTSTANDING);
}

@GetMapping(value = "/pending")
TokenAirdropsResponse getPendingAirdrops(
@PathVariable EntityIdParameter id,
@RequestParam(defaultValue = DEFAULT_LIMIT) @Positive @Max(MAX_LIMIT) int limit,
@RequestParam(defaultValue = "asc") Sort.Direction order,
@RequestParam(name = SENDER_ID, required = false) @Size(max = 2) List<EntityIdRangeParameter> senderIds,
@RequestParam(name = TOKEN_ID, required = false) @Size(max = 2) List<EntityIdRangeParameter> tokenIds) {
var entityIdsBound = new Bound(senderIds, true, ACCOUNT_ID);
return processRequest(id, entityIdsBound, limit, order, tokenIds, PENDING, SENDER_ID);
@RequestParam(name = SENDER_ID, required = false) @Size(max = 2) EntityIdRangeParameter[] senderIds,
@RequestParam(name = SERIAL_NUMBER, required = false) @Size(max = 2) NumberRangeParameter[] serialNumbers,
@RequestParam(name = TOKEN_ID, required = false) @Size(max = 2) EntityIdRangeParameter[] tokenIds) {
return processRequest(id, senderIds, limit, order, serialNumbers, tokenIds, PENDING);
}

@SuppressWarnings("java:S107")
private TokenAirdropsResponse processRequest(
EntityIdParameter id,
Bound entityIdsBound,
EntityIdRangeParameter[] entityIds,
int limit,
Sort.Direction order,
List<EntityIdRangeParameter> tokenIds,
AirdropRequestType type,
String primarySortField) {
NumberRangeParameter[] serialNumbers,
EntityIdRangeParameter[] tokenIds,
AirdropRequestType type) {
var entityIdsBound = new Bound(entityIds, true, ACCOUNT_ID, type.getPrimaryField());
var request = TokenAirdropRequest.builder()
.accountId(id)
.entityIds(entityIdsBound)
.limit(limit)
.order(order)
.tokenIds(new Bound(tokenIds, false, TOKEN_ID))
.serialNumbers(new Bound(serialNumbers, false, SERIAL_NUMBER, TOKEN_AIRDROP.SERIAL_NUMBER))
.tokenIds(new Bound(tokenIds, false, TOKEN_ID, TOKEN_AIRDROP.TOKEN_ID))
.type(type)
.build();

var response = service.getAirdrops(request);
var airdrops = tokenAirdropMapper.map(response);
var sort = Sort.by(order, primarySortField, TOKEN_ID);
var sort = Sort.by(order, type.getParameter(), TOKEN_ID);
var pageable = PageRequest.of(0, limit, sort);
var links = linkFactory.create(airdrops, pageable, EXTRACTOR);
return new TokenAirdropsResponse().airdrops(airdrops).links(links);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.mirror.restjava.common.EntityIdParameter;
import com.hedera.mirror.restjava.service.Bound;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import org.springframework.data.domain.Sort;
Expand All @@ -37,7 +38,14 @@ public class NftAllowanceRequest {
@Builder.Default
private boolean isOwner = true;

private Bound ownerOrSpenderIds;
@Builder.Default
private Bound ownerOrSpenderIds = Bound.EMPTY;

@Builder.Default
private Bound tokenIds = Bound.EMPTY;

private Bound tokenIds;
public List<Bound> getBounds() {
var primaryBound = !ownerOrSpenderIds.isEmpty() ? ownerOrSpenderIds : tokenIds;
return tokenIds.isEmpty() ? List.of(primaryBound) : List.of(primaryBound, tokenIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.hedera.mirror.restjava.dto;

import static com.hedera.mirror.restjava.common.Constants.RECEIVER_ID;
import static com.hedera.mirror.restjava.common.Constants.SENDER_ID;
import static com.hedera.mirror.restjava.jooq.domain.Tables.TOKEN_AIRDROP;

import com.hedera.mirror.restjava.common.EntityIdParameter;
import com.hedera.mirror.restjava.service.Bound;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
Expand All @@ -41,18 +44,23 @@ public class TokenAirdropRequest {
private Sort.Direction order = Sort.Direction.ASC;

// Receiver Id for Outstanding Airdrops, Sender Id for Pending Airdrops
private Bound entityIds;
@Builder.Default
private Bound entityIds = Bound.EMPTY;

private Bound tokenIds;
@Builder.Default
private Bound serialNumbers = Bound.EMPTY;

@Builder.Default
private Bound tokenIds = Bound.EMPTY;

@Builder.Default
private AirdropRequestType type = AirdropRequestType.OUTSTANDING;

@Getter
@RequiredArgsConstructor
public enum AirdropRequestType {
OUTSTANDING(TOKEN_AIRDROP.SENDER_ACCOUNT_ID, TOKEN_AIRDROP.RECEIVER_ACCOUNT_ID),
PENDING(TOKEN_AIRDROP.RECEIVER_ACCOUNT_ID, TOKEN_AIRDROP.SENDER_ACCOUNT_ID);
OUTSTANDING(TOKEN_AIRDROP.SENDER_ACCOUNT_ID, TOKEN_AIRDROP.RECEIVER_ACCOUNT_ID, RECEIVER_ID),
PENDING(TOKEN_AIRDROP.RECEIVER_ACCOUNT_ID, TOKEN_AIRDROP.SENDER_ACCOUNT_ID, SENDER_ID);

// The base field is the conditional clause for the base DB query.
// The base field is the path parameter accountId, which is Sender Id for Outstanding Airdrops and Receiver Id
Expand All @@ -63,5 +71,22 @@ public enum AirdropRequestType {
// The primary field is the optional query parameter 'entityIds', which is Receiver Id for Outstanding Airdrops
// and Sender Id for Pending Airdrops
private final Field<Long> primaryField;

// The primary query parameter
private final String parameter;
}

public List<Bound> getBounds() {
var primaryBound = !entityIds.isEmpty() ? entityIds : tokenIds;
if (primaryBound.isEmpty()) {
return List.of(serialNumbers);
}

var secondaryBound = !tokenIds.isEmpty() ? tokenIds : serialNumbers;
if (secondaryBound.isEmpty()) {
return List.of(primaryBound);
}

return List.of(primaryBound, secondaryBound, serialNumbers);
}
}
Loading