Total Control Output

BeanDisplay is very quick and easy, but limited. To have total control of the content, layout and style, you can easily code up the template yourself.
Id:
1
Version:
76
Name:
456, Acme2
Region:
East Coast
Start Date:
19 June 1952 AD

4 new things:

References: .row and .col-, Expansions, Property Expressions, Output component, @Inject, Messages.

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>Total Control Output</h3>
    
    BeanDisplay is very quick and easy, but limited. To have total control of the content, layout and 
    style, you can easily code up the template yourself. 

    <div class="eg">
        <div class="row">
            <div class="col-sm-2 lbl">Id:</div>
            <div class="col-sm-4">${person.id}</div>
            <div class="col-sm-2 lbl">Version:</div>
            <div class="col-sm-4">${person.version}</div>
        </div>
        <div class="row">
            <div class="col-sm-2 lbl">Name:</div>
            <div class="col-sm-10">${person.lastName}, ${person.firstName}</div>
        </div>
        <div class="row">
            <div class="col-sm-2 lbl">Region:</div>
            <div class="col-sm-10">${regionName}</div>
        </div>
        <div class="row">
            <div class="col-sm-2 lbl">Start Date:</div>
            <div class="col-sm-10"><t:output value="person.startDate" format="startDateFormat"/></div>
        </div>
    </div>

    <p>4 new things:</p>
    <ul>
        <li>
            <strong>Property expressions</strong> 
            Used in expansions and component parameters. For example, <code>person.id</code> is 
            equivalent to <code>getPerson().getId()</code>.
        </li>
        <li>
            <code>Output</code>
            A component for <em>formatting</em> output.
        </li>
        <li>
            <code>@Inject</code>
            A field annotation. We use it ask Tapestry to inject its Messages service at runtime.
        </li>
        <li>
            <code>Messages</code>
            A Tapestry service that provides access to properties files. We use it to convert the enum to a message. It looks
            first for <em>TotalControlOutput.properties</em> and finds it.
        </li>
    </ul>
    
    References: 
    <a href="http://getbootstrap.com/css/#grid">.row and .col-</a>, 
    <a href="http://tapestry.apache.org/component-templates.html#ComponentTemplates-Expansions">Expansions</a>, 
    <a href="http://tapestry.apache.org/property-expressions.html">Property Expressions</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Output.html">Output component</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/ioc/Messages.html">Messages</a>.<br/><br/> 
    
    <t:pagelink t:page="Index" href="#">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/output/TotalControlOutput.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/output/TotalControlOutput.java"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/output/TotalControlOutput.properties"/>
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/totalcontroloutput.css"/>
        <t:sourcecodetab src="/business/src/main/java/jumpstart/business/domain/person/Person.java"/>
        <t:sourcecodetab src="/business/src/main/java/jumpstart/business/domain/person/Regions.java"/>
    </t:tabgroup>
</body>
</html>


package jumpstart.web.pages.examples.output;

import java.text.Format;
import java.text.SimpleDateFormat;

import javax.ejb.EJB;

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

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

@Import(stylesheet = "css/examples/totalcontroloutput.css")
public class TotalControlOutput {

    @Property
    private Person person;

    @Property
    private String regionName;

    @Property
    private Format startDateFormat;

    @EJB
    private IPersonFinderServiceLocal personFinderService;

    @Inject
    private Messages messages;

    void setupRender() {

        // Get person with id 1 - ask business service to find it (from the database).
        
        person = personFinderService.findPerson(1L);

        if (person == null) {
            throw new IllegalStateException("Database data has not been set up!");
        }

        regionName = messages.get(Regions.class.getSimpleName() + "." + person.getRegion().name());

        startDateFormat = new SimpleDateFormat("dd MMMM yyyy G");
    }

}


## These enum conversions could be moved to the central message properties file called app.properties
## The structure we've chosen for the key (enum class name, dot, enum value) is common.
Regions.EAST_COAST=East Coast
Regions.WEST_COAST=West Coast


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

.eg .lbl {
                font-weight: bold;
                color: olive;
}

@media ( min-width : 768px) {
                .eg .lbl {
                                text-align: right;
                }
}


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

}


package jumpstart.business.domain.person;

public enum Regions {
    EAST_COAST, WEST_COAST;
}