Commit | Line | Data |
---|---|---|
c547eea0 MS |
1 | package org.ufcspa.simdecs.bn.ui; |
2 | ||
3 | import org.ufcspa.simdecs.bn.entity.Node; | |
4 | import org.ufcspa.simdecs.bn.ui.util.JsfUtil; | |
5 | import org.ufcspa.simdecs.bn.ui.util.PaginationHelper; | |
6 | import org.ufcspa.simdecs.bn.jpa.NodeJpaController; | |
7 | ||
8 | import java.io.Serializable; | |
9 | import java.util.ResourceBundle; | |
10 | import javax.annotation.Resource; | |
11 | import javax.faces.bean.ManagedBean; | |
12 | import javax.faces.bean.SessionScoped; | |
13 | import javax.faces.component.UIComponent; | |
14 | import javax.faces.context.FacesContext; | |
15 | import javax.faces.convert.Converter; | |
16 | import javax.faces.convert.FacesConverter; | |
17 | import javax.faces.model.DataModel; | |
18 | import javax.faces.model.ListDataModel; | |
19 | import javax.faces.model.SelectItem; | |
20 | import javax.persistence.EntityManagerFactory; | |
21 | import javax.persistence.PersistenceUnit; | |
22 | import javax.transaction.UserTransaction; | |
3f2db471 | 23 | import org.primefaces.event.RowEditEvent; |
c547eea0 MS |
24 | |
25 | @ManagedBean(name = "nodeController") | |
26 | @SessionScoped | |
27 | public 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 | ||
171 | public DataModel getItems() { | |
172 | if (items == null) { | |
173 | items = getPagination().createPageDataModel(); | |
174 | } | |
175 | return items; | |
176 | } | |
177 | ||
178 | private void recreateModel() { | |
179 | items = null; | |
180 | } | |
181 | ||
182 | private void recreatePagination() { | |
183 | pagination = null; | |
184 | } | |
185 | ||
186 | public String next() { | |
187 | getPagination().nextPage(); | |
188 | recreateModel(); | |
189 | return "List"; | |
190 | } | |
191 | ||
192 | public String previous() { | |
193 | getPagination().previousPage(); | |
194 | recreateModel(); | |
195 | return "List"; | |
196 | } | |
197 | ||
198 | public SelectItem[] getItemsAvailableSelectMany() { | |
199 | return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), false); | |
200 | } | |
201 | ||
202 | public SelectItem[] getItemsAvailableSelectOne() { | |
203 | return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), true); | |
204 | } | |
205 | ||
206 | @FacesConverter(forClass = Node.class) | |
207 | public static class NodeControllerConverter implements Converter { | |
208 | ||
209 | public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { | |
210 | if (value == null || value.length() == 0) { | |
211 | return null; | |
212 | } | |
213 | NodeController controller = (NodeController) facesContext.getApplication().getELResolver(). | |
214 | getValue(facesContext.getELContext(), null, "nodeController"); | |
215 | return controller.getJpaController().findNode(getKey(value)); | |
216 | } | |
217 | ||
218 | java.lang.Long getKey(String value) { | |
219 | java.lang.Long key; | |
220 | key = Long.valueOf(value); | |
221 | return key; | |
222 | } | |
223 | ||
224 | String getStringKey(java.lang.Long value) { | |
225 | StringBuffer sb = new StringBuffer(); | |
226 | sb.append(value); | |
227 | return sb.toString(); | |
228 | } | |
229 | ||
230 | public String getAsString(FacesContext facesContext, UIComponent component, Object object) { | |
231 | if (object == null) { | |
232 | return null; | |
233 | } | |
234 | if (object instanceof Node) { | |
235 | Node o = (Node) object; | |
236 | return getStringKey(o.getId()); | |
237 | } else { | |
238 | throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + NodeController.class.getName()); | |
239 | } | |
240 | } | |
241 | } | |
242 | } |