Package br.net.woodstock.rockframework.web.faces.utils

Source Code of br.net.woodstock.rockframework.web.faces.utils.FacesContexts

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.web.faces.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.net.woodstock.rockframework.core.util.LabelSupport;
import br.net.woodstock.rockframework.core.utils.Conditions;
import br.net.woodstock.rockframework.core.utils.Enums;
import br.net.woodstock.rockframework.web.common.utils.HttpServletResponses;

public abstract class FacesContexts {

  private FacesContexts() {
    //
  }

  // Faces
  public static void addMessage(final String message) {
    FacesContexts.addMessage(FacesMessage.SEVERITY_INFO, message, null);
  }

  public static void addMessage(final Severity severity, final String message, final String detail) {
    FacesContext fc = FacesContexts.getFacesContext();
    fc.addMessage(null, new FacesMessage(severity, message, detail));
  }

  public static void addError(final Exception exception) {
    FacesContext fc = FacesContexts.getFacesContext();
    fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, exception.getMessage(), null));
  }

  public static FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
  }

  public static ResourceBundle getResourceBundle(final String name) {
    FacesContext fc = FacesContexts.getFacesContext();
    Application application = fc.getApplication();
    ResourceBundle resource = application.getResourceBundle(fc, name);
    return resource;
  }

  @SuppressWarnings({ "cast", "unchecked" })
  public static <T> T getBean(final String name, final Class<T> clazz) {
    FacesContext fc = FacesContexts.getFacesContext();
    Application app = fc.getApplication();
    ELContext el = fc.getELContext();
    ExpressionFactory factory = app.getExpressionFactory();
    ValueExpression expression = (ValueExpression) factory.createValueExpression(el, "#{" + name + "}", clazz);
    T value = (T) expression.getValue(el);
    return value;
  }

  public static String getMessage(final String name, final String key) {
    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    ResourceBundle bundle = application.getResourceBundle(context, name);
    String message = bundle.getString(key);
    return message;
  }

  // Http
  public static HttpServletRequest getRequest() {
    FacesContext fc = FacesContexts.getFacesContext();
    return (HttpServletRequest) fc.getExternalContext().getRequest();
  }

  public static HttpServletResponse getResponse() {
    FacesContext fc = FacesContexts.getFacesContext();
    return (HttpServletResponse) fc.getExternalContext().getResponse();
  }

  public static HttpSession getSession() {
    FacesContext fc = FacesContexts.getFacesContext();
    return (HttpSession) fc.getExternalContext().getSession(true);
  }

  public static ServletContext getServletContext() {
    FacesContext fc = FacesContexts.getFacesContext();
    return (ServletContext) fc.getExternalContext().getContext();
  }

  // Utils
  public static void download(final byte[] bytes, final String fileName) throws IOException {
    FacesContext fc = FacesContexts.getFacesContext();
    HttpServletResponse response = FacesContexts.getResponse();
    HttpServletResponses.download(response, bytes, fileName);
    fc.responseComplete();
  }

  // SelectItems
  public static List<SelectItem> getItemsFromEnum(final Class<? extends Enum<?>> clazz) {
    return FacesContexts.getItemsFromEnum(clazz, null);
  }

  public static List<SelectItem> getItemsFromMap(final Map<Object, Object> map) {
    return FacesContexts.getItemsFromMap(map, null);
  }

  @SuppressWarnings("rawtypes")
  public static List<SelectItem> getItemsFromEnum(final Class<? extends Enum<?>> clazz, final String noSelectionLabel) {
    List<SelectItem> list = new ArrayList<SelectItem>();

    if (Conditions.isNotEmpty(noSelectionLabel)) {
      SelectItem selectItem = new SelectItem(null, noSelectionLabel, null, false, true);
      list.add(selectItem);
    }

    Enum[] array = Enums.getValues(clazz);
    for (Enum tmp : array) {
      String value = tmp.name();
      String label = null;
      if (tmp instanceof LabelSupport) {
        LabelSupport le = (LabelSupport) tmp;
        label = le.getLabel();
      } else {
        label = tmp.name();
      }
      list.add(new SelectItem(value, label));
    }
    return list;
  }

  public static List<SelectItem> getItemsFromMap(final Map<Object, Object> map, final String noSelectionLabel) {
    List<SelectItem> list = new ArrayList<SelectItem>();

    if (Conditions.isNotEmpty(noSelectionLabel)) {
      SelectItem selectItem = new SelectItem(null, noSelectionLabel, null, false, true);
      list.add(selectItem);
    }

    for (Entry<Object, Object> entry : map.entrySet()) {
      Object value = entry.getKey();
      String label = null;
      if (entry.getValue() != null) {
        label = entry.getValue().toString();
      } else {
        label = entry.getKey().toString();
      }
      list.add(new SelectItem(value, label));
    }
    return list;
  }
}
TOP

Related Classes of br.net.woodstock.rockframework.web.faces.utils.FacesContexts

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.