LayoutView.java 6.68 KB
package fi.codecrew.moya.web.helper;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

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.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.MenuBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.enums.apps.ContentPermission;
import fi.codecrew.moya.handler.SessionStore;
import fi.codecrew.moya.model.LanEventProperty;
import fi.codecrew.moya.model.LanEventPropertyKey;
import fi.codecrew.moya.model.MenuNavigation;

@Named
@RequestScoped
public class LayoutView implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4468645109141859301L;
	@Inject
	private FacesContext context;
	private ResourceBundle rb;
	private String pagename;
	@EJB
	private transient EventBeanLocal eventbean;
	@EJB
	private transient PermissionBeanLocal permbean;
	private StreamedContent headerimage;
	private String headertext;
	@Inject
	private SessionStore sessionStore;
	@EJB
	private transient MenuBeanLocal menubean;

	private static final Logger logger = LoggerFactory.getLogger(LayoutView.class);

	public void init() {
		logger.info("Initialized layoutView");
	}

	public boolean isManageContent() {
		return sessionStore.isManageContentLinks() && permbean.hasPermission(ContentPermission.MANAGE_PAGES);
	}

	public boolean isCanManageContent() {
		return permbean.hasPermission(ContentPermission.MANAGE_PAGES);
	}

	private Set<MenuNavigation> selectedSet;

	public Set<MenuNavigation> getSelectedSet()
	{
		if (selectedSet == null) {
			getSelectedTopmenu();
		}
		return selectedSet;
	}

	private MenuNavigation selectedTop;

	public MenuNavigation getSelectedTopmenu()
	{
		if (selectedTop == null)
		{
			selectedSet = new HashSet<>();
			selectedTop = menubean.findNavigation(getPagepath());
			if (selectedTop == null)
				return null;

			while (selectedTop.getParent() != null) {
				logger.info("Traversing to top {}, key {}", selectedTop, selectedTop.getKey());
				selectedSet.add(selectedTop);
				selectedTop = selectedTop.getParent();
			}
			selectedSet.add(selectedTop);

			List<MenuNavigation> tops = menubean.getTopmenus();

			if (!tops.contains(selectedTop) && !tops.isEmpty()) {
				selectedTop = tops.get(0);
			}
			selectedSet = Collections.unmodifiableSet(selectedSet);
		}
		return selectedTop;
	}

	public String getPagepath() {
		if (pagename == null &&
				context != null &&
				context.getExternalContext() != null &&
				context.getExternalContext().getRequest() != null &&
				context.getExternalContext().getRequest() instanceof HttpServletRequest) {

			HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
			String[] splitted = req.getServletPath().split("\\.");
			if (splitted.length > 0) {
				pagename = splitted[0];
			}
		}
		return pagename;
	}

	private String localize(String key) {
		String value = null;
		try {
			value = getResourcebundle().getString(key);
		} catch (MissingResourceException e) {
			value = null;
		}

		if (key == null) {
			value = "########";
		} else if (value == null) {
			value = "???" + key + "???";

		}
		return value;

	}

	private ResourceBundle getResourcebundle() {
		if (rb == null) {
			rb = context.getApplication().getResourceBundle(context, "i18n");
		}
		return rb;
	}

	public String getHeader() {

		return new StringBuilder().append(eventbean.getCurrentEvent().getName()).append(" - ")
				.append(localize(new StringBuilder("submenu").append(getPagepath().replace('/', '.')).toString())).toString();

	}

	// public String header() {
	// logger.info("Context {}", context);
	// UIViewRoot root = context.getViewRoot();
	// Map<String, Object> viewmap = root.getViewMap(false);
	// if (viewmap != null) {
	// for (Entry<String, Object> entry : viewmap.entrySet()) {
	// logger.info("Got entry {}: {}", entry.getKey(), entry.getValue());
	// }
	// }
	// logger.info("Attrmap");
	// if (context.getAttributes() != null) {
	// for (Entry<Object, Object> e : context.getAttributes().entrySet()) {
	// logger.info("Got attr {}: {}", e.getKey(), e.getValue());
	// }
	// }
	// return "";
	// }

	public boolean getHeaderIsImage()
	{
		return getHeaderimage() != null;
	}

	public StreamedContent getHeaderimage() {
		if (headertext == null && headerimage == null)
		{
			LanEventProperty logo = eventbean.getProperty(LanEventPropertyKey.EVENT_LOGO);
			if (logo != null)
			{
				if (logo.getByteMime() == null || logo.getByteValue() == null) {
					headertext = logo.getTextvalue();
				} else {
					headerimage = new DefaultStreamedContent(new ByteArrayInputStream(logo.getByteValue()), logo.getByteMime());
				}
			} else {
				headertext = "logo_not_set";
			}
		}
		return headerimage;
	}

	public void setHeaderimage(StreamedContent headerimage) {
		this.headerimage = headerimage;
	}

	public String getHeadertext() {
		return headertext;
	}

	public void setHeadertext(String headertext) {
		this.headertext = headertext;
	}

	public void setPageName(String menuChange) {
		this.pagename = menuChange;
		selectedTop = null;
	}

	public String getLayoutProperty1(String value) {
		LanEventProperty val = eventbean.getProperty(LanEventPropertyKey.TEMPLATE_PROPERTY1);
		if (val != null) {
			value = val.getTextvalue();
		}
		return value;
	}

	public String getLayoutProperty2(String value) {
		LanEventProperty val = eventbean.getProperty(LanEventPropertyKey.TEMPLATE_PROPERTY2);
		if (val != null) {
			value = val.getTextvalue();
		}
		return value;
	}

	public String getLayoutProperty3(String value) {
		LanEventProperty val = eventbean.getProperty(LanEventPropertyKey.TEMPLATE_PROPERTY3);
		if (val != null) {
			value = val.getTextvalue();
		}
		return value;
	}

	public String getLayoutProperty4(String value) {
		LanEventProperty val = eventbean.getProperty(LanEventPropertyKey.TEMPLATE_PROPERTY4);
		if (val != null) {
			value = val.getTextvalue();
		}
		return value;
	}

	public String getLayoutProperty5(String value) {
		LanEventProperty val = eventbean.getProperty(LanEventPropertyKey.TEMPLATE_PROPERTY5);
		if (val != null) {
			value = val.getTextvalue();
		}
		return value;
	}

	public SessionStore getSessionStore() {
		return sessionStore;
	}

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

}