Skip to content
This repository was archived by the owner on Mar 28, 2019. It is now read-only.

Using Filtered Entity Lists

Steven Davelaar edited this page Mar 4, 2016 · 2 revisions

By default, the generated entity CRUD service classes have one getter method that returns a collection of entity instances. For example, the EmployeeService class has the following method

public List<Employee> getEmployee() {   
  return getEntityList(); 
}  

The list returned is stored in a private member variable entityList in the EntityCRUDService superclass. If you have checked the Auto Query checkbox in the Runtime Options panel, then by default this list contains all entity instances, retrieved from the local database and/or by invoking the Find All REST resource.

To filter this list based on a quick search field in the user interface, you can use the find[entityName] method, which is also generated in the entity CRUD service class:

public void findEmployee(String searchValue) {
  super.find(searchValue);
}

You can drag and drop this method from the data control palette onto your AMX page which will provide you with a search field and a button to invoke the method. When this button is invoked the find[entityName] method in the Entity CRUD service class delegates to the find method in the DBPersistenceManager class , see section Accessing the SQLite Database for an explanation of how this find method works. The result returned by this database finder method is then stored as the new content of the entityList member variable and a data change event is send to the user interface so the list will repaint correctly after clicking the quick search button.

You can also write a custom method to filter the entity list. Here is an example of a method that filters the list to only show clerks:

public void filterClerks()
{
  DBPersistenceManager pm = new DBPersistenceManager();
  Map<String,String> searchAttrs = new HashMap<String,String>();
  searchAttrs.put("jobId","CLERK");
  List<Employee> clerks = pm.find(Employee.class,searchAttrs); 
  setEntityList(clerks);
}

The call to setEntityList both updates the entityList member variable, and also sends the required data change events to update the user interface once this method is invoked. Note that it doesn't matter whether this method is invoked through some UI action, or through Java code in a background thread.

Now, suppose your user interface should be able to show multiple filtered lists at the same time, for example, you want to show a list of all employees and a list of clerks. In such a use case, the filterClerks method cannot update the standard entityList member variable because that list if already used to show all employees in the UI. The solution is simple, we just add a getter method that returns a list of clerks. This adds a "clerks" collection attribute to the EmployeeService data control that we can drag and drop onto the AMX page to show the list of clerks. Here is the code create this method:

public List<Employee> getClerks()
{
  DBPersistenceManager pm = new DBPersistenceManager();
  Map<String,String> searchAttrs = new HashMap<String,String>();
  searchAttrs.put("jobId","CLERK");
  List<Employee> clerks = pm.find(Employee.class,searchAttrs); 
  return clerks;
}

@Override
protected void setEntityList(List<Employee> entityList)
{
  super.setEntityList(entityList);
  // we also need to refresh the clerks list
  getPropertyChangeSupport().firePropertyChange("clerks", null, getClerks());
  getProviderChangeSupport().fireProviderRefresh("clerks");      
}

The getClerks() method should be self explanatory, but you might wonder why we override method setEntityList. Well, this is needed to handle the situation where we retrieve the latest list of employees in the background by calling the Find All resource. When AMPA executes a Find All REST call in the background, it calls setEntityList when the response is returned and the database has been updated with the latest set of entity instance included in the response payload. In our example, the user interface shows both a list of all employees and a list of clerks, and we want both lists to be updated when the REST call is done. So, that's why we override method setEntityList: to ensure that getClerks method is executed again once the latest set of employees has been retrieved from the server and to ensure that data change events are sent to also redraw the clerks list.

Clone this wiki locally