/*
 * Copyright 2013 the original author or 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 org.elasticsearch.hadoop.serialization;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Value writer for JDK types.
 */
public class JdkValueWriter implements ValueWriter<Object> {

    protected final boolean writeUnknownTypes;

    public JdkValueWriter() {
        writeUnknownTypes = false;
    }

    public JdkValueWriter(boolean writeUnknownTypes) {
        this.writeUnknownTypes = writeUnknownTypes;
    }

    @Override
    public boolean write(Object value, Generator generator) {
        if (value == null) {
            generator.writeNull();
        }
        else if (value instanceof String) {
            generator.writeString((String) value);
        }
        else if (value instanceof Integer) {
            generator.writeNumber(((Integer) value).intValue());
        }
        else if (value instanceof Long) {
            generator.writeNumber(((Long) value).longValue());
        }
        else if (value instanceof Float) {
            generator.writeNumber(((Float) value).floatValue());
        }
        else if (value instanceof Double) {
            generator.writeNumber(((Double) value).doubleValue());
        }
        else if (value instanceof Short) {
            generator.writeNumber(((Short) value).shortValue());
        }
        else if (value instanceof Byte) {
            generator.writeNumber(((Byte) value).byteValue());
        }
        // Big Decimal/Integer
        else if (value instanceof Number) {
            generator.writeString(value.toString());
        }
        else if (value instanceof Boolean) {
            generator.writeBoolean(((Boolean) value).booleanValue());
        }
        else if (value instanceof byte[]) {
            generator.writeBinary((byte[]) value);
        }
        else if (value instanceof Object[]) {
            generator.writeBeginArray();
            for (Object o : (Object[]) value) {
                write(o, generator);
            }
            generator.writeEndArray();
        }
        else if (value instanceof Map) {
            generator.writeBeginObject();
            for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                generator.writeFieldName(entry.getKey().toString());
                write(entry.getValue(), generator);
            }
            generator.writeEndObject();
        }
        else if (value instanceof Iterable) {
            generator.writeBeginArray();
            for (Object o : (Iterable<?>) value) {
                write(o, generator);
            }
            generator.writeEndArray();
        }
        else if (value instanceof Date) {
            throw new UnsupportedOperationException();
        }
        else if (value instanceof Calendar) {
            throw new UnsupportedOperationException();
        }
        else if (value instanceof Timestamp) {
            throw new UnsupportedOperationException();
        }
        else {
            if (writeUnknownTypes) {
                return handleUnknown(value, generator);
            }
            return false;
        }
        return true;
    }

    protected boolean handleUnknown(Object value, Generator generator) {
        generator.writeString(value.toString());
        return true;
    }
}

@@ -786,16 +786,58 @@
 Entry;%0A%0A
+import javax.xml.bind.DatatypeConverter;%0A%0A
 /**%0A * V
@@ -3363,271 +3363,543 @@
     
-throw new UnsupportedOperationException();%0A        %7D%0A        else if (value instanceof Calendar) %7B%0A            throw new UnsupportedOperationException();%0A        %7D%0A        else if (value instanceof Timestamp) %7B%0A            throw new UnsupportedOperationException(
+Calendar cal = Calendar.getInstance();%0A            cal.setTime((Date) value);%0A            generator.writeString(DatatypeConverter.printDateTime(cal));%0A        %7D%0A        else if (value instanceof Calendar) %7B%0A            generator.writeString(DatatypeConverter.printDateTime((Calendar) value));%0A        %7D%0A        else if (value instanceof Timestamp) %7B%0A            Calendar cal = Calendar.getInstance();%0A            cal.setTimeInMillis(((Timestamp) value).getTime());%0A            generator.writeString(DatatypeConverter.printDateTime(cal)
 );%0A 

add date handling in JdkValueWriter