Refactor some RDE import logic to be generic

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=136076610
This commit is contained in:
Wolfgang Meyers 2016-10-13 13:10:24 -07:00 committed by Ben McIlwain
parent 71d7a382f3
commit 94af94ddff
4 changed files with 51 additions and 50 deletions

View file

@ -22,6 +22,7 @@ import com.googlecode.objectify.Key;
import com.googlecode.objectify.Work;
import google.registry.config.ConfigModule.Config;
import google.registry.gcs.GcsUtils;
import google.registry.model.EppResource;
import google.registry.model.contact.ContactResource;
import google.registry.model.index.EppResourceIndex;
import google.registry.model.index.ForeignKeyIndex;
@ -60,6 +61,44 @@ public class RdeImportUtils {
this.escrowBucketName = escrowBucketName;
}
private <T extends EppResource> boolean importEppResource(final T resource, final String type) {
@SuppressWarnings("unchecked")
final Class<T> resourceClass = (Class<T>) resource.getClass();
return ofy.transact(
new Work<Boolean>() {
@Override
public Boolean run() {
EppResource existing = ofy.load().key(Key.create(resource)).now();
if (existing == null) {
ForeignKeyIndex<T> existingForeignKeyIndex =
ForeignKeyIndex.load(
resourceClass, resource.getForeignKey(), clock.nowUtc());
// foreign key index should not exist, since existing resource was not found.
checkState(
existingForeignKeyIndex == null,
"New %s resource has existing foreign key index; foreignKey=%s, repoId=%s",
type,
resource.getForeignKey(),
resource.getRepoId());
ofy.save().entity(resource);
ofy.save().entity(ForeignKeyIndex.create(resource, resource.getDeletionTime()));
ofy.save().entity(EppResourceIndex.create(Key.create(resource)));
logger.infofmt(
"Imported %s resource - ROID=%s, id=%s",
type, resource.getRepoId(), resource.getForeignKey());
return true;
} else if (!existing.getRepoId().equals(resource.getRepoId())) {
logger.warningfmt(
"Existing %s with same id but different ROID. "
+ "id=%s, existing ROID=%s, new ROID=%s",
type, resource.getForeignKey(), existing.getRepoId(), resource.getRepoId());
}
return false;
}
});
}
/**
/**
* Imports a contact from an escrow file.
*
@ -71,38 +110,7 @@ public class RdeImportUtils {
* @return true if the contact was created or updated, false otherwise.
*/
public boolean importContact(final ContactResource resource) {
return ofy.transact(
new Work<Boolean>() {
@Override
public Boolean run() {
ContactResource existing = ofy.load().key(Key.create(resource)).now();
if (existing == null) {
ForeignKeyIndex<ContactResource> existingForeignKeyIndex =
ForeignKeyIndex.load(
ContactResource.class, resource.getContactId(), clock.nowUtc());
// foreign key index should not exist, since existing contact was not found.
checkState(
existingForeignKeyIndex == null,
String.format(
"New contact resource has existing foreign key index. "
+ "contactId=%s, repoId=%s",
resource.getContactId(), resource.getRepoId()));
ofy.save().entity(resource);
ofy.save().entity(ForeignKeyIndex.create(resource, resource.getDeletionTime()));
ofy.save().entity(EppResourceIndex.create(Key.create(resource)));
logger.infofmt(
"Imported contact resource - ROID=%s, id=%s",
resource.getRepoId(), resource.getContactId());
return true;
} else if (!existing.getRepoId().equals(resource.getRepoId())) {
logger.warningfmt(
"Existing contact with same contact id but different ROID. "
+ "contactId=%s, existing ROID=%s, new ROID=%s",
resource.getContactId(), existing.getRepoId(), resource.getRepoId());
}
return false;
}
});
return importEppResource(resource, "contact");
}
/**