package com.scaleset.search.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.scaleset.geo.geojson.GeoJsonModule;
import com.scaleset.search.Query;

public abstract class AbstractSearchMapping<T, K> implements SearchMapping<T, K> {

    private ObjectMapper objectMapper = new ObjectMapper().registerModule(new GeoJsonModule());
    private Class<? extends T> type;
    private String defaultIndex;
    private String defaultType;

    public AbstractSearchMapping(Class<? extends T> type, String defaultIndex, String defaultType) {
        this.type = type;
        this.defaultIndex = defaultIndex;
        this.defaultType = defaultType;
    }

    public T fromDocument(String id, String doc) throws Exception {
        T obj = objectMapper.readValue(doc, type);
        return obj;
    }

    @Override
    public abstract String id(T obj) throws Exception;

    @Override
    public abstract String idForKey(K key) throws Exception;

    @Override
    public String index(T object) throws Exception {
        return defaultIndex;
    }

    @Override
    public String indexForKey(K key) throws Exception {
        return defaultIndex;
    }

    @Override
    public String indexForQuery(Query query) throws Exception {
        return defaultIndex;
    }

    @Override
    public String toDocument(T obj) throws Exception {
        String result = objectMapper.writeValueAsString(obj);
        return result;
    }

    @Override
    public String type(T object) throws Exception {
        return defaultType;
    }

    @Override
    public String typeForKey(K key) throws Exception {
        return defaultType;
    }

    @Override
    public String typeForQuery(Query query) throws Exception {
        return defaultType;
    }
}

@@ -26,16 +26,118 @@
 ch.es;%0A%0A
+import com.fasterxml.jackson.core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.JavaType;%0A
 import c
@@ -180,16 +180,16 @@
 Mapper;%0A
-
 import c
@@ -454,36 +454,30 @@
 private 
-Class%3C? extends T%3E t
+JavaType javaT
 ype;%0A   
@@ -657,19 +657,346 @@
 his.
-type = type
+javaType = objectMapper.getTypeFactory().constructType(type);%0A        this.defaultIndex = defaultIndex;%0A        this.defaultType = defaultType;%0A    %7D%0A%0A    public AbstractSearchMapping(TypeReference typeReference, String defaultIndex, String defaultType) %7B%0A        this.javaType = objectMapper.getTypeFactory().constructType(typeReference)
 ;%0A  
@@ -1194,17 +1194,21 @@
 ue(doc, 
-t
+javaT
 ype);%0A  

add support for type reference