<!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_4.xsd">
<body class="container">
<h3>Loop With Delete Column (1)</h3>
A table built with the Loop component, including only the Person columns we want, and adding a Delete column.<br/><br/>
To demonstrate a server-side error, try to delete a person whose First Name is <em>${GOOD_NAME}</em>.<br/>
<div class="eg">
<t:form t:id="deletables">
<t:errors globalOnly="true"/>
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Start Date</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<t:loop source="persons" value="person" encoder="personEncoder">
<tr>
<td>${person.id}</td>
<td>${person.firstName}</td>
<td>${person.lastName}</td>
<td><t:output value="person.startDate" format="dateFormat"/></td>
<td><t:checkbox t:id="delete" value="delete"/></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.firstName"/>
<t:hidden value="person.lastName"/>
<t:hidden value="person.startDate" t:encoder="dateEncoder"/>
<!-- If optimistic locking is not needed then comment out this next line. -->
<t:hidden value="person.version"/>
</tr>
</t:loop>
</tbody>
</table>
<t:submit/>
<t:eventlink event="refresh" class="btn btn-default">Refresh</t:eventlink>
</t:form>
</div>
This example works very much like the Editable Loop For Update example.<br/><br/>
References:
<a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Loop.html">Loop</a>,
<a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/ValueEncoder.html">ValueEncoder</a>,
<a href="http://tapestry.apache.org/forms-and-validation.html">Forms and Validation</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/tables/LoopWithDeleteColumn1.tml"/>
<t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/tables/LoopWithDeleteColumn1.java"/>
<t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css"/>
<t:sourcecodetab src="/web/src/main/java/jumpstart/web/commons/FieldCopy.java"/>
<t:sourcecodetab src="/business/src/main/java/jumpstart/business/domain/person/Person.java"/>
</t:tabgroup>
</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.commons.IdVersion;
import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;
import jumpstart.util.ExceptionUtil;
import jumpstart.web.commons.FieldCopy;
import org.apache.tapestry5.Field;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Checkbox;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
@Import(stylesheet = "css/examples/plain.css")
public class LoopWithDeleteColumn1 {
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 final String GOOD_NAME = "Mary";
// Work fields
private List<Person> personsInDB;
private boolean inFormSubmission;
private List<Person> personsSubmitted;
private List<IdVersion> personsToDelete;
private int rowNum;
private Map<Integer, FieldCopy> deleteFieldCopyByRowNum;
// Other pages
@InjectPage
private LoopWithDeleteColumn2 page2;
// Generally useful bits and pieces
@InjectComponent("deletables")
private Form form;
@InjectComponent("delete")
private Checkbox deleteField;
@EJB
private IPersonFinderServiceLocal personFinderService;
@Inject
private Locale currentLocale;
// The code
void onActivate() {
inFormSubmission = false;
}
// Form bubbles up the PREPARE_FOR_RENDER event during form render.
void onPrepareForRender() {
// 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);
persons = new ArrayList<Person>();
for (Person personInDB : personsInDB) {
persons.add(personInDB);
}
}
}
// Form bubbles up the PREPARE_FOR_SUBMIT event during form submission.
void onPrepareForSubmit() {
inFormSubmission = true;
personsSubmitted = new ArrayList<Person>();
personsToDelete = new ArrayList<IdVersion>();
// 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 = -1;
deleteFieldCopyByRowNum = new HashMap<Integer, FieldCopy>();
}
void onValidateFromDelete() {
// Unfortunately, this method is never called because Checkbox doesn't bubble up VALIDATE. It's a shame because
// this would be the perfect place to validate whether deleting is OK, or to put an entry in
// deleteFieldCopyByRowNum.
// Please vote for https://issues.apache.org/jira/browse/TAP5-2075 .
}
void onValidateFromDeletables() {
// Error if any person to delete has a null id - it means toValue(...) found they are no longer in the database.
for (IdVersion personToDelete : personsToDelete) {
if (personToDelete.getId() == null) {
form.recordError("The list of persons is out of date. Please refresh and try again.");
return;
}
}
// Populate our list of persons to delete with the submitted versions (see setDelete(...) for more).
// Also, simulate a server-side validation error: return error if deleting a person with first name BAD_NAME.
for (IdVersion personToDelete : personsToDelete) {
rowNum = -1;
for (Person personSubmitted : personsSubmitted) {
rowNum++;
if (personSubmitted.getId() != null && personSubmitted.getId().equals(personToDelete.getId())) {
personToDelete.setVersion(personSubmitted.getVersion());
// Unfortunately, at this point the deleteField 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.
if (personSubmitted.getId() == 2 && personSubmitted.getFirstName() != null
&& personSubmitted.getFirstName().equals(GOOD_NAME)) {
Field field = deleteFieldCopyByRowNum.get(rowNum);
form.recordError(field, "Cannot delete " + GOOD_NAME + ".");
}
break;
}
}
}
if (form.getHasErrors()) {
// We get here only if a server-side validator detected an error.
return;
}
try {
System.out.println(">>> personsSubmitted = " + personsSubmitted);
System.out.println(">>> personsToDelete = " + personsToDelete);
// In a real application we would persist them to the database instead of printing them
// personManagerService.bulkEditPersons(new ArrayList<Person>(), new ArrayList<Person>(), personsToDelete);
}
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(personsToDelete);
return page2;
}
void onFailure() {
persons = 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.
private class PersonEncoder implements ValueEncoder<Person> {
@Override
public String toClient(Person value) {
Long id = person.getId();
return id == null ? null : id.toString();
}
@Override
public Person toValue(String idAsString) {
Long id = idAsString == null ? null : new Long(idAsString);
Person 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 person : personsInDB) {
if (person.getId().equals(id)) {
return person;
}
}
return null;
}
};
// The Loop component will automatically call this for every row as it is rendered.
public boolean isDelete() {
return false;
}
// The Loop component will automatically call this for every row on submit.
public void setDelete(boolean delete) {
if (inFormSubmission) {
rowNum++;
deleteFieldCopyByRowNum.put(rowNum, new FieldCopy(this.deleteField));
if (delete) {
// Put the current person in our list of ones to delete. Record their id but not version - we shouldn't
// assume person.version has been overwritten yet with the submitted value - it may still hold the
// database value.
personsToDelete.add(new IdVersion(person.getId(), 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;
}
}
}
.eg {
margin: 20px 0;
padding: 14px;
border: 1px solid #ddd;
border-radius: 6px;
-webkit-border-radius: 6px;
-mox-border-radius: 6px;
}
// 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;
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;
}
}