// Path: predictor/src/main/java/predictions/model/db/Community.java
// public class Community {
// 
// 	@JsonProperty
// 	private String name;
// 
// 	@JsonProperty
// 	private boolean createAccountEnabled = true;
// 
// 	@JsonProperty
// 
// 	private ZonedDateTime openingDate = null;
// 
// 	@JsonProperty
// 	private AccessType groupsAccess = AccessType.N;
// 
// 	@JsonProperty
// 	private AccessType finalsAccess = AccessType.N;
// 
// 	public Community() {
// 		
// 	}
// 
// 	public Community(String name, boolean createAccountEnabled, ZonedDateTime openingDate, AccessType groupsAccess, AccessType finalsAccess) {
// 		super();
// 		this.name = name;
// 		this.openingDate = openingDate;
// 		this.createAccountEnabled = createAccountEnabled;
// 		this.groupsAccess = groupsAccess;
// 		this.finalsAccess = finalsAccess;
// 	}
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public boolean isCreateAccountEnabled() {
// 		return createAccountEnabled;
// 	}
// 
// 	public AccessType getGroupsAccess() {
// 		return groupsAccess;
// 	}
// 	
// 	public AccessType getFinalsAccess() {
// 		return finalsAccess;
// 	}
// 
// 	public ZonedDateTime getOpeningDate() {
// 		return openingDate;
// 	}
// }
// 
// Path: predictor/src/main/java/predictions/model/db/CommunityDAO.java
// @RegisterRowMapper(CommunityMapper.class)
// public interface CommunityDAO {
// 	
// 	@SqlUpdate("MERGE INTO COMMUNITY KEY(COMMUNITY_NAME) VALUES(:name, :createAccountEnabled, :openingDate, :groupsAccess, :finalsAccess)")
// 	void updateCommunity(@Bind("name") String name, @Bind("createAccountEnabled") boolean createAccountEnabled, @Bind("openingDate")ZonedDateTime openingDate, @Bind("groupsAccess") AccessType groupsAccess, @Bind("finalsAccess") AccessType finalsAccess);
// 	
// 	@SqlQuery("SELECT COMMUNITY.* FROM COMMUNITY WHERE COMMUNITY_NAME = :name")
// 	Community getCommunity(@Bind("name") String name);
// 
// }


import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import predictions.model.db.Community;
import predictions.model.db.CommunityDAO;
package predictions.phases;




public class PhaseFilter implements Filter {
	
	private final static Logger LOGGER = LoggerFactory.getLogger( PhaseFilter.class );

	private PhaseManager phaseManager;
	private CommunityDAO communityDAO;

	public PhaseFilter(PhaseManager phaseManager, CommunityDAO communityDAO) {
		this.phaseManager = phaseManager;
		this.communityDAO = communityDAO;
	}

	public void init(FilterConfig filterConfig) {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		// get current phase
		Phase currentPhase = phaseManager.getCurrentPhase();
		if (currentPhase.getWelcomePage() == null) {
			chain.doFilter(request, response);
			return;
		}

		HttpServletRequest servletRequest = (HttpServletRequest) request;
		String requestURI = servletRequest.getRequestURI();
		
		String communityName = (String) request.getAttribute("community");

		// TODO: cache communities
// Path: predictor/src/main/java/predictions/model/db/Community.java
// public class Community {
// 
// 	@JsonProperty
// 	private String name;
// 
// 	@JsonProperty
// 	private boolean createAccountEnabled = true;
// 
// 	@JsonProperty
// 
// 	private ZonedDateTime openingDate = null;
// 
// 	@JsonProperty
// 	private AccessType groupsAccess = AccessType.N;
// 
// 	@JsonProperty
// 	private AccessType finalsAccess = AccessType.N;
// 
// 	public Community() {
// 		
// 	}
// 
// 	public Community(String name, boolean createAccountEnabled, ZonedDateTime openingDate, AccessType groupsAccess, AccessType finalsAccess) {
// 		super();
// 		this.name = name;
// 		this.openingDate = openingDate;
// 		this.createAccountEnabled = createAccountEnabled;
// 		this.groupsAccess = groupsAccess;
// 		this.finalsAccess = finalsAccess;
// 	}
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public boolean isCreateAccountEnabled() {
// 		return createAccountEnabled;
// 	}
// 
// 	public AccessType getGroupsAccess() {
// 		return groupsAccess;
// 	}
// 	
// 	public AccessType getFinalsAccess() {
// 		return finalsAccess;
// 	}
// 
// 	public ZonedDateTime getOpeningDate() {
// 		return openingDate;
// 	}
// }
// 
// Path: predictor/src/main/java/predictions/model/db/CommunityDAO.java
// @RegisterRowMapper(CommunityMapper.class)
// public interface CommunityDAO {
// 	
// 	@SqlUpdate("MERGE INTO COMMUNITY KEY(COMMUNITY_NAME) VALUES(:name, :createAccountEnabled, :openingDate, :groupsAccess, :finalsAccess)")
// 	void updateCommunity(@Bind("name") String name, @Bind("createAccountEnabled") boolean createAccountEnabled, @Bind("openingDate")ZonedDateTime openingDate, @Bind("groupsAccess") AccessType groupsAccess, @Bind("finalsAccess") AccessType finalsAccess);
// 	
// 	@SqlQuery("SELECT COMMUNITY.* FROM COMMUNITY WHERE COMMUNITY_NAME = :name")
// 	Community getCommunity(@Bind("name") String name);
// 
// }


// Path: predictor/src/main/java/predictions/phases/PhaseFilter.java
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import predictions.model.db.Community;
import predictions.model.db.CommunityDAO;

package predictions.phases;




public class PhaseFilter implements Filter {
	
	private final static Logger LOGGER = LoggerFactory.getLogger( PhaseFilter.class );

	private PhaseManager phaseManager;
	private CommunityDAO communityDAO;

	public PhaseFilter(PhaseManager phaseManager, CommunityDAO communityDAO) {
		this.phaseManager = phaseManager;
		this.communityDAO = communityDAO;
	}

	public void init(FilterConfig filterConfig) {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		// get current phase
		Phase currentPhase = phaseManager.getCurrentPhase();
		if (currentPhase.getWelcomePage() == null) {
			chain.doFilter(request, response);
			return;
		}

		HttpServletRequest servletRequest = (HttpServletRequest) request;
		String requestURI = servletRequest.getRequestURI();
		
		String communityName = (String) request.getAttribute("community");

		// TODO: cache communities
		Community community = communityDAO.getCommunity(communityName);