Programming Thoughts
J2EE - Pagination
Editing

Displaying search results in pages

The previous set of articles described a Template class, list cache, that aids pagination of search results. This article expands it for record editing.

Update function

Search results are a restricted snapshot of a database, so changes to the database ought to be seen by re-reading the search results. Periodically re-reading is usually too impractical but if the user is editing the database, he/she expects changes to be applied to the list cache as well as the database.

For updating, users select and view an existing Data Transfer Object in search results and edits the database record represented by that. Usually, such changes should also be applied to the DTO but some back-end functions can also set fields, such as audit fields or calculated fields, not set by the user. In such cases, the DTO must be re-read and the list cache updated. The update function is below.

/** * Replaces selected item in list. */ public void setSelected(T value) { checkReloadAccess(); if (selectedIndex != -1) { setSelectedItemUnchecked(value); } }

Add function

After creating a new database record, users expect it in the search results, even if it doesn't meet the search criteria. It should be placed at the end of the list, and the current page shifted to it, so users can quickly find it, even if it's out of place in a sorted list. The add function is below.

/** * Adds new item to end of list and sets it as selected item. */ public synchronized void addItem(T item) throws IllegalStateException, Exception { ItemData<K,T> itemData; checkReloadAccess(); itemData = new ItemData(); itemData.setId(keyExtractor.getKey(item)); itemData.setItem(item); itemData.setPageExtensionReload(false); itemData.setReload(false); list.add(itemData); // Change selected index to refer to new item setSelectedIndex(list.size() - 1); }

Remove function

The remove function simply removes the currently selected item and ensures the selection index is still in range.

/** * Removes currently, selected item from list and returns it. Returns null if the list is empty or there is no * selected item. */ public synchronized T removeSelected() throws IllegalStateException, Exception { T result; checkReloadAccess(); if (selectedIndex >= 0) { result = list.get(selectedIndex).getItem(); list.remove(selectedIndex); checkSelectedIndex(); } else { result = null; } return result; }

Next part

Continued in Editing Example.