/** * A utility method for generating a string to represent a single item's entry in the browse * * @param config * @return * @throws SQLException */ private String fullListingString(ItemListConfig config) throws SQLException { // report on all the results contained herein StringBuffer sb = new StringBuffer(); Iterator itr = results.iterator(); while (itr.hasNext()) { BrowseItem bi = (BrowseItem) itr.next(); if (bi == null) { sb.append("{{ NULL ITEM }}"); break; } sb.append("{{Item ID: " + Integer.toString(bi.getID()) + " :: "); for (int j = 1; j <= config.numCols(); j++) { String[] md = config.getMetadata(j); if (md == null) { sb.append("{{ NULL METADATA }}"); break; } Metadatum[] values = bi.getMetadata(md[0], md[1], md[2], Item.ANY); StringBuffer value = new StringBuffer(); if (values != null) { for (int i = 0; i < values.length; i++) { if (i > 0) { value.append(","); } value.append(values[i].value); } } else { value.append("-"); } String metadata = "[" + md[0] + "." + md[1] + "." + md[2] + ":" + value.toString() + "]"; sb.append(metadata); } sb.append("}}"); } return sb.toString(); }
/** * Get the database id of the item at the top of what will be the previous page of results. This * is so that a "back" button can be generated by the User Interface when paging through results. * The callback argument is there so that if the caller wishes the actual results from the * previous page can also be returned (this is useful, for example, if you are on the very last * page of results, and need some previous results to pad out the full number of results per * page). If the callback is null, then no results are kept * * @param callback A List object for holding BrowseItem objects indexed numerically in the correct * order * @return the database id of the top of the previous page * @throws SQLException * @throws BrowseException */ private int getPreviousPageID(List callback) throws SQLException, BrowseException { log.debug(LogManager.getHeader(context, "get_previous_page_id", "")); // do we want to capture the results? boolean capture = false; if (callback != null) { capture = true; } // the only thing we need to do is reverse the direction // of the query, and set it to not use the "=" part of the // comparator (i.e. < and > not <= and >=). boolean isAscending = dao.isAscending(); dao.setAscending(!isAscending); boolean useEquals = dao.useEqualsComparator(); dao.setEqualsComparator(false); // store in local scope the things that we are going to change // during this method int resultLimit = dao.getLimit(); // if the database supports it (postgres does), we use limit // and offset to minimise the work it has to do. // the limit will be the size of a page, or double the size of the // page if we are capturing the result set (because the first page's worth // will be the results, so the previous link will need to come from the // page *before* that) int limit = scope.getResultsPerPage(); if (capture) { limit *= 2; } dao.setLimit(limit); // now we have a query which is exactly the opposite of the // original query. So lets execute it: List results = dao.doQuery(); // before we continue, put back the variables we messed with dao.setAscending(isAscending); dao.setEqualsComparator(useEquals); dao.setLimit(resultLimit); Iterator itr = results.iterator(); // work our way through the list, capturing if necessary, and finally // having the last result, which will be the top of the previous page int i = 0; BrowseItem prev = null; while (itr.hasNext()) { BrowseItem browseItem = (BrowseItem) itr.next(); // we need to copy this, because of the scoping vs by-reference issue prev = browseItem; // if we need to capture the results in the call back, do it here. // Note that since the results will come in backwards, we place // each one at the start of the array so that it displaces the others // in the right direction. By the end, they should be sorted correctly // we use the index i to be sure that we only grab one page's worth // of results (see note above about limit) if (capture && i < scope.getResultsPerPage()) { callback.add(0, browseItem); i++; } } if (prev != null) { return prev.getID(); } else { return -1; } }