<!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/together/totalcontrolcrudedit.css}"/>
</head>
<body>
<h1>Update</h1>
<div class="eg">
<form t:type="CustomForm" t:id="personForm" >
<t:errors/>
<t:if test="person">
<!-- If optimistic locking is not needed then comment out this next line. It works because Hidden fields are part of the submit. -->
<t:hidden value="person.version"/>
<table class="edit">
<tr>
<th><t:label for="firstName"/>:</th>
<td><input t:type="TextField" t:id="firstName" value="person.firstName" t:validate="required, maxlength=10" size="10"/></td>
<td>(required)</td>
</tr>
<tr class="err">
<th></th>
<td colspan="2"><t:CustomError for="firstName"/></td>
</tr>
<tr>
<th><t:label for="lastName"/>:</th>
<td><input t:type="TextField" t:id="lastName" value="person.lastName" t:validate="required, maxlength=10" size="10"/></td>
<td>(required)</td>
</tr>
<tr class="err">
<th></th>
<td colspan="2"><t:CustomError for="lastName"/></td>
</tr>
<tr>
<th><t:label for="region"/>:</th>
<td><input t:type="Select" t:id="region" value="person.region" t:validate="required"/></td>
<td>(required)</td>
</tr>
<tr class="err">
<th></th>
<td colspan="2"><t:CustomError for="region"/></td>
</tr>
<tr>
<th><t:label for="startDate"/>:</th>
<td><input t:type="DateField" t:id="startDate" value="person.startDate" t:format="dd/MM/yyyy" t:validate="required" size="10"/></td>
<td>(required, dd/mm/yyyy)</td>
</tr>
<tr class="err">
<th></th>
<td colspan="2"><t:CustomError for="startDate"/></td>
</tr>
<tr>
<th> </th>
<td colspan="2">
<input t:type="submit" value="Save"/>
<a t:type="pageLink" t:page="together/totalcontrolcrud/Persons" href="#">Cancel</a>
</td>
</tr>
</table>
</t:if>
<t:if negate="true" test="person">
Person ${personId} does not exist.<br/><br/>
<a t:type="pageLink" t:page="together/totalcontrolcrud/Persons" href="#">Cancel</a>
</t:if>
</form>
</div>
<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/together/totalcontrolcrud/person/PersonUpdate.tml"/>
<t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/together/totalcontrolcrud/person/PersonUpdate.java"/>
<t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/css/together/totalcontrolcrudedit.css"/>
<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.together.totalcontrolcrud.person;
import javax.ejb.EJB;
import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;
import jumpstart.business.domain.person.iface.IPersonManagerServiceLocal;
import jumpstart.util.ExceptionUtil;
import jumpstart.web.components.CustomForm;
import jumpstart.web.pages.together.totalcontrolcrud.Persons;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
public class PersonUpdate {
// The activation context
@Property
private Long personId;
// Screen fields
@Property
private Person person;
// Work fields
// This carries version through the redirect that follows a server-side validation failure.
@Persist(PersistenceConstants.FLASH)
private Integer versionFlash;
// Other pages
@InjectPage
private Persons indexPage;
// Generally useful bits and pieces
@Component(id = "personForm")
private CustomForm personForm;
@EJB
private IPersonFinderServiceLocal personFinderService;
@EJB
private IPersonManagerServiceLocal personManagerService;
// The code
// onPassivate() is called by Tapestry to get the activation context to put in the URL.
Long onPassivate() {
return personId;
}
// onActivate() is called by Tapestry to pass in the activation context from the URL.
void onActivate(Long personId) {
this.personId = personId;
}
// PersonForm bubbles up the PREPARE_FOR_RENDER event during form render.
void onPrepareForRender() {
person = personFinderService.findPerson(personId);
// Handle null person in the template.
// If the form has errors then we're redisplaying after a redirect.
// Form will restore your input values but it's up to us to restore Hidden values.
if (personForm.getHasErrors()) {
if (person != null) {
person.setVersion(versionFlash);
}
}
}
// PersonForm bubbles up the PREPARE_FOR_SUBMIT event during form submission.
void onPrepareForSubmit() {
// Get objects for the form fields to overlay.
person = personFinderService.findPerson(personId);
if (person == null) {
person = new Person();
personForm.recordError("Person has been deleted by another process.");
}
}
// PersonForm bubbles up the VALIDATE event when it is submitted
void onValidateFromPersonForm() {
if (personForm.getHasErrors()) {
// We get here only if a server-side validator detected an error.
return;
}
try {
personManagerService.changePerson(person);
}
catch (Exception e) {
// Display the cause. In a real system we would try harder to get a user-friendly message.
personForm.recordError(ExceptionUtil.getRootCauseMessage(e));
}
}
// PersonForm bubbles up SUCCESS or FAILURE when it is submitted, depending on whether VALIDATE records an error
Object onSuccess() {
return indexPage;
}
void onFailure() {
versionFlash = person.getVersion();
}
}
body, td { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; font-weight: normal; color: #333;
line-height: 17px; }
.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; }
.edit { border-collapse: collapse; border-spacing: 0; border: 1px solid #dddddd; width: 50%; margin: auto;
background-color: #eeeeee; }
.edit th { padding: 2px 5px; text-align: right; width: 35%; }
.edit td { padding: 2px 5px; text-align: left; }
.edit tr.err th { padding: 0; }
.edit tr.err td { padding: 0; }
.edit tr.err td.error-msg-c { padding: 0 0 4px 7px; font-size: 11px; color: red; }
.error { text-align: center; padding-bottom: 13px; color: red; }
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;
}