Changed controllers to handle table row edit.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / ui / AnswerController.java
CommitLineData
c547eea0
MS
1package org.ufcspa.simdecs.bn.ui;
2
c547eea0
MS
3import java.io.Serializable;
4import java.util.ResourceBundle;
5import javax.annotation.Resource;
a496c612 6import javax.faces.bean.ApplicationScoped;
c547eea0 7import javax.faces.bean.ManagedBean;
c547eea0
MS
8import javax.faces.component.UIComponent;
9import javax.faces.context.FacesContext;
10import javax.faces.convert.Converter;
11import javax.faces.convert.FacesConverter;
12import javax.faces.model.DataModel;
13import javax.faces.model.ListDataModel;
14import javax.faces.model.SelectItem;
15import javax.persistence.EntityManagerFactory;
16import javax.persistence.PersistenceUnit;
17import javax.transaction.UserTransaction;
a496c612
MS
18import org.primefaces.event.RowEditEvent;
19import org.ufcspa.simdecs.bn.entity.Answer;
20import org.ufcspa.simdecs.bn.jpa.AnswerJpaController;
21import org.ufcspa.simdecs.bn.ui.util.JsfUtil;
22import org.ufcspa.simdecs.bn.ui.util.PaginationHelper;
c547eea0
MS
23
24@ManagedBean(name = "answerController")
a496c612 25@ApplicationScoped
c547eea0
MS
26public class AnswerController implements Serializable {
27
28 @Resource
29 private UserTransaction utx = null;
30 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
31 private EntityManagerFactory emf = null;
32 private Answer current;
33 private DataModel items = null;
34 private AnswerJpaController jpaController = null;
35 private PaginationHelper pagination;
36 private int selectedItemIndex;
37
38 public AnswerController() {
39 }
40
a496c612
MS
41 public void rowEditListener(RowEditEvent event) throws Exception {
42 Answer answer = (Answer) event.getObject();
43 getJpaController().edit(answer);
44 }
45
c547eea0
MS
46 public Answer getSelected() {
47 if (current == null) {
48 current = new Answer();
49 selectedItemIndex = -1;
50 }
51 return current;
52 }
53
54 private AnswerJpaController getJpaController() {
55 if (jpaController == null) {
56 jpaController = new AnswerJpaController(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().getAnswerCount();
68 }
69
70 @Override
71 public DataModel createPageDataModel() {
72 return new ListDataModel(getJpaController().findAnswerEntities(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 = (Answer) getItems().getRowData();
86 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
87 return "View";
88 }
89
90 public String prepareCreate() {
91 current = new Answer();
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("AnswerCreated"));
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 = (Answer) 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("AnswerUpdated"));
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 = (Answer) 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("AnswerDeleted"));
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().getAnswerCount();
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().findAnswerEntities(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 }
c9a8e64a
MS
176
177 public DataModel getItems() {
178 if (items == null) {
a496c612 179 getPagination();
c9a8e64a
MS
180 items = new ListDataModel(getJpaController().findAnswerEntities());
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().findAnswerEntities(), false);
207 }
208
209 public SelectItem[] getItemsAvailableSelectOne() {
210 return JsfUtil.getSelectItems(getJpaController().findAnswerEntities(), true);
211 }
212
213 @FacesConverter(forClass = Answer.class)
214 public static class AnswerControllerConverter 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 AnswerController controller = (AnswerController) facesContext.getApplication().getELResolver().
221 getValue(facesContext.getELContext(), null, "answerController");
222 return controller.getJpaController().findAnswer(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 Answer) {
242 Answer o = (Answer) object;
243 return getStringKey(o.getId());
244 } else {
245 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + AnswerController.class.getName());
246 }
247 }
248 }
249}