1 package org.ufcspa.simdecs.bn.ui;
3 import java.io.Serializable;
4 import java.util.ResourceBundle;
5 import javax.annotation.Resource;
6 import javax.faces.bean.ApplicationScoped;
7 import javax.faces.bean.ManagedBean;
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;
18 import org.primefaces.event.RowEditEvent;
19 import org.ufcspa.simdecs.bn.entity.Node;
20 import org.ufcspa.simdecs.bn.jpa.NodeJpaController;
21 import org.ufcspa.simdecs.bn.ui.util.JsfUtil;
22 import org.ufcspa.simdecs.bn.ui.util.PaginationHelper;
24 @ManagedBean(name = "nodeController")
26 public class NodeController implements Serializable {
29 private UserTransaction utx = null;
30 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
31 private EntityManagerFactory emf = null;
33 private DataModel items = null;
34 private NodeJpaController jpaController = null;
35 private PaginationHelper pagination;
36 private int selectedItemIndex;
38 public NodeController() {
41 public void rowEditListener(RowEditEvent event) throws Exception {
42 Node node = (Node) event.getObject();
43 getJpaController().edit(node);
46 public Node getSelected() {
47 if (current == null) {
49 selectedItemIndex = -1;
54 private NodeJpaController getJpaController() {
55 if (jpaController == null) {
56 jpaController = new NodeJpaController(utx, emf);
61 public PaginationHelper getPagination() {
62 if (pagination == null) {
63 pagination = new PaginationHelper(10) {
66 public int getItemsCount() {
67 return getJpaController().getNodeCount();
71 public DataModel createPageDataModel() {
72 return new ListDataModel(getJpaController().findNodeEntities(getPageSize(), getPageFirstItem()));
79 public String prepareList() {
84 public String prepareView() {
85 current = (Node) getItems().getRowData();
86 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
90 public String prepareCreate() {
92 selectedItemIndex = -1;
96 public String create() {
98 getJpaController().create(current);
99 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeCreated"));
100 return prepareCreate();
101 } catch (Exception e) {
102 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
107 public String prepareEdit() {
108 current = (Node) getItems().getRowData();
109 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
113 public String update() {
115 getJpaController().edit(current);
116 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeUpdated"));
118 } catch (Exception e) {
119 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
124 public String destroy() {
125 current = (Node) getItems().getRowData();
126 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
128 recreatePagination();
133 public String destroyAndView() {
137 if (selectedItemIndex >= 0) {
140 // all items were removed - go back to list
146 private void performDestroy() {
148 getJpaController().destroy(current.getId());
149 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeDeleted"));
150 } catch (Exception e) {
151 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
155 private void updateCurrentItem() {
156 int count = getJpaController().getNodeCount();
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();
165 if (selectedItemIndex >= 0) {
166 current = getJpaController().findNodeEntities(1, selectedItemIndex).get(0);
170 public DataModel getPaginatedItems() {
172 items = getPagination().createPageDataModel();
177 public DataModel getItems() {
180 items = new ListDataModel(getJpaController().findNodeEntities());
185 public void recreateModel() {
189 private void recreatePagination() {
193 public String next() {
194 getPagination().nextPage();
199 public String previous() {
200 getPagination().previousPage();
205 public SelectItem[] getItemsAvailableSelectMany() {
206 return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), false);
209 public SelectItem[] getItemsAvailableSelectOne() {
210 return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), true);
213 @FacesConverter(forClass = Node.class)
214 public static class NodeControllerConverter implements Converter {
216 public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
217 if (value == null || value.length() == 0) {
220 NodeController controller = (NodeController) facesContext.getApplication().getELResolver().
221 getValue(facesContext.getELContext(), null, "nodeController");
222 return controller.getJpaController().findNode(getKey(value));
225 java.lang.Long getKey(String value) {
227 key = Long.valueOf(value);
231 String getStringKey(java.lang.Long value) {
232 StringBuffer sb = new StringBuffer();
234 return sb.toString();
237 public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
238 if (object == null) {
241 if (object instanceof Node) {
242 Node o = (Node) object;
243 return getStringKey(o.getId());
245 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + NodeController.class.getName());