package org.gbif.common.messaging.api.messages;

import org.gbif.api.model.common.DOI;
import org.gbif.api.model.common.DoiStatus;
import org.gbif.common.messaging.api.Message;

import java.net.URI;
import java.util.Objects;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;

/**
 * A message to request an update to a DOIs metadata and target URL in DataCite.
 * The DOI can be in any current state (registered, reserved, deleted) or even yet unknown to DataCite.
 */
public class ChangeDoiMessage implements Message {
  public static final String ROUTING_KEY = "doi.change";

  private final DOI doi;
  private final DoiStatus status;
  private final String metadata;
  private final URI target;

  @JsonCreator
  public ChangeDoiMessage(@JsonProperty("status") DoiStatus status, @JsonProperty("doi") DOI doi,
    @JsonProperty("metadata") String metadata, @JsonProperty("target") URI target) {
    this.status = status;
    this.doi = doi;
    this.metadata = metadata;
    this.target = target;
  }

  public DOI getDoi() {
    return doi;
  }

  /**
   * @return the desired status this doi should be updated to
   */
  public DoiStatus getStatus() {
    return status;
  }

  /**
   * @return the metadata as datacite xml
   */
  public String getMetadata() {
    return metadata;
  }

  public URI getTarget() {
    return target;
  }

  @Override
  public String getRoutingKey() {
    return ROUTING_KEY;
  }

  @Override
  public int hashCode() {
    return Objects.hash(doi, status, metadata, target);
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
      return false;
    }
    final ChangeDoiMessage other = (ChangeDoiMessage) obj;
    return Objects.equals(this.doi, other.doi) && Objects.equals(this.status, other.status) && Objects
      .equals(this.metadata, other.metadata) && Objects.equals(this.target, other.target);
  }
}

@@ -321,16 +321,82 @@
 perty;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0A
 /**%0A * A
@@ -1030,65 +1030,343 @@
 s = 
-status;%0A    this.doi = doi;%0A    this.metadata = metadata;
+checkNotNull(status, %22status can't be null%22);%0A    this.doi = checkNotNull(doi, %22doi can't be null%22);%0A    if (status != DoiStatus.DELETED) %7B%0A      checkNotNull(metadata, %22metadata can't be null%22);%0A    %7D%0A    this.metadata = metadata;%0A    if (status == DoiStatus.REGISTERED) %7B%0A      checkNotNull(target, %22target URI can't be null%22);%0A    %7D
 %0A   

Validate ChangeDoiMessage in constructor