package com.example.forgeWiz3.view; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.ejb.SessionContext; import javax.ejb.Stateful; import javax.enterprise.context.Conversation; import javax.enterprise.context.ConversationScoped; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import com.example.forgeWiz3.model.Staff; /** * Backing bean for Staff entities. *

* This class provides CRUD functionality for all Staff entities. It focuses * purely on Java EE 6 standards (e.g. @ConversationScoped for * state management, PersistenceContext for persistence, * CriteriaBuilder for searches) rather than introducing a CRUD framework or * custom base class. */ @Named @Stateful @ConversationScoped public class StaffBean implements Serializable { private static final long serialVersionUID = 1L; /* * Support creating and retrieving Staff entities */ private Byte id; public Byte getId() { return this.id; } public void setId(Byte id) { this.id = id; } private Staff staff; public Staff getStaff() { return this.staff; } @Inject private Conversation conversation; @PersistenceContext(type = PersistenceContextType.EXTENDED) private EntityManager entityManager; public String create() { this.conversation.begin(); return "create?faces-redirect=true"; } public void retrieve() { if (FacesContext.getCurrentInstance().isPostback()) { return; } if (this.conversation.isTransient()) { this.conversation.begin(); } if (this.id == null) { this.staff = this.example; } else { this.staff = findById(getId()); } } public Staff findById(Byte id) { return this.entityManager.find(Staff.class, id); } /* * Support updating and deleting Staff entities */ public String update() { this.conversation.end(); try { if (this.id == null) { this.entityManager.persist(this.staff); return "search?faces-redirect=true"; } else { this.entityManager.merge(this.staff); return "view?faces-redirect=true&id=" + this.staff.getStaffId(); } } catch (Exception e) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage())); return null; } } public String delete() { this.conversation.end(); try { Staff deletableEntity = findById(getId()); this.entityManager.remove(deletableEntity); this.entityManager.flush(); return "search?faces-redirect=true"; } catch (Exception e) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage())); return null; } } /* * Support searching Staff entities with pagination */ private int page; private long count; private List pageItems; private Staff example = new Staff(); public int getPage() { return this.page; } public void setPage(int page) { this.page = page; } public int getPageSize() { return 10; } public Staff getExample() { return this.example; } public void setExample(Staff example) { this.example = example; } public void search() { this.page = 0; } public void paginate() { CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); // Populate this.count CriteriaQuery countCriteria = builder.createQuery(Long.class); Root root = countCriteria.from(Staff.class); countCriteria = countCriteria.select(builder.count(root)).where( getSearchPredicates(root)); this.count = this.entityManager.createQuery(countCriteria) .getSingleResult(); // Populate this.pageItems CriteriaQuery criteria = builder.createQuery(Staff.class); root = criteria.from(Staff.class); TypedQuery query = this.entityManager.createQuery(criteria .select(root).where(getSearchPredicates(root))); query.setFirstResult(this.page * getPageSize()).setMaxResults( getPageSize()); this.pageItems = query.getResultList(); } private Predicate[] getSearchPredicates(Root root) { CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); List predicatesList = new ArrayList(); byte staffId = this.example.getStaffId(); if (staffId != 0) { predicatesList.add(builder.equal(root.get("staffId"), staffId)); } String firstName = this.example.getFirstName(); if (firstName != null && !"".equals(firstName)) { predicatesList.add(builder.like(root. get("firstName"), '%' + firstName + '%')); } String lastName = this.example.getLastName(); if (lastName != null && !"".equals(lastName)) { predicatesList.add(builder.like(root. get("lastName"), '%' + lastName + '%')); } short addressId = this.example.getAddressId(); if (addressId != 0) { predicatesList.add(builder.equal(root.get("addressId"), addressId)); } byte picture = this.example.getPicture(); if (picture != 0) { predicatesList.add(builder.equal(root.get("picture"), picture)); } return predicatesList.toArray(new Predicate[predicatesList.size()]); } public List getPageItems() { return this.pageItems; } public long getCount() { return this.count; } /* * Support listing and POSTing back Staff entities (e.g. from inside an * HtmlSelectOneMenu) */ public List getAll() { CriteriaQuery criteria = this.entityManager .getCriteriaBuilder().createQuery(Staff.class); return this.entityManager.createQuery( criteria.select(criteria.from(Staff.class))).getResultList(); } @Resource private SessionContext sessionContext; public Converter getConverter() { final StaffBean ejbProxy = this.sessionContext.getBusinessObject(StaffBean.class); return new Converter() { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { return ejbProxy.findById(Byte.valueOf(value)); } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (value == null) { return ""; } return String.valueOf(((Staff) value).getStaffId()); } }; } /* * Support adding children to bidirectional, one-to-many tables */ private Staff add = new Staff(); public Staff getAdd() { return this.add; } public Staff getAdded() { Staff added = this.add; this.add = new Staff(); return added; } }