Replace loadByUniqueId() with methods that don't overload unique id

It is replaced by loadByForeignKey(), which does the same thing that
loadByUniqueId() did for contacts, hosts, and domains, and also
loadDomainApplication(), which loads domain application by ROID. This eliminates
the ugly mode-switching of attemping to load by other foreign key or ROID.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133980156
This commit is contained in:
mcilwain 2016-09-22 11:45:58 -07:00 committed by Ben McIlwain
parent 025a4ae012
commit 21a98b899c
57 changed files with 367 additions and 340 deletions

View file

@ -15,7 +15,8 @@
package google.registry.tools.javascrap;
import static com.google.common.collect.Maps.uniqueIndex;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static org.joda.time.DateTimeZone.UTC;
@ -32,7 +33,7 @@ import java.util.Arrays;
import java.util.Map;
import org.joda.time.DateTime;
/** Load and resave an object in the probers, to trigger @OnSave changes. */
/** A command to load and resave an entity, which triggers @OnSave changes. */
@Parameters(
separators = " =",
commandDescription = "Load and resave an object, to trigger @OnSave changes")
@ -45,9 +46,9 @@ public final class LoadAndResaveCommand extends MutatingCommand {
protected String type;
@Parameter(
names = "--key",
description = "Foreign key of the resource. ")
protected String foreignKey;
names = "--id",
description = "Foreign key of the resource, or application ID of the domain application.")
protected String uniqueId;
private static final Map<String, Class<? extends EppResource>> CLASSES_BY_NAME =
uniqueIndex(
@ -64,12 +65,15 @@ public final class LoadAndResaveCommand extends MutatingCommand {
@Override
protected void init() throws Exception {
Class<? extends EppResource> clazz = CLASSES_BY_NAME.get(type);
EppResource existing =
(clazz == DomainApplication.class)
? loadDomainApplication(uniqueId, DateTime.now(UTC))
: loadByForeignKey(clazz, uniqueId, DateTime.now(UTC));
// Find the resource by foreign key, and then reload it directly, bypassing loadByUniqueId().
// We need to do a reload because otherwise stageEntityChange() can fail due to the implicit
// changes done when forwarding the resource to "now" in cloneProjectedAtTime().
EppResource resource = ofy().load().entity(
loadByUniqueId(CLASSES_BY_NAME.get(type), foreignKey, DateTime.now(UTC))).now();
EppResource resource = ofy().load().entity(existing).now();
stageEntityChange(resource, resource);
}
}