<commit_before>package com.app.challenge.event.dao;

import java.io.ByteArrayInputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.app.challenge.constants.ChallengeConstants;
import com.app.challenge.domain.Player;
import com.app.challenge.domain.UserAccount;
import com.app.challenge.domain.UserToken;
import com.app.challenge.event.vo.ChallengeVO;

@Repository("eventManagerDao")
public class EventManagerDao {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	@Transactional
	public void createEvent() {

	}

	public boolean userExists(String emailId) {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, emailId);
		Map<String, Object> queryForMap = namedParameterJdbcTemplate.queryForMap("", paramMap);
		boolean result = null == queryForMap ? true : false;
		return result;
	}

	public UserToken checkUserExists(String emailId) {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, emailId);
		UserToken userToken = namedParameterJdbcTemplate.queryForObject("", paramMap, UserToken.class);
		return userToken;
	}

	@Transactional(rollbackFor = SQLException.class)
	public String createNewUserId(String userEmail, String token) throws SQLException {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, userEmail);
		paramMap.put(ChallengeConstants.DB_TOKEN, token);
		paramMap.put(ChallengeConstants.DB_DATE, new Date());

		String sql = "insert into rivals.user_tokens(fbtoken,createddate,useremail) values(:TOKEN,:DATE,:EMAIL)";
		try {
			namedParameterJdbcTemplate.update(sql, paramMap);
		} catch (DataAccessException e) {
			throw new SQLException();
		} catch (Exception e) {
			throw new SQLException();
		}
		return "Success";
	}

	@Transactional(rollbackFor = SQLException.class)
	public String updateUserToken(Long uid,String userEmail, String token) throws SQLException {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, userEmail);
		paramMap.put(ChallengeConstants.DB_TOKEN, token);
		paramMap.put(ChallengeConstants.DB_DATE, new Date());
		paramMap.put("uid", uid);
		String sql = "update rivals.user_tokens set fbtoken=:TOKEN,lastupdteddate=:DATE,useremail=:EMAIL where uid=:uid";
		try {
			namedParameterJdbcTemplate.update(sql, paramMap);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new SQLException();
		} catch (Exception e) {
			e.printStackTrace();
			throw new SQLException();
		}
		return "Success";
	}

	@Transactional(rollbackFor = SQLException.class)
	public String registerDevice(UserAccount userAccount) throws SQLException {
		byte[] bytes = userAccount.getUserImage().getBytes();
		ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_DEVICE_ID, userAccount.getDeviceId());
		paramMap.put(ChallengeConstants.DB_DEVICE_TYPE,
				userAccount.getDeviceType());
		paramMap.put(ChallengeConstants.DB_PLAYER_IMAGE, baos);
		paramMap.put(ChallengeConstants.DB_USERNAME, userAccount.getUserName());
		paramMap.put(ChallengeConstants.DB_EMAIL, userAccount.getUserEmail());
		paramMap.put(ChallengeConstants.DB_CREATED_DATE, new Date());
		paramMap.put(ChallengeConstants.DB_TOKEN, userAccount.getUserToken()
				.getFcbkToken());
		paramMap.put(ChallengeConstants.DB_DATE, new Date());
		paramMap.put(ChallengeConstants.DB_STATUS, userAccount.getStatus());
		KeyHolder keyHolder = new GeneratedKeyHolder();
		String sql = "SELECT EXISTS(select * from rivals.user_account where useremail=:EMAIL)";
		Boolean userExists = false;
		String uid = null;
		try {
			userExists = namedParameterJdbcTemplate.queryForObject(sql,
					paramMap, Boolean.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (userExists.booleanValue()) {
			// update block
			try {
				sql = "update rivals.user_account set deviceid=:DEVICEID, devicetype=:DEVICETYPE, userimage=:PLAYERIMAGE,lastupdateddate=:DATE";
				namedParameterJdbcTemplate.update(sql, paramMap);
				sql = "select id from rivals.user_account where useremail=:EMAIL";
				Long userId = namedParameterJdbcTemplate.queryForObject(sql,
						paramMap, Long.class);
				uid = Long.toString(userId);
				paramMap.put("USER_ID", userId);
				sql = "update rivals.user_tokens set fbtoken=:TOKEN, lastupdateddate=:DATE where uid=:USER_ID";
				namedParameterJdbcTemplate.update(sql, paramMap);
			} catch (Exception e) {
				e.printStackTrace();
				throw new SQLException();
			}
		} else {
			sql = "insert into rivals.user_account(deviceid,devicetype,userimage,username,useremail,createddate) values(:DEVICEID,:DEVICETYPE,:PLAYERIMAGE,:USERNAME,:EMAIL,:CREATED_DATE)";

			try {
				SqlParameterSource paramSource = new MapSqlParameterSource(
						paramMap);
				namedParameterJdbcTemplate.update(sql, paramSource, keyHolder);
				Map<String, Object> keys = keyHolder.getKeys();
				Long id = (Long) keys.get("GENERATED_KEY");
				uid = id.toString();
				paramMap.put(ChallengeConstants.DB_UID, id);
				sql = "insert into rivals.user_tokens(uid,fbtoken,createddate,useremail) values(:UID,:TOKEN,:DATE,:EMAIL)";
				namedParameterJdbcTemplate.update(sql, paramMap);
			} catch (DataAccessException e) {
				e.printStackTrace();
				throw new SQLException();
			} catch (Exception e) {
				e.printStackTrace();
				throw new SQLException();
			}
		}
		return uid;
	}
<commit_msg>added update strategy for the device in registration<commit_after>package com.app.challenge.event.dao;

import java.io.ByteArrayInputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.app.challenge.constants.ChallengeConstants;
import com.app.challenge.domain.Player;
import com.app.challenge.domain.UserAccount;
import com.app.challenge.domain.UserToken;
import com.app.challenge.event.vo.ChallengeVO;

@Repository("eventManagerDao")
public class EventManagerDao {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	@Transactional
	public void createEvent() {

	}

	public boolean userExists(String emailId) {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, emailId);
		Map<String, Object> queryForMap = namedParameterJdbcTemplate.queryForMap("", paramMap);
		boolean result = null == queryForMap ? true : false;
		return result;
	}

	public UserToken checkUserExists(String emailId) {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, emailId);
		UserToken userToken = namedParameterJdbcTemplate.queryForObject("", paramMap, UserToken.class);
		return userToken;
	}

	@Transactional(rollbackFor = SQLException.class)
	public String createNewUserId(String userEmail, String token) throws SQLException {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, userEmail);
		paramMap.put(ChallengeConstants.DB_TOKEN, token);
		paramMap.put(ChallengeConstants.DB_DATE, new Date());

		String sql = "insert into rivals.user_tokens(fbtoken,createddate,useremail) values(:TOKEN,:DATE,:EMAIL)";
		try {
			namedParameterJdbcTemplate.update(sql, paramMap);
		} catch (DataAccessException e) {
			throw new SQLException();
		} catch (Exception e) {
			throw new SQLException();
		}
		return "Success";
	}

	@Transactional(rollbackFor = SQLException.class)
	public String updateUserToken(Long uid,String userEmail, String token) throws SQLException {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_EMAIL, userEmail);
		paramMap.put(ChallengeConstants.DB_TOKEN, token);
		paramMap.put(ChallengeConstants.DB_DATE, new Date());
		paramMap.put("uid", uid);
		String sql = "update rivals.user_tokens set fbtoken=:TOKEN,lastupdteddate=:DATE,useremail=:EMAIL where uid=:uid";
		try {
			namedParameterJdbcTemplate.update(sql, paramMap);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new SQLException();
		} catch (Exception e) {
			e.printStackTrace();
			throw new SQLException();
		}
		return "Success";
	}

	@Transactional(rollbackFor = SQLException.class)
	public String registerDevice(UserAccount userAccount) throws SQLException {
		byte[] bytes = userAccount.getUserImage().getBytes();
		ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(ChallengeConstants.DB_DEVICE_ID, userAccount.getDeviceId());
		paramMap.put(ChallengeConstants.DB_DEVICE_TYPE,
				userAccount.getDeviceType());
		paramMap.put(ChallengeConstants.DB_PLAYER_IMAGE, baos);
		paramMap.put(ChallengeConstants.DB_USERNAME, userAccount.getUserName());
		paramMap.put(ChallengeConstants.DB_EMAIL, userAccount.getUserEmail());
		paramMap.put(ChallengeConstants.DB_CREATED_DATE, new Date());
		paramMap.put(ChallengeConstants.DB_TOKEN, userAccount.getUserToken()
				.getFcbkToken());
		paramMap.put(ChallengeConstants.DB_DATE, new Date());
		paramMap.put(ChallengeConstants.DB_STATUS, userAccount.getStatus());
		KeyHolder keyHolder = new GeneratedKeyHolder();
		String sql = "SELECT EXISTS(select * from rivals.user_account where useremail=:EMAIL)";
		Boolean userExists = false;
		String uid = null;
		try {
			userExists = namedParameterJdbcTemplate.queryForObject(sql,
					paramMap, Boolean.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (userExists.booleanValue()) {
			// update block
			try {
				sql = "update rivals.user_account set deviceid=:DEVICEID, devicetype=:DEVICETYPE, userimage=:PLAYERIMAGE,lastupdateddate=:DATE";
				namedParameterJdbcTemplate.update(sql, paramMap);
				sql = "select id from rivals.user_account where useremail=:EMAIL";
				Long userId = namedParameterJdbcTemplate.queryForObject(sql,
						paramMap, Long.class);
				uid = Long.toString(userId);
				paramMap.put("USER_ID", userId);
				sql = "update rivals.user_tokens set fbtoken=:TOKEN, lastupdateddate=:DATE where uid=:USER_ID";
				namedParameterJdbcTemplate.update(sql, paramMap);
			} catch (Exception e) {
				e.printStackTrace();
				throw new SQLException();
			}
		} else {
			sql = "insert into rivals.user_account(deviceid,devicetype,userimage,username,useremail,createddate,status) values(:DEVICEID,:DEVICETYPE,:PLAYERIMAGE,:USERNAME,:EMAIL,:CREATED_DATE,:STATUS)";

			try {
				SqlParameterSource paramSource = new MapSqlParameterSource(
						paramMap);
				namedParameterJdbcTemplate.update(sql, paramSource, keyHolder);
				Map<String, Object> keys = keyHolder.getKeys();
				Long id = (Long) keys.get("GENERATED_KEY");
				uid = id.toString();
				paramMap.put(ChallengeConstants.DB_UID, id);
				sql = "insert into rivals.user_tokens(uid,fbtoken,createddate,useremail) values(:UID,:TOKEN,:DATE,:EMAIL)";
				namedParameterJdbcTemplate.update(sql, paramMap);
			} catch (DataAccessException e) {
				e.printStackTrace();
				throw new SQLException();
			} catch (Exception e) {
				e.printStackTrace();
				throw new SQLException();
			}
		}
		return uid;
	}

{'fraction_non_alphanumeric': 0.11307728931915569, 'fraction_numerical': 0.0, 'mean_word_length': 5.834598698481562, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '6815220', 'n_tokens_mistral': 4213, 'n_tokens_neox': 3802, 'n_words': 741}