package org.ufcspa.simdecs.bn.ui; import java.io.Serializable; import java.util.ResourceBundle; import javax.annotation.Resource; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; import javax.faces.model.DataModel; import javax.faces.model.ListDataModel; import javax.faces.model.SelectItem; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.transaction.UserTransaction; import org.primefaces.event.RowEditEvent; import org.ufcspa.simdecs.bn.entity.Node; import org.ufcspa.simdecs.bn.jpa.NodeJpaController; import org.ufcspa.simdecs.bn.ui.util.JsfUtil; import org.ufcspa.simdecs.bn.ui.util.PaginationHelper; @ManagedBean(name = "nodeController") @ApplicationScoped public class NodeController implements Serializable { @Resource private UserTransaction utx = null; @PersistenceUnit(unitName = "simdecsEclipseLinkPU") private EntityManagerFactory emf = null; private Node current; private DataModel items = null; private NodeJpaController jpaController = null; private PaginationHelper pagination; private int selectedItemIndex; public NodeController() { } public void rowEditListener(RowEditEvent event) throws Exception { Node node = (Node) event.getObject(); getJpaController().edit(node); } public Node getSelected() { if (current == null) { current = new Node(); selectedItemIndex = -1; } return current; } private NodeJpaController getJpaController() { if (jpaController == null) { jpaController = new NodeJpaController(utx, emf); } return jpaController; } public PaginationHelper getPagination() { if (pagination == null) { pagination = new PaginationHelper(10) { @Override public int getItemsCount() { return getJpaController().getNodeCount(); } @Override public DataModel createPageDataModel() { return new ListDataModel(getJpaController().findNodeEntities(getPageSize(), getPageFirstItem())); } }; } return pagination; } public String prepareList() { recreateModel(); return "List"; } public String prepareView() { current = (Node) getItems().getRowData(); selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex(); return "View"; } public String prepareCreate() { current = new Node(); selectedItemIndex = -1; return "Create"; } public String create() { try { getJpaController().create(current); JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeCreated")); return prepareCreate(); } catch (Exception e) { JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured")); return null; } } public String prepareEdit() { current = (Node) getItems().getRowData(); selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex(); return "Edit"; } public String update() { try { getJpaController().edit(current); JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeUpdated")); return "View"; } catch (Exception e) { JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured")); return null; } } public String destroy() { current = (Node) getItems().getRowData(); selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex(); performDestroy(); recreatePagination(); recreateModel(); return "List"; } public String destroyAndView() { performDestroy(); recreateModel(); updateCurrentItem(); if (selectedItemIndex >= 0) { return "View"; } else { // all items were removed - go back to list recreateModel(); return "List"; } } private void performDestroy() { try { getJpaController().destroy(current.getId()); JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeDeleted")); } catch (Exception e) { JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured")); } } private void updateCurrentItem() { int count = getJpaController().getNodeCount(); if (selectedItemIndex >= count) { // selected index cannot be bigger than number of items: selectedItemIndex = count - 1; // go to previous page if last page disappeared: if (pagination.getPageFirstItem() >= count) { pagination.previousPage(); } } if (selectedItemIndex >= 0) { current = getJpaController().findNodeEntities(1, selectedItemIndex).get(0); } } public DataModel getPaginatedItems() { if (items == null) { items = getPagination().createPageDataModel(); } return items; } public DataModel getItems() { if (items == null) { getPagination(); items = new ListDataModel(getJpaController().findNodeEntities()); } return items; } public void recreateModel() { items = null; } private void recreatePagination() { pagination = null; } public String next() { getPagination().nextPage(); recreateModel(); return "List"; } public String previous() { getPagination().previousPage(); recreateModel(); return "List"; } public SelectItem[] getItemsAvailableSelectMany() { return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), false); } public SelectItem[] getItemsAvailableSelectOne() { return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), true); } @FacesConverter(forClass = Node.class) public static class NodeControllerConverter implements Converter { public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { if (value == null || value.length() == 0) { return null; } NodeController controller = (NodeController) facesContext.getApplication().getELResolver(). getValue(facesContext.getELContext(), null, "nodeController"); return controller.getJpaController().findNode(getKey(value)); } java.lang.Long getKey(String value) { java.lang.Long key; key = Long.valueOf(value); return key; } String getStringKey(java.lang.Long value) { StringBuffer sb = new StringBuffer(); sb.append(value); return sb.toString(); } public String getAsString(FacesContext facesContext, UIComponent component, Object object) { if (object == null) { return null; } if (object instanceof Node) { Node o = (Node) object; return getStringKey(o.getId()); } else { throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + NodeController.class.getName()); } } } }