String handlerUrl = ProcessorManager.getInstance().process(ProcessorManager.URL, TableHandler.URL, context).toString();
String id = parameters.get(TableHandler.ID).toString();
String storeName = parameters.get(TableHandler.STORE).toString();
Store store = context.getStore(StoreHelper.getStoreByName(storeName));
MapValue idResource = (MapValue)store.get(id);
if ( idResource == null ) return new Result(FAILED);
Map state = idResource.getMap();
int itemsPerPage = ((NumberValue)state.get(TableMapRenderer.ITEMS_PER_PAGE)).getNumber().intValue();
if (itemsPerPage <= 0) {
return new Result(FAILED);
}
Result result = new Result(StateDescriptor.OK);
NumberValue firstItem = ((NumberValue)state.get(TableHandler.CURRENT_POSITION));
int currentPosition = firstItem.getNumber().intValue();
int currentPage = currentPosition / itemsPerPage;
int length = ((NumberValue)state.get(TableMapRenderer.LENGTH)).getNumber().intValue();
int totalPages = (int)Math.floor((double)length / ((double)itemsPerPage) + 1);
int lastItem = Math.min(currentPosition + itemsPerPage, length);
result.addResultEntry(FIRST_ITEM, firstItem);
result.addResultEntry(LAST_ITEM, new NumberValue(new Integer(lastItem)));
result.addResultEntry(CURRENT_PAGE, new NumberValue(new Integer(currentPage)));
result.addResultEntry(TOTAL_PAGES, new NumberValue(new Integer(totalPages)));
boolean hasNext = false, hasPrevious = false;
if (itemsPerPage > 0) {
hasNext = (currentPosition + itemsPerPage < length);
hasPrevious = (currentPosition > 0);
}
StringBuffer buffer = new StringBuffer();
if (pre != null) pre.evaluate(buffer, parameters);
createHandler(0, itemsPerPage, handlerUrl, storeName, id, parameters, parameter);
if ( firstSelected != null && !hasPrevious ) {
firstSelected.evaluate(buffer, parameters);
} else if ( first != null) {
first.evaluate(buffer, parameters);
}
createHandler(Math.max(0, currentPosition - itemsPerPage), itemsPerPage, handlerUrl, storeName, id, parameters, parameter);
if (hasPrevious) {
if ( previous != null) previous.evaluate(buffer, parameters);
} else {
if ( previousDisabled != null) previousDisabled.evaluate(buffer, parameters);
}
int step = itemsPerPage > 0 ? itemsPerPage : 1;
boolean first = true;
lastItem = 0;
int itemsInRange = range * step;
int lowerBorder = currentPosition - itemsInRange;
int upperBorder = currentPosition + itemsInRange;
int i = 0;
for (; i < length; i += step) {
lastItem = i;
parameters.put(CURRENT_PAGE, new NumberValue(new Integer(i / itemsPerPage + 1)));
createHandler(i, Math.min(i+itemsPerPage, length), handlerUrl, storeName, id, parameters, parameter);
int rangeState = IN;
if (showRange && (i < lowerBorder || i > upperBorder)) rangeState = OUT;
if (showRange && (i == lowerBorder || i == upperBorder)) rangeState = BORDER;
boolean pagingTemplate = false;
Template templateToUse = null;
if (i == currentPosition && selectedPage != null) {
pagingTemplate = true;
templateToUse = selectedPage;
} else if (rangeState == IN && page != null) {
pagingTemplate = true;
templateToUse = page;
} else if (rangeState == IN) {
pagingTemplate = true;
templateToUse = page;
} else if (rangeState == BORDER) {
templateToUse = etc;
}
if (templateToUse != null ) {
if (pagingTemplate) {
if (!first) {
if (pageSeparator != null) pageSeparator.evaluate(buffer, parameters);
}
first = false;
}
templateToUse.evaluate(buffer, parameters);
}
}
createHandler(currentPosition + itemsPerPage, itemsPerPage, handlerUrl, storeName, id, parameters, parameter);
if (hasNext) {
if ( next != null) next.evaluate(buffer, parameters);
} else {
if ( nextDisabled != null) nextDisabled.evaluate(buffer, parameters);
}
createHandler(i-itemsPerPage, length, handlerUrl, storeName, id, parameters, parameter);
if ( lastSelected != null && !hasNext ) {
lastSelected.evaluate(buffer, parameters);
} else if ( last != null) {
last.evaluate(buffer, parameters);
}
if ( post != null) post.evaluate(buffer, parameters);
result.addResultEntry(OUTPUT, new StringValue(buffer.toString()));
return result;
}