1 package org.ufcspa.simdecs.bn.ui;
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;
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 import org.primefaces.event.RowEditEvent;
25 @ManagedBean(name = "nodeController")
27 public class NodeController implements Serializable {
30 private UserTransaction utx = null;
31 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
32 private EntityManagerFactory emf = null;
34 private DataModel items = null;
35 private NodeJpaController jpaController = null;
36 private PaginationHelper pagination;
37 private int selectedItemIndex;
39 public NodeController() {
42 public void rowEditListener(RowEditEvent event) throws Exception {
43 Node node = (Node) event.getObject();
44 getJpaController().edit(node);
47 public Node getSelected() {
48 if (current == null) {
50 selectedItemIndex = -1;
55 private NodeJpaController getJpaController() {
56 if (jpaController == null) {
57 jpaController = new NodeJpaController(utx, emf);
62 public PaginationHelper getPagination() {
63 if (pagination == null) {
64 pagination = new PaginationHelper(10) {
67 public int getItemsCount() {
68 return getJpaController().getNodeCount();
72 public DataModel createPageDataModel() {
73 return new ListDataModel(getJpaController().findNodeEntities(getPageSize(), getPageFirstItem()));
80 public String prepareList() {
85 public String prepareView() {
86 current = (Node) getItems().getRowData();
87 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
91 public String prepareCreate() {
93 selectedItemIndex = -1;
97 public String create() {
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"));
108 public String prepareEdit() {
109 current = (Node) getItems().getRowData();
110 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
114 public String update() {
116 getJpaController().edit(current);
117 JsfUtil.addSuccessMessage(ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("NodeUpdated"));
119 } catch (Exception e) {
120 JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("org/ufcspa/simdecs/bn/Bundle").getString("PersistenceErrorOccured"));
125 public String destroy() {
126 current = (Node) getItems().getRowData();
127 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
129 recreatePagination();
134 public String destroyAndView() {
138 if (selectedItemIndex >= 0) {
141 // all items were removed - go back to list
147 private void performDestroy() {
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"));
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();
166 if (selectedItemIndex >= 0) {
167 current = getJpaController().findNodeEntities(1, selectedItemIndex).get(0);
171 public DataModel getItems() {
173 items = getPagination().createPageDataModel();
178 private void recreateModel() {
182 private void recreatePagination() {
186 public String next() {
187 getPagination().nextPage();
192 public String previous() {
193 getPagination().previousPage();
198 public SelectItem[] getItemsAvailableSelectMany() {
199 return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), false);
202 public SelectItem[] getItemsAvailableSelectOne() {
203 return JsfUtil.getSelectItems(getJpaController().findNodeEntities(), true);
206 @FacesConverter(forClass = Node.class)
207 public static class NodeControllerConverter implements Converter {
209 public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
210 if (value == null || value.length() == 0) {
213 NodeController controller = (NodeController) facesContext.getApplication().getELResolver().
214 getValue(facesContext.getELContext(), null, "nodeController");
215 return controller.getJpaController().findNode(getKey(value));
218 java.lang.Long getKey(String value) {
220 key = Long.valueOf(value);
224 String getStringKey(java.lang.Long value) {
225 StringBuffer sb = new StringBuffer();
227 return sb.toString();
230 public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
231 if (object == null) {
234 if (object instanceof Node) {
235 Node o = (Node) object;
236 return getStringKey(o.getId());
238 throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + NodeController.class.getName());