Changed ui controllers to let primefaces handle the pagination.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / ui / NodeController.java
CommitLineData
c547eea0
MS
1package org.ufcspa.simdecs.bn.ui;
2
3import org.ufcspa.simdecs.bn.entity.Node;
4import org.ufcspa.simdecs.bn.ui.util.JsfUtil;
5import org.ufcspa.simdecs.bn.ui.util.PaginationHelper;
6import org.ufcspa.simdecs.bn.jpa.NodeJpaController;
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;
3f2db471 23import org.primefaces.event.RowEditEvent;
c547eea0
MS
24
25@ManagedBean(name = "nodeController")
26@SessionScoped
27public class NodeController implements Serializable {
28
29 @Resource
30 private UserTransaction utx = null;
31 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
32 private EntityManagerFactory emf = null;
33 private Node current;
34 private DataModel items = null;
35 private NodeJpaController jpaController = null;
36 private PaginationHelper pagination;
37 private int selectedItemIndex;
38
39 public NodeController() {
40 }
3f2db471
MS
41
42 public void rowEditListener(RowEditEvent event) throws Exception {
43 Node node = (Node) event.getObject();
44 getJpaController().edit(node);
45 }
c547eea0
MS
46
47 public Node getSelected() {
48 if (current == null) {
49 current = new Node();
50 selectedItemIndex = -1;
51 }
52 return current;
53 }
54
55 private NodeJpaController getJpaController() {
56 if (jpaController == null) {
57 jpaController = new NodeJpaController(utx, emf);
58 }
59 return jpaController;
60 }
61
62 public PaginationHelper getPagination() {
63 if (pagination == null) {
64 pagination = new PaginationHelper(10) {
65
66 @Override
67 public int getItemsCount() {
68 return getJpaController().getNodeCount();
69 }
70
71 @Override
72 public DataModel createPageDataModel() {
73 return new ListDataModel(getJpaController().findNodeEntities(getPageSize(), getPageFirstItem()));
74 }
75 };
76 }
77 return pagination;
78 }
79
80 public String prepareList() {
81 recreateModel();
82 return "List";
83 }
84
85 public String prepareView() {
86 current = (Node) getItems().getRowData();
87 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
88 return "View";
89 }
90
91 public String prepareCreate() {
92 current = new Node();
93 selectedItemIndex = -1;
94 return "Create";
95 }
96
97 public String create() {
98 try {
99 getJpaController().create(current);
100 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeCreated"));
101 return prepareCreate();
102 } catch (Exception e) {
103 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
104 return null;
105 }
106 }
107
108 public String prepareEdit() {
109 current = (Node) getItems().getRowData();
110 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
111 return "Edit";
112 }
113
114 public String update() {
115 try {
116 getJpaController().edit(current);
117 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeUpdated"));
118 return "View";
119 } catch (Exception e) {
120 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
121 return null;
122 }
123 }
124
125 public String destroy() {
126 current = (Node) getItems().getRowData();
127 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
128 performDestroy();
129 recreatePagination();
130 recreateModel();
131 return "List";
132 }
133
134 public String destroyAndView() {
135 performDestroy();
136 recreateModel();
137 updateCurrentItem();
138 if (selectedItemIndex >= 0) {
139 return "View";
140 } else {
141 // all items were removed - go back to list
142 recreateModel();
143 return "List";
144 }
145 }
146
147 private void performDestroy() {
148 try {
149 getJpaController().destroy(current.getId());
150 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeDeleted"));
151 } catch (Exception e) {
152 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
153 }
154 }
155
156 private void updateCurrentItem() {
157 int count = getJpaController().getNodeCount();
158 if (selectedItemIndex >= count) {
159 // selected index cannot be bigger than number of items:
160 selectedItemIndex = count - 1;
161 // go to previous page if last page disappeared:
162 if (pagination.getPageFirstItem() >= count) {
163 pagination.previousPage();
164 }
165 }
166 if (selectedItemIndex >= 0) {
167 current = getJpaController().findNodeEntities(1, selectedItemIndex).get(0);
168 }
169 }
170
c9a8e64a 171 public DataModel getPaginatedItems() {
c547eea0
MS
172 if (items == null) {
173 items = getPagination().createPageDataModel();
174 }
175 return items;
176 }
c9a8e64a
MS
177
178 public DataModel getItems() {
179 if (items == null) {
180 items = new ListDataModel(getJpaController().findNodeEntities());
181 }
182 return items;
183 }
c547eea0 184
c9a8e64a 185 public void recreateModel() {
c547eea0
MS
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().findNodeEntities(), false);
207 }
208
209 public SelectItem[] getItemsAvailableSelectOne() {
210 return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), true);
211 }
212
213 @FacesConverter(forClass = Node.class)
214 public static class NodeControllerConverter 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 NodeController controller = (NodeController) facesContext.getApplication().getELResolver().
221 getValue(facesContext.getELContext(), null, "nodeController");
222 return controller.getJpaController().findNode(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 Node) {
242 Node o = (Node) object;
243 return getStringKey(o.getId());
244 } else {
245 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + NodeController.class.getName());
246 }
247 }
248 }
249}