package com.github.aureliano.evtbridge.output.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.github.aureliano.evtbridge.core.agent.AbstractDataWriter;
import com.github.aureliano.evtbridge.core.jdbc.ConnectionPool;
import com.github.aureliano.evtbridge.core.jdbc.JdbcConnectionModel;

public class JdbcDataWriter extends AbstractDataWriter {

	private JdbcConnectionModel schemaConnection;
	private Connection connection;
	
	private static final Logger logger = Logger.getLogger(JdbcDataWriter.class);
	
	public JdbcDataWriter() {
		super();
	}

	@Override
	public void initializeResources() {
		this.initialize();
	}

	@Override
	public void finalizeResources() { }

	@SuppressWarnings("unchecked")
	@Override
	public void write(Object data) {
		this.insert((Map<String, Object>) data);
	}
	
	private void initialize() {
		this.schemaConnection = ((JdbcOutputConfig) super.outputConfiguration).getConnection();
		logger.info("Table for inserting: " + this.schemaConnection.getTable());
	}
	
	private void insert(Map<String, Object> data) {
		try {
			if (this.connection == null) {
				this.connection = ConnectionPool.instance().getConnection(this.schemaConnection);
			}
			
			PreparedStatement stmt = JdbcHelper.createStatement(connection, this.schemaConnection.getTable(), data);
			stmt.executeUpdate();
			
			stmt.close();
		} catch (SQLException ex) {
			throw new JdbcOutputException(ex);
		}
	}
}
@@ -195,16 +195,82 @@
 ogger;%0A%0A
+import com.github.aureliano.evtbridge.common.helper.StringHelper;%0A
 import c
@@ -1091,16 +1091,175 @@
 tion();%0A
+%09%09if (StringHelper.isEmpty(this.schemaConnection.getTable())) %7B%0A%09%09%09throw new JdbcOutputException(%22Table attribute must be provided. Found empty string.%22);%0A%09%09%7D%0A
 %09%09logger

Apply table name validation.