<!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
We use a "strict" DTD to make IE follow the alignment rules. -->
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
<head>
<link rel="stylesheet" type="text/css" href="${context:css/examples/tables/editableloop.css}"/>
</head>
<body>
<h1>Editable Loop For Update (1)</h1>
A table built with a Form around a Loop to allow update of persons.<br/>
To demonstrate a server-side error, change any First Name to <em>${BAD_NAME}</em>.<br/><br/>
The key difference from the Editable Loop example is that now we are dealing with existing entities, <br/>
so we drop formstate="<em>ITERATION</em>" from the Loop and give it a custom ValueEncoder instead.<br/>
<div class="eg">
<form t:type="form" t:id="personsEdit">
<t:errors/>
<table class="grid">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Start Date</th>
</tr>
</thead>
<tbody>
<tr t:type="Loop" t:source="persons" t:value="person" t:encoder="personEncoder" class="prop:evenodd.next">
<td>${person.id}</td>
<td><input t:id="firstName" t:type="TextField" t:value="person.firstName" t:validate="required, maxlength=10" size="10"/></td>
<td>${person.lastName}</td>
<td><t:output value="person.startDate" format="dateFormat"/></td>
<!-- We shadow each output-only with a hidden field to enable redisplay of the list exactly as it was submitted. -->
<t:hidden value="person.lastName"/>
<t:hidden value="person.startDate" t:encoder="dateEncoder"/>
<!-- We ensure version is submitted, to ensure optimistic locking. Optimistic locking is essential for this solution. -->
<t:hidden value="person.version"/>
</tr>
</tbody>
</table><br/>
<input t:type="submit" value="Save"/>
<a t:type="eventlink" t:event="refresh" href="#" style="margin-left: 5px;">Refresh</a>
</form>
</div>
Features:<br/>
<ul>
<li>If another process creates a person by the time you submit, we ignore it. The encoder ensures we target the submitted persons only.</li>
<li>If another process updates a person by the time you submit, your update will be rejected by the business layer (optimistic locking exception).</li>
<li>If another process deletes a person by the time you submit, we treat it as an error.</li>
<li>On error, we redisplay the list with the same persons and values you submitted. Here's how:
<ul>
<li>Form doesn't submit output-only fields but it does submit hidden fields, so we shadow each output-only field with a Hidden.</li>
<li>Form doesn't carry Hidden values through to redisplay, so we do it ourselves use FLASH-persistence - see onFailure() and onPrepareForRender().</li>
</ul>
</li>
</ul>
References:
<a href="http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/Loop.html">Loop</a>,
<a href="http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/ValueEncoder.html">ValueEncoder</a>,
<a href="http://tapestry.apache.org/forms-and-validation.html">Forms and Validation</a>.<br/><br/>
<a t:type="pagelink" t:page="Index" href="#">Home</a><br/><br/>
The source for PersonFinderService, @EJB handling, etc. is shown in the @EJB example.<br/><br/>
<t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/examples/tables/EditableLoopForUpdate1.tml"/>
<t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/examples/tables/EditableLoopForUpdate1.java"/>
<t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/css/examples/tables/editableloop.css"/>
<t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/commons/FieldCopy.java"/>
<t:sourcecodedisplay src="/business/src/main/java/jumpstart/business/domain/person/Person.java"/>
<t:sourcecodedisplay src="/business/src/main/java/jumpstart/business/domain/person/Regions.java"/>
</body>
</html>
package jumpstart.web.pages.examples.tables;
import java.text.DateFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.ejb.EJB;
import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;
import jumpstart.util.ExceptionUtil;
import jumpstart.web.commons.EvenOdd;
import jumpstart.web.commons.FieldCopy;
import org.apache.tapestry5.Field;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
public class EditableLoopForUpdate1 {
static private final int MAX_RESULTS = 30;
// Screen fields
@Property
private List<Person> persons;
private Person person;
@Property
private final PersonEncoder personEncoder = new PersonEncoder();
@Property
private final DateEncoder dateEncoder = new DateEncoder();
@Property
private EvenOdd evenOdd;
@Property
private final String BAD_NAME = "Acme";
// Work fields
private List<Person> personsInDB;
private boolean inFormSubmission;
private List<Person> personsSubmitted;
// This carries the list of submitted persons through the redirect that follows a server-side validation failure.
// We do this to compensate for the fact that Form doesn't carry Hidden component values through a redirect.
@Persist(PersistenceConstants.FLASH)
private List<Person> personsSubmittedFlash;
private int rowNum;
private Map<Integer, FieldCopy> firstNameCopyByRowNum;
// Other pages
@InjectPage
private EditableLoopForUpdate2 page2;
// Generally useful bits and pieces
@Component(id = "personsEdit")
private Form form;
@InjectComponent
private TextField firstName;
@EJB
private IPersonFinderServiceLocal personFinderService;
@Inject
private Locale currentLocale;
// The code
void onActivate() {
inFormSubmission = false;
}
// Form bubbles up the PREPARE_FOR_SUBMIT event during form submission.
void onPrepareForRender() {
evenOdd = new EvenOdd();
// If fresh start, populate screen with all persons from the database
if (form.isValid()) {
// Get all persons - ask business service to find them (from the database)
personsInDB = personFinderService.findPersons(MAX_RESULTS);
// Populate the persons to be edited.
persons = new ArrayList<Person>();
for (Person personInDB : personsInDB) {
persons.add(personInDB);
}
}
// Else, we're rendering after a redirect, so rebuild the list with the same persons as were submitted
else {
persons = new ArrayList<Person>(personsSubmittedFlash);
}
}
// Form bubbles up the PREPARE_FOR_SUBMIT event during form submission.
void onPrepareForSubmit() {
inFormSubmission = true;
personsSubmitted = new ArrayList<Person>();
// Get all persons - ask business service to find them (from the database)
personsInDB = personFinderService.findPersons(MAX_RESULTS);
// Prepare to take a copy of each editable field.
rowNum = 0;
firstNameCopyByRowNum = new HashMap<Integer, FieldCopy>();
}
void onValidateFromFirstName() {
rowNum++;
firstNameCopyByRowNum.put(rowNum, new FieldCopy(firstName));
}
void onValidateFromPersonsEdit() {
if (form.getHasErrors()) {
// We get here only if a server-side validator detected an error.
return;
}
// Error if any person submitted has a null id - it means toValue(...) found they are no longer in the database.
for (Person personSubmitted : personsSubmitted) {
if (personSubmitted.getId() == null) {
form.recordError("The list of persons is out of date. Please refresh and try again.");
return;
}
}
// Simulate a server-side validation error: return error if anyone's first name is BAD_NAME.
rowNum = 0;
for (Person personSubmitted : personsSubmitted) {
rowNum++;
if (personSubmitted.getFirstName() != null && personSubmitted.getFirstName().equals(BAD_NAME)) {
// Unfortunately, at this point the field firstName is from the final row of the Loop.
// Fortunately, we have a copy of the correct field, so we can record the error with that.
Field field = firstNameCopyByRowNum.get(rowNum);
form.recordError(field, "First name cannot be " + BAD_NAME + ".");
return;
}
}
try {
System.out.println(">>> personsSubmitted = " + personsSubmitted);
// In a real application we would persist them to the database instead of printing them
// personManagerService.changePersons(personsSubmitted);
}
catch (Exception e) {
// Display the cause. In a real system we would try harder to get a user-friendly message.
form.recordError(ExceptionUtil.getRootCauseMessage(e));
}
}
Object onSuccess() {
page2.set(personsSubmitted);
return page2;
}
void onFailure() {
personsSubmittedFlash = new ArrayList<Person>(personsSubmitted);
}
void onRefresh() {
// By doing nothing the page will be displayed afresh.
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
if (inFormSubmission) {
personsSubmitted.add(person);
}
}
// This encoder is used by our Loop:
// - during render, to convert each person to an id (Loop then stores the ids in the form, hidden).
// - during form submission, to convert each id back to a person which it puts in our person field. Loop will
// overwrite the person returned with the input fields and hidden fields.
private class PersonEncoder implements ValueEncoder<Person> {
@Override
public String toClient(Person person) {
Long id = person.getId();
return id == null ? null : id.toString();
}
@Override
public Person toValue(String idAsString) {
Person person = null;
if (idAsString == null) {
person = new Person();
}
else {
Long id = new Long(idAsString);
person = findPerson(id);
// If person has since been deleted from the DB. Create a skeleton person.
if (person == null) {
person = new Person();
}
}
// Loop will overwrite the firstName of the person returned.
return person;
}
private Person findPerson(Long id) {
// We could find the person in the database, but it's cheaper to search the list we got in
// onPrepareForSubmit().
for (Person personInDB : personsInDB) {
if (personInDB.getId().equals(id)) {
return personInDB;
}
}
return null;
}
};
public Format getDateFormat() {
return DateFormat.getDateInstance(DateFormat.MEDIUM, currentLocale);
}
private class DateEncoder implements ValueEncoder<Date> {
@Override
public String toClient(Date date) {
long timeMillis = date.getTime();
return Long.toString(timeMillis);
}
@Override
public Date toValue(String timeMillisAsString) {
long timeMillis = Long.parseLong(timeMillisAsString);
Date date = new Date(timeMillis);
return date;
}
}
}
body, td { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; font-weight: normal; color: #333;
line-height: 17px; }
h1 { font-size: 26px; line-height: 20px; } /* For IE 7 */
.eg { margin: 20px 0; padding: 20px;
border: 1px solid #ddd; border-radius: 4px; -webkit-border-radius: 4px; -mox-border-radius: 4px; }
a { text-decoration: none; color: #3D69B6; }
a:hover { text-decoration: underline; }
.grid { border-collapse: collapse; border-spacing: 0; border: 1px solid #dddddd; font-size: 13px; }
.grid tr.odd { background-color: #f8f8f8; }
.grid th { padding: 3px 5px; text-align: left; min-width: 130px; border: 1px solid #dddddd;
font-weight: normal; background-color: #eeeeee;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fbfbfb', endColorstr='#e4e4e4'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#e4e4e4)); /* for webkit browsers */
background: -moz-linear-gradient(top, #fbfbfb, #e4e4e4); /* for firefox 3.6+ */ }
.grid td { padding: 3px 5px; text-align:left; }
.nodata { margin-top: 5px; margin-left:20px; }
// Based on a solution by Stephan Windmüller in http://tapestry.1045711.n5.nabble.com/Cross-Validation-in-dynamic-Forms-td2427275.html
// and Shing Hing Man in http://tapestry.1045711.n5.nabble.com/how-to-recordError-against-a-form-field-in-a-loop-td5719832.html .
package jumpstart.web.commons;
import org.apache.tapestry5.Field;
/**
* An immutable copy of a Field. Handy for taking a copy of a Field in a row as a Loop iterates through them.
*/
public class FieldCopy implements Field {
private String clientId;
private String controlName;
private String label;
private boolean disabled;
private boolean required;
public FieldCopy(Field field) {
clientId = field.getClientId();
controlName = field.getControlName();
label = field.getLabel();
disabled = field.isDisabled();
required = field.isRequired();
}
@Override
public String getClientId() {
return clientId;
}
@Override
public String getControlName() {
return controlName;
}
@Override
public String getLabel() {
return label;
}
@Override
public boolean isDisabled() {
return disabled;
}
@Override
public boolean isRequired() {
return required;
}
}
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;
/**
* 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)
private String firstName;
@Column(length = 10, nullable = false)
private String lastName;
@Enumerated(EnumType.STRING)
private Regions region;
@Temporal(TemporalType.DATE)
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 EJB3.
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 {
// Validate syntax...
if ((firstName == null) || (firstName.trim().length() == 0)) {
throw new ValidationException("First name is required.");
}
if ((lastName == null) || (lastName.trim().length() == 0)) {
throw new ValidationException("Last name is required.");
}
if (region == null) {
throw new ValidationException("Region is required.");
}
if (startDate == null) {
throw new ValidationException("Start date is required.");
}
}
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;
}
}
package jumpstart.business.domain.person;
public enum Regions {
EAST_COAST, WEST_COAST;
}