diff --git a/java/google/registry/flows/domain/DomainFlowUtils.java b/java/google/registry/flows/domain/DomainFlowUtils.java index bb166424a..3b18df8a2 100644 --- a/java/google/registry/flows/domain/DomainFlowUtils.java +++ b/java/google/registry/flows/domain/DomainFlowUtils.java @@ -17,6 +17,7 @@ package google.registry.flows.domain; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Predicates.equalTo; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableMap.toImmutableMap; import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Sets.difference; @@ -270,22 +271,25 @@ public class DomainFlowUtils { Key registrant, Set> nameservers) throws EppException { - for (DesignatedContact contact : nullToEmpty(contacts)) { - verifyNotInPendingDelete(contact.getContactKey()); - } - if (registrant != null) { - verifyNotInPendingDelete(registrant); - } - for (Key host : nullToEmpty(nameservers)) { - verifyNotInPendingDelete(host); - } + ImmutableList.Builder> keysToLoad = new ImmutableList.Builder<>(); + nullToEmpty(contacts).stream().map(DesignatedContact::getContactKey).forEach(keysToLoad::add); + Optional.ofNullable(registrant).ifPresent(keysToLoad::add); + keysToLoad.addAll(nullToEmpty(nameservers)); + // This cast is safe because, in Objectify, Key can also be + // treated as a Key. + @SuppressWarnings("unchecked") + ImmutableList> typedKeys = + keysToLoad.build().stream().map(key -> (Key) key).collect(toImmutableList()); + verifyNotInPendingDelete(ofy().load().keys(typedKeys).values()); } - private static void verifyNotInPendingDelete(Key resourceKey) + private static void verifyNotInPendingDelete(Iterable resources) throws EppException { - EppResource resource = ofy().load().key(resourceKey).now(); - if (resource.getStatusValues().contains(StatusValue.PENDING_DELETE)) { - throw new LinkedResourceInPendingDeleteProhibitsOperationException(resource.getForeignKey()); + for (EppResource resource : resources) { + if (resource.getStatusValues().contains(StatusValue.PENDING_DELETE)) { + throw new LinkedResourceInPendingDeleteProhibitsOperationException( + resource.getForeignKey()); + } } }