package com.booking.replication.util;

import com.booking.replication.augmenter.AugmentedRow;
import com.booking.replication.augmenter.AugmentedSchemaChangeEvent;
import com.booking.replication.schema.ActiveSchemaVersion;
import com.booking.replication.schema.table.TableSchema;

import com.google.code.or.binlog.BinlogEventV4;
import com.google.code.or.binlog.impl.event.TableMapEvent;
import com.google.code.or.common.util.MySQLConstants;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;

/**
 * Json Builder.
 */

public class JsonBuilder {

    public String binlogEventV4ToJson(BinlogEventV4 event) {

        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            switch (event.getHeader().getEventType()) {

                // todo: make this nicer - currently it is just a quick fix since jackson cant parse tableMapEvent
                case MySQLConstants.TABLE_MAP_EVENT:
                    json =  mapper.writeValueAsString(((TableMapEvent) event).toString());
                    break;

                default:
                    json = mapper.writeValueAsString(event);
                    break;
            }
        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event type " + event.getHeader().getEventType());
        }
        return json;
    }

    public static String dataEventToJson(AugmentedRow augmentedRow) {

        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(augmentedRow);
        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event");
            e.printStackTrace();
        }
        return json;
    }

    public static String schemaVersionToJson(ActiveSchemaVersion activeSchemaVersion) {
        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            //json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(activeSchemaVersion);
            json = mapper.writeValueAsString(activeSchemaVersion);

        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event");
            e.printStackTrace();
        }
        return json;
    }

    public static String schemaChangeEventToJson(AugmentedSchemaChangeEvent augmentedSchemaChangeEvent) {
        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(augmentedSchemaChangeEvent);
        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event");
            e.printStackTrace();
        }
        return json;
    }

    public static String schemaTransitionSequenceToJson(HashMap<String,String> schemaTransitionSequence) {
        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(schemaTransitionSequence);

        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event");
            e.printStackTrace();
        }
        return json;
    }

    public static String schemaVersionTablesToJson(HashMap<String, TableSchema> schemaVersionTables) {
        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(schemaVersionTables);

        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event");
            e.printStackTrace();
        }
        return json;
    }

    public static String schemaCreateStatementsToJson(HashMap<String, String> schemaCreateStatements) {
        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(schemaCreateStatements);

        } catch (IOException e) {
            System.out.println("ERROR: could not serialize event");
            e.printStackTrace();
        }
        return json;
    }

}

@@ -593,24 +593,88 @@
 nBuilder %7B%0A%0A
+    private static final ObjectMapper om = new ObjectMapper();%0A%0A
     public S
@@ -769,62 +769,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -1018,22 +1018,18 @@
 json =  
+o
 m
-apper
 .writeVa
@@ -1149,38 +1149,34 @@
          json = 
+o
 m
-apper
 .writeValueAsStr
@@ -1526,62 +1526,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -1533,38 +1533,34 @@
          json = 
+o
 m
-apper
 .writeValueAsStr
@@ -1885,62 +1885,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -1994,38 +1994,34 @@
          json = 
+o
 m
-apper
 .writeValueAsStr
@@ -2372,62 +2372,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -2379,38 +2379,34 @@
          json = 
+o
 m
-apper
 .writeValueAsStr
@@ -2764,62 +2764,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -2771,38 +2771,34 @@
          json = 
+o
 m
-apper
 .writeValueAsStr
@@ -2828,17 +2828,16 @@
 uence);%0A
-%0A
         
@@ -3150,62 +3150,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -3157,38 +3157,34 @@
          json = 
+o
 m
-apper
 .writeValueAsStr
@@ -3205,25 +3205,24 @@
 ionTables);%0A
-%0A
         %7D ca
@@ -3532,62 +3532,8 @@
 y %7B%0A
-            ObjectMapper mapper = new ObjectMapper();%0A
     
@@ -3543,30 +3543,26 @@
      json = 
+o
 m
-apper
 .writeValueA
@@ -3594,17 +3594,16 @@
 ments);%0A
-%0A
         

Use single instance of ObjectMapper