/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * 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.fusesource.insight.log.elasticsearch;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

import static org.fusesource.insight.log.elasticsearch.ElasticSender.quote;

public class InsightEventHandler implements EventHandler {

    private String name;
    private String index;
    private String type;
    private ElasticSender sender;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public ElasticSender getSender() {
        return sender;
    }

    public void setSender(ElasticSender sender) {
        this.sender = sender;
    }

    public void init() {
        CreateIndexRequest request = new CreateIndexRequest(index);
        sender.createIndexIfNeeded(request);
    }

    public void handleEvent(final Event event) {
        try {
            StringBuilder writer = new StringBuilder();
            writer.append("{ host : ");
            quote(name, writer);
            writer.append(", topic: ");
            quote(event.getTopic(), writer);
            writer.append(", properties: { ");
            boolean first = true;
            for (String name : event.getPropertyNames()) {
                if (first) {
                    first = false;
                } else {
                    writer.append(", ");
                }
                quote(name, writer);
                writer.append(": ");
                quote(event.getProperty(name).toString(), writer);
            }
            writer.append(" } }");

            IndexRequest request = new IndexRequest()
                    .index(index)
                    .type(type)
                    .source(writer.toString())
                    .create(true);
            sender.put(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

@@ -669,16 +669,75 @@
 earch;%0A%0A
+import java.text.SimpleDateFormat;%0Aimport java.util.Date;%0A%0A
 import o
@@ -890,16 +890,62 @@
 .Event;%0A
+import org.osgi.service.event.EventConstants;%0A
 import o
@@ -1229,16 +1229,113 @@
 ender;%0A%0A
+    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ssZ%22);%0A%0A
     publ
@@ -2175,13 +2175,16 @@
 (%22%7B 
+%5C%22
 host
- 
+%5C%22
 : %22)
@@ -2251,13 +2251,17 @@
 (%22, 
+%5C%22
 topic
+%5C%22
 : %22)
@@ -2336,16 +2336,18 @@
 pend(%22, 
+%5C%22
 properti
@@ -2348,16 +2348,18 @@
 operties
+%5C%22
 : %7B %22);%0A
@@ -2669,24 +2669,193 @@
 pend(%22: %22);%0A
+                if (EventConstants.TIMESTAMP.equals(name)) %7B%0A                    quote(formatDate((Long) event.getProperty(name)), writer);%0A                %7D else %7B%0A    
             
@@ -2901,32 +2901,50 @@
 ing(), writer);%0A
+                %7D%0A
             %7D%0A  
@@ -3294,10 +3294,126 @@
 %0A    %7D%0A%0A
+    private String formatDate(long timestamp) %7B%0A        return simpleDateFormat.format(new Date(timestamp));%0A    %7D%0A%0A
 %7D%0A

Fix event storage in elasticsearch