<commit_before>/**
 * BenchwarmerController is the main controller for the NHL Time Machine web application.
 * It handles all request mapping, as well as flow control and access to utility classes.
 * 
 * @author  <NAME>
 * @version 1.0.0
 * @since 	2017-08-26
 */
package com.benchwarmer.spring;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.benchwarmer.spring.service.GoalieSeasonsService;
import com.benchwarmer.spring.service.PlayerService;
import com.benchwarmer.spring.model.Goalieseasons;
import com.benchwarmer.spring.model.Player;
import com.benchwarmer.spring.service.SeasonService;
import com.benchwarmer.spring.model.Season;
import com.benchwarmer.spring.service.SkaterSeasonsService;
import com.benchwarmer.spring.model.Skaterseasons;

@Controller
@SessionAttributes({"playerbios", "searchableplayers", "seasons", "skater", "baseS", "targetS", "min", "sort", "pageNum", "players", "goalies"})
public class BenchwarmerController {
	@Autowired private SeasonService seasonService;
	@Autowired private SkaterSeasonsService skaterService;
	@Autowired private GoalieSeasonsService goalieService;
	@Autowired private PlayerService playerService;
	
	@ModelAttribute("playerbios") public List<Player> playerbios() { return playerService.playerList(); } // used to populate player select inputs
	@ModelAttribute("seasons") public List<Season> seasons() { return seasonService.seasonList(); } // used to populate season select inputs
	@ModelAttribute("baseS") public int baseS() { return 20162017; } // holds base season for stat transformation - the season to transform
	@ModelAttribute("targetS") public int targetS() { return 20162017; } // holds target season for stat transformation - the production levels to transform to
	@ModelAttribute("min") public int min() { return 20; } // used to filter results by a minimum number of player games played
	@ModelAttribute("sort") public String sort() { return "pts"; } // used to hold the column currently used to sort the data
	@ModelAttribute("pageNum") public int pageNum() { return 1; } // used to hold the current table data page - pagination
	@ModelAttribute("players") public List<ConvertedSkater> players() { return transformStats(skaterService.skaterList(20162017, 20), 20162017, 20162017); } // holds the skater transformation result sets
	@ModelAttribute("goalies") public List<ConvertedGoalie> goalies() { return transformGoalies(goalieService.goalieList(20162017, 20), 20162017, 20162017); } // holds the goalie transformation result sets
	@ModelAttribute("searchableplayers") // ensures player bio data is only accessed once and can then be searched by playerid
	public Map<Integer, Player> searchableplayers(@ModelAttribute("playerbios") List<Player> playerbios) {  // retrieves all player bios from db
		Map<Integer, Player> players = new HashMap<Integer, Player>(); // creates new hashmap with playerid as key and player bio as value
		for(Player p : playerbios) players.put(p.getPlayerid(), p); // adds each player bio to map with key added
		return players; // returns new player bio hashmap
	} // searchableplayers()
	
	@RequestMapping({"/welcome"}) // handles initial loading of the application
	public String welcome(@ModelAttribute("seasons") List<Season> seasons, @ModelAttribute("baseS") int baseS, @ModelAttribute("targetS") int targetS, @ModelAttribute("min") int min) {
		return "welcome";
	} // welcome()
	
	@RequestMapping({"/intro"}) // handles initial loading of the application
	public String intro(@ModelAttribute("seasons") List<Season> seasons, @ModelAttribute("baseS") int baseS, @ModelAttribute("targetS") int targetS, @ModelAttribute("min") int min) {
		return "intro";
	} // intro()
	
	@RequestMapping({"/timemachine"}) // handles initial loading of the application
	public String timemachine(@ModelAttribute("seasons") List<Season> seasons, @ModelAttribute("baseS") int baseS, @ModelAttribute("targetS") int targetS, @ModelAttribute("min") int min) {
		return "timemachine";
	} // timemachine()
	
