UserGameIDView.java 2.85 KB
package fi.codecrew.moya.web.cdiview.user;

import java.util.HashMap;
import java.util.List;

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

import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.TournamentBeanLocal;
import fi.codecrew.moya.beans.UserBeanLocal;
import fi.codecrew.moya.enums.apps.TournamentPermission;
import fi.codecrew.moya.enums.apps.UserPermission;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.GameID;
import fi.codecrew.moya.model.Tournament;
import fi.codecrew.moya.model.TournamentGame;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.utilities.jsf.MessageHelper;
import fi.codecrew.moya.web.cdiview.GenericCDIView;

@Named
@ConversationScoped
public class UserGameIDView extends GenericCDIView {
	private static final long serialVersionUID = -1154119029203251525L;

	@EJB private UserBeanLocal userBean;
	@EJB private TournamentBeanLocal tournamentBean;
	@EJB private PermissionBeanLocal permissionBean;
	private EventUser eventUser;
	private TournamentGame gameToAddIdTo;
	private String identifier;
	private List<TournamentGame> games;
	
	public void initView() {
		if(this.eventUser == null) {
			this.beginConversation();
			this.eventUser = permissionBean.getCurrentUser();
		}
		
		if(super.requirePermissions(UserPermission.VIEW_SELF) && super.requirePermissions(TournamentPermission.VIEW)) {
			this.setGameToAddIdTo(null);
			refreshGames();
		}
	}
	
	public void addGameID() {
		if(gameToAddIdTo != null && identifier != null && identifier.trim().length() > 0) {
			userBean.addGameID(gameToAddIdTo, identifier);
			this.setGameToAddIdTo(null);
			this.eventUser = permissionBean.getCurrentUser();
			this.identifier = null;
			refreshGames();
			
			MessageHelper.info("user.game.gameid_added");
		}
	}
	
	public void removeGameID(String gameIdIdStr) {
		Integer gameIdId = Integer.parseInt(gameIdIdStr);
		userBean.removeGameIdById(gameIdId);
		this.eventUser = permissionBean.getCurrentUser();
		refreshGames();
		
		MessageHelper.info("user.game.gameid_removed");
	}
	
	private void refreshGames() {
		this.games = tournamentBean.getGames();
		for(GameID gi : eventUser.getGameIDs()) {
			if(this.games.contains(gi.getGame())) this.games.remove(gi.getGame());
		}
	}

	public EventUser getEventUser() {
		return eventUser;
	}

	public void setUser(EventUser eventUser) {
		this.eventUser = eventUser;
	}

	public List<TournamentGame> getGames() {
		return games;
	}

	public void setGames(List<TournamentGame> games) {
		this.games = games;
	}

	public TournamentGame getGameToAddIdTo() {
		return gameToAddIdTo;
	}

	public void setGameToAddIdTo(TournamentGame gameToAddIdTo) {
		this.gameToAddIdTo = gameToAddIdTo;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}
}