<commit_before>package com.github.aureliano.verbum_domini.web.listener;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.github.aureliano.verbum_domini.core.bean.UserBean;
import com.github.aureliano.verbum_domini.core.dao.UserDao;
import com.github.aureliano.verbum_domini.core.helper.HashHelper;
import com.github.aureliano.verbum_domini.core.impl.bean.UserBeanImpl;
import com.github.aureliano.verbum_domini.core.impl.dao.UserDaoImpl;

public class PrepareUserServletContextListener implements ServletContextListener {

	private static final Logger logger = Logger.getLogger(PrepareUserServletContextListener.class);
	
	private UserDao userDao;
	
	public PrepareUserServletContextListener() {
		this.userDao = new UserDaoImpl();
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		logger.info(" >>> Prepare default users.");
		this.saveUser("aureliano", "santo-antonio-2015");
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) { }
	
	private void saveUser(String login, String password) {
		UserBean user = this.findUserWithLogin(login);
		
		if (user == null) {
			user = this.createUser(login);
		}
		
		user.setPassword(password);
		user.setPassword(HashHelper.generatePasswordHash(user));
		
		this.userDao.save(user);
		logger.info("User " + login + " was saved.");
	}
	
	private UserBean createUser(String login) {<commit_msg>Create servlet context listener to initialize user data.<commit_after>package com.github.aureliano.verbum_domini.web.listener;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.github.aureliano.verbum_domini.core.bean.UserBean;
import com.github.aureliano.verbum_domini.core.dao.UserDao;
import com.github.aureliano.verbum_domini.core.helper.HashHelper;
import com.github.aureliano.verbum_domini.core.impl.bean.UserBeanImpl;
import com.github.aureliano.verbum_domini.core.impl.dao.UserDaoImpl;

public class PrepareUserServletContextListener implements ServletContextListener {

	private static final Logger logger = Logger.getLogger(PrepareUserServletContextListener.class);
	
	private UserDao userDao;
	
	public PrepareUserServletContextListener() {
		this.userDao = new UserDaoImpl();
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		logger.info(" >>> Prepare default users.");
		this.saveUser("santo_antonio", "santo-antonio-2015");
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) { }
	
	private void saveUser(String login, String password) {
		UserBean user = this.findUserWithLogin(login);
		
		if (user == null) {
			user = this.createUser(login);
		}
		
		user.setPassword(password);
		user.setPassword(HashHelper.generatePasswordHash(user));
		
		this.userDao.save(user);
		logger.info("User " + login + " was saved.");
	}
	
	private UserBean createUser(String login) {
{'fraction_non_alphanumeric': 0.10504065040650407, 'fraction_numerical': 0.0032520325203252032, 'mean_word_length': 5.61505376344086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3026488', 'n_tokens_mistral': 1016, 'n_tokens_neox': 952, 'n_words': 171}