	@RequestMapping(value = "/skaters") // handles view for skater transformation menu with default values
	public String skaters(Model model) {
		model.addAttribute("baseS", "20162017");
		model.addAttribute("targetS", "20162017");
		model.addAttribute("sort", "pts");
		model.addAttribute("pageNum", 1);
		model.addAttribute("min", 20);
		return "skaters";
	} // skaters()
	
	@RequestMapping(value = "/goalies") // handles view for goalie transformation menu with default values
	public String goalies(Model model) {
		model.addAttribute("baseS", "20162017");
		model.addAttribute("targetS", "20162017");
		model.addAttribute("sort", "pts");
		model.addAttribute("pageNum", 1);
		model.addAttribute("min", 20);
		return "goalies";
	} // goalies()
	
	@RequestMapping(value = "/comparator") // handles view for player comparator menu with default values
	public String comparator(Model model, @ModelAttribute("playerbios") List<Player> playerbios, @ModelAttribute("searchableplayers") Map<Integer, Player> searchableplayers) {
		model.addAttribute("baseS", "20162017");
		model.addAttribute("p1", searchableplayers.get(8478550));
		model.addAttribute("p2", searchableplayers.get(8476453));
		model.addAttribute("sort", "pts");
		model.addAttribute("pageNum", 1);
		return "comparator";
	}// comparator()
	
	@RequestMapping(value = "/skatertable", method = RequestMethod.GET) // handles view for the presentation of skater transformation data
	public String skatertable(@RequestParam Map<String, String> params, Model model, @ModelAttribute("players") List<ConvertedSkater> players, @ModelAttribute("seasons") List<Season> seasons) {
		int targetSeason = Integer.parseInt(params.get("targetSeason")); // retrieves target season for transformation from get request
		int min = Integer.parseInt(params.get("min")); // retrieves minimum games played for filtering the transformation from get request
		int baseSeason; // will hold user-specified base season
		if (params.get("baseSeason").equals("0")) { // retrieves base season for transformation from get request
			baseSeason = 0; // value used when the user wishes to transform skater data from all available seasons
			players = new ArrayList<ConvertedSkater>(); // prepares for new result set
			for (Season s : seasons) // gets skater data for each season
				players.addAll(transformStats(skaterService.skaterList(s.getSeasonid(), min), s.getSeasonid(), targetSeason)); // transforms skater data
		} // if (season==0)
		else { // handles individual season transformation requests
			baseSeason = Integer.parseInt(params.get("baseSeason")); // retrieves requested base season from get
			players = transformStats(skaterService.skaterList(baseSeason, min), baseSeason, targetSeason); // transforms base season skater data
			if (params.containsKey("include")) { // determines whether the user has requested to include skater data from target season for comparison
				players.addAll(transformStats(skaterService.skaterList(targetSeason, min), targetSeason, targetSeason)); // transforms target season skater data
			} // if (include)
		} // else
		sortSkaters(players, params.get("sort")); // retrieves user-specified sort column and sorts skater data
		model.addAttribute("players", players); // adds newly created skater transformation data to model
		model.addAttribute("skaters", players.subList(0, 25)); // limits data to first 25 results - used for pagination
		model.addAttribute("baseS", baseSeason); // sets user-specified base season to model
		model.addAttribute("targetS", targetSeason); // sets user-specified target season to model
		model.addAttribute("min", min); // sets user-specified minimum games played for filtering to model
		return "skatertable"; // directs to skater table view
	}// skatertable()
	
