Editable Loop For Update (1)

A table built with a Form around a Loop to allow update of persons.
To demonstrate a server-side error, change any First Name to Acme.

The key difference from the Editable Loop example is that now we are dealing with existing entities,
so we drop formstate="ITERATION" from the Loop and give it a custom ValueEncoder instead.
Id First Name Last Name Start Date
3 km Dec 14, 2023
5 De scoop Jul 7, 2007
1 eeeeasdas Jun 4, 2026
2 dgzfgdf Jun 10, 2024
4 3clover Feb 12, 2008
Refresh
Features:

Loop doesn't support JSR-303 validation, so, as in the previous example, we're demonstrating 3 workarounds:

References: Loop, ValueEncoder, Forms and Validation.

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>Editable Loop For Update (1)</h3>

    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">
        <t:form t:id="personsEdit">
            <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>
                    </tr>
                </thead>
                <tbody>
                    <t:loop source="persons" value="person" encoder="personEncoder">
                        <tr>
                            <td>${person.id}</td>
                            <td><t:textfield t:id="firstName" value="person.firstName" validate="maxlength=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>
                    </t:loop>
                </tbody>
            </table>
            <t:submit value="Save"/>
            <t:eventlink event="refresh" class="btn btn-default">Refresh</t:eventlink>
         </t: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>
        </ul>
    </li>
    </ul>

    <p>Loop doesn't support JSR-303 validation, so, as in the previous example, we're demonstrating 3 workarounds:</p>
    <ul>
        <li>Client-side, we're validating field lengths with traditional Tapestry validators specified in the template.</li>
        <li>Server-side, we're invoking the validator for each field, which includes JSR-303 validation.</li>
        <li>Server-side, we're checking First Name is not <em>${BAD_NAME}</em>, which is a custom validation.</li>
    </ul>

    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/EditableLoopForUpdate1.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/tables/EditableLoopForUpdate1.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.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.ejb.EJB;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

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.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;

@Import(stylesheet = "css/examples/plain.css")
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 final String BAD_NAME = "Acme";

    // Work fields

    private List<Person> personsInDB;

    private boolean inFormSubmission;

    private List<Person> personsSubmitted;

    private int rowNum;
    private Map<Integer, FieldCopy> firstNameFieldCopyByRowNum;

    // Other pages

    @InjectPage
    private EditableLoopForUpdate2 page2;

    // Generally useful bits and pieces

    @InjectComponent("personsEdit")
    private Form form;

    @InjectComponent("firstName")
    private TextField firstNameField;

    @EJB
    private IPersonFinderServiceLocal personFinderService;

    @Inject
    private Locale currentLocale;

    @Inject
    private ValidatorFactory validatorFactory;

    // 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);

            // Populate the persons to be edited.
            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>();

        // 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;
        firstNameFieldCopyByRowNum = new HashMap<Integer, FieldCopy>();
    }

    void onValidateFromFirstName() {
        rowNum++;
        firstNameFieldCopyByRowNum.put(rowNum, new FieldCopy(firstNameField));
    }

    void onValidateFromPersonsEdit() {

        // 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;
            }
        }

        rowNum = -1;

        for (Person personSubmitted : personsSubmitted) {
            rowNum++;

            // Unfortunately, at this point the field firstNameField is from the final row of the Grid.
            // Fortunately, we have a copy of the correct field, so we can record the error with that.

            validate(personSubmitted, "firstName", firstNameFieldCopyByRowNum.get(rowNum), form);

            if (personSubmitted.getFirstName() != null && personSubmitted.getFirstName().equals(BAD_NAME)) {
                Field field = firstNameFieldCopyByRowNum.get(rowNum);
                form.recordError(field, "First name must not be " + BAD_NAME + ".");
            }

            if (personSubmitted.getId() == 2 && !personSubmitted.getFirstName().equals("Mary")) {
                Field field = firstNameFieldCopyByRowNum.get(rowNum);
                form.recordError(field, field.getLabel() + " for this person must be Mary.");
            }
        }

        if (form.getHasErrors()) {
            // We get here only if a server-side validator detected an error.
            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() {
        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. 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;
        }

    }

    private void validate(Object bean, String propertyName, Field field, Form form) {
        String errorMessage = validate(bean, propertyName, field);

        if (errorMessage != null) {
            form.recordError(field, errorMessage);
        }
    }

    /**
     * Use this method to validate fields that aren't being validated elsewhere, eg. derived fields, or fields that are
     * disabled in screen (because disabled input fields are not submitted or validated). Based on Tapestry's
     * BeanFieldValidator#validate(Object).
     * 
     * @param bean
     * @param propertyName
     * @param field
     * @return Error message string to use in Form#recordError or Tracker#recordError.
     */
    private <T> String validate(T bean, String propertyName, Field field) {
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<T>> constraintViolations = validator.validateProperty(bean, propertyName);

        if (constraintViolations.isEmpty()) {
            return null;
        }

        final StringBuilder builder = new StringBuilder();

        for (Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator(); iterator.hasNext();) {
            ConstraintViolation<T> violation = (ConstraintViolation<T>) iterator.next();

            builder.append(String.format("%s %s", field.getLabel(), violation.getMessage()));

            if (iterator.hasNext()) {
                builder.append(", ");
            }
        }

        return builder.toString();
    }

}


.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;
    }

}