<commit_before>package cz.metacentrum.perun.taskslib.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import cz.metacentrum.perun.core.api.Facility;
import cz.metacentrum.perun.core.api.Pair;
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
import cz.metacentrum.perun.core.impl.FacilitiesManagerImpl;
import cz.metacentrum.perun.core.impl.ServicesManagerImpl;
import cz.metacentrum.perun.core.impl.Utils;
import cz.metacentrum.perun.core.impl.Compatibility;
import cz.metacentrum.perun.taskslib.dao.TaskDao;
import cz.metacentrum.perun.taskslib.model.ExecService;
import cz.metacentrum.perun.taskslib.model.Task;
import cz.metacentrum.perun.taskslib.model.Task.TaskStatus;

/**
 *
 * @author <NAME> JavaDoc coming soon...
 *         TODO: Remove "" where not necessary...
 */
public class TaskDaoJdbc extends JdbcDaoSupport implements TaskDao {

	private static final Logger log = LoggerFactory.getLogger(TaskDaoJdbc.class);
	// It would be nice to have some comments here, wouldn't it? :-)
	// public static final SimpleDateFormat dbFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
	public static final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

	public final static String taskMappingSelectQuery = " tasks.id as tasks_id, tasks.schedule as tasks_schedule, tasks.recurrence as tasks_recurrence, " +
		"tasks.delay as tasks_delay, tasks.status as tasks_status, tasks.start_time as tasks_start_time, tasks.end_time as tasks_end_time, tasks.engine_id as tasks_engine_id ";