	@RequestMapping(value = "/goalietable", method = RequestMethod.GET) // handles view for the presentation of goalie transformation data
	public String goalietable(@RequestParam Map<String, String> params, Model model, @ModelAttribute("goalies") List<ConvertedGoalie> goalies, @ModelAttribute("seasons") List<Season> seasons) {
		int targetSeason = Integer.parseInt(params.get("targetSeason")); // retrieves target season for transformation from get request
		int min = Integer.parseInt(params.get("min")); // retrieves minimum games played for filtering the transformation from get request
		int baseSeason; // will hold user-specified base season
		if (params.get("baseSeason").equals("0")) { // retrieves base season for transformation from get request
			baseSeason = 0; // values used when the user wishes to transform goalie data from all available seasons
			goalies = new ArrayList<ConvertedGoalie>(); // prepares for new result set
			for (Season s : seasons) // gets skater data for each season
				goalies.addAll(transformGoalies(goalieService.goalieList(s.getSeasonid(), min), s.getSeasonid(), targetSeason)); // transforms goalie data
		} // if (season==0)
		else { // handles individual season transformation requests
			baseSeason= Integer.parseInt(params.get("baseSeason")); // retrieves requested base season from get
			goalies = transformGoalies(goalieService.goalieList(baseSeason, min), baseSeason, targetSeason); // transforms base season goalie data
			if (params.containsKey("include")) { // determines whether the user has requested to include goalie data from target season for comparison
				goalies.addAll(transformGoalies(goalieService.goalieList(targetSeason, min), targetSeason, targetSeason)); // transforms target season goalie data
			} // if (include)
		} // else
		sortGoalies(goalies, params.get("sort")); // retrieves user-specified sort column and sorts goalie data
		model.addAttribute("goalies", goalies); // adds newly created goalie transformation data to model
		model.addAttribute("current", goalies.subList(0, 25)); // limits data to first 25 results - used for pagination
		model.addAttribute("baseS", baseSeason); // sets user-specified base season to model
		model.addAttribute("targetS", targetSeason); // sets user-specified target season to model
		model.addAttribute("min", min); // sets user-specified minimum games played for filtering to model
		return "goalietable"; // directs to goalie table view
	} // goalietable()
	
