Package com.fabelist.view.page

Source Code of com.fabelist.view.page.AbstractPage

package com.fabelist.view.page;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fabelist.view.bean.UserBean;

/**
*
* @author Hasan Keklik
*
*/
public abstract class AbstractPage implements Serializable {

  private static final long serialVersionUID = 1L;

  public AbstractPage() {
  }

  protected FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
  }

  protected Application getApplication() {
    return FacesContext.getCurrentInstance().getApplication();
  }

  protected ExternalContext getExternalContext() {
    return FacesContext.getCurrentInstance().getExternalContext();
  }

  protected ELContext getElContext() {
    return FacesContext.getCurrentInstance().getELContext();
  }

  protected ServletContext getServletContext() {
    return (ServletContext) FacesContext.getCurrentInstance()
        .getExternalContext().getContext();
  }

  protected UIViewRoot getViewRoot() {
    return FacesContext.getCurrentInstance().getViewRoot();
  }

  protected Map<String, Object> getApplicationMap() {
    return getExternalContext().getApplicationMap();
  }

  protected Map<String, Object> getRequestMap() {
    return getExternalContext().getRequestMap();
  }

  protected Map<String, Object> getSessionMap() {
    return getExternalContext().getSessionMap();
  }

  protected Object getBean(String objectName) {
    ELContext elContext = getElContext();
    return elContext.getELResolver().getValue(elContext, null, objectName);
  }

  protected void storeOnSession(String key, Object object) {
    getSessionMap().put(key, object);
  }

  protected void storeOnRequest(String key, Object object) {
    getRequestMap().put(key, object);
  }

  protected Map<String, String> getRequestParameterMap() {
    return getExternalContext().getRequestParameterMap();
  }

  protected ValueExpression getValueExpression(String expression) {
    ELContext elContext = getElContext();
    return getApplication().getExpressionFactory().createValueExpression(
        elContext, expression, null);
  }

  protected Object getValue(String expression) {
    ELContext elContext = getElContext();
    return getValueExpression(expression).getValue(elContext);
  }

  protected void setValue(String expression, Object value) {
    ELContext elContext = getElContext();
    getValueExpression(expression).setValue(elContext, value);
  }

  protected boolean isPostBack() {
    FacesContext context = getFacesContext();
    return getFacesContext().getRenderKit().getResponseStateManager()
        .isPostback(context);
  }

  protected Lifecycle getLifecycle() {
    String lifecycleId = getExternalContext().getInitParameter(
        "javax.faces.LIFECYCLE_ID");
    if (lifecycleId == null || lifecycleId.length() == 0) {
      lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE;
    }
    LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
        .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    return lifecycleFactory.getLifecycle(lifecycleId);
  }

  protected void navigate(String outcome) {
    FacesContext context = getFacesContext();
    context.getApplication().getNavigationHandler()
        .handleNavigation(context, null, outcome);
  }

  protected void erase() {
    erase(getFacesContext().getViewRoot());
  }

  private void erase(UIComponent component) {
    if (component instanceof EditableValueHolder) {
      ((EditableValueHolder) component).setSubmittedValue(null);
    }
    Iterator<UIComponent> kids = component.getFacetsAndChildren();
    while (kids.hasNext()) {
      erase(kids.next());
    }
  }

  protected void log(String message) {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
      getExternalContext().log(message);
    } else {
      System.out.println(message);
    }
  }

  protected void log(String message, Throwable throwable) {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
      getExternalContext().log(message, throwable);
    } else {
      System.out.println(message);
      throwable.printStackTrace(System.out);
    }
  }

  protected void info(String summary) {
    addMessage(null, FacesMessage.SEVERITY_INFO, summary);
  }

  protected void info(UIComponent component, String summary) {
    addMessage(component.getClientId(getFacesContext()),
        FacesMessage.SEVERITY_INFO, summary);
  }

  protected void warn(String summary) {
    addMessage(null, FacesMessage.SEVERITY_WARN, summary);
  }

  protected void warn(UIComponent component, String summary) {
    addMessage(component.getClientId(getFacesContext()),
        FacesMessage.SEVERITY_WARN, summary);
  }

  protected void error(String summary) {
    addMessage(null, FacesMessage.SEVERITY_ERROR, summary);
  }

  protected void error(Exception e) {
    addMessage(null, FacesMessage.SEVERITY_ERROR, e.toString());
  }

  protected void error(UIComponent component, String summary) {
    addMessage(component.getClientId(getFacesContext()),
        FacesMessage.SEVERITY_ERROR, summary);
  }

  protected void fatal(String summary) {
    addMessage(null, FacesMessage.SEVERITY_FATAL, summary);
  }

  protected void fatal(UIComponent component, String summary) {
    addMessage(component.getClientId(getFacesContext()),
        FacesMessage.SEVERITY_FATAL, summary);
  }

  protected void addMessage(String cientId, Severity severity, String message) {
    getFacesContext().addMessage(cientId,
        new FacesMessage(severity, message, null));
    storeOnRequest("showMessages", true);
  }

  protected HttpServletRequest getRequest() {
    return (HttpServletRequest) getExternalContext().getRequest();
  }

  protected HttpServletResponse getResponse() {
    return (HttpServletResponse) getExternalContext().getResponse();
  }

  protected String getResource(String key) {
    Application application = getApplication();
    Locale loc = application.getDefaultLocale();
    UserBean uBean = (UserBean) getBean("userBean");
    if (uBean != null && uBean.getLocale() != null) {
      loc = new Locale(uBean.getLocale());

      application.setDefaultLocale(loc);
    }
    return getResource(application.getMessageBundle(), key, loc);
  }

  protected static ClassLoader getCurrentClassLoader(Object defaultObject) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (loader == null)
      loader = defaultObject.getClass().getClassLoader();
    return loader;
  }

  public static String getResource(String bundleName, String key,
      Locale locale) {
    String text = null;
    ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
    try {
      text = bundle.getString(key);
    } catch (MissingResourceException e) {
      text = "?? key " + key + " not found ??";
    }
    return text;
  }

}
TOP

Related Classes of com.fabelist.view.page.AbstractPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.