Easy Object Select

This page demonstrates selecting from a more complex model - a list of objects from the database.
This is easy to achieve using SelectModelFactory and a custom ValueEncoder, and together
they allow us to avoid storing the list of objects in the session and avoid rebuilding the list on submit.
Person:
You chose:
In this example we feed a list of Persons (from the database) into SelectModelFactory to produce a SelectModel.
We tell it to use firstName as the option label.
We want the option value to be the person's id so we provide PersonEncoder, which converts between a Person, server-side, and their id, for client-side.
Note that PersonEncoder goes to the database when it converts an id to a Person, but this happens only on submit, and only for the chosen person.

References: Select, Using Select with a List, SelectModel, SelectModelFactory, ValueEncoder, EventContext, How To Select With Objects.

Home

The source for IPersonFinderServiceLocal and @EJB is shown in the Session Beans and @EJB examples.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- We need a doctype to allow us to use special characters like &nbsp; 
     We use a "strict" DTD to make IE follow the alignment rules. -->
     
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">
<body class="container">
    <h3>Easy Object Select</h3>
    
    This page demonstrates selecting from a more complex model - a list of objects from the database.<br/>
    This is easy to achieve using <strong>SelectModelFactory</strong> and a custom <strong>ValueEncoder</strong>, and together <br/>
    they allow us to avoid storing the list of objects in the session and avoid rebuilding the list on submit.

    <div class="eg">
        <t:form t:id="form" class="form-inline">
            <div class="form-group">
                Person: 
                <t:select model="personsModel" value="person" encoder="personEncoder"/>
            </div>
            <div class="form-group">
                <t:submit/>
            </div>
            <div class="form-group">
                You chose: ${person}
            </div>
        </t:form>
    </div>
    
    In this example we feed a list of Persons (from the database) into SelectModelFactory to produce a SelectModel.<br/>
    We tell it to use firstName as the option label. <br/>
    We want the option value to be the person's id so we provide PersonEncoder, which converts between a Person, server-side, and their id, for client-side.<br/>
    Note that PersonEncoder goes to the database when it converts an id to a Person, but this happens only on submit, and only for the chosen person.<br/><br/>

    References:
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Select.html">Select</a>,
    <a href="http://tapestry.apache.org/using-select-with-a-list.html">Using Select with a List</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/SelectModel.html">SelectModel</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/services/SelectModelFactory.html">SelectModelFactory</a>,  
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/ValueEncoder.html">ValueEncoder</a>,  
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/EventContext.html">EventContext</a>, 
    <a href="http://wiki.apache.org/tapestry/Tapestry5HowtoSelectWithObjects">How To Select With Objects</a>.<br/><br/>
    
    <t:pagelink page="Index">Home</t:pagelink><br/><br/>
    
    The source for IPersonFinderServiceLocal and @EJB is shown in the Session Beans and @EJB examples.<br/><br/>
    
    <t:tabgroup>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/select/EasyObjectSelect.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/select/EasyObjectSelect.java"/>
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/select.css"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/encoders/examples/PersonEncoder.java"/>
        <t:sourcecodetab src="/business/src/main/java/jumpstart/business/domain/person/Person.java"/>
    </t:tabgroup>
</body>
</html>


package jumpstart.web.pages.examples.select;

import java.util.List;

import javax.ejb.EJB;

import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;
import jumpstart.web.encoders.examples.PersonEncoder;

import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.SelectModelFactory;

@Import(stylesheet = "css/examples/select.css")
public class EasyObjectSelect {
    static private final int MAX_RESULTS = 30;

    // The activation context

    private Long personId;

    // Screen fields

    @Property
    private SelectModel personsModel;

    @Property
    private Person person;

    // Generally useful bits and pieces

    @Inject
    private SelectModelFactory selectModelFactory;

    @EJB
    private IPersonFinderServiceLocal personFinderService;

    // The code

    void onActivate(EventContext context) {
        if (context.getCount() > 0) {
            personId = context.get(Long.class, 0);
        }
    }

    Long onPassivate() {
        return personId;
    }

    void onPrepare() {
        // Get all persons - ask business service to find them (from the database)
        List<Person> persons = personFinderService.findPersons(MAX_RESULTS);

        if (personId != null) {
            person = findPersonInList(personId, persons);
        }

        personsModel = selectModelFactory.create(persons, "firstName");
    }

    void onValidateFromForm() {
        personId = person == null ? null : person.getId();
    }

    private Person findPersonInList(Long personId, List<Person> persons) {
        for (Person person : persons) {
            if (person.getId().equals(personId)) {
                return person;
            }
        }
        return null;
    }

    public PersonEncoder getPersonEncoder() {
        return new PersonEncoder(personFinderService);
    }

}


.eg {
                margin: 20px 0;
                padding: 14px;
                color: #333;
                border: 1px solid #ddd;
                border-radius: 6px;
                -webkit-border-radius: 6px;
                -mox-border-radius: 6px;
}

.eg select.form-control {
                width: auto;
}


package jumpstart.web.encoders.examples;

import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;

import org.apache.tapestry5.ValueEncoder;

public class PersonEncoder implements ValueEncoder<Person> {

    private IPersonFinderServiceLocal personFinderService;

    public PersonEncoder(IPersonFinderServiceLocal personFinderService) {
        this.personFinderService = personFinderService;
    }

    @Override
    public String toClient(Person value) {
        return String.valueOf(value.getId());
    }

    @Override
    public Person toValue(String id) {
        return personFinderService.findPerson(Long.parseLong(id));
    }

}


package jumpstart.business.domain.person;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


/**
 * The Person entity.
 */
@Entity
@SuppressWarnings("serial")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long id;

    @Version
    @Column(nullable = false)
    private Integer version;

    @Column(length = 10, nullable = false)
    @NotNull
    @Size(max = 10)
    private String firstName;

    @Column(length = 10, nullable = false)
    @NotNull
    @Size(max = 10)
    private String lastName;
    
    @Enumerated(EnumType.STRING)
    @NotNull
    private Regions region;

    @Temporal(TemporalType.DATE)
    @NotNull
    private Date startDate;

    public String toString() {
        final String DIVIDER = ", ";
        
        StringBuilder buf = new StringBuilder();
        buf.append(this.getClass().getSimpleName() + ": ");
        buf.append("[");
        buf.append("id=" + id + DIVIDER);
        buf.append("version=" + version + DIVIDER);
        buf.append("firstName=" + firstName + DIVIDER);
        buf.append("lastName=" + lastName + DIVIDER);
        buf.append("region=" + region + DIVIDER);
        buf.append("startDate=" + startDate);
        buf.append("]");
        return buf.toString();
    }

    // Default constructor is required by JPA.
    public Person() {
    }

    public Person(String firstName, String lastName, Regions region, Date startDate) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.region = region;
        this.startDate = startDate;
    }

    // The need for an equals() method is discussed at http://www.hibernate.org/109.html
    
    @Override
    public boolean equals(Object obj) {
        return (obj == this) || (obj instanceof Person) && id != null && id.equals(((Person) obj).getId());
    }

    // The need for a hashCode() method is discussed at http://www.hibernate.org/109.html

    @Override
    public int hashCode() {
        return id == null ? super.hashCode() : id.hashCode();
    }

    @PrePersist
    @PreUpdate
    public void validate() throws ValidationException {

    }

    public Long getId() {
        return id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Regions getRegion() {
        return region;
    }

    public void setRegion(Regions region) {
        this.region = region;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

}