X-Git-Url: http://200.18.67.61/gitweb/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fufcspa%2Fsimdecs%2Fbn%2Fui%2FBayesianNetworkController.java;fp=src%2Fjava%2Forg%2Fufcspa%2Fsimdecs%2Fbn%2Fui%2FBayesianNetworkController.java;h=9dbe96fc2326dbfdfdb1ac3ec2d1d9e034f82670;hb=c547eea0255390e5763eac8ffbca336a1acf5b41;hp=0000000000000000000000000000000000000000;hpb=5d05ee58812cfa8b58ab2dcf93a5d3a3bb71d6aa;p=simdecs.git diff --git a/src/java/org/ufcspa/simdecs/bn/ui/BayesianNetworkController.java b/src/java/org/ufcspa/simdecs/bn/ui/BayesianNetworkController.java new file mode 100644 index 0000000..9dbe96f --- /dev/null +++ b/src/java/org/ufcspa/simdecs/bn/ui/BayesianNetworkController.java @@ -0,0 +1,236 @@ +package org.ufcspa.simdecs.bn.ui; + +import org.ufcspa.simdecs.bn.entity.BayesianNetwork; +import org.ufcspa.simdecs.bn.ui.util.JsfUtil; +import org.ufcspa.simdecs.bn.ui.util.PaginationHelper; +import org.ufcspa.simdecs.bn.jpa.BayesianNetworkJpaController; + +import java.io.Serializable; +import java.util.ResourceBundle; +import javax.annotation.Resource; +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; +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; + +@ManagedBean(name = "bayesianNetworkController") +@SessionScoped +public class BayesianNetworkController implements Serializable { + + @Resource + private UserTransaction utx = null; + @PersistenceUnit(unitName = "simdecsEclipseLinkPU") + private EntityManagerFactory emf = null; + private BayesianNetwork current; + private DataModel items = null; + private BayesianNetworkJpaController jpaController = null; + private PaginationHelper pagination; + private int selectedItemIndex; + + public BayesianNetworkController() { + } + + public BayesianNetwork getSelected() { + if (current == null) { + current = new BayesianNetwork(); + selectedItemIndex = -1; + } + return current; + } + + private BayesianNetworkJpaController getJpaController() { + if (jpaController == null) { + jpaController = new BayesianNetworkJpaController(utx, emf); + } + return jpaController; + } + + public PaginationHelper getPagination() { + if (pagination == null) { + pagination = new PaginationHelper(10) { + + @Override + public int getItemsCount() { + return getJpaController().getBayesianNetworkCount(); + } + + @Override + public DataModel createPageDataModel() { + return new ListDataModel(getJpaController().findBayesianNetworkEntities(getPageSize(), getPageFirstItem())); + } + }; + } + return pagination; + } + + public String prepareList() { + recreateModel(); + return "List"; + } + + public String prepareView() { + current = (BayesianNetwork) getItems().getRowData(); + selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex(); + return "View"; + } + + public String prepareCreate() { + current = new BayesianNetwork(); + selectedItemIndex = -1; + return "Create"; + } + + public String create() { + try { + getJpaController().create(current); + JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkCreated")); + return prepareCreate(); + } catch (Exception e) { + JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured")); + return null; + } + } + + public String prepareEdit() { + current = (BayesianNetwork) 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("BayesianNetworkUpdated")); + return "View"; + } catch (Exception e) { + JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured")); + return null; + } + } + + public String destroy() { + current = (BayesianNetwork) 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("BayesianNetworkDeleted")); + } catch (Exception e) { + JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured")); + } + } + + private void updateCurrentItem() { + int count = getJpaController().getBayesianNetworkCount(); + 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().findBayesianNetworkEntities(1, selectedItemIndex).get(0); + } + } + + public DataModel getItems() { + if (items == null) { + items = getPagination().createPageDataModel(); + } + return items; + } + + private 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().findBayesianNetworkEntities(), false); + } + + public SelectItem[] getItemsAvailableSelectOne() { + return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), true); + } + + @FacesConverter(forClass = BayesianNetwork.class) + public static class BayesianNetworkControllerConverter implements Converter { + + public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { + if (value == null || value.length() == 0) { + return null; + } + BayesianNetworkController controller = (BayesianNetworkController) facesContext.getApplication().getELResolver(). + getValue(facesContext.getELContext(), null, "bayesianNetworkController"); + return controller.getJpaController().findBayesianNetwork(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 BayesianNetwork) { + BayesianNetwork o = (BayesianNetwork) object; + return getStringKey(o.getId()); + } else { + throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + BayesianNetworkController.class.getName()); + } + } + } +}