Commit | Line | Data |
---|---|---|
c547eea0 MS |
1 | package org.ufcspa.simdecs.bn.ui; |
2 | ||
c547eea0 MS |
3 | import java.io.Serializable; |
4 | import java.util.ResourceBundle; | |
5 | import javax.annotation.Resource; | |
a496c612 | 6 | import javax.faces.bean.ApplicationScoped; |
c547eea0 | 7 | import javax.faces.bean.ManagedBean; |
c547eea0 MS |
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; | |
a496c612 MS |
18 | import org.primefaces.event.RowEditEvent; |
19 | import org.ufcspa.simdecs.bn.entity.Question; | |
20 | import org.ufcspa.simdecs.bn.jpa.QuestionJpaController; | |
21 | import org.ufcspa.simdecs.bn.ui.util.JsfUtil; | |
22 | import org.ufcspa.simdecs.bn.ui.util.PaginationHelper; | |
c547eea0 MS |
23 | |
24 | @ManagedBean(name = "questionController") | |
a496c612 | 25 | @ApplicationScoped |
c547eea0 MS |
26 | public class QuestionController implements Serializable { |
27 | ||
28 | @Resource | |
29 | private UserTransaction utx = null; | |
30 | @PersistenceUnit(unitName = "simdecsEclipseLinkPU") | |
31 | private EntityManagerFactory emf = null; | |
32 | private Question current; | |
33 | private DataModel items = null; | |
34 | private QuestionJpaController jpaController = null; | |
35 | private PaginationHelper pagination; | |
36 | private int selectedItemIndex; | |
37 | ||
38 | public QuestionController() { | |
39 | } | |
40 | ||
a496c612 MS |
41 | public void rowEditListener(RowEditEvent event) throws Exception { |
42 | Question question = (Question) event.getObject(); | |
43 | getJpaController().edit(question); | |
44 | } | |
45 | ||
c547eea0 MS |
46 | public Question getSelected() { |
47 | if (current == null) { | |
48 | current = new Question(); | |
49 | selectedItemIndex = -1; | |
50 | } | |
51 | return current; | |
52 | } | |
53 | ||
54 | private QuestionJpaController getJpaController() { | |
55 | if (jpaController == null) { | |
56 | jpaController = new QuestionJpaController(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().getQuestionCount(); | |
68 | } | |
69 | ||
70 | @Override | |
71 | public DataModel createPageDataModel() { | |
72 | return new ListDataModel(getJpaController().findQuestionEntities(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 = (Question) getItems().getRowData(); | |
86 | selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex(); | |
87 | return "View"; | |
88 | } | |
89 | ||
90 | public String prepareCreate() { | |
91 | current = new Question(); | |
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("QuestionCreated")); | |
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 = (Question) 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("QuestionUpdated")); | |
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 = (Question) 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("QuestionDeleted")); | |
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().getQuestionCount(); | |
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().findQuestionEntities(1, selectedItemIndex).get(0); | |
167 | } | |
168 | } | |
169 | ||
c9a8e64a | 170 | public DataModel getPaginatedItems() { |
c547eea0 MS |
171 | if (items == null) { |
172 | items = getPagination().createPageDataModel(); | |
173 | } | |
174 | return items; | |
175 | } | |
176 | ||
c9a8e64a MS |
177 | public DataModel getItems() { |
178 | if (items == null) { | |
a496c612 | 179 | getPagination(); |
c9a8e64a MS |
180 | items = new ListDataModel(getJpaController().findQuestionEntities()); |
181 | } | |
182 | return items; | |
183 | } | |
184 | ||
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().findQuestionEntities(), false); | |
207 | } | |
208 | ||
209 | public SelectItem[] getItemsAvailableSelectOne() { | |
210 | return JsfUtil.getSelectItems(getJpaController().findQuestionEntities(), true); | |
211 | } | |
212 | ||
213 | @FacesConverter(forClass = Question.class) | |
214 | public static class QuestionControllerConverter 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 | QuestionController controller = (QuestionController) facesContext.getApplication().getELResolver(). | |
221 | getValue(facesContext.getELContext(), null, "questionController"); | |
222 | return controller.getJpaController().findQuestion(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 Question) { | |
242 | Question o = (Question) object; | |
243 | return getStringKey(o.getId()); | |
244 | } else { | |
245 | throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + QuestionController.class.getName()); | |
246 | } | |
247 | } | |
248 | } | |
249 | } |