Adicionado classificaĆ§Ć£o do log conforme exemplo
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / ui / BayesianNetworkController.java
1 package org.ufcspa.simdecs.bn.ui;
2
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;
23
24 @ManagedBean(name = "bayesianNetworkController")
25 @ApplicationScoped
26 public class BayesianNetworkController implements Serializable {
27
28     @Resource
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;
37
38     public BayesianNetworkController() {
39     }
40
41     public void rowEditListener(RowEditEvent event) throws Exception {
42         BayesianNetwork bn = (BayesianNetwork) event.getObject();
43         getJpaController().edit(bn);
44     }
45     
46     public BayesianNetwork getSelected() {
47         if (current == null) {
48             current = new BayesianNetwork();
49             selectedItemIndex = -1;
50         }
51         return current;
52     }
53
54     private BayesianNetworkJpaController getJpaController() {
55         if (jpaController == null) {
56             jpaController = new BayesianNetworkJpaController(utx, emf);
57         }
58         return jpaController;
59     }
60
61     public PaginationHelper getPagination() {
62         if (pagination == null) {
63             pagination = new PaginationHelper(10) {
64
65                 @Override
66                 public int getItemsCount() {
67                     return getJpaController().getBayesianNetworkCount();
68                 }
69
70                 @Override
71                 public DataModel createPageDataModel() {
72                     return new ListDataModel(getJpaController().findBayesianNetworkEntities(getPageSize(), getPageFirstItem()));
73                 }
74             };
75         }
76         return pagination;
77     }
78
79     public String prepareList() {
80         recreateModel();
81         return "List";
82     }
83
84     public String prepareView() {
85         current = (BayesianNetwork) getItems().getRowData();
86         selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
87         return "View";
88     }
89
90     public String prepareCreate() {
91         current = new BayesianNetwork();
92         selectedItemIndex = -1;
93         return "Create";
94     }
95
96     public String create() {
97         try {
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"));
103             return null;
104         }
105     }
106
107     public String prepareEdit() {
108         current = (BayesianNetwork) getItems().getRowData();
109         selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
110         return "Edit";
111     }
112
113     public String update() {
114         try {
115             getJpaController().edit(current);
116             JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkUpdated"));
117             return "View";
118         } catch (Exception e) {
119             JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
120             return null;
121         }
122     }
123
124     public String destroy() {
125         current = (BayesianNetwork) getItems().getRowData();
126         selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
127         performDestroy();
128         recreatePagination();
129         recreateModel();
130         return "List";
131     }
132
133     public String destroyAndView() {
134         performDestroy();
135         recreateModel();
136         updateCurrentItem();
137         if (selectedItemIndex >= 0) {
138             return "View";
139         } else {
140             // all items were removed - go back to list
141             recreateModel();
142             return "List";
143         }
144     }
145
146     private void performDestroy() {
147         try {
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"));
152         }
153     }
154
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();
163             }
164         }
165         if (selectedItemIndex >= 0) {
166             current = getJpaController().findBayesianNetworkEntities(1, selectedItemIndex).get(0);
167         }
168     }
169
170     public DataModel getPaginatedItems() {
171         if (items == null) {
172             items = getPagination().createPageDataModel();
173         }
174         return items;
175     }
176     
177     public DataModel getItems() {
178         if (items == null) {
179             getPagination();
180             items = new ListDataModel(getJpaController().findBayesianNetworkEntities());
181         }
182         return items;
183     }
184
185     public void recreateModel() {
186         items = null;
187     }
188
189     private void recreatePagination() {
190         pagination = null;
191     }
192
193     public String next() {
194         getPagination().nextPage();
195         recreateModel();
196         return "List";
197     }
198
199     public String previous() {
200         getPagination().previousPage();
201         recreateModel();
202         return "List";
203     }
204
205     public SelectItem[] getItemsAvailableSelectMany() {
206         return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), false);
207     }
208
209     public SelectItem[] getItemsAvailableSelectOne() {
210         return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), true);
211     }
212
213     @FacesConverter(forClass = BayesianNetwork.class)
214     public static class BayesianNetworkControllerConverter implements Converter {
215
216         public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
217             if (value == null || value.length() == 0) {
218                 return null;
219             }
220             BayesianNetworkController controller = (BayesianNetworkController) facesContext.getApplication().getELResolver().
221                     getValue(facesContext.getELContext(), null, "bayesianNetworkController");
222             return controller.getJpaController().findBayesianNetwork(getKey(value));
223         }
224
225         java.lang.Long getKey(String value) {
226             java.lang.Long key;
227             key = Long.valueOf(value);
228             return key;
229         }
230
231         String getStringKey(java.lang.Long value) {
232             StringBuffer sb = new StringBuffer();
233             sb.append(value);
234             return sb.toString();
235         }
236
237         public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
238             if (object == null) {
239                 return null;
240             }
241             if (object instanceof BayesianNetwork) {
242                 BayesianNetwork o = (BayesianNetwork) object;
243                 return getStringKey(o.getId());
244             } else {
245                 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + BayesianNetworkController.class.getName());
246             }
247         }
248     }
249 }