1616
1717import static com .google .common .base .MoreObjects .firstNonNull ;
1818import static com .google .common .base .Preconditions .checkNotNull ;
19- import static com .google .common .collect .Iterables .getOnlyElement ;
2019import static com .google .common .collect .Sets .difference ;
2120import static google .registry .util .CollectionUtils .difference ;
22- import static google .registry .util .CollectionUtils .forceEmptyToNull ;
21+ import static google .registry .util .CollectionUtils .isNullOrEmpty ;
2322import static google .registry .util .CollectionUtils .nullSafeImmutableCopy ;
24- import static google .registry .util .CollectionUtils .nullToEmpty ;
2523import static google .registry .util .CollectionUtils .nullToEmptyImmutableCopy ;
26- import static google .registry .util .CollectionUtils .union ;
2724
2825import com .google .common .base .MoreObjects ;
29- import com .google .common .base .Strings ;
3026import com .google .common .collect .ImmutableMap ;
3127import com .google .common .collect .ImmutableSet ;
32- import google .registry .model .EppResource ;
28+ import google .registry .flows .EppException .ParameterValuePolicyErrorException ;
29+ import google .registry .flows .domain .DomainFlowUtils .RegistrantProhibitedException ;
30+ import google .registry .flows .exceptions .ContactsProhibitedException ;
3331import google .registry .model .ForeignKeyUtils ;
3432import google .registry .model .ImmutableObject ;
3533import google .registry .model .contact .Contact ;
@@ -67,7 +65,8 @@ public class DomainCommand {
6765 */
6866 public interface CreateOrUpdate <T extends CreateOrUpdate <T >> extends SingleResourceCommand {
6967 /** Creates a copy of this command with hard links to hosts and contacts. */
70- T cloneAndLinkReferences (DateTime now ) throws InvalidReferencesException ;
68+ T cloneAndLinkReferences (DateTime now )
69+ throws InvalidReferencesException , ParameterValuePolicyErrorException ;
7170 }
7271
7372 /** The fields on "chgType" from <a href="http://tools.ietf.org/html/rfc5731">RFC5731</a>. */
@@ -171,26 +170,15 @@ public DomainAuthInfo getAuthInfo() {
171170
172171 /** Creates a copy of this {@link Create} with hard links to hosts and contacts. */
173172 @ Override
174- public Create cloneAndLinkReferences (DateTime now ) throws InvalidReferencesException {
173+ public Create cloneAndLinkReferences (DateTime now )
174+ throws InvalidReferencesException , ParameterValuePolicyErrorException {
175175 Create clone = clone (this );
176176 clone .nameservers = linkHosts (clone .nameserverHostNames , now );
177- if (registrantContactId == null ) {
178- clone .contacts = linkContacts (clone .foreignKeyedDesignatedContacts , now );
179- } else {
180- // Load the registrant and contacts in one shot.
181- ForeignKeyedDesignatedContact registrantPlaceholder = new ForeignKeyedDesignatedContact ();
182- registrantPlaceholder .contactId = clone .registrantContactId ;
183- registrantPlaceholder .type = DesignatedContact .Type .REGISTRANT ;
184- Set <DesignatedContact > contacts = linkContacts (
185- union (nullToEmpty (clone .foreignKeyedDesignatedContacts ), registrantPlaceholder ),
186- now );
187- for (DesignatedContact contact : contacts ) {
188- if (DesignatedContact .Type .REGISTRANT .equals (contact .getType ())) {
189- clone .registrant = contact .getContactKey ();
190- clone .contacts = forceEmptyToNull (difference (contacts , contact ));
191- break ;
192- }
193- }
177+ if (registrantContactId != null ) {
178+ throw new RegistrantProhibitedException ();
179+ }
180+ if (!isNullOrEmpty (foreignKeyedDesignatedContacts )) {
181+ throw new ContactsProhibitedException ();
194182 }
195183 return clone ;
196184 }
@@ -369,27 +357,25 @@ public ImmutableSet<DesignatedContact> getContacts() {
369357 }
370358
371359 /** Creates a copy of this {@link AddRemove} with hard links to hosts and contacts. */
372- private AddRemove cloneAndLinkReferences (DateTime now ) throws InvalidReferencesException {
360+ private AddRemove cloneAndLinkReferences (DateTime now )
361+ throws InvalidReferencesException , ContactsProhibitedException {
373362 AddRemove clone = clone (this );
374363 clone .nameservers = linkHosts (clone .nameserverHostNames , now );
375- clone .contacts = linkContacts (clone .foreignKeyedDesignatedContacts , now );
364+ if (!isNullOrEmpty (foreignKeyedDesignatedContacts )) {
365+ throw new ContactsProhibitedException ();
366+ }
376367 return clone ;
377368 }
378369 }
379370
380371 /** The inner change type on a domain update command. */
381372 @ XmlType (propOrder = {"registrantContactId" , "authInfo" })
382373 public static class Change extends DomainCreateOrChange <Domain .Builder > {
383- /** Creates a copy of this {@link Change} with hard links to hosts and contacts. */
384- Change cloneAndLinkReferences (DateTime now ) throws InvalidReferencesException {
374+ Change cloneAndLinkReferences () throws RegistrantProhibitedException {
385375 Change clone = clone (this );
386- clone .registrant =
387- Strings .isNullOrEmpty (clone .registrantContactId )
388- ? null
389- : getOnlyElement (
390- loadByForeignKeysCached (
391- ImmutableSet .of (clone .registrantContactId ), Contact .class , now )
392- .values ());
376+ if (clone .registrantContactId != null ) {
377+ throw new RegistrantProhibitedException ();
378+ }
393379 return clone ;
394380 }
395381 }
@@ -401,11 +387,12 @@ Change cloneAndLinkReferences(DateTime now) throws InvalidReferencesException {
401387 * of those classes, which is harmless because the getters do that anyways.
402388 */
403389 @ Override
404- public Update cloneAndLinkReferences (DateTime now ) throws InvalidReferencesException {
390+ public Update cloneAndLinkReferences (DateTime now )
391+ throws InvalidReferencesException , ParameterValuePolicyErrorException {
405392 Update clone = clone (this );
406393 clone .innerAdd = clone .getInnerAdd ().cloneAndLinkReferences (now );
407394 clone .innerRemove = clone .getInnerRemove ().cloneAndLinkReferences (now );
408- clone .innerChange = clone .getInnerChange ().cloneAndLinkReferences (now );
395+ clone .innerChange = clone .getInnerChange ().cloneAndLinkReferences ();
409396 return clone ;
410397 }
411398 }
@@ -415,37 +402,17 @@ private static Set<VKey<Host>> linkHosts(Set<String> hostNames, DateTime now)
415402 if (hostNames == null ) {
416403 return null ;
417404 }
418- return ImmutableSet .copyOf (loadByForeignKeysCached (hostNames , Host . class , now ).values ());
405+ return ImmutableSet .copyOf (loadByForeignKeysCached (hostNames , now ).values ());
419406 }
420407
421- private static Set <DesignatedContact > linkContacts (
422- Set <ForeignKeyedDesignatedContact > contacts , DateTime now ) throws InvalidReferencesException {
423- if (contacts == null ) {
424- return null ;
425- }
426- ImmutableSet .Builder <String > foreignKeys = new ImmutableSet .Builder <>();
427- for (ForeignKeyedDesignatedContact contact : contacts ) {
428- foreignKeys .add (contact .contactId );
429- }
430- ImmutableMap <String , VKey <Contact >> loadedContacts =
431- loadByForeignKeysCached (foreignKeys .build (), Contact .class , now );
432- ImmutableSet .Builder <DesignatedContact > linkedContacts = new ImmutableSet .Builder <>();
433- for (ForeignKeyedDesignatedContact contact : contacts ) {
434- linkedContacts .add (
435- DesignatedContact .create (contact .type , loadedContacts .get (contact .contactId )));
436- }
437- return linkedContacts .build ();
438- }
439-
440- /** Loads keys to cached EPP resources by their foreign keys. */
441- private static <T extends EppResource > ImmutableMap <String , VKey <T >> loadByForeignKeysCached (
442- final Set <String > foreignKeys , final Class <T > clazz , final DateTime now )
443- throws InvalidReferencesException {
444- ImmutableMap <String , VKey <T >> fks =
445- ForeignKeyUtils .loadKeysByCacheIfEnabled (clazz , foreignKeys , now );
408+ /** Loads host keys to cached EPP resources by their foreign keys. */
409+ private static ImmutableMap <String , VKey <Host >> loadByForeignKeysCached (
410+ final Set <String > foreignKeys , final DateTime now ) throws InvalidReferencesException {
411+ ImmutableMap <String , VKey <Host >> fks =
412+ ForeignKeyUtils .loadKeysByCacheIfEnabled (Host .class , foreignKeys , now );
446413 if (!fks .keySet ().equals (foreignKeys )) {
447414 throw new InvalidReferencesException (
448- clazz , ImmutableSet .copyOf (difference (foreignKeys , fks .keySet ())));
415+ Host . class , ImmutableSet .copyOf (difference (foreignKeys , fks .keySet ())));
449416 }
450417 return fks ;
451418 }
0 commit comments