1 package org.ufcspa.simdecs.bn.ui;
3 import java.io.Serializable;
4 import java.util.ResourceBundle;
5 import javax.annotation.Resource;
6 import javax.faces.bean.ApplicationScoped;
7 import javax.faces.bean.ManagedBean;
8 import javax.faces.component.UIComponent;
9 import javax.faces.context.FacesContext;
10 import javax.faces.convert.Converter;
11 import javax.faces.convert.FacesConverter;
12 import javax.faces.model.DataModel;
13 import javax.faces.model.ListDataModel;
14 import javax.faces.model.SelectItem;
15 import javax.persistence.EntityManagerFactory;
16 import javax.persistence.PersistenceUnit;
17 import javax.transaction.UserTransaction;
18 import org.primefaces.event.RowEditEvent;
19 import org.ufcspa.simdecs.bn.entity.BayesianNetwork;
20 import org.ufcspa.simdecs.bn.jpa.BayesianNetworkJpaController;
21 import org.ufcspa.simdecs.bn.ui.util.JsfUtil;
22 import org.ufcspa.simdecs.bn.ui.util.PaginationHelper;
24 @ManagedBean(name = "bayesianNetworkController")
26 public class BayesianNetworkController implements Serializable {
29 private UserTransaction utx = null;
30 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
31 private EntityManagerFactory emf = null;
32 private BayesianNetwork current;
33 private DataModel items = null;
34 private BayesianNetworkJpaController jpaController = null;
35 private PaginationHelper pagination;
36 private int selectedItemIndex;
38 public BayesianNetworkController() {
41 public void rowEditListener(RowEditEvent event) throws Exception {
42 BayesianNetwork bn = (BayesianNetwork) event.getObject();
43 getJpaController().edit(bn);
46 public BayesianNetwork getSelected() {
47 if (current == null) {
48 current = new BayesianNetwork();
49 selectedItemIndex = -1;
54 private BayesianNetworkJpaController getJpaController() {
55 if (jpaController == null) {
56 jpaController = new BayesianNetworkJpaController(utx, emf);
61 public PaginationHelper getPagination() {
62 if (pagination == null) {
63 pagination = new PaginationHelper(10) {
66 public int getItemsCount() {
67 return getJpaController().getBayesianNetworkCount();
71 public DataModel createPageDataModel() {
72 return new ListDataModel(getJpaController().findBayesianNetworkEntities(getPageSize(), getPageFirstItem()));
79 public String prepareList() {
84 public String prepareView() {
85 current = (BayesianNetwork) getItems().getRowData();
86 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
90 public String prepareCreate() {
91 current = new BayesianNetwork();
92 selectedItemIndex = -1;
96 public String create() {
98 getJpaController().create(current);
99 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkCreated"));
100 return prepareCreate();
101 } catch (Exception e) {
102 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
107 public String prepareEdit() {
108 current = (BayesianNetwork) getItems().getRowData();
109 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
113 public String update() {
115 getJpaController().edit(current);
116 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkUpdated"));
118 } catch (Exception e) {
119 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
124 public String destroy() {
125 current = (BayesianNetwork) getItems().getRowData();
126 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
128 recreatePagination();
133 public String destroyAndView() {
137 if (selectedItemIndex >= 0) {
140 // all items were removed - go back to list
146 private void performDestroy() {
148 getJpaController().destroy(current.getId());
149 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkDeleted"));
150 } catch (Exception e) {
151 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
155 private void updateCurrentItem() {
156 int count = getJpaController().getBayesianNetworkCount();
157 if (selectedItemIndex >= count) {
158 // selected index cannot be bigger than number of items:
159 selectedItemIndex = count - 1;
160 // go to previous page if last page disappeared:
161 if (pagination.getPageFirstItem() >= count) {
162 pagination.previousPage();
165 if (selectedItemIndex >= 0) {
166 current = getJpaController().findBayesianNetworkEntities(1, selectedItemIndex).get(0);
170 public DataModel getPaginatedItems() {
172 items = getPagination().createPageDataModel();
177 public DataModel getItems() {
180 items = new ListDataModel(getJpaController().findBayesianNetworkEntities());
185 public void recreateModel() {
189 private void recreatePagination() {
193 public String next() {
194 getPagination().nextPage();
199 public String previous() {
200 getPagination().previousPage();
205 public SelectItem[] getItemsAvailableSelectMany() {
206 return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), false);
209 public SelectItem[] getItemsAvailableSelectOne() {
210 return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), true);
213 @FacesConverter(forClass = BayesianNetwork.class)
214 public static class BayesianNetworkControllerConverter implements Converter {
216 public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
217 if (value == null || value.length() == 0) {
220 BayesianNetworkController controller = (BayesianNetworkController) facesContext.getApplication().getELResolver().
221 getValue(facesContext.getELContext(), null, "bayesianNetworkController");
222 return controller.getJpaController().findBayesianNetwork(getKey(value));
225 java.lang.Long getKey(String value) {
227 key = Long.valueOf(value);
231 String getStringKey(java.lang.Long value) {
232 StringBuffer sb = new StringBuffer();
234 return sb.toString();
237 public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
238 if (object == null) {
241 if (object instanceof BayesianNetwork) {
242 BayesianNetwork o = (BayesianNetwork) object;
243 return getStringKey(o.getId());
245 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + BayesianNetworkController.class.getName());