/**
 * Copyright 2011-2012 Intellectual Reserve, 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 org.gedcomx.common;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.gedcomx.rt.json.JsonElementWrapper;
import org.gedcomx.rt.json.JsonSimpleValue;

import javax.xml.XMLConstants;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import java.io.IOException;

/**
 * A generic reference to a resource.
 *
 * @author Ryan Heaton
 */
@XmlRootElement
@XmlType ( name = "ResourceReference" )
@JsonElementWrapper ( name = "resourceReference" )
@JsonSerialize (using = ResourceReference.JsonSerializer.class)
@JsonDeserialize (using = ResourceReference.JsonDeserializer.class)
@JsonSimpleValue ( example = "http://path/to/resource" )
public final class ResourceReference {

  private URI resource;

  public ResourceReference() {
  }

  public ResourceReference(URI resource) {
    this.resource = resource;
  }

  /**
   * The URI to the resource. For more information, see <a href="http://www.w3.org/TR/webarch/#identification">Architecture of the World
   * Wide Web, Volume One, Section 2</a>
   *
   * @link http://www.w3.org/TR/webarch/#identification
   * @return The URI to the resource.
   */
  @XmlAttribute
  @XmlSchemaType (name = "anyURI", namespace = XMLConstants.W3C_XML_SCHEMA_NS_URI)
  public URI getResource() {
    return resource;
  }

  /**
   * The URI to the resource. For more information, see <a href="http://www.w3.org/TR/webarch/#identification">Architecture of the World
   * Wide Web, Volume One, Section 2</a>
   *
   * @link http://www.w3.org/TR/webarch/#identification
   * @param resource The URI to the resource.
   */
  public void setResource(URI resource) {
    this.resource = resource;
  }

  /**
   * Provide a simple toString() method.
   */
  @Override
  public String toString() {
    return (resource == null) ? "" : resource.toString();
  }

  public static class JsonSerializer extends org.codehaus.jackson.map.JsonSerializer<ResourceReference> {
    @Override
    public void serialize(ResourceReference value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
      if (value == null || value.getResource() == null) {
        jgen.writeNull();
      }
      else {
        jgen.writeString(value.getResource().toString());
      }
    }
  }

  public static class JsonDeserializer extends org.codehaus.jackson.map.JsonDeserializer<ResourceReference> {

    @Override
    public ResourceReference deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
      String text = jp.getText();
      if (text == null) {
        return null;
      }
      else {
        return new ResourceReference(URI.create(text));
      }
    }
  }
}

@@ -651,444 +651,42 @@
 org.
-codehaus.jackson.JsonGenerator;%0Aimport org.codehaus.jackson.JsonParser;%0Aimport org.codehaus.jackson.JsonProcessingException;%0Aimport org.codehaus.jackson.map.DeserializationContext;%0Aimport org.codehaus.jackson.map.SerializerProvider;%0Aimport org.codehaus.jackson.map.annotate.JsonDeserialize;%0Aimport org.codehaus.jackson.map.annotate.JsonSerialize;%0Aimport org.gedcomx.rt.json.JsonElementWrapper;%0Aimport org.gedcomx.rt.json.JsonSimpleValue
+gedcomx.rt.json.JsonElementWrapper
 ;%0A%0Ai
@@ -904,36 +904,8 @@
 ype;
-%0Aimport java.io.IOException;
 %0A%0A/*
@@ -1085,197 +1085,8 @@
 %22 )%0A
-@JsonSerialize (using = ResourceReference.JsonSerializer.class)%0A@JsonDeserialize (using = ResourceReference.JsonDeserializer.class)%0A@JsonSimpleValue ( example = %22http://path/to/resource%22 )%0A
 publ
@@ -1144,16 +1144,37 @@
 esource;
+%0A  private String id;
 %0A%0A  publ
@@ -1273,32 +1273,259 @@
  resource;%0A  %7D%0A%0A
+  public ResourceReference(URI resource, String id) %7B%0A    this.resource = resource;%0A    this.id = id;%0A  %7D%0A%0A  @XmlAttribute%0A  public String getId() %7B%0A    return id;%0A  %7D%0A%0A  public void setId( String id ) %7B%0A    this.id = id;%0A  %7D%0A%0A
   /**%0A   * The U
@@ -2415,24 +2415,24 @@
 oString() %7B%0A
+
     return (
@@ -2484,863 +2484,7 @@
 %0A  %7D
-%0A%0A  public static class JsonSerializer extends org.codehaus.jackson.map.JsonSerializer%3CResourceReference%3E %7B%0A    @Override%0A    public void serialize(ResourceReference value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException %7B%0A      if (value == null %7C%7C value.getResource() == null) %7B%0A        jgen.writeNull();%0A      %7D%0A      else %7B%0A        jgen.writeString(value.getResource().toString());%0A      %7D%0A    %7D%0A  %7D%0A%0A  public static class JsonDeserializer extends org.codehaus.jackson.map.JsonDeserializer%3CResourceReference%3E %7B%0A%0A    @Override%0A    public ResourceReference deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException %7B%0A      String text = jp.getText();%0A      if (text == null) %7B%0A        return null;%0A      %7D%0A      else %7B%0A        return new ResourceReference(URI.create(text));%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

add id to ResourceReference