Added UI (JSF) and JPA to bayesian network entities.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / ui / BayesianNetworkController.java
CommitLineData
c547eea0
MS
1package org.ufcspa.simdecs.bn.ui;
2
3import org.ufcspa.simdecs.bn.entity.BayesianNetwork;
4import org.ufcspa.simdecs.bn.ui.util.JsfUtil;
5import org.ufcspa.simdecs.bn.ui.util.PaginationHelper;
6import org.ufcspa.simdecs.bn.jpa.BayesianNetworkJpaController;
7
8import java.io.Serializable;
9import java.util.ResourceBundle;
10import javax.annotation.Resource;
11import javax.faces.bean.ManagedBean;
12import javax.faces.bean.SessionScoped;
13import javax.faces.component.UIComponent;
14import javax.faces.context.FacesContext;
15import javax.faces.convert.Converter;
16import javax.faces.convert.FacesConverter;
17import javax.faces.model.DataModel;
18import javax.faces.model.ListDataModel;
19import javax.faces.model.SelectItem;
20import javax.persistence.EntityManagerFactory;
21import javax.persistence.PersistenceUnit;
22import javax.transaction.UserTransaction;
23
24@ManagedBean(name = "bayesianNetworkController")
25@SessionScoped
26public 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 BayesianNetwork getSelected() {
42 if (current == null) {
43 current = new BayesianNetwork();
44 selectedItemIndex = -1;
45 }
46 return current;
47 }
48
49 private BayesianNetworkJpaController getJpaController() {
50 if (jpaController == null) {
51 jpaController = new BayesianNetworkJpaController(utx, emf);
52 }
53 return jpaController;
54 }
55
56 public PaginationHelper getPagination() {
57 if (pagination == null) {
58 pagination = new PaginationHelper(10) {
59
60 @Override
61 public int getItemsCount() {
62 return getJpaController().getBayesianNetworkCount();
63 }
64
65 @Override
66 public DataModel createPageDataModel() {
67 return new ListDataModel(getJpaController().findBayesianNetworkEntities(getPageSize(), getPageFirstItem()));
68 }
69 };
70 }
71 return pagination;
72 }
73
74 public String prepareList() {
75 recreateModel();
76 return "List";
77 }
78
79 public String prepareView() {
80 current = (BayesianNetwork) getItems().getRowData();
81 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
82 return "View";
83 }
84
85 public String prepareCreate() {
86 current = new BayesianNetwork();
87 selectedItemIndex = -1;
88 return "Create";
89 }
90
91 public String create() {
92 try {
93 getJpaController().create(current);
94 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkCreated"));
95 return prepareCreate();
96 } catch (Exception e) {
97 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
98 return null;
99 }
100 }
101
102 public String prepareEdit() {
103 current = (BayesianNetwork) getItems().getRowData();
104 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
105 return "Edit";
106 }
107
108 public String update() {
109 try {
110 getJpaController().edit(current);
111 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkUpdated"));
112 return "View";
113 } catch (Exception e) {
114 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
115 return null;
116 }
117 }
118
119 public String destroy() {
120 current = (BayesianNetwork) getItems().getRowData();
121 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
122 performDestroy();
123 recreatePagination();
124 recreateModel();
125 return "List";
126 }
127
128 public String destroyAndView() {
129 performDestroy();
130 recreateModel();
131 updateCurrentItem();
132 if (selectedItemIndex >= 0) {
133 return "View";
134 } else {
135 // all items were removed - go back to list
136 recreateModel();
137 return "List";
138 }
139 }
140
141 private void performDestroy() {
142 try {
143 getJpaController().destroy(current.getId());
144 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("BayesianNetworkDeleted"));
145 } catch (Exception e) {
146 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
147 }
148 }
149
150 private void updateCurrentItem() {
151 int count = getJpaController().getBayesianNetworkCount();
152 if (selectedItemIndex >= count) {
153 // selected index cannot be bigger than number of items:
154 selectedItemIndex = count - 1;
155 // go to previous page if last page disappeared:
156 if (pagination.getPageFirstItem() >= count) {
157 pagination.previousPage();
158 }
159 }
160 if (selectedItemIndex >= 0) {
161 current = getJpaController().findBayesianNetworkEntities(1, selectedItemIndex).get(0);
162 }
163 }
164
165 public DataModel getItems() {
166 if (items == null) {
167 items = getPagination().createPageDataModel();
168 }
169 return items;
170 }
171
172 private void recreateModel() {
173 items = null;
174 }
175
176 private void recreatePagination() {
177 pagination = null;
178 }
179
180 public String next() {
181 getPagination().nextPage();
182 recreateModel();
183 return "List";
184 }
185
186 public String previous() {
187 getPagination().previousPage();
188 recreateModel();
189 return "List";
190 }
191
192 public SelectItem[] getItemsAvailableSelectMany() {
193 return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), false);
194 }
195
196 public SelectItem[] getItemsAvailableSelectOne() {
197 return JsfUtil.getSelectItems(getJpaController().findBayesianNetworkEntities(), true);
198 }
199
200 @FacesConverter(forClass = BayesianNetwork.class)
201 public static class BayesianNetworkControllerConverter implements Converter {
202
203 public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
204 if (value == null || value.length() == 0) {
205 return null;
206 }
207 BayesianNetworkController controller = (BayesianNetworkController) facesContext.getApplication().getELResolver().
208 getValue(facesContext.getELContext(), null, "bayesianNetworkController");
209 return controller.getJpaController().findBayesianNetwork(getKey(value));
210 }
211
212 java.lang.Long getKey(String value) {
213 java.lang.Long key;
214 key = Long.valueOf(value);
215 return key;
216 }
217
218 String getStringKey(java.lang.Long value) {
219 StringBuffer sb = new StringBuffer();
220 sb.append(value);
221 return sb.toString();
222 }
223
224 public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
225 if (object == null) {
226 return null;
227 }
228 if (object instanceof BayesianNetwork) {
229 BayesianNetwork o = (BayesianNetwork) object;
230 return getStringKey(o.getId());
231 } else {
232 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + BayesianNetworkController.class.getName());
233 }
234 }
235 }
236}