Remove a couple additional ofy() calls (#1171)

* Remove a couple additional ofy() calls
This commit is contained in:
gbrodman 2021-05-24 13:12:40 -04:00 committed by GitHub
parent 2f2e9dd49f
commit 20a0e4ce3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 26 deletions

View file

@ -16,7 +16,7 @@ package google.registry.model;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.ofy.ObjectifyService.auditedOfy;
import google.registry.model.ofy.ObjectifyService;
import google.registry.util.TypeUtils.TypeInstantiator;
@ -57,8 +57,14 @@ public interface Buildable {
// If this object has a Long or long Objectify @Id field that is not set, set it now.
Field idField = null;
try {
idField = ModelUtils.getAllFields(instance.getClass()).get(
ofy().factory().getMetadata(instance.getClass()).getKeyMetadata().getIdFieldName());
idField =
ModelUtils.getAllFields(instance.getClass())
.get(
auditedOfy()
.factory()
.getMetadata(instance.getClass())
.getKeyMetadata()
.getIdFieldName());
} catch (Exception e) {
// Expected if the class is not registered with Objectify.
}

View file

@ -17,7 +17,6 @@ package google.registry.model.common;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.base.Splitter;
@ -210,12 +209,7 @@ public class Cursor extends ImmutableObject implements DatastoreAndSqlEntity {
private static void checkValidCursorTypeForScope(
CursorType cursorType, Key<? extends ImmutableObject> scope) {
checkArgument(
cursorType
.getScopeClass()
.equals(
scope.equals(getCrossTldKey())
? EntityGroupRoot.class
: ofy().factory().getMetadata(scope).getEntityClass()),
cursorType.getScopeClass().getSimpleName().equals(scope.getKind()),
"Class required for cursor does not match scope class");
}

View file

@ -14,10 +14,10 @@
package google.registry.model.poll;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import google.registry.model.EppResource;
import google.registry.model.contact.ContactResource;
@ -26,6 +26,7 @@ import google.registry.model.host.HostResource;
import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey;
import java.util.List;
import java.util.Map;
/**
* A converter between external key strings for {@link PollMessage}s (i.e. what registrars use to
@ -49,24 +50,23 @@ public class PollMessageExternalKeyConverter {
/** An exception thrown when an external key cannot be parsed. */
public static class PollMessageExternalKeyParseException extends RuntimeException {}
/**
* A map of IDs used in external keys corresponding to which EppResource class the poll message
* belongs to.
*/
public static final ImmutableBiMap<Class<? extends EppResource>, Long> EXTERNAL_KEY_CLASS_ID_MAP =
ImmutableBiMap.of(
DomainBase.class, 1L,
ContactResource.class, 2L,
HostResource.class, 3L);
/** Maps that detail the correspondence between EppResource classes and external IDs. */
private static final ImmutableMap<Long, Class<? extends EppResource>> ID_TO_CLASS_MAP =
ImmutableMap.of(
1L, DomainBase.class,
2L, ContactResource.class,
3L, HostResource.class);
private static final ImmutableMap<String, Long> KEY_KIND_TO_ID_MAP =
ID_TO_CLASS_MAP.entrySet().stream()
.collect(toImmutableMap(entry -> entry.getValue().getSimpleName(), Map.Entry::getKey));
/** Returns an external poll message ID for the given poll message. */
public static String makePollMessageExternalId(PollMessage pollMessage) {
@SuppressWarnings("unchecked")
Key<EppResource> ancestorResource =
(Key<EppResource>) (Key<?>) pollMessage.getParentKey().getParent();
long externalKeyClassId =
EXTERNAL_KEY_CLASS_ID_MAP.get(
ofy().factory().getMetadata(ancestorResource.getKind()).getEntityClass());
long externalKeyClassId = KEY_KIND_TO_ID_MAP.get(ancestorResource.getKind());
return String.format(
"%d-%s-%d-%d-%d",
externalKeyClassId,
@ -92,8 +92,7 @@ public class PollMessageExternalKeyConverter {
throw new PollMessageExternalKeyParseException();
}
try {
Class<?> resourceClazz =
EXTERNAL_KEY_CLASS_ID_MAP.inverse().get(Long.parseLong(idComponents.get(0)));
Class<?> resourceClazz = ID_TO_CLASS_MAP.get(Long.parseLong(idComponents.get(0)));
if (resourceClazz == null) {
throw new PollMessageExternalKeyParseException();
}