package org.jongo.spike.dbref.jackson;

import com.mongodb.DBRef;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ReferenceSerializer extends JsonSerializer<Object> {

    private Map<Class<?>, ReferenceLink<?>> referenceLinks = new HashMap<Class<?>, ReferenceLink<?>>();

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {

        if (!referenceLinks.containsKey(value.getClass())) {
            throw new IllegalArgumentException("Unable to serialize " + value.getClass() + ", no translators has been defined.");
        }

        ReferenceLink link = referenceLinks.get(value.getClass());
        String json = new DBRef(null, link.getReferenceCollectionName(value), getId(value, link)).toString();
        jgen.writeRawValue(json);
    }

    private String getId(Object value, ReferenceLink link) {

        String id = link.getId(value);
        if (id == null) {
            throw new NullPointerException("Cannot create DRRef because its id is null into " + value);
        }
        return id;
    }

    public void registerReferenceLink(Class<?> typeClass, ReferenceLink<?> link) {
        referenceLinks.put(typeClass, link);
    }

}

@@ -1086,16 +1086,20 @@
  private
+ %3CT%3E
  String 
@@ -1104,22 +1104,17 @@
 g getId(
-Object
+T
  value, 
@@ -1122,24 +1122,27 @@
 eferenceLink
+%3CT%3E
  link) %7B%0A%0A  
@@ -1350,24 +1350,28 @@
 %0A%0A    public
+ %3CT%3E
  void regist
@@ -1392,17 +1392,17 @@
 k(Class%3C
-?
+T
 %3E typeCl
@@ -1420,17 +1420,17 @@
 nceLink%3C
-?
+T
 %3E link) 

Add 'T' Type on ReferenceSerializer. refs #29