Guest User

Untitled

a guest
Sep 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.14 KB | None | 0 0
  1. package br.com.gddoc.controllers;
  2.  
  3. import br.com.gddoc.models.Agenda;
  4. import br.com.gddoc.controllers.util.JsfUtil;
  5. import br.com.gddoc.controllers.util.PaginationHelper;
  6. import br.com.gddoc.facade.AgendaFacade;
  7.  
  8. import java.io.Serializable;
  9. import java.util.ResourceBundle;
  10. import javax.ejb.EJB;
  11. import javax.faces.bean.ManagedBean;
  12. import javax.faces.bean.SessionScoped;
  13. import javax.faces.component.UIComponent;
  14. import javax.faces.context.FacesContext;
  15. import javax.faces.convert.Converter;
  16. import javax.faces.convert.FacesConverter;
  17. import javax.faces.model.DataModel;
  18. import javax.faces.model.ListDataModel;
  19. import javax.faces.model.SelectItem;
  20.  
  21. @ManagedBean(name = "agendaController")
  22. @SessionScoped
  23. public class AgendaController implements Serializable {
  24.  
  25.     private Agenda current;
  26.     private DataModel items = null;
  27.     @EJB
  28.     private br.com.gddoc.facade.AgendaFacade ejbFacade;
  29.     private PaginationHelper pagination;
  30.     private int selectedItemIndex;
  31.  
  32.     public AgendaController() {
  33.     }
  34.  
  35.     public Agenda getSelected() {
  36.         if (current == null) {
  37.             current = new Agenda();
  38.             selectedItemIndex = -1;
  39.         }
  40.         return current;
  41.     }
  42.  
  43.     private AgendaFacade getFacade() {
  44.         return ejbFacade;
  45.     }
  46.  
  47.     public PaginationHelper getPagination() {
  48.         if (pagination == null) {
  49.             pagination = new PaginationHelper(10) {
  50.                 @Override
  51.                 public int getItemsCount() {
  52.                     return getFacade().count();
  53.                 }
  54.  
  55.                 @Override
  56.                 public DataModel createPageDataModel() {
  57.                     return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
  58.                 }
  59.             };
  60.         }
  61.         return pagination;
  62.     }
  63.  
  64.     public String prepareList() {
  65.         recreateModel();
  66.         return "List";
  67.     }
  68.  
  69.     public String prepareView() {
  70.         current = (Agenda) getItems().getRowData();
  71.         selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
  72.         return "View";
  73.     }
  74.  
  75.     public String prepareCreate() {
  76.         current = new Agenda();
  77.         selectedItemIndex = -1;
  78.         return "Create";
  79.     }
  80.  
  81.     public String create() {
  82.         try {
  83.             getFacade().create(current);
  84.             JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("AgendaCreated"));
  85.             return prepareCreate();
  86.         } catch (Exception e) {
  87.             JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
  88.             return null;
  89.         }
  90.     }
  91.  
  92.     public String prepareEdit() {
  93.         current = (Agenda) getItems().getRowData();
  94.         selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
  95.         return "Edit";
  96.     }
  97.  
  98.     public String update() {
  99.         try {
  100.             getFacade().edit(current);
  101.             JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("AgendaUpdated"));
  102.             return "View";
  103.         } catch (Exception e) {
  104.             JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
  105.             return null;
  106.         }
  107.     }
  108.  
  109.     public String destroy() {
  110.         current = (Agenda) getItems().getRowData();
  111.         selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
  112.         performDestroy();
  113.         recreatePagination();
  114.         recreateModel();
  115.         return "List";
  116.     }
  117.  
  118.     public String destroyAndView() {
  119.         performDestroy();
  120.         recreateModel();
  121.         updateCurrentItem();
  122.         if (selectedItemIndex >= 0) {
  123.             return "View";
  124.         } else {
  125.             // all items were removed - go back to list
  126.             recreateModel();
  127.             return "List";
  128.         }
  129.     }
  130.  
  131.     private void performDestroy() {
  132.         try {
  133.             getFacade().remove(current);
  134.             JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("AgendaDeleted"));
  135.         } catch (Exception e) {
  136.             JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
  137.         }
  138.     }
  139.  
  140.     private void updateCurrentItem() {
  141.         int count = getFacade().count();
  142.         if (selectedItemIndex >= count) {
  143.             // selected index cannot be bigger than number of items:
  144.             selectedItemIndex = count - 1;
  145.             // go to previous page if last page disappeared:
  146.             if (pagination.getPageFirstItem() >= count) {
  147.                 pagination.previousPage();
  148.             }
  149.         }
  150.         if (selectedItemIndex >= 0) {
  151.             current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
  152.         }
  153.     }
  154.  
  155.     public DataModel getItems() {
  156.         if (items == null) {
  157.             items = getPagination().createPageDataModel();
  158.         }
  159.         return items;
  160.     }
  161.  
  162.     private void recreateModel() {
  163.         items = null;
  164.     }
  165.  
  166.     private void recreatePagination() {
  167.         pagination = null;
  168.     }
  169.  
  170.     public String next() {
  171.         getPagination().nextPage();
  172.         recreateModel();
  173.         return "List";
  174.     }
  175.  
  176.     public String previous() {
  177.         getPagination().previousPage();
  178.         recreateModel();
  179.         return "List";
  180.     }
  181.  
  182.     public SelectItem[] getItemsAvailableSelectMany() {
  183.         return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
  184.     }
  185.  
  186.     public SelectItem[] getItemsAvailableSelectOne() {
  187.         return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
  188.     }
  189.  
  190.     @FacesConverter(forClass = Agenda.class)
  191.     public static class AgendaControllerConverter implements Converter {
  192.  
  193.         public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
  194.             if (value == null || value.length() == 0) {
  195.                 return null;
  196.             }
  197.             AgendaController controller = (AgendaController) facesContext.getApplication().getELResolver().
  198.                     getValue(facesContext.getELContext(), null, "agendaController");
  199.             return controller.ejbFacade.find(getKey(value));
  200.         }
  201.  
  202.         java.lang.Integer getKey(String value) {
  203.             java.lang.Integer key;
  204.             key = Integer.valueOf(value);
  205.             return key;
  206.         }
  207.  
  208.         String getStringKey(java.lang.Integer value) {
  209.             StringBuffer sb = new StringBuffer();
  210.             sb.append(value);
  211.             return sb.toString();
  212.         }
  213.  
  214.         public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
  215.             if (object == null) {
  216.                 return null;
  217.             }
  218.             if (object instanceof Agenda) {
  219.                 Agenda o = (Agenda) object;
  220.                 return getStringKey(o.getAgCod());
  221.             } else {
  222.                 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Agenda.class.getName());
  223.             }
  224.         }
  225.        
  226.        
  227.     }
  228. }
Add Comment
Please, Sign In to add comment