TournamentCreateView.java 4.82 KB
package fi.codecrew.moya.web.cdiview.tournaments;

import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;

import org.primefaces.event.FlowEvent;

import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.TournamentBeanLocal;
import fi.codecrew.moya.enums.TournamentStatus;
import fi.codecrew.moya.enums.TournamentType;
import fi.codecrew.moya.enums.apps.TournamentPermission;
import fi.codecrew.moya.model.Tournament;
import fi.codecrew.moya.model.TournamentGame;
import fi.codecrew.moya.model.TournamentRule;
import fi.codecrew.moya.utilities.jsf.MessageHelper;
import fi.codecrew.moya.web.cdiview.GenericCDIView;

@Named
@ConversationScoped
public class TournamentCreateView extends GenericCDIView {
	private static final long serialVersionUID = 2547358764980373797L;

	private List<TournamentGame> tournamentGames;
	private List<TournamentRule> tournamentRules;

	private TournamentRule rules = null;
	private TournamentGame game = null;
	private Tournament tournament = null;

	private String tournamentGameName;
	private String tournamentGameDescription;

	private String rulesetName;
	private String rulesetDescription;
	private String rulesetRules;

	private Integer backupPlayers;

	@EJB
	TournamentBeanLocal tournamentBean;
	@EJB
	EventBeanLocal eventBean;

	public void initView() {
		if (super.requirePermissions(TournamentPermission.MANAGE_ALL) && tournament == null) {
			this.beginConversation();
			tournament = new Tournament();
			tournament.setPlayersPerMatch(0);
			tournament.setMaxParticipants(0);
			backupPlayers = 0;
			tournamentGames = tournamentBean.getGames();
		}
	}

	public String save() {
		tournament.setPlayersPerTeam(tournament.getPlayersPerMatch() + backupPlayers);
		tournament.setLanEvent(eventBean.getCurrentEvent());
		tournament.setTournamentStatus(TournamentStatus.SETUP);
		tournament.setTournamentGame(game);
		tournament.setRules(rules);

		try {
			tournamentBean.createTournament(tournament);
			this.endConversation();
			return "success";
		} catch (Exception e) {
			MessageHelper.err(e.getMessage());
			return "";
		}
	}

	public List<TournamentGame> getTournamentGames() {
		return tournamentGames;
	}

	public List<TournamentRule> getTournamentRules() {
		return tournamentRules;
	}

	public void uploadListener(org.primefaces.event.FileUploadEvent event) {
		System.out.println("ZZ");
	}

	public TournamentType[] getTournamentTypes() {
		TournamentType[] items = TournamentType.values();
		return items;
	}

	public String onFlowProcess(FlowEvent event) {
		switch (event.getOldStep()) {
		case "selectGame":
			if (tournamentGameName.length() > 0) {
				// oh lurd, we want to create a new gamy now
				TournamentGame tg = new TournamentGame();
				tg.setName(tournamentGameName);
				tg.setDescription(tournamentGameDescription);
				tg.setLanEvent(eventBean.getCurrentEvent());

				game = tournamentBean.createGame(tg);
			}

			tournamentRules = tournamentBean.getRulesByGame(game);
			break;
		case "selectRuleset":
			if (rulesetName != null && rulesetName.length() > 0) {
				TournamentRule tr = new TournamentRule();
				tr.setName(rulesetName);
				tr.setDescription(rulesetDescription);
				tr.setRules(rulesetRules);
				tr.setTournamentGame(game);
				rules = tournamentBean.createRule(tr);

				tournamentRules = tournamentBean.getRulesByGame(game);
			}
			break;
		}
		return event.getNewStep();
	}

	public TournamentGame getGame() {
		return game;
	}

	public void setGame(TournamentGame game) {
		this.game = game;
	}

	public TournamentRule getRules() {
		return rules;
	}

	public void setRules(TournamentRule rules) {
		this.rules = rules;
	}

	public String getTournamentGameName() {
		return tournamentGameName;
	}

	public void setTournamentGameName(String tournamentGameName) {
		this.tournamentGameName = tournamentGameName;
	}

	public String getTournamentGameDescription() {
		return tournamentGameDescription;
	}

	public void setTournamentGameDescription(String tournamentGameDescription) {
		this.tournamentGameDescription = tournamentGameDescription;
	}

	public String getRulesetName() {
		return rulesetName;
	}

	public void setRulesetName(String rulesetName) {
		this.rulesetName = rulesetName;
	}

	public String getRulesetDescription() {
		return rulesetDescription;
	}

	public void setRulesetDescription(String rulesetDescription) {
		this.rulesetDescription = rulesetDescription;
	}

	public Tournament getTournament() {
		return tournament;
	}

	public void setTournament(Tournament tournament) {
		this.tournament = tournament;
	}

	public Integer getBackupPlayers() {
		return backupPlayers;
	}

	public void setBackupPlayers(Integer backupPlayers) {
		this.backupPlayers = backupPlayers;
	}

	public String getRulesetRules() {
		return rulesetRules;
	}

	public void setRulesetRules(String rulesetRules) {
		this.rulesetRules = rulesetRules;
	}
}