Справочник Пользователя для Red Hat Web Application Framework 6.1

Скачать
Страница из 230
56
Chapter 8. WAF Application Development Tutorial
In many ways,
Note.java
is just like
Binder.java
. It declares its data object type; it defines an
instantiator; and it adds some plumbing for getting the runtime data object type and for constructing
a wrapper
DomainObject
.
Since the properties of the data object type differ from those of Binder, the Java accessors and mutators
are different. Of more interest is the fact that
Note
has additional business logic for outputting the
Note
object as XML.
Note
The
setTitle
method uses assertions to enforce the contract set out in the object-type definition.
The
title
property is marked
[1..1]
in
Note.pdl
, so it cannot have a null value. Therefore, we
assert that calling
setTitle(null)
is a failure condition.
Note
If your code includes assertions that do work in their arguments, make sure to wrap the assertion
code in an if statement that checks that assertions are turned on:
if (Assert.isEnabled() && body != null) {
Assert.truth(body.length() <= 4000, "The body text is too long");
}
Example 8-5. Using assertions
Be careful, however, that this is what you really want. Assertions exist to detect unexpected, unrecov-
erable program failures. Don’t use them for other types of errors, such as user input validation, since
they can be disabled.
The WAF persistence API gives us efficient cursor-oriented access to the elements of an association.
The
NoteCollection
object gives our UI code access to the data for each row returned from a query
for multiple
Notes
.
package com.example.binder;
import com.arsdigita.domain.DomainCollection;
import com.arsdigita.persistence.DataCollection;
import org.apache.log4j.Logger;
/**
* Represents a collection of {@link com.example.binder.Note notes}.
*
* @author Justin Ross
*/
public class NoteCollection extends DomainCollection {
private static final Logger s_log = Logger.getLogger(NoteCollection.class);
public NoteCollection(final DataCollection data) {
super(data);
}
/**
* Gets the title of the current note.