|
0
|
1 |
package hirondelle.web4j.action;
|
|
|
2 |
|
|
|
3 |
import java.util.logging.*;
|
|
|
4 |
import java.util.regex.Pattern;
|
|
|
5 |
|
|
|
6 |
import hirondelle.web4j.request.RequestParameter;
|
|
|
7 |
import hirondelle.web4j.request.RequestParser;
|
|
|
8 |
import hirondelle.web4j.util.Util;
|
|
|
9 |
import hirondelle.web4j.model.AppException;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
<b>Template</b> for search screens.
|
|
|
13 |
|
|
|
14 |
<P>Here, a search action has the following :
|
|
|
15 |
<ul>
|
|
|
16 |
<li>it uses a form that allows the user to input search criteria
|
|
|
17 |
<li>the form must have <tt>GET</tt> as its method, not <tt>POST</tt>
|
|
|
18 |
<li>the underlying database operation is a <tt>SELECT</tt>, and does not edit the database in any way
|
|
|
19 |
</ul>
|
|
|
20 |
|
|
|
21 |
<P>Search operations never require a redirect operation (since they do not edit the database).
|
|
|
22 |
|
|
|
23 |
<P>Search operations have an interesting property : if you build a Model Object to validate and represent
|
|
|
24 |
user input into the search form, then its <tt>getXXX</tt> methods can usually be made package-private, instead
|
|
|
25 |
of <tt>public</tt>. The reason is that such Model Objects are usually not used by JSPs directly.
|
|
|
26 |
If desired, such methods can safely return <tt>String</tt> instead of
|
|
|
27 |
{@link hirondelle.web4j.security.SafeText}. (The Model Objects themselves cannot be made package-private, since
|
|
|
28 |
the {@link hirondelle.web4j.model.ModelFromRequest} class works only with <tt>public</tt> classes.)
|
|
|
29 |
*/
|
|
|
30 |
public abstract class ActionTemplateSearch extends ActionImpl {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
Constructor.
|
|
|
34 |
|
|
|
35 |
@param aForward renders the result of the search
|
|
|
36 |
@param aRequestParser passed to the superclass constructor.
|
|
|
37 |
*/
|
|
|
38 |
protected ActionTemplateSearch(ResponsePage aForward, RequestParser aRequestParser){
|
|
|
39 |
super(aForward, aRequestParser);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
The operations supported by this template.
|
|
|
44 |
|
|
|
45 |
<P>The supported operations are :
|
|
|
46 |
<ul>
|
|
|
47 |
<li> {@link Operation#Show}
|
|
|
48 |
<li> {@link Operation#Search}
|
|
|
49 |
</ul>
|
|
|
50 |
|
|
|
51 |
The source of the <tt>Operation</tt> is described by {@link ActionImpl#getOperation()}.
|
|
|
52 |
*/
|
|
|
53 |
public static final RequestParameter SUPPORTED_OPERATION = RequestParameter.withRegexCheck(
|
|
|
54 |
"Operation", Pattern.compile("(" + Operation.Show + "|" + Operation.Search + ")")
|
|
|
55 |
);
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
<b>Template</b> method.
|
|
|
59 |
|
|
|
60 |
<P>In order to clearly understand the operation of this method, here is the
|
|
|
61 |
core of its implementation, with all abstract methods in <em>italics</em> :
|
|
|
62 |
<PRE>
|
|
|
63 |
if (Operation.Show == getOperation() ){
|
|
|
64 |
//default forward
|
|
|
65 |
}
|
|
|
66 |
else if ( Operation.Search == getOperation() ){
|
|
|
67 |
<em>validateUserInput();</em>
|
|
|
68 |
if( ! hasErrors() ){
|
|
|
69 |
<em>listSearchResults();</em>
|
|
|
70 |
if ( ! hasErrors() ){
|
|
|
71 |
fLogger.fine("List executed successfully.");
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
</PRE>
|
|
|
76 |
*/
|
|
|
77 |
@Override public ResponsePage execute() throws AppException {
|
|
|
78 |
if (Operation.Show == getOperation() ){
|
|
|
79 |
//default forward
|
|
|
80 |
}
|
|
|
81 |
else if ( Operation.Search == getOperation() ){
|
|
|
82 |
fLogger.fine("'Search' Operation.");
|
|
|
83 |
validateUserInput();
|
|
|
84 |
if( ! hasErrors() ){
|
|
|
85 |
fLogger.fine("Passes validation.");
|
|
|
86 |
listSearchResults();
|
|
|
87 |
if ( ! hasErrors() ){
|
|
|
88 |
fLogger.fine("List executed successfully.");
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
else {
|
|
|
93 |
throw new AssertionError("Unexpected value for Operation : " + getOperation());
|
|
|
94 |
}
|
|
|
95 |
return getResponsePage();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
Validate items input by the user into a form.
|
|
|
100 |
|
|
|
101 |
<P>The form is used to define the criteria for the search (if any).
|
|
|
102 |
|
|
|
103 |
<P>Applies only for {@link Operation#Search}. If an error occurs, then
|
|
|
104 |
<tt>addError</tt> must be called.
|
|
|
105 |
*/
|
|
|
106 |
protected abstract void validateUserInput() throws AppException;
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
Query the database, and place the results (usually) into request scope.
|
|
|
110 |
*/
|
|
|
111 |
protected abstract void listSearchResults() throws AppException;
|
|
|
112 |
|
|
|
113 |
// PRIVATE //
|
|
|
114 |
private static final Logger fLogger = Util.getLogger(ActionTemplateSearch.class);
|
|
|
115 |
}
|