	@RequestMapping(value = "/validatecomparator", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody String validatecomparator(@RequestParam("player1") String player1, @RequestParam("player2") String player2, @ModelAttribute("searchableplayers") Map<Integer, Player> searchableplayers) {
		String result;
		Player p1 = searchableplayers.get(Integer.parseInt(player1)); // retrieves user-specified player to be used for comparison
		Player p2 = searchableplayers.get(Integer.parseInt(player2)); // retrieves user-specified player to be used for comparison
		System.out.println("In Java methopd:" + p1.getPlayerName());
		if ((p1.getPosition().equals("G") && !p2.getPosition().equals("G")) || (p2.getPosition().equals("G") && !p1.getPosition().equals("G"))) { // handles incomparable data
			 result = "false";
		} // if (incomparable data)
		result = "true";
		return result;
	} // validatecomparator()
	
	@RequestMapping(value = "/comptable", method = RequestMethod.GET) // handles view for the presentation of player comparison data
	public String comptable(HttpServletRequest request, @RequestParam Map<String, String> params, Model model, @ModelAttribute("players") List<ConvertedSkater> players, @ModelAttribute("goalies") List<ConvertedGoalie> goalies, @ModelAttribute("searchableplayers") Map<Integer, Player> searchableplayers) {
		int baseSeason = Integer.parseInt(params.get("baseSeason")); // retrieves user-specified base season to be used for player comparison
		Player p1 = searchableplayers.get(Integer.parseInt(params.get("player1"))); // retrieves user-specified player to be used for comparison
		Player p2 = searchableplayers.get(Integer.parseInt(params.get("player2"))); // retrieves user-specified player to be used for comparison
		if ((p1.getPosition().equals("G") && !p2.getPosition().equals("G")) || (p2.getPosition().equals("G") && !p1.getPosition().equals("G"))) { // handles incomparable data
			String referer = request.getHeader("Referer");
			return "redirect:" + referer;
		} // if (incomparable data)
		if (!p1.getPosition().equals("G")) { // handles skater comparisons<commit_msg>adding validation<commit_after>/**
 * BenchwarmerController is the main controller for the NHL Time Machine web application.
 * It handles all request mapping, as well as flow control and access to utility classes.
 * 
 * @author  <NAME>
 * @version 1.0.0
 * @since 	2017-08-26
 */
package com.benchwarmer.spring;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.benchwarmer.spring.service.GoalieSeasonsService;
import com.benchwarmer.spring.service.PlayerService;
import com.benchwarmer.spring.model.Goalieseasons;
import com.benchwarmer.spring.model.Player;
import com.benchwarmer.spring.service.SeasonService;
import com.benchwarmer.spring.model.Season;
import com.benchwarmer.spring.service.SkaterSeasonsService;
import com.benchwarmer.spring.model.Skaterseasons;

@Controller
@SessionAttributes({"playerbios", "searchableplayers", "seasons", "skater", "baseS", "targetS", "min", "sort", "pageNum", "players", "goalies"})
public class BenchwarmerController {
	@Autowired private SeasonService seasonService;
	@Autowired private SkaterSeasonsService skaterService;
	@Autowired private GoalieSeasonsService goalieService;
	@Autowired private PlayerService playerService;
	
	@ModelAttribute("playerbios") public List<Player> playerbios() { return playerService.playerList(); } // used to populate player select inputs
	@ModelAttribute("seasons") public List<Season> seasons() { return seasonService.seasonList(); } // used to populate season select inputs
	@ModelAttribute("baseS") public int baseS() { return 20162017; } // holds base season for stat transformation - the season to transform
	@ModelAttribute("targetS") public int targetS() { return 20162017; } // holds target season for stat transformation - the production levels to transform to
	@ModelAttribute("min") public int min() { return 20; } // used to filter results by a minimum number of player games played
	@ModelAttribute("sort") public String sort() { return "pts"; } // used to hold the column currently used to sort the data
	@ModelAttribute("pageNum") public int pageNum() { return 1; } // used to hold the current table data page - pagination
	@ModelAttribute("players") public List<ConvertedSkater> players() { return transformStats(skaterService.skaterList(20162017, 20), 20162017, 20162017); } // holds the skater transformation result sets
	@ModelAttribute("goalies") public List<ConvertedGoalie> goalies() { return transformGoalies(goalieService.goalieList(20162017, 20), 20162017, 20162017); } // holds the goalie transformation result sets
	@ModelAttribute("searchableplayers") // ensures player bio data is only accessed once and can then be searched by playerid
	public Map<Integer, Player> searchableplayers(@ModelAttribute("playerbios") List<Player> playerbios) {  // retrieves all player bios from db
		Map<Integer, Player> players = new HashMap<Integer, Player>(); // creates new hashmap with playerid as key and player bio as value
		for(Player p : playerbios) players.put(p.getPlayerid(), p); // adds each player bio to map with key added
		return players; // returns new player bio hashmap
	} // searchableplayers()
	
	@RequestMapping({"/welcome"}) // handles initial loading of the application
	public String welcome(@ModelAttribute("seasons") List<Season> seasons, @ModelAttribute("baseS") int baseS, @ModelAttribute("targetS") int targetS, @ModelAttribute("min") int min) {
		return "welcome";
	} // welcome()
	
	@RequestMapping({"/intro"}) // handles initial loading of the application
	public String intro(@ModelAttribute("seasons") List<Season> seasons, @ModelAttribute("baseS") int baseS, @ModelAttribute("targetS") int targetS, @ModelAttribute("min") int min) {
		return "intro";
	} // intro()
	
	@RequestMapping({"/timemachine"}) // handles initial loading of the application
	public String timemachine(@ModelAttribute("seasons") List<Season> seasons, @ModelAttribute("baseS") int baseS, @ModelAttribute("targetS") int targetS, @ModelAttribute("min") int min) {
		return "timemachine";
	} // timemachine()
	
	@RequestMapping(value = "/skaters") // handles view for skater transformation menu with default values
	public String skaters(Model model) {
		model.addAttribute("baseS", "20162017");
		model.addAttribute("targetS", "20162017");
		model.addAttribute("sort", "pts");
		model.addAttribute("pageNum", 1);
		model.addAttribute("min", 20);
		return "skaters";
	} // skaters()
	
	@RequestMapping(value = "/goalies") // handles view for goalie transformation menu with default values
	public String goalies(Model model) {
		model.addAttribute("baseS", "20162017");
		model.addAttribute("targetS", "20162017");
		model.addAttribute("sort", "pts");
		model.addAttribute("pageNum", 1);
		model.addAttribute("min", 20);
		return "goalies";
	} // goalies()
	
	@RequestMapping(value = "/comparator") // handles view for player comparator menu with default values
	public String comparator(Model model, @ModelAttribute("playerbios") List<Player> playerbios, @ModelAttribute("searchableplayers") Map<Integer, Player> searchableplayers) {
		model.addAttribute("baseS", "20162017");
		model.addAttribute("p1", searchableplayers.get(8478550));
		model.addAttribute("p2", searchableplayers.get(8476453));
		model.addAttribute("sort", "pts");
		model.addAttribute("pageNum", 1);
		return "comparator";
	}// comparator()
	
	@RequestMapping(value = "/skatertable", method = RequestMethod.GET) // handles view for the presentation of skater transformation data
	public String skatertable(@RequestParam Map<String, String> params, Model model, @ModelAttribute("players") List<ConvertedSkater> players, @ModelAttribute("seasons") List<Season> seasons) {
		int targetSeason = Integer.parseInt(params.get("targetSeason")); // retrieves target season for transformation from get request
		int min = Integer.parseInt(params.get("min")); // retrieves minimum games played for filtering the transformation from get request
		int baseSeason; // will hold user-specified base season
		if (params.get("baseSeason").equals("0")) { // retrieves base season for transformation from get request
			baseSeason = 0; // value used when the user wishes to transform skater data from all available seasons
			players = new ArrayList<ConvertedSkater>(); // prepares for new result set
			for (Season s : seasons) // gets skater data for each season
				players.addAll(transformStats(skaterService.skaterList(s.getSeasonid(), min), s.getSeasonid(), targetSeason)); // transforms skater data
		} // if (season==0)
		else { // handles individual season transformation requests
			baseSeason = Integer.parseInt(params.get("baseSeason")); // retrieves requested base season from get
			players = transformStats(skaterService.skaterList(baseSeason, min), baseSeason, targetSeason); // transforms base season skater data
			if (params.containsKey("include")) { // determines whether the user has requested to include skater data from target season for comparison
				players.addAll(transformStats(skaterService.skaterList(targetSeason, min), targetSeason, targetSeason)); // transforms target season skater data
			} // if (include)
		} // else
		sortSkaters(players, params.get("sort")); // retrieves user-specified sort column and sorts skater data
		model.addAttribute("players", players); // adds newly created skater transformation data to model
		model.addAttribute("skaters", players.subList(0, 25)); // limits data to first 25 results - used for pagination
		model.addAttribute("baseS", baseSeason); // sets user-specified base season to model
		model.addAttribute("targetS", targetSeason); // sets user-specified target season to model
		model.addAttribute("min", min); // sets user-specified minimum games played for filtering to model
		return "skatertable"; // directs to skater table view
	}// skatertable()
	
	@RequestMapping(value = "/goalietable", method = RequestMethod.GET) // handles view for the presentation of goalie transformation data
	public String goalietable(@RequestParam Map<String, String> params, Model model, @ModelAttribute("goalies") List<ConvertedGoalie> goalies, @ModelAttribute("seasons") List<Season> seasons) {
		int targetSeason = Integer.parseInt(params.get("targetSeason")); // retrieves target season for transformation from get request
		int min = Integer.parseInt(params.get("min")); // retrieves minimum games played for filtering the transformation from get request
		int baseSeason; // will hold user-specified base season
		if (params.get("baseSeason").equals("0")) { // retrieves base season for transformation from get request
			baseSeason = 0; // values used when the user wishes to transform goalie data from all available seasons
			goalies = new ArrayList<ConvertedGoalie>(); // prepares for new result set
			for (Season s : seasons) // gets skater data for each season
				goalies.addAll(transformGoalies(goalieService.goalieList(s.getSeasonid(), min), s.getSeasonid(), targetSeason)); // transforms goalie data
		} // if (season==0)
		else { // handles individual season transformation requests
			baseSeason= Integer.parseInt(params.get("baseSeason")); // retrieves requested base season from get
			goalies = transformGoalies(goalieService.goalieList(baseSeason, min), baseSeason, targetSeason); // transforms base season goalie data
			if (params.containsKey("include")) { // determines whether the user has requested to include goalie data from target season for comparison
				goalies.addAll(transformGoalies(goalieService.goalieList(targetSeason, min), targetSeason, targetSeason)); // transforms target season goalie data
			} // if (include)
		} // else
		sortGoalies(goalies, params.get("sort")); // retrieves user-specified sort column and sorts goalie data
		model.addAttribute("goalies", goalies); // adds newly created goalie transformation data to model
		model.addAttribute("current", goalies.subList(0, 25)); // limits data to first 25 results - used for pagination
		model.addAttribute("baseS", baseSeason); // sets user-specified base season to model
		model.addAttribute("targetS", targetSeason); // sets user-specified target season to model
		model.addAttribute("min", min); // sets user-specified minimum games played for filtering to model
		return "goalietable"; // directs to goalie table view
	} // goalietable()
	
	@RequestMapping(value = "/validatecomparator", method = RequestMethod.GET)
	public @ResponseBody String validatecomparator(@RequestParam("player1") String player1, @RequestParam("player2") String player2, @ModelAttribute("searchableplayers") Map<Integer, Player> searchableplayers) {
		String result;
		Player p1 = searchableplayers.get(Integer.parseInt(player1)); // retrieves user-specified player to be used for comparison
		Player p2 = searchableplayers.get(Integer.parseInt(player2)); // retrieves user-specified player to be used for comparison
		System.out.println("In Java methopd:" + p1.getPlayerName());
		if ((p1.getPosition().equals("G") && !p2.getPosition().equals("G")) || (p2.getPosition().equals("G") && !p1.getPosition().equals("G"))) { // handles incomparable data
			 result = "false";
		} // if (incomparable data)
		result = "true";
		return result;
	} // validatecomparator()
	
	@RequestMapping(value = "/comptable", method = RequestMethod.GET) // handles view for the presentation of player comparison data
	public String comptable(HttpServletRequest request, @RequestParam Map<String, String> params, Model model, @ModelAttribute("players") List<ConvertedSkater> players, @ModelAttribute("goalies") List<ConvertedGoalie> goalies, @ModelAttribute("searchableplayers") Map<Integer, Player> searchableplayers) {
		int baseSeason = Integer.parseInt(params.get("baseSeason")); // retrieves user-specified base season to be used for player comparison
		Player p1 = searchableplayers.get(Integer.parseInt(params.get("player1"))); // retrieves user-specified player to be used for comparison
		Player p2 = searchableplayers.get(Integer.parseInt(params.get("player2"))); // retrieves user-specified player to be used for comparison
		if ((p1.getPosition().equals("G") && !p2.getPosition().equals("G")) || (p2.getPosition().equals("G") && !p1.getPosition().equals("G"))) { // handles incomparable data
			String referer = request.getHeader("Referer");
			return "redirect:" + referer;
		} // if (incomparable data)
		if (!p1.getPosition().equals("G")) { // handles skater comparisons
{'fraction_non_alphanumeric': 0.11892037371679035, 'fraction_numerical': 0.014072051982006228, 'mean_word_length': 5.54833836858006, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, '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': '5558973', 'n_tokens_mistral': 7573, 'n_tokens_neox': 6908, 'n_words': 2196}