package cz.metacentrum.perun.engine.dao.jdbc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import cz.metacentrum.perun.engine.dao.EngineDao;
import cz.metacentrum.perun.engine.exceptions.DispatcherNotConfiguredException;
import cz.metacentrum.perun.engine.exceptions.EngineNotConfiguredException;

/**
 * EngineDaoJdbc
 * 
 * @author Michal Karm Babacek
 * 
 */
@Transactional
public class EngineDaoJdbc extends JdbcDaoSupport implements EngineDao {
	private final static Logger log = LoggerFactory
			.getLogger(EngineDaoJdbc.class);

	@Autowired
	private Properties propertiesBean;
	private SimpleDateFormat formater = new SimpleDateFormat(
			"yyyyMMdd HH:mm:ss");
	private int engineId;

	public void initialize() {
		this.engineId = Integer.parseInt(propertiesBean
				.getProperty("engine.unique.id"));
	}

	@Override
	public void registerEngine() throws EngineNotConfiguredException {
		if (this.getJdbcTemplate().queryForInt(
				"select count(*) from engines where id = ?", engineId) != 1) {
			// throw new
			// EngineNotConfiguredException("This Perun-engine instance marked with ID["
			// + engineId
			// + "] is not configured in the Perun Database.");
			log.warn("This engine instance ID[" + this.engineId
					+ "] is not properly configured in database");
		} else {

			this.getJdbcTemplate()
					.update("update engines set ip_address = ?, port = ?, last_check_in = to_date(?,'YYYYMMDD HH24:MI:SS') where id = ?",
							propertiesBean.getProperty("engine.ip.address"),
							Integer.parseInt(propertiesBean
									.getProperty("engine.port")),
							formater.format(new Date(System.currentTimeMillis())),
							engineId);
		}

	}

	@Override
	public void checkIn() {
		this.getJdbcTemplate()
				.update("update engines set last_check_in = to_date(?,'YYYYMMDD HH24:MI:SS') where id = ?",
						formater.format(new Date(System.currentTimeMillis())),
						engineId);
	}

	@Override
	public void loadDispatcherAddress() throws DispatcherNotConfiguredException {
		/*
		 * String ipAddress = this.getJdbcTemplate().queryForObject(
		 * "select ip_address from dispatcher_settings", String.class); if
		 * (ipAddress == null) { //throw new DispatcherNotConfiguredException(
		 * "It looks like the Dispatcher's IP address is null. That's not gonna work bro :-)"
		 * ); }
		 * 
		 * int port = this.getJdbcTemplate().queryForObject(
		 * "select port from dispatcher_settings", Integer.class); if (port >
		 * 65535 || port < 1) { //throw new DispatcherNotConfiguredException(
		 * "It looks like the Dispatcher's port is a really weird number (" +
		 * port + "), that's not gonna work bro :-)"); }
		 */
		String ipAddress = "";
		int port = 0;
		if (log.isDebugEnabled()) {
			log.debug("We are not gonna set following properties: dispatcher.ip.address:"
					+ ipAddress + ", dispatcher.port:" + port);
		}

		// propertiesBean.put("dispatcher.ip.address", ipAddress);
		// propertiesBean.put("dispatcher.port", "" + port);
	}

	public void setPropertiesBean(Properties propertiesBean) {
		this.propertiesBean = propertiesBean;
	}

	public Properties getPropertiesBean() {
		return propertiesBean;
	}
}

@@ -1288,19 +1288,16 @@
 1) %7B%0A%09%09%09
-// 
 throw ne
@@ -1297,22 +1297,16 @@
 hrow new
-%0A%09%09%09//
  EngineN
@@ -1375,19 +1375,18 @@
 ID%5B%22%0A%09%09%09
-// 
+%09%09
 + engine
@@ -1391,19 +1391,18 @@
 neId%0A%09%09%09
-// 
+%09%09
 + %22%5D is 
@@ -1446,115 +1446,8 @@
 %22);%0A
-%09%09%09log.warn(%22This engine instance ID%5B%22 + this.engineId%0A%09%09%09%09%09+ %22%5D is not properly configured in database%22);%0A
 %09%09%7D 
@@ -2161,16 +2161,8 @@
 %7B%0A%09%09
-/*%0A%09%09 * 
 Stri
@@ -2209,35 +2209,34 @@
 eryForObject(%0A%09%09
+%09
- * 
+%09
 %22select ip_addre
@@ -2284,15 +2284,13 @@
 s); 
-if
 %0A%09%09
- *
+if
  (ip
@@ -2304,26 +2304,28 @@
  == null) %7B 
-//
+%0A%09%09%09
 throw new Di
@@ -2350,35 +2350,35 @@
 redException(%0A%09%09
- * 
+%09%09%09
 %22It looks like t
@@ -2450,27 +2450,25 @@
 %22%0A%09%09
- * ); 
+%09%09%09); %0A%09%09
 %7D%0A%09%09 
-* 
 %0A%09%09
- * 
 int 
@@ -2515,19 +2515,18 @@
 ject(%0A%09%09
- * 
+%09%09
 %22select 
@@ -2573,16 +2573,19 @@
 class); 
+%0A%09%09
 if (port
@@ -2586,21 +2586,16 @@
  (port %3E
-%0A%09%09 *
  65535 %7C
@@ -2608,18 +2608,20 @@
  %3C 1) %7B 
-//
+%0A%09%09%09
 throw ne
@@ -2658,19 +2658,19 @@
 tion(%0A%09%09
- * 
+%09%09%09
 %22It look
@@ -2730,19 +2730,21 @@
  (%22 +%0A%09%09
- * 
+%09%09%09%09%09
 port + %22
@@ -2783,56 +2783,12 @@
 %22); 
-%7D
 %0A%09%09
- */%0A%09%09String ipAddress = %22%22;%0A%09%09int port = 0;
+%7D
 %0A%09%09i
@@ -2835,20 +2835,16 @@
 %22We are 
-not 
 gonna se
@@ -2947,19 +2947,16 @@
 %0A%09%09%7D%0A%0A%09%09
-// 
 properti
@@ -3009,11 +3009,8 @@
 ;%0A%09%09
-// 
 prop

remove modifications made for testing