/**
 * Copyright 2015 Confluent Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package io.confluent.kafkarest.entities;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.NotNull;

public class ConsumerInstanceConfig {

  private static final EmbeddedFormat DEFAULT_FORMAT = EmbeddedFormat.BINARY;

  private String id;
  @NotNull
  private EmbeddedFormat format;
  private String autoOffsetReset;
  private String autoCommitEnable;

  public ConsumerInstanceConfig() {
    this(DEFAULT_FORMAT);
  }

  public ConsumerInstanceConfig(EmbeddedFormat format) {
    this(null, format, null, null);
  }

  public ConsumerInstanceConfig(@JsonProperty("id") String id,
                                @JsonProperty("format") EmbeddedFormat format,
                                @JsonProperty("auto.offset.reset") String autoOffsetReset,
                                @JsonProperty("auto.commit.enable") String autoCommitEnable) {
    this.id = id;
    this.format = format != null ? format : DEFAULT_FORMAT;
    this.autoOffsetReset = autoOffsetReset;
    this.autoCommitEnable = autoCommitEnable;
  }

  @JsonProperty
  public String getId() {
    return id;
  }

  @JsonProperty
  public void setId(String id) {
    this.id = id;
  }

  @JsonProperty
  public EmbeddedFormat getFormat() {
    return format;
  }

  @JsonProperty
  public void setFormat(EmbeddedFormat format) {
    this.format = format;
  }

  @JsonProperty("auto.offset.reset")
  public String getAutoOffsetReset() {
    return autoOffsetReset;
  }

  @JsonProperty("auto.offset.reset")
  public void setAutoOffsetReset(String autoOffsetReset) {
    this.autoOffsetReset = autoOffsetReset;
  }

  @JsonProperty("auto.commit.enable")
  public String getAutoCommitEnable() {
    return autoCommitEnable;
  }

  @JsonProperty("auto.commit.enable")
  public void setAutoCommitEnable(String autoCommitEnable) {
    this.autoCommitEnable = autoCommitEnable;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    ConsumerInstanceConfig that = (ConsumerInstanceConfig) o;

    if (autoCommitEnable != null ? !autoCommitEnable.equals(that.autoCommitEnable)
                                 : that.autoCommitEnable != null) {
      return false;
    }
    if (autoOffsetReset != null ? !autoOffsetReset.equals(that.autoOffsetReset)
                                : that.autoOffsetReset != null) {
      return false;
    }
    if (format != that.format) {
      return false;
    }
    if (id != null ? !id.equals(that.id) : that.id != null) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = id != null ? id.hashCode() : 0;
    result = 31 * result + (format != null ? format.hashCode() : 0);
    result = 31 * result + (autoOffsetReset != null ? autoOffsetReset.hashCode() : 0);
    result = 31 * result + (autoCommitEnable != null ? autoCommitEnable.hashCode() : 0);
    return result;
  }
}

@@ -631,16 +631,68 @@
 ities;%0A%0A
+import com.fasterxml.jackson.annotation.JsonIgnore;%0A
 import c
@@ -784,16 +784,87 @@
 tNull;%0A%0A
+import io.confluent.rest.exceptions.RestConstraintViolationException;%0A%0A
 public c
@@ -1224,32 +1224,109 @@
 ormat format) %7B%0A
+    // This constructor is only for tests so reparsing the format name is ok%0A
     this(null, f
@@ -1330,16 +1330,23 @@
 , format
+.name()
 , null, 
@@ -1476,30 +1476,22 @@
 ormat%22) 
-EmbeddedFormat
+String
  format,
@@ -1703,63 +1703,499 @@
     
-this.format = format != null ? format : DEFAULT_FORMAT;
+if (format == null) %7B%0A      this.format = DEFAULT_FORMAT;%0A    %7D else %7B%0A      String formatCanonical = format.toUpperCase();%0A      for (EmbeddedFormat f : EmbeddedFormat.values()) %7B%0A        if (f.name().equals(formatCanonical)) %7B%0A          this.format = f;%0A          break;%0A        %7D%0A      %7D%0A      if (this.format == null) %7B%0A        throw new RestConstraintViolationException(%0A            %22Invalid format type.%22,%0A            RestConstraintViolationException.DEFAULT_ERROR_CODE);%0A      %7D%0A    %7D
 %0A   
@@ -2423,32 +2423,30 @@
   %7D%0A%0A  @Json
-Property
+Ignore
 %0A  public Em
@@ -2488,32 +2488,137 @@
 rn format;%0A  %7D%0A%0A
+  @JsonProperty(%22format%22)%0A  public String getFormatJson() %7B%0A    return format.name().toLowerCase();%0A  %7D%0A%0A
   @JsonProperty%0A

Manage parsing of EmbeddedFormat name in ConsumerInstanceConfig manually so we can throw a RestConstraintViolationException to get a 422 when it is invalid instead of the 500 generated by Jackson's parsing error.