/*
 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
 *
 * 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 org.gbif.registry.domain.ws;

import java.io.IOException;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(
    using = LegacyDatasetResponseListWrapper.LegacyDatasetResponseListWrapperJsonSerializer.class)
@XmlRootElement(name = "legacyDatasetResponses")
public class LegacyDatasetResponseListWrapper {

  private List<LegacyDatasetResponse> legacyDatasetResponses;

  public LegacyDatasetResponseListWrapper() {}

  public LegacyDatasetResponseListWrapper(List<LegacyDatasetResponse> legacyDatasetResponses) {
    this.legacyDatasetResponses = legacyDatasetResponses;
  }

  @XmlElement(name = "resource")
  public List<LegacyDatasetResponse> getLegacyDatasetResponses() {
    return legacyDatasetResponses;
  }

  public void setLegacyDatasetResponses(List<LegacyDatasetResponse> legacyDatasetResponses) {
    this.legacyDatasetResponses = legacyDatasetResponses;
  }

  public static class LegacyDatasetResponseListWrapperJsonSerializer
      extends JsonSerializer<LegacyDatasetResponseListWrapper> {

    @Override
    public void serialize(
        LegacyDatasetResponseListWrapper value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException {
      if (value == null) {
        jgen.writeNull();
        return;
      }

      jgen.writeStartArray();
      if (value.getLegacyDatasetResponses() != null) {
        for (LegacyDatasetResponse item : value.getLegacyDatasetResponses()) {
          jgen.writeObject(item);
        }
      }
      jgen.writeEndArray();
    }
  }
}

@@ -858,16 +858,333 @@
 erator;%0A
+import com.fasterxml.jackson.core.JsonParseException;%0Aimport com.fasterxml.jackson.core.JsonParser;%0Aimport com.fasterxml.jackson.core.JsonToken;%0Aimport com.fasterxml.jackson.core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.DeserializationContext;%0Aimport com.fasterxml.jackson.databind.JsonDeserializer;%0A
 import c
@@ -1283,24 +1283,90 @@
 erProvider;%0A
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;%0A
 import com.f
@@ -1533,16 +1533,135 @@
 .class)%0A
+@JsonDeserialize(%0A    using = LegacyDatasetResponseListWrapper.LegacyDatasetResponseListWrapperJsonDeserializer.class)%0A
 @XmlRoot
@@ -2950,11 +2950,648 @@
    %7D%0A  %7D
+%0A%0A  public static class LegacyDatasetResponseListWrapperJsonDeserializer%0A      extends JsonDeserializer%3CLegacyDatasetResponseListWrapper%3E %7B%0A%0A    @Override%0A    public LegacyDatasetResponseListWrapper deserialize(JsonParser p, DeserializationContext ctxt)%0A        throws IOException %7B%0A      if (p.getCurrentToken() != JsonToken.START_ARRAY) %7B%0A        throw new JsonParseException(p, %22Start array expected%22);%0A      %7D%0A%0A      List%3CLegacyDatasetResponse%3E legacyDatasetResponses =%0A          p.readValueAs(new TypeReference%3CList%3CLegacyDatasetResponse%3E%3E() %7B%7D);%0A%0A      return new LegacyDatasetResponseListWrapper(legacyDatasetResponses);%0A    %7D%0A  %7D
 %0A%7D%0A

Add LegacyDatasetResponseListWrapper json deserializer