SessionHandler.java 6.21 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
/*

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

import java.util.EnumMap;
import java.util.Locale;
import java.util.TimeZone;

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

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

import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.RoleBeanLocal;
import fi.codecrew.moya.clientutils.BortalLocalContextHolder;
import fi.codecrew.moya.enums.apps.IAppPermission;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEventProperty;
import fi.codecrew.moya.model.LanEventPropertyKey;

/**
 * 
 * @author tuukka
 */
@Named("mgmtSessionHandler")
@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;

	@Inject
	private SessionStore sessionStore;

	private String template;

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

	}

	public String getLocale() {

		Locale ret = sessionStore.getLocale();
		String retStr = "fi_FI";
		if (ret != null) {
			retStr = ret.toLanguageTag();
			logger.info("Got langtag {}", retStr);
		}
		return retStr;
	}

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

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

	public String getTemplateName()
	{
		if (template == null) {
			template = eventbean.getPropertyString(LanEventPropertyKey.EVENT_LAYOUT);
		}
		if (template == null) {
			template = "insomnia2";

		}
		return template;
	}

	public String getTemplatePath()
	{

		return "/resources/templates/" + getTemplateName();

	}

	public String getTemplate() {
		return getTemplatePath() + "/template.xhtml";
	}

	private EnumMap<LanEventPropertyKey, Boolean> boolPropertyCache = new EnumMap<>(LanEventPropertyKey.class);

	public boolean isEventBoolProperty(String property)
	{
		LanEventPropertyKey prop = LanEventPropertyKey.valueOf(property);
		if (!prop.isBoolean()) {
			throw new RuntimeException("Trying to fetch boolean value for non-boolean property!");
		}
		boolean value = false;
		if (boolPropertyCache.containsKey(prop)) {
			value = boolPropertyCache.get(prop);
		} else {
			LanEventProperty propertyValue = eventbean.getProperty(prop);
			if (propertyValue == null) {
				if (prop.getDefaultvalue() != null && prop.getDefaultvalue().equals("1"))
					value = true;
			} else {
				value = propertyValue.isBooleanValue();
			}
			boolPropertyCache.put(prop, value);
		}
		return value;
	}

	// 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 String getShortDatetimeFormat()
	{
		return "dd.MM HH:MM:ss";
	}

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

	public SessionStore getSessionStore() {
		return sessionStore;
	}

	public void setSessionStore(SessionStore sessionStore) {
		this.sessionStore = sessionStore;
	}

}