	public static final RowMapper<Task> TASK_ROWMAPPER = new RowMapper<Task>() {

		public Task mapRow(ResultSet rs, int i) throws SQLException {

			Task task = new Task();

			task.setDelay(rs.getInt("tasks_delay"));
			task.setId(rs.getInt("tasks_id"));
			task.setRecurrence(rs.getInt("tasks_recurrence"));

			if (rs.getTimestamp("tasks_start_time") != null) {
				task.setStartTime(rs.getTimestamp("tasks_start_time"));
			}
			if (rs.getTimestamp("tasks_schedule") != null) {
				task.setSchedule(rs.getTimestamp("tasks_schedule"));
			}
			if (rs.getTimestamp("tasks_end_time") != null) {
				task.setEndTime(rs.getTimestamp("tasks_end_time"));
			}

			if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.DONE.toString())) {
				task.setStatus(TaskStatus.DONE);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.ERROR.toString())) {
				task.setStatus(TaskStatus.ERROR);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.NONE.toString())) {
				task.setStatus(TaskStatus.NONE);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.PLANNED.toString())) {
				task.setStatus(TaskStatus.PLANNED);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.PROCESSING.toString())) {
				task.setStatus(TaskStatus.PROCESSING);
			} else {
				throw new IllegalArgumentException("Task status unknown :-(");
			}

			task.setFacility(FacilitiesManagerImpl.FACILITY_MAPPER.mapRow(rs, i));

			task.setExecService(ExecServiceDaoJdbc.EXEC_SERVICE_ROWMAPPER.mapRow(rs, i));

			return task;
		}

	};

	public static final RowMapper<Pair<Task, Integer>> TASK_CLIENT_ROWMAPPER = new RowMapper<Pair<Task, Integer>>() {
	
		public Pair<Task, Integer> mapRow(ResultSet rs, int i) throws SQLException {
		
			Task task = TASK_ROWMAPPER.mapRow(rs, i);
			
			return new Pair<Task, Integer>(task, rs.getInt("tasks_engine_id"));
		}
			
	};	
	
	@Override
	public int scheduleNewTask(Task task, int engineID) throws InternalErrorException {
		int newTaskId = 0;
		try {
			newTaskId = Utils.getNewId(this.getJdbcTemplate(), "tasks_id_seq");
			this.getJdbcTemplate().update(
					"insert into tasks(id, exec_service_id, facility_id, schedule, recurrence, delay, status, engine_id) values (?,?,?, " + Compatibility.toDate("?","'DD-MM-YYYY HH24:MI:SS'") + ",?,?,?,?)",
					newTaskId, task.getExecServiceId(), task.getFacilityId(), formatter.format(task.getSchedule()), task.getRecurrence(), task.getDelay(), task.getStatus().toString(), engineID);
			return newTaskId;
		} catch (DataIntegrityViolationException ex) {
			log.error("Data: id, exec_service_id, facility_id, schedule, recurrence, delay, status is: " + newTaskId + ", " + task.getExecServiceId() + ", " + task.getFacilityId() + ", "
					+ formatter.format(task.getSchedule()) + ", " + task.getRecurrence() + ", " + task.getDelay() + ", " + task.getStatus().toString() + ". Exception:" + ex.toString(), ex);
		}
		return 0;
	}
	
	@Override
	public int insertTask(Task task, int engineID) throws InternalErrorException {
		int newTaskId = 0;
		try {
			newTaskId = task.getId();
			this.getJdbcTemplate().update(
					"insert into tasks(id, exec_service_id, facility_id, schedule, recurrence, delay, status, engine_id) values (?,?,?,to_date(?,'DD-MM-YYYY HH24:MI:SS'),?,?,?,?)",
					newTaskId, task.getExecServiceId(), task.getFacilityId(), formatter.format(task.getSchedule()), task.getRecurrence(), task.getDelay(), task.getStatus().toString(), engineID);
			return newTaskId;
		} catch (DataIntegrityViolationException ex) {
			log.error("Data: id, exec_service_id, facility_id, schedule, recurrence, delay, status is: " + newTaskId + ", " + task.getExecServiceId() + ", " + task.getFacilityId() + ", "
					+ formatter.format(task.getSchedule()) + ", " + task.getRecurrence() + ", " + task.getDelay() + ", " + task.getStatus().toString() + ". Exception:" + ex.toString(), ex);
		}
		return 0;
	}
	
	@Override
	public int insertTask(Task task, int engineID) throws InternalErrorException {
		int newTaskId = 0;
		try {
			newTaskId = task.getId();
			this.getJdbcTemplate().update(
					"insert into tasks(id, exec_service_id, facility_id, schedule, recurrence, delay, status, engine_id) values (?,?,?,to_date(?,'DD-MM-YYYY HH24:MI:SS'),?,?,?,?)",
					newTaskId, task.getExecServiceId(), task.getFacilityId(), formatter.format(task.getSchedule()), task.getRecurrence(), task.getDelay(), task.getStatus().toString(), engineID);
			return newTaskId;
		} catch (DataIntegrityViolationException ex) {
			log.error("Data: id, exec_service_id, facility_id, schedule, recurrence, delay, status is: " + newTaskId + ", " + task.getExecServiceId() + ", " + task.getFacilityId() + ", "
					+ formatter.format(task.getSchedule()) + ", " + task.getRecurrence() + ", " + task.getDelay() + ", " + task.getStatus().toString() + ". Exception:" + ex.toString(), ex);
		}
		return 0;
	}
	
	@Override
	public int insertTask(Task task, int engineID) throws InternalErrorException {
		int newTaskId = 0;
		try {
			newTaskId = task.getId();
			this.getJdbcTemplate().update(
					"insert into tasks(id, exec_service_id, facility_id, schedule, recurrence, delay, status, engine_id) values (?,?,?,to_date(?,'DD-MM-YYYY HH24:MI:SS'),?,?,?,?)",
					newTaskId, task.getExecServiceId(), task.getFacilityId(), formatter.format(task.getSchedule()), task.getRecurrence(), task.getDelay(), task.getStatus().toString(), engineID);
			return newTaskId;
		} catch (DataIntegrityViolationException ex) {
			log.error("Data: id, exec_service_id, facility_id, schedule, recurrence, delay, status is: " + newTaskId + ", " + task.getExecServiceId() + ", " + task.getFacilityId() + ", "
					+ formatter.format(task.getSchedule()) + ", " + task.getRecurrence() + ", " + task.getDelay() + ", " + task.getStatus().toString() + ". Exception:" + ex.toString(), ex);
		}
		return 0;
	}
<commit_msg>Remove duplicity of method insertTask in TaskDaoJdbc<commit_after>package cz.metacentrum.perun.taskslib.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import cz.metacentrum.perun.core.api.Facility;
import cz.metacentrum.perun.core.api.Pair;
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
import cz.metacentrum.perun.core.impl.FacilitiesManagerImpl;
import cz.metacentrum.perun.core.impl.ServicesManagerImpl;
import cz.metacentrum.perun.core.impl.Utils;
import cz.metacentrum.perun.core.impl.Compatibility;
import cz.metacentrum.perun.taskslib.dao.TaskDao;
import cz.metacentrum.perun.taskslib.model.ExecService;
import cz.metacentrum.perun.taskslib.model.Task;
import cz.metacentrum.perun.taskslib.model.Task.TaskStatus;

/**
 *
 * @author <NAME> JavaDoc coming soon...
 *         TODO: Remove "" where not necessary...
 */
public class TaskDaoJdbc extends JdbcDaoSupport implements TaskDao {

	private static final Logger log = LoggerFactory.getLogger(TaskDaoJdbc.class);
	// It would be nice to have some comments here, wouldn't it? :-)
	// public static final SimpleDateFormat dbFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
	public static final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

	public final static String taskMappingSelectQuery = " tasks.id as tasks_id, tasks.schedule as tasks_schedule, tasks.recurrence as tasks_recurrence, " +
		"tasks.delay as tasks_delay, tasks.status as tasks_status, tasks.start_time as tasks_start_time, tasks.end_time as tasks_end_time, tasks.engine_id as tasks_engine_id ";

