Commit | Line | Data |
---|---|---|
c547eea0 MS |
1 | package org.ufcspa.simdecs.bn.ui; |
2 | ||
3 | import org.ufcspa.simdecs.bn.entity.Question; | |
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.QuestionJpaController; | |
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; | |
23 | ||
24 | @ManagedBean(name = "questionController") | |
25 | @SessionScoped | |
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 | ||
41 | public Question getSelected() { | |
42 | if (current == null) { | |
43 | current = new Question(); | |
44 | selectedItemIndex = -1; | |
45 | } | |
46 | return current; | |
47 | } | |
48 | ||
49 | private QuestionJpaController getJpaController() { | |
50 | if (jpaController == null) { | |
51 | jpaController = new QuestionJpaController(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().getQuestionCount(); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public DataModel createPageDataModel() { | |
67 | return new ListDataModel(getJpaController().findQuestionEntities(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 = (Question) getItems().getRowData(); | |
81 | selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex(); | |
82 | return "View"; | |
83 | } | |
84 | ||
85 | public String prepareCreate() { | |
86 | current = new Question(); | |
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("QuestionCreated")); | |
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 = (Question) 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("QuestionUpdated")); | |
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 = (Question) 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("QuestionDeleted")); | |
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().getQuestionCount(); | |
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().findQuestionEntities(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().findQuestionEntities(), false); | |
194 | } | |
195 | ||
196 | public SelectItem[] getItemsAvailableSelectOne() { | |
197 | return JsfUtil.getSelectItems(getJpaController().findQuestionEntities(), true); | |
198 | } | |
199 | ||
200 | @FacesConverter(forClass = Question.class) | |
201 | public static class QuestionControllerConverter 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 | QuestionController controller = (QuestionController) facesContext.getApplication().getELResolver(). | |
208 | getValue(facesContext.getELContext(), null, "questionController"); | |
209 | return controller.getJpaController().findQuestion(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 Question) { | |
229 | Question o = (Question) object; | |
230 | return getStringKey(o.getId()); | |
231 | } else { | |
232 | throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + QuestionController.class.getName()); | |
233 | } | |
234 | } | |
235 | } | |
236 | } |