Hello,
Sorry for the delay in responding.
And so if we break your use case down into its two main parts, it sounds like you are asking.
1. How do I add the back end capability to return a list of Requests that fall between two dates
2. How do I add two date fields to a user interface and pass them to the server to load the result set into the list
As with most coding tasks, there are a few ways you could do it, i will cover the basics of one approach.
Lets start with the Service. We already have the RequestService and RequestServiceImpl that has a findAllRequests(Integer start, Integer maxRows) method.
You can add a new method to the Service interface and Service impl called findAllRequestsByDate(Integer start, Integer max, Date after, Date before) that will return Requests within your date range.
Now we have our Service contract for this capability defined, and you can implement the findAllRequestsByDate() method with the code to load the data.
To implement that method, add code like this: (You can look at the NamedQueries in your entity for guidance on the query strings).
| Code: |
public List<Request> findAllRequestsByDate(Integer startResult, Integer maxRows, Date after, Date before) {
Query query = requestDAO.createQuery("select myRequest from Request myRequest where myRequest.submitted > ?1 and myRequest.submitted < ?2", startResult, maxRows, after, before);
return new java.util.ArrayList<Request>(query.getResultList());
}
|
Now we need to update the Class that controls the Service access to the GWT Layer.
Open RequestRequest and add a line for your new method (look at the existing findAllRequests method in this class for guidance.
OK, that is the back end, for the front end, again you have a few options, but here are the basic concepts.
Open the RequestRequest interface that you just modified, and double click on the findAllRequests method name, right click, and select References / Workspace
This will help you find the code that is calling from the GWT front end to the GWT RequestHandler that is managing calls to your Service.
You will see that the only caller is the RequestListActivity class.
The code should read something like:
| Code: |
protected Request<List<gwt.client.managed.request.RequestProxy>> createRangeRequest(Range range) {
return requests.requestRequest().findAllRequests(range.getStart(), range.getLength());
}
|
Change this method to call your findAllRequestsByDate method and leave the dates blank for now, we will next add the UI code to collect the dates to pass in.
...
Lets start by adding the ability for ProxyListView's to expose SearchCriteria. You can make your own object model here, but to demonstrate, open ProxyListView and add a method like:
| Code: |
Object[] getSearchCriteria()
|
Next, we will want to add the actual Date fields to the view.
Open the RequestListView.ui.xml file and add something like this above the <b:CellTable>:
| Code: |
After:<dp:DateBox ui:field='after'></dp:DateBox>
Before:<dp:DateBox ui:field='before'></dp:DateBox
|
Next, you need to add the Java entries in the RequestListView.java for these controls.
Open RequestListView.java and add:
| Code: |
@UiField
DateBox after;
@UiField
DateBox before;
|
Now add code to implement the getSearchCriteria in the RequestListView object
| Code: |
@Override
public Object[] getSearchCriteria() {
return new Object[] {after.getValue(), before.getValue()};
}
|
Add this code to the constructor of RequestListView to register code that will refresh the list when the date values change. You can do additional work to move this code out of the view, this is an example.
| Code: |
ValueChangeHandler<Date> dateRangeHandler = new ValueChangeHandler<Date>() {
@Override
public void onValueChange(ValueChangeEvent<Date> arg0) {
asHasData().setVisibleRange(asHasData().getVisibleRange());
}
};
after.addValueChangeHandler(dateRangeHandler);
before.addValueChangeHandler(dateRangeHandler);
|
Now we can go back and fill in our blank dates in RequestListActivity with dates from the UI.
open the createRangeRequest method and change it to look like:
| Code: |
protected Request<List<gwt.client.managed.request.RequestProxy>> createRangeRequest(Range range) {
Object[] params = getView().getSearchCriteria();
Date after = (Date)params[0];
Date before = (Date)params[1];
if (after != null && before != null)
return requests.requestRequest().findAllRequestsByDate(range.getStart(), range.getLength(), after, before);
else
return requests.requestRequest().findAllRequests(range.getStart(), range.getLength());
}
|
Obviously, you need to clean this up, add null checking, error handling, and likely a full fledged Object for managing SearchCriteria. You could also pass SearchCriteria back and forth to the Server if you like.
The goal of this post was to give you some specific examples of how you could modify the generated code and to see the major touchpoints.
Hope this helps.
Jack