/*
 * Copyright (C) 2012 sol-dock-r authors.
 * 
 * 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 at.pagu.soldockr.core.convert;

import java.util.Map;

import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.core.convert.converter.Converter;

final class SolrjConverters {

  private SolrjConverters() {

  }

  public static class ObjectToSolrInputDocumentConverter implements Converter<Object, SolrInputDocument> {

    private final DocumentObjectBinder documentObejctBinder;

    public ObjectToSolrInputDocumentConverter(DocumentObjectBinder binder) {
      if (binder != null) {
        this.documentObejctBinder = binder;
      } else {
        this.documentObejctBinder = new DocumentObjectBinder();
      }
    }

    @Override
    public SolrInputDocument convert(Object source) {
      if (source == null) {
        return null;
      }

      return documentObejctBinder.toSolrInputDocument(source);
    }
  }

  public static class SolrInputDocumentToObjectConverter<T> implements Converter<Map<String, ?>, T> {

    private static final DocumentObjectBinder documentObejctBinder = new DocumentObjectBinder();
    private Class<T> clazz;

    public SolrInputDocumentToObjectConverter(Class<T> clazz) {
      this.clazz = clazz;
    }

    @Override
    public T convert(Map<String, ?> source) {
      if (source == null) {
        return null;
      }
      SolrDocument document = new SolrDocument();
      document.putAll(source);

      return documentObejctBinder.getBean(clazz, document);
    }

  }

}

@@ -972,38 +972,40 @@
 ) %7B%0D%0A%0D%0A  %7D%0D%0A%0D%0A  
-public
+abstract
  static class Ob
@@ -1006,107 +1006,38 @@
 ass 
-ObjectToSolrInputDocumentConverter implements Converter%3CObject, SolrInputDocument%3E %7B%0D%0A%0D%0A    private
+DocumentBinderConverter %7B%0D%0A   
  fin
@@ -1100,33 +1100,16 @@
 lic 
-ObjectToSolrInput
 Document
 Conv
@@ -1104,16 +1104,22 @@
 Document
+Binder
 Converte
@@ -1317,32 +1317,290 @@
     %7D%0D%0A    %7D%0D%0A%0D%0A
+  %7D%0D%0A%0D%0A  public static class ObjectToSolrInputDocumentConverter extends DocumentBinderConverter implements Converter%3CObject, SolrInputDocument%3E %7B%0D%0A%0D%0A    public ObjectToSolrInputDocumentConverter(DocumentObjectBinder binder) %7B%0D%0A      super(binder);%0D%0A    %7D%0D%0A%0D%0A
     @Override%0D%0A 
@@ -1852,16 +1852,48 @@
 erter%3CT%3E
+ extends DocumentBinderConverter
  impleme
@@ -1946,77 +1946,55 @@
 ate 
-static final DocumentObjectBinder documentObejctBinder = new 
+Class%3CT%3E clazz;%0D%0A%0D%0A    public SolrInput
 Document
 Obje
@@ -1993,52 +1993,76 @@
 ment
+To
 Object
-Binder();%0D%0A    private Class%3CT%3E clazz;
+Converter(Class%3CT%3E clazz) %7B%0D%0A      this(clazz, null);%0D%0A    %7D
 %0D%0A%0D%0A
@@ -2113,35 +2113,86 @@
 r(Class%3CT%3E clazz
-) %7B
+, DocumentObjectBinder binder) %7B%0D%0A      super(binder);
 %0D%0A      this.cla

use provided document binder within converters