Skip to content

Commit

Permalink
AdminRuleRepositoryJpaAdaptor fix on count() and readability improvem…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
groldan committed Dec 17, 2023
1 parent 5507c94 commit 833b24d
Showing 1 changed file with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private org.geoserver.acl.domain.rules.InsertPosition map(InsertPosition positio

@Override
public Optional<AdminRule> findById(@NonNull String id) {
return jparepo.findById(decodeId(id).longValue()).map(modelMapper::toModel);
return jparepo.findById(decodeId(id)).map(modelMapper::toModel);
}

@Override
Expand All @@ -142,8 +142,7 @@ public int count() {
@Override
public int count(AdminRuleFilter filter) {
Optional<? extends Predicate> predicate = queryMapper.toPredicate(filter);
if (predicate.isEmpty()) return (int) jparepo.count(predicate.get());
return (int) jparepo.count();
return predicate.map(jparepo::count).map(Long::intValue).orElseGet(this::count);
}

@Override
Expand Down Expand Up @@ -222,13 +221,11 @@ public Stream<AdminRule> findAll(@NonNull RuleQuery<AdminRuleFilter> query) {
private CloseableIterator<org.geoserver.acl.jpa.model.AdminRule> queryOrderByPriority(
Predicate predicate) {

CloseableIterator<org.geoserver.acl.jpa.model.AdminRule> iterator =
new JPAQuery<org.geoserver.acl.jpa.model.AdminRule>(em)
.from(QAdminRule.adminRule)
.where(predicate)
.orderBy(new OrderSpecifier<>(Order.ASC, QAdminRule.adminRule.priority))
.iterate();
return iterator;
return new JPAQuery<org.geoserver.acl.jpa.model.AdminRule>(em)
.from(QAdminRule.adminRule)
.where(predicate)
.orderBy(new OrderSpecifier<>(Order.ASC, QAdminRule.adminRule.priority))
.iterate();
}

private Stream<org.geoserver.acl.jpa.model.AdminRule> stream(
Expand All @@ -242,9 +239,7 @@ private java.util.function.Predicate<? super AdminRule> filterByAddress(
if (filter.isEmpty()) return r -> true;

IPAddressRangeFilter ipFilter = filter.get().getSourceAddress();
return rule -> {
return ipFilter.test(rule.getIdentifier().getAddressRange());
};
return rule -> ipFilter.test(rule.getIdentifier().getAddressRange());
}

@Override
Expand Down Expand Up @@ -284,7 +279,7 @@ public boolean deleteById(@NonNull String id) {
private org.geoserver.acl.jpa.model.AdminRule getOrThrowIAE(@NonNull String ruleId) {
org.geoserver.acl.jpa.model.AdminRule rule;
try {
rule = jparepo.getReferenceById(decodeId(ruleId).longValue());
rule = jparepo.getReferenceById(decodeId(ruleId));
} catch (EntityNotFoundException e) {
throw new NoSuchElementException("AdminRule " + ruleId + " does not exist");
}
Expand Down

0 comments on commit 833b24d

Please sign in to comment.