	public static final RowMapper<Task> TASK_ROWMAPPER = new RowMapper<Task>() {

		public Task mapRow(ResultSet rs, int i) throws SQLException {

			Task task = new Task();

			task.setDelay(rs.getInt("tasks_delay"));
			task.setId(rs.getInt("tasks_id"));
			task.setRecurrence(rs.getInt("tasks_recurrence"));

			if (rs.getTimestamp("tasks_start_time") != null) {
				task.setStartTime(rs.getTimestamp("tasks_start_time"));
			}
			if (rs.getTimestamp("tasks_schedule") != null) {
				task.setSchedule(rs.getTimestamp("tasks_schedule"));
			}
			if (rs.getTimestamp("tasks_end_time") != null) {
				task.setEndTime(rs.getTimestamp("tasks_end_time"));
			}

			if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.DONE.toString())) {
				task.setStatus(TaskStatus.DONE);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.ERROR.toString())) {
				task.setStatus(TaskStatus.ERROR);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.NONE.toString())) {
				task.setStatus(TaskStatus.NONE);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.PLANNED.toString())) {
				task.setStatus(TaskStatus.PLANNED);
			} else if (rs.getString("tasks_status").equalsIgnoreCase(TaskStatus.PROCESSING.toString())) {
				task.setStatus(TaskStatus.PROCESSING);
			} else {
				throw new IllegalArgumentException("Task status unknown :-(");
			}

			task.setFacility(FacilitiesManagerImpl.FACILITY_MAPPER.mapRow(rs, i));

			task.setExecService(ExecServiceDaoJdbc.EXEC_SERVICE_ROWMAPPER.mapRow(rs, i));

			return task;
		}

	};

	public static final RowMapper<Pair<Task, Integer>> TASK_CLIENT_ROWMAPPER = new RowMapper<Pair<Task, Integer>>() {
	
		public Pair<Task, Integer> mapRow(ResultSet rs, int i) throws SQLException {
		
			Task task = TASK_ROWMAPPER.mapRow(rs, i);
			
			return new Pair<Task, Integer>(task, rs.getInt("tasks_engine_id"));
		}
			
	};	
	
	@Override
	public int scheduleNewTask(Task task, int engineID) throws InternalErrorException {
		int newTaskId = 0;
		try {
			newTaskId = Utils.getNewId(this.getJdbcTemplate(), "tasks_id_seq");
			this.getJdbcTemplate().update(
					"insert into tasks(id, exec_service_id, facility_id, schedule, recurrence, delay, status, engine_id) values (?,?,?, " + Compatibility.toDate("?","'DD-MM-YYYY HH24:MI:SS'") + ",?,?,?,?)",
					newTaskId, task.getExecServiceId(), task.getFacilityId(), formatter.format(task.getSchedule()), task.getRecurrence(), task.getDelay(), task.getStatus().toString(), engineID);
			return newTaskId;
		} catch (DataIntegrityViolationException ex) {
			log.error("Data: id, exec_service_id, facility_id, schedule, recurrence, delay, status is: " + newTaskId + ", " + task.getExecServiceId() + ", " + task.getFacilityId() + ", "
					+ formatter.format(task.getSchedule()) + ", " + task.getRecurrence() + ", " + task.getDelay() + ", " + task.getStatus().toString() + ". Exception:" + ex.toString(), ex);
		}
		return 0;
	}
	
	@Override
	public int insertTask(Task task, int engineID) throws InternalErrorException {
		int newTaskId = 0;
		try {
			newTaskId = task.getId();
			this.getJdbcTemplate().update(
					"insert into tasks(id, exec_service_id, facility_id, schedule, recurrence, delay, status, engine_id) values (?,?,?,to_date(?,'DD-MM-YYYY HH24:MI:SS'),?,?,?,?)",
					newTaskId, task.getExecServiceId(), task.getFacilityId(), formatter.format(task.getSchedule()), task.getRecurrence(), task.getDelay(), task.getStatus().toString(), engineID);
			return newTaskId;
		} catch (DataIntegrityViolationException ex) {
			log.error("Data: id, exec_service_id, facility_id, schedule, recurrence, delay, status is: " + newTaskId + ", " + task.getExecServiceId() + ", " + task.getFacilityId() + ", "
					+ formatter.format(task.getSchedule()) + ", " + task.getRecurrence() + ", " + task.getDelay() + ", " + task.getStatus().toString() + ". Exception:" + ex.toString(), ex);
		}
		return 0;
	}

{'fraction_non_alphanumeric': 0.14869079567708712, 'fraction_numerical': 0.002030898672662653, 'mean_word_length': 5.1581062974542204, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4944312', 'n_tokens_mistral': 4834, 'n_tokens_neox': 4446, 'n_words': 824}