package com.marcelherd.oot.persistence.repository.highscore;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.marcelherd.oot.persistence.Database;
import com.marcelherd.oot.persistence.DatabaseFactory;
import com.marcelherd.oot.persistence.domain.Highscore;

public class JDBCHighscoreService implements HighscoreRepository {

	private Database database;

	protected JDBCHighscoreService() {
		database = DatabaseFactory.getInstance();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void save(Highscore entity) {
		String sql = null;

		if (findOne(entity.getId()) != null) { // update
			sql = String.format("UPDATE `highscores` SET `name`='%s', `date`='%t', `sum`='%d' WHERE `id`=%d",
					entity.getName(), entity.getDate(), entity.getSum(), entity.getId());
		} else { // create
			sql = String.format("INSERT INTO `highscores` (`name`, `date`, `sum`) VALUES ('%s', '%t', '%d')",
					entity.getName(), entity.getDate(), entity.getSum(), entity.getId());
		}

		try (Connection connection = database.getConnection(); Statement statement = connection.createStatement()) {
			statement.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Highscore findOne(Long id) {
		try (Connection connection = database.getConnection();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement.executeQuery("SELECT * FROM highscores WHERE id = " + id)) {
			return JDBCHighscoreParser.parseOne(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Highscore> findAll() {
		try (Connection connection = database.getConnection();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement.executeQuery("SELECT * FROM highscores")) {
			return JDBCHighscoreParser.parseAll(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Highscore> findAll(Iterable<Long> ids) {
		String joined = StreamSupport.stream(ids.spliterator(), false).map(l -> Long.toString(l))
				.collect(Collectors.joining(","));

		try (Connection connection = database.getConnection();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement
						.executeQuery("SELECT * FROM highscores WHERE id IN ( " + joined + ")")) {
			return JDBCHighscoreParser.parseAll(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Long count() {
		try (Connection connection = database.getConnection();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement.executeQuery("SELECT count(id) AS count FROM highscores")) {
			return resultSet.getLong("count");
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Highscore> findTopTen() {
		try (Connection connection = database.getConnection();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement.executeQuery("SELECT * FROM highscores ORDER BY sum DESC LIMIT 10")) {
			return JDBCHighscoreParser.parseAll(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected void setDatabase(Database database) {
		this.database = database;
	}

}

@@ -702,75 +702,8 @@
 ql =
- null;%0A%0A%09%09if (findOne(entity.getId()) != null) %7B // update%0A%09%09%09sql =
  Str
@@ -718,14 +718,19 @@
 at(%22
-UPDATE
+INSERT INTO
  %60hi
@@ -743,31 +743,23 @@
 es%60 
-SET 
+(
 %60name%60
-='%25s'
 , %60date%60
 ='%25t
@@ -758,228 +758,85 @@
 ate%60
-='%25t'
 , %60sum%60
-='%25d' WHERE %60id%60=%25d%22,%0A%09%09%09%09%09entity.getName(), entity.getDate(), entity.getSum(), entity.getId());%0A%09%09%7D else %7B // create%0A%09%09%09sql = String.format(%22INSERT INTO %60highscores%60 (%60name%60, %60date%60, %60sum%60) VALUES ('%25s', '%25t
+) VALUES ('%25s', '%22 + new java.sql.Date(entity.getDate().getTime()) + %22
 ', '
@@ -841,22 +841,17 @@
  '%25d')%22,
-%0A%09%09%09%09%09
+ 
 entity.g
@@ -874,53 +874,15 @@
 .get
-Date(), entity.getSum(), entity.getId());%0A%09%09%7D
+Sum());
 %0A%0A%09%09

Insert Highscore bug fixed