I’ve worked recently on an Oracle MAF application which required to display 3 selectboxes that were dependent:

  1. Countries;
  2. Counties within a country;
  3. Cities within a county;

The requirement is to have them working also when the mobile application runs in offline mode. So, the only way to have that is to initially load all the data from the server to the local mobile database via REST calls. The first implementation was done with AMPA persistence framework, which was easy with almost no development.

You can find more information about AMPA here.

When testing this feature I’ve realized that there are moments when the mobile application is really slow, this was due to the fact that I have to load all the information on the mobile database, using the code bellow:

public List<Countries> getCountries() {
    List<Countries> countries = getEntityList();

    if (countries != null && !countries.isEmpty()) {
        for (Countries country : countries) {
            List<Counties> county = country.getCounties();
            for (Counties c : county) {
                List<Cities> cities = c.getCities();
                for (Cities city : cities) {
                    city.getName();
                }
            }
        }
    }
    return countries;
}

Behind the scene AMPA was making a request to get the Counties for each Country, and for each County a request to get the Cities. This resulted in hundreds of requests which made the application really slow.

So I had to minimize the number of requests made by AMPA, for this I implemented 3 new services that loads the complete set of information for each level. So the 3 new services are:

  1. Get all Countries
  2. Get all Counties
  3. Get all Cities

Now the above function looks like:

public List<Countries> getCountries() {
    List<Countries> countries = getEntityList();

    CustomRestJSONPersistentManager pm = new CustomRestJSONPersistentManager();
    Map<String, String> headerParamMap = new HashMap<String, String>();
    headerParamMap.put("Content-Type", "application/json");
    headerParamMap.put("Accept", "application/json; charset=UTF-8");
    String jsonResponseCountries =
        pm.invokeRestService("MyConnection", RestServiceAdapter.REQUEST_TYPE_POST, "/MyApplication/getAllCountries", null, headerParamMap, 0, false);
    List<Countries> listCountries = pm.handleReadResponse(jsonResponseCountries, Countries.class, "root", null, null, true);

    String jsonResponseCounties =
        pm.invokeRestService("MyConnection", RestServiceAdapter.REQUEST_TYPE_POST, "/MyApplication/getAllCounties", null, headerParamMap, 0, false);
    List<Counties> listCounties = pm.handleReadResponse(jsonResponseCounties, Counties.class, "root", null, null, true);

    String jsonResponseCities =
        pm.invokeRestService("MyConnection", RestServiceAdapter.REQUEST_TYPE_POST, "/MyApplication/getAllCities", null, headerParamMap, 0, false);
    List<Cities> listCities = pm.handleReadResponse(jsonResponseCities, Cities.class, "root", null, null, true);

    return countries;
}

The function invokeRestService is executing the REST call using a connection defined in the application, the returned JSON is passed to the handleReadResponse to be processed and persist it in the local mobile database.

AMPA provides not only a framework to make it easy developing mobile applications with Oracle MAF but also provides great utility functions for executing REST calls, persist information in local mobile database, etc.