// 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;
	}

	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);

		if (communityName.equals("test")||  communityName.equals("localhost")) {
			chain.doFilter(request, response);
			return;
		}
		
		if ((requestURI.endsWith("/") || requestURI.endsWith(".html")) && ! ( requestURI.startsWith( currentPhase.getWelcomePage() ))) {
			HttpServletResponse servletResponse = (HttpServletResponse) response;
			LOGGER.info("Redirecting request {} to {}", requestURI, currentPhase.getWelcomePage() );
			String url = String.format("https://%s%s?phaseEnd=%s", request.getServerName(), currentPhase.getWelcomePage(), new SimpleDateFormat("yyyy/MM/dd").format(currentPhase.getPhaseEnd()));
			servletResponse.sendRedirect(url);
// 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;

	}

	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);

		if (communityName.equals("test")||  communityName.equals("localhost")) {
			chain.doFilter(request, response);
			return;
		}
		
		if ((requestURI.endsWith("/") || requestURI.endsWith(".html")) && ! ( requestURI.startsWith( currentPhase.getWelcomePage() ))) {
			HttpServletResponse servletResponse = (HttpServletResponse) response;
			LOGGER.info("Redirecting request {} to {}", requestURI, currentPhase.getWelcomePage() );
			String url = String.format("https://%s%s?phaseEnd=%s", request.getServerName(), currentPhase.getWelcomePage(), new SimpleDateFormat("yyyy/MM/dd").format(currentPhase.getPhaseEnd()));
			servletResponse.sendRedirect(url);
		} else {