/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.pepe.controller; import com.pepe.jpa.entities.Estado; import com.pepe.jpa.sesions.EstadoFacade; import java.util.List; import javax.ejb.EJB; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; /** * * @author Windows 8 */ @ManagedBean @SessionScoped public class EstadoController { @EJB private EstadoFacade estadoFacade; private Estado estadoActual; private List<Estado> listaEstado = null; /** * Creates a new instance of CiudadController */ public EstadoController() { } public EstadoFacade getEstadoFacade() { return estadoFacade; } public Estado getEstadoActual() { if (estadoActual == null) { estadoActual = new Estado(); } return estadoActual; } public void setEstadoActual(Estado estadoActual) { this.estadoActual = estadoActual; } public List<Estado> getListaEstado() { if (listaEstado == null) { try { listaEstado = getEstadoFacade().findAll(); } catch (Exception e) { addErrorMessage("Error closing resource " + e.getClass().getName(), "Message: " + e.getMessage()); } } return listaEstado; } private void recargarLista() { listaEstado = null; } public String prepareCreate() { estadoActual = new Estado(); return "/admin/crear_estado"; } public String prepareEdit() { return ""; } public String prepareView() { return ""; } public String prepareList() { recargarLista(); return ""; } public String addEstado() { try { getEstadoFacade().create(estadoActual); recargarLista(); return ""; } catch (Exception e) { addErrorMessage("Error closing resource " + e.getClass().getName(), "Message: " + e.getMessage()); return null; } } public String updateEstado() { try { getEstadoFacade().edit(estadoActual); recargarLista(); return ""; } catch (Exception e) { addErrorMessage("Error closing resource " + e.getClass().getName(), "Message: " + e.getMessage()); return null; } } public String deleteEstado() { try { getEstadoFacade().remove(estadoActual); recargarLista(); } catch (Exception e) { addErrorMessage("Error closing resource " + e.getClass().getName(), "Message: " + e.getMessage()); } return "List"; } private void addErrorMessage(String title, String msg) { FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, title, msg); FacesContext.getCurrentInstance().addMessage(null, facesMsg); } private void addSuccessMessage(String title, String msg) { FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, title, msg); FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg); } public Estado getEstado(java.lang.Integer id) { return getEstadoFacade().find(id); } @FacesConverter(forClass = Estado.class) public static class EstadoControllerConverter implements Converter { @Override public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { if (value == null || value.length() == 0) { return null; } EstadoController controller = (EstadoController) facesContext.getApplication().getELResolver(). getValue(facesContext.getELContext(), null, "estadoController"); return controller.getEstado(getKey(value)); } java.lang.Integer getKey(String value) { java.lang.Integer key; key = Integer.valueOf(value); return key; } String getStringKey(java.lang.Integer value) { StringBuilder sb = new StringBuilder(); sb.append(value); return sb.toString(); } @Override public String getAsString(FacesContext facesContext, UIComponent component, Object object) { if (object == null) { return null; } if (object instanceof Estado) { Estado o = (Estado) object; return getStringKey(o.getIdEstado()); } else { throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Estado.class.getName()); } } } }