Table of Contents
In this chapter, we show how to build a back-end web application with enumeration attributes, using Java with JPA and JSF. In addition to the topic of enumeration attributes, we also show how to deal with multi-valued attributes.
We again consider the simple data management problem that was considered in Part 2 of this tutorial, but now we have four additional enumeration attributes, as shown in the UML class diagram in Figure 9.1 below:
the single-valued mandatory attribute originalLanguage
with the
enumeration datatype LanguageEL
as its range,
the multi-valued optional attribute otherAvailableLanguages
with range
LanguageEL
,
the single-valued mandatory attribute category
with range
BookCategoryEL
the multi-valued mandatory attribute publicationForms
with range
PublicationFormEL
Notice that the attributes otherAvailableLanguages
and
publicationForms
are multivalued, as indicated by
their multiplicity expressions [*] and [1..*]. This means that the possible values of these
attributes are sets of enumeration literals, such as the set {ePub, PDF}, which can be represented
in Java as a corresponding set, or array list, of enumeration literals.
The meaning of the design model and its enumeration properties can be illustrated by a sample
data population for the model class Book
:
Table 9.1. Sample data for Book
ISBN | Title | Original language | Other languages | Category | Publication forms |
---|---|---|---|---|---|
0553345842 | The Mind's I | English (en) | de, es, fr | novel | paperback, ePub, PDF |
1463794762 | The Critique of Pure Reason | German (de) | de, es, fr, pt, ru | other | paperback, PDF |
1928565379 | The Critique of Practical Reason | German (de) | de, es, fr, pt, ru | other | paperback |
0465030793 | I Am A Strange Loop | English (en) | es | textbook | hardcover, ePub |
Compared to the Validation App discussed in Part 2 we have to deal with the following new issues:
Enumeration datatypes have to be defined in a suitable way as part of the model code.
Enumeration attributes have to be defined in model classes and handled in the user interface.
In terms of coding, the new issues are:
In the model code we have to take care of
enumeration
datatypes to be defined in the form of Java enum
classes;
single-valued enumeration
attributes (like Book::originalLanguage
) requiring
specific JPA annotations;
multi-valued enumeration
attributes (like Book::publicationForms
) requiring
specific JPA annotations and mapping methods for serializing their values in the form of
suitable strings;
extending the methods Book.add
, and Book.update
such that
they take care of the added enumeration attributes.
In the user interface code we have to take care of
adding new table columns in listAll.xhtml
;
adding suitable form controls (such as selection lists, radio button groups or checkbox groups) in create.xhtml
and
upate.xhtml
;
rendering multi-valued enumeration attributes in the table view of
listAll.xhtml
and in the choice widgets of create.xhtml
and
upate.xhtml
(with the help of an array of JSF SelectItem
s each
consisting of an enumeration value and its label).