/*
 * 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() {

  }

  abstract static class DocumentBinderConverter {
    final DocumentObjectBinder documentObejctBinder;

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

  }

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

    public ObjectToSolrInputDocumentConverter(DocumentObjectBinder binder) {
      super(binder);
    }

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

      return documentObejctBinder.toSolrInputDocument(source);
    }
  }

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

    private Class<T> clazz;

    public SolrInputDocumentToObjectConverter(Class<T> clazz) {
      this(clazz, null);
    }

    public SolrInputDocumentToObjectConverter(Class<T> clazz, DocumentObjectBinder binder) {
      super(binder);
      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);
    }

  }

}

@@ -1033,16 +1033,26 @@
  %7B%0D%0A    
+protected 
 final Do
@@ -1093,16 +1093,16 @@
 inder;%0D%0A
-
 %0D%0A    pu
@@ -1166,39 +1166,8 @@
  %7B%0D%0A
-      if (binder != null) %7B%0D%0A  
     
@@ -1206,62 +1206,27 @@
 nder
-;%0D%0A      %7D else %7B%0D%0A        this.documentObejctB
+ != null ? b
 inder 
-=
+:
  new
@@ -1251,25 +1251,16 @@
 der();%0D%0A
-      %7D%0D%0A
     %7D%0D%0A%0D

simplify conditional