Populating Lookup Column in JSF Table using Seam
johnurban Oct 8, 2006 9:20 AMI have a jsf datatable thats maps to a personlist list of person objects. While displaying the list of person objects I need to lookup a special attendance code in another table. I am thinking the best way is to call a method the PersonFinderBean called getSecurityCode(). This will go "lookup" the securitycode in the attendance table given the person ID.
I have posted the JSF and SFSB. My question is this the correct way of doing this in Seam?
uPersonCheckin.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <f:view> <f:loadBundle basename="messages" var="msg"/> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Kidslife Checkin</title> <style type="text/css" media="all"> @import "style/default/screen.css"; </style> </head> <body> <%@ include file="header.htm" %> <h:form> <div class="rvgFind"> <fieldset class="rvgFieldSet"> <legend><h:outputText value="#{msg.Person} #{msg.SearchCriteria}"/></legend> <span class="rvgInputs"> <h:outputLabel value="#{msg.Person_firstName}" for="firstName"> <h:inputText value="#{personFinder.example.firstName}" id="firstName"/> </h:outputLabel> <h:outputLabel value="#{msg.Person_lastName}" for="lastName"> <h:inputText value="#{personFinder.example.lastName}" id="lastName"/> </h:outputLabel> </span> <span class="rvgActions"> <h:commandButton type="submit" value="#{msg.Clear}" action="#{personFinder.clear}"/> <h:commandButton type="submit" value="#{msg.Find}" action="#{personFinder.findFirstPage}"/> </span> </fieldset> </div> <div class="rvgResults"> <span class="rvgResultsNone"> <h:outputText value="#{msg.EnterSearchCriteria}" rendered="#{personList==null}"/> <h:outputText value="#{msg.No} #{msg.Person} #{msg.MatchedSearchCriteria}" rendered="#{personList.rowCount==0 && !personFinder.previousPage}"/> </span> <h:dataTable value="#{personList}" var="person" rendered="#{personList.rowCount>0}" rowClasses="rvgRowOne,rvgRowTwo" headerClass="rvgOrder"> <h:column> <f:facet name="header"> <h:outputText value="Edit"/> </f:facet> <h:commandLink value="profile" action="#{personFinder.edit}"> <f:param name="id" value="#{id}"/> </h:commandLink> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Register"/> </f:facet> <h:commandLink value="checkin" action="#{personFinder.checkIn}"> <f:param name="id" value="#{id}"/> </h:commandLink> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Security Code"/> </f:facet> <h:outputText value="#{personFinder.securityCode}"/> </h:column> <h:column> <f:facet name="header"> <h:commandLink value="#{msg.Person_firstName}" action="#{personFinder.reorder}"> <f:param name="orderBy" value="firstName"/> </h:commandLink> </f:facet> <h:outputText value="#{person.firstName}"/> </h:column> <h:column> <f:facet name="header"> <h:commandLink value="#{msg.Person_middleName}" action="#{personFinder.reorder}"> <f:param name="orderBy" value="middleName"/> </h:commandLink> </f:facet> <h:outputText value="#{person.middleName}"/> </h:column> <h:column> <f:facet name="header"> <h:commandLink value="#{msg.Person_lastName}" action="#{personFinder.reorder}"> <f:param name="orderBy" value="lastName"/> </h:commandLink> </f:facet> <h:outputText value="#{person.lastName}"/> </h:column> </h:dataTable> <span class="rvgPage"> <h:commandButton action="#{personFinder.findPreviousPage}" value="#{msg.PreviousPage}" disabled="#{!personFinder.previousPage}" /> <h:commandButton action="#{personFinder.findNextPage}" value="#{msg.NextPage}" disabled="#{!personFinder.nextPage}" /> <h:commandButton action="editPerson" value="#{msg.Create}" rendered="#{personSelector.createEnabled}"/> <h:commandButton action="#{personSelector.selectNone}" value="#{msg.SelectNone}" rendered="#{!personSelector.createEnabled}"/> <h:commandButton action="#{personSelector.cancel}" value="#{msg.Cancel}" rendered="#{!personSelector.createEnabled}"/> </span> </div> </h:form> </body> </f:view> </html>
PersonFinderBean
package testSeam; // Generated Sep 23, 2006 1:30:01 PM by Hibernate Tools 3.2.0.beta7 import static javax.ejb.TransactionAttributeType.NOT_SUPPORTED; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Random; import java.util.Date; import javax.ejb.Remove; import javax.ejb.Stateful; import javax.ejb.TransactionAttribute; import javax.interceptor.Interceptors; import javax.persistence.EntityManager; import javax.persistence.Query; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Destroy; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Out; import org.jboss.seam.annotations.Begin; import org.jboss.seam.annotations.End; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.RequestParameter; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.datamodel.DataModel; import org.jboss.seam.annotations.datamodel.DataModelSelection; import org.jboss.seam.ejb.SeamInterceptor; @Name("personFinder") @Stateful @Scope(ScopeType.SESSION) @Interceptors(SeamInterceptor.class) public class PersonFinderBean implements PersonFinder { private Person example = new Person(); public Person getExample() { return example; } @DataModelSelection @Out(required=false) private Person selectedPerson; @In(required=false) private Login login; @In(create=true) private AttendanceEditor attendanceEditor; private int pageNumber = 0; private int pageSize = 20; private boolean registered = false; public void setRegistered(boolean value) { registered = value; } public boolean getRegistered() { return registered; } public void setPageSize(int size) { pageSize = size; } public int getPageSize() { return pageSize; } public boolean isPreviousPage() { return personList != null && pageNumber > 0; } public boolean isNextPage() { return personList != null && personList.size() == pageSize; } @DataModel private List<Person> personList; @In(create = true) private EntityManager entityManager; private void executeQuery() { Map<String, Object> parameters = new HashMap<String, Object>(); StringBuffer queryString = new StringBuffer(); if (example.getId() != 0) { queryString.append(" and person.id = :id"); parameters.put("id", example.getId()); } if (example.getRoomId() != 0) { queryString.append(" and person.roomId = :roomId"); parameters.put("roomId", example.getRoomId()); } if (example.getOrganizationId() != 0) { queryString.append(" and person.organizationId = :organizationId"); parameters.put("organizationId", example.getOrganizationId()); } if (example.getFirstName() != null && example.getFirstName().length() > 0) { queryString.append(" and person.firstName like :firstName"); parameters.put("firstName", '%' + example.getFirstName() + '%'); } if (example.getMiddleName() != null && example.getMiddleName().length() > 0) { queryString.append(" and person.middleName like :middleName"); parameters.put("middleName", '%' + example.getMiddleName() + '%'); } if (example.getLastName() != null && example.getLastName().length() > 0) { queryString.append(" and person.lastName like :lastName"); parameters.put("lastName", '%' + example.getLastName() + '%'); } if (example.getAddress1() != null && example.getAddress1().length() > 0) { queryString.append(" and person.address1 like :address1"); parameters.put("address1", '%' + example.getAddress1() + '%'); } if (example.getCity() != null && example.getCity().length() > 0) { queryString.append(" and person.city like :city"); parameters.put("city", '%' + example.getCity() + '%'); } if (example.getState() != null && example.getState().length() > 0) { queryString.append(" and person.state like :state"); parameters.put("state", '%' + example.getState() + '%'); } if (example.getZip() != null && example.getZip().length() > 0) { queryString.append(" and person.zip like :zip"); parameters.put("zip", '%' + example.getZip() + '%'); } if (example.getEmail() != null && example.getEmail().length() > 0) { queryString.append(" and person.email like :email"); parameters.put("email", '%' + example.getEmail() + '%'); } if (example.getBirthday() != null) { queryString.append(" and person.birthday = :birthday"); parameters.put("birthday", example.getBirthday()); } if (example.getSex() != null) { queryString.append(" and person.sex = :sex"); parameters.put("sex", example.getSex()); } if (example.getHomePhone() != null && example.getHomePhone().length() > 0) { queryString.append(" and person.homePhone like :homePhone"); parameters.put("homePhone", '%' + example.getHomePhone() + '%'); } if (example.getWorkPhone() != null && example.getWorkPhone().length() > 0) { queryString.append(" and person.workPhone like :workPhone"); parameters.put("workPhone", '%' + example.getWorkPhone() + '%'); } if (example.getBeeperPhone() != null && example.getBeeperPhone().length() > 0) { queryString.append(" and person.beeperPhone like :beeperPhone"); parameters.put("beeperPhone", '%' + example.getBeeperPhone() + '%'); } if (example.getCellPhone() != null && example.getCellPhone().length() > 0) { queryString.append(" and person.cellPhone like :cellPhone"); parameters.put("cellPhone", '%' + example.getCellPhone() + '%'); } if (example.getExtraPhone() != null && example.getExtraPhone().length() > 0) { queryString.append(" and person.extraPhone like :extraPhone"); parameters.put("extraPhone", '%' + example.getExtraPhone() + '%'); } if (example.getExtraPhoneMemo() != null && example.getExtraPhoneMemo().length() > 0) { queryString .append(" and person.extraPhoneMemo like :extraPhoneMemo"); parameters.put("extraPhoneMemo", '%' + example.getExtraPhoneMemo() + '%'); } if (example.getSchool() != null && example.getSchool().length() > 0) { queryString.append(" and person.school like :school"); parameters.put("school", '%' + example.getSchool() + '%'); } if (example.getSchoolGrade() != null && example.getSchoolGrade().length() > 0) { queryString.append(" and person.schoolGrade like :schoolGrade"); parameters.put("schoolGrade", '%' + example.getSchoolGrade() + '%'); } if (example.getContact() != null && example.getContact().length() > 0) { queryString.append(" and person.contact like :contact"); parameters.put("contact", '%' + example.getContact() + '%'); } if (example.getParentName() != null && example.getParentName().length() > 0) { queryString.append(" and person.parentName like :parentName"); parameters.put("parentName", '%' + example.getParentName() + '%'); } if (example.getComments() != null && example.getComments().length() > 0) { queryString.append(" and person.comments like :comments"); parameters.put("comments", '%' + example.getComments() + '%'); } if (example.getMinsitry() != null && example.getMinsitry().length() > 0) { queryString.append(" and person.minsitry like :minsitry"); parameters.put("minsitry", '%' + example.getMinsitry() + '%'); } if (example.getDrama() != null) { queryString.append(" and person.drama = :drama"); parameters.put("drama", example.getDrama()); } if (example.getMusic() != null) { queryString.append(" and person.music = :music"); parameters.put("music", example.getMusic()); } if (example.getStatus() != null) { queryString.append(" and person.status = :status"); parameters.put("status", example.getStatus()); } if (example.getVisitor() != null) { queryString.append(" and person.visitor = :visitor"); parameters.put("visitor", example.getVisitor()); } if (example.getMailList() != null) { queryString.append(" and person.mailList = :mailList"); parameters.put("mailList", example.getMailList()); } if (example.getLeader() != null) { queryString.append(" and person.leader = :leader"); parameters.put("leader", example.getLeader()); } if (example.getLastAttended() != null) { queryString.append(" and person.lastAttended = :lastAttended"); parameters.put("lastAttended", example.getLastAttended()); } if (example.getCreated() != null) { queryString.append(" and person.created = :created"); parameters.put("created", example.getCreated()); } if (example.getUpdated() != null) { queryString.append(" and person.updated = :updated"); parameters.put("updated", example.getUpdated()); } if (example.getVcall() != null) { queryString.append(" and person.vcall = :vcall"); parameters.put("vcall", example.getVcall()); } if (example.getVhouseCall() != null) { queryString.append(" and person.vhouseCall = :vhouseCall"); parameters.put("vhouseCall", example.getVhouseCall()); } if (example.getTeam() != null && example.getTeam().length() > 0) { queryString.append(" and person.team like :team"); parameters.put("team", '%' + example.getTeam() + '%'); } if (example.getPhotoFile() != null && example.getPhotoFile().length() > 0) { queryString.append(" and person.photoFile like :photoFile"); parameters.put("photoFile", '%' + example.getPhotoFile() + '%'); } if (example.getUniqueId() != null && example.getUniqueId().length() > 0) { queryString.append(" and person.uniqueId like :uniqueId"); parameters.put("uniqueId", '%' + example.getUniqueId() + '%'); } if (queryString.length() == 0) { queryString.append("select person from Person person"); } else { queryString.delete(0, 4).insert(0, "select person from Person person where"); } if (order != null) { queryString.append(" order by person.").append(order); if (descending) queryString.append(" desc"); } Query query = entityManager.createQuery(queryString.toString()); for (Entry<String, Object> param : parameters.entrySet()) { query.setParameter(param.getKey(), param.getValue()); } personList = (List<Person>) query.setMaxResults(pageSize) .setFirstResult(pageSize * pageNumber).getResultList(); } public String findFirstPage() { pageNumber = 0; executeQuery(); return null; } public String findNextPage() { pageNumber++; executeQuery(); return null; } public String findPreviousPage() { pageNumber--; executeQuery(); return null; } public void refresh() { if (personList != null) executeQuery(); } public String clear() { personList = null; example = new Person(); return null; } public Person getSelection() { return entityManager.merge(selectedPerson); } @Destroy @Remove public void destroy() { } private String order; private boolean descending = false; @RequestParameter private String orderBy; private int id; public String reorder() { if (orderBy.equals(order)) { descending = !descending; } else { descending = false; } order = orderBy; executeQuery(); return null; } @Begin @End public String checkIn() { // insert new record into attendance table Users loggedInUser = login.getInstance(); System.out.println("I check'd em in...:"+selectedPerson.getId()); System.out.println("I am from church:"+loggedInUser.getOrganizationId()); java.util.Random rNum = new Random(); int securityCode = Math.abs(rNum.nextInt()); Attendance attend = attendanceEditor.getInstance(); attend.setCheckedByUserId(loggedInUser.getId()); attend.setDateTimeEntry(new Date()); attend.setPersonId(selectedPerson.getId()); attend.setRoomId(example.getRoomId()); attend.setSecurityCode(""+securityCode); attend.setType("In"); attendanceEditor.create(); return "code:"+securityCode; } public String getSecurityCode() { Attendance attend = attendanceEditor.getInstance(); //Here is where I want to do the lookup.. return "special code"; } public String edit() { return "/editPerson.jsp"; } }