SessionHandler.java 4.21 KB
/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fi.insomnia.bortal.handler;

import java.util.TimeZone;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.beans.EventBeanLocal;
import fi.insomnia.bortal.beans.PermissionBeanLocal;
import fi.insomnia.bortal.beans.RoleBeanLocal;
import fi.insomnia.bortal.clientutils.BortalLocalContextHolder;
import fi.insomnia.bortal.enums.apps.IAppPermission;
import fi.insomnia.bortal.model.EventUser;
import fi.insomnia.bortal.model.LanEventPropertyKey;

/**
 * 
 * @author tuukka
 */
@Named()
@RequestScoped
public class SessionHandler {

	private static final Logger logger = LoggerFactory.getLogger(SessionHandler.class);
	//
	// @Inject
	// private HttpServletRequest httprequest;
	//
	@EJB
	private RoleBeanLocal rolebean;
	@EJB
	private EventBeanLocal eventbean;
	@EJB
	private PermissionBeanLocal permbean;

	private String template;

	public TimeZone getTimezone() {
		return TimeZone.getTimeZone("Europe/Helsinki");
	}

	public String getLocale() {
		// TODO: Locale selection code missing
		// return "en_ST_v7";
		String ret = eventbean.getCurrentEvent().getOrganiser().getBundleCountry();
		if (ret == null || ret.isEmpty())
		{
			ret = "fi_FI";
		}
		return ret;
	}

	public String getFullscreen() {
		template = "blipview";
		return template;
	}

	public String getAdduserfullscreen() {
		template = "adduser";
		return template;
	}

	public String getLayout() {
		if (template == null) {
			template = eventbean.getPropertyString(LanEventPropertyKey.EVENT_LAYOUT);
		}
		if (template == null) {
			template = "template1";
		}
		return template;
	}

	// public boolean hasPermission(String target, String permission) {
	// RolePermission perm = RolePermission.valueOf(permission.toUpperCase());
	// // RolePermission perm = null;
	// // if (permission.equalsIgnoreCase("read")) {
	// // perm = RolePermission.READ;
	// // } else if (permission.equals("write")) {
	// // perm = RolePermission.WRITE;
	// // } else if (permission.equals("execute")) {
	// // perm = RolePermission.EXECUTE;
	// // }else {
	// // throw new RuntimeException("permission " + permission +
	// // " does not match any")
	// // }
	// if (perm == null) {
	// logger.warn("Permission {} does not have matching value in RolePermission enum!");
	// throw new
	// RuntimeException("Matching role permission could not be found!");
	// }
	//
	// return hasPermission(target, perm);
	// }

	//
	// private HttpSession getHttpSession() {
	// FacesContext ctx = FacesContext.getCurrentInstance();
	// HttpSession sess = (HttpSession)
	// ctx.getExternalContext().getSession(false);
	// return sess;
	// }

	// public boolean hasPermission(String perm) {
	// return permbean.hasPermission(perm);
	// }

	public String getDateFormat()
	{
		return "dd.MM.yyyy";
	}

	public String getDatetimeFormat()
	{
		return "dd.MM.yyyy HH:mm";
	}

	public boolean hasPermission(IAppPermission permission) {
		if (permission == null) {
			logger.warn("permission is null");
			throw new RuntimeException("Empty target or permission!");
		}
		boolean ret = permbean.hasPermission(permission);

		return ret;
	}

	public boolean isLoggedIn() {

		boolean ret = permbean.isLoggedIn();
		return ret;
	}

	public boolean isSuperadmin() {
		return permbean.getCurrentUser().getUser().isSuperadmin();

	}

	public EventUser getCurrentUser() {
		return permbean.getCurrentUser();
	}

	public String flushCache() {
		return eventbean.flushCache();

	}

	public String navigateTo(String url) {
		return url;
	}

	private String preurlString;

	public String getRequestPreUrl()
	{
		if (preurlString == null)
		{
			Object ext = FacesContext.getCurrentInstance().getExternalContext().getRequest();
			if (ext instanceof HttpServletRequest)
			{
				StringBuffer url = ((HttpServletRequest) ext).getRequestURL();

				preurlString = url.substring(0, url.indexOf("/", 8));

			} else {
				preurlString = "";
			}
		}
		return preurlString;
	}

	public boolean isInDevelopmentMode() {
		return BortalLocalContextHolder.isInDevelopmentMode();
	}

}