if (db.getConnection().isClosed())
{
throw new HandleRequestDelegationException(new Exception("handleRequest: Connection is closed!"));
}
final TupleTable tupleTable = tupleTableBuilder.create(request);
tupleTable.setColLimit(maxVisibleColumnCount);
if (tupleTable instanceof DatabaseTupleTable)
{
((DatabaseTupleTable) tupleTable).setDb(db);
}
final Operation operation = StringUtils.isNotEmpty(request.getString(OPERATION)) ? Operation
.valueOf(request.getString(OPERATION)) : Operation.RENDER_DATA;
switch (operation)
{
case LOAD_CONFIG:
if (callback != null)
{
callback.beforeLoadConfig(request, tupleTable);
}
loadTupleTableConfig(db, request, tupleTable);
break;
case HIDE_COLUMN:
String columnToRemove = request.getString("column");
tupleTable.hideColumn(columnToRemove);
loadTupleTableConfig(db, request, tupleTable);
break;
case SHOW_COLUMN:
String columnToShow = request.getString("column");
tupleTable.showColumn(columnToShow);
loadTupleTableConfig(db, request, tupleTable);
break;
case SET_COLUMN_PAGE:
// TODO put this in a util class (default value for
// requestparams)
int colPage;
try
{
colPage = request.getInt("colPage");
}
catch (Exception e)
{
colPage = 1;
}
// TODO put maxColPage function in util class
int maxColPage = (int) Math.floor(tupleTable.getColCount() / (double) tupleTable.getColLimit());
if ((tupleTable.getColCount() % tupleTable.getColLimit()) > 0)
{
maxColPage++;
}
colPage = Math.min(colPage, maxColPage);
int colOffset = (colPage - 1) * tupleTable.getColLimit();
colOffset = Math.max(colOffset, 0);
tupleTable.setColOffset(colOffset);
loadTupleTableConfig(db, request, tupleTable);
break;
case NEXT_COLUMNS:
tupleTable.setColOffset(tupleTable.getColOffset() + maxVisibleColumnCount);
loadTupleTableConfig(db, request, tupleTable);
break;
case PREVIOUS_COLUMNS:
tupleTable.setColOffset(tupleTable.getColOffset() - maxVisibleColumnCount);
loadTupleTableConfig(db, request, tupleTable);
break;
case RENDER_DATA:
final List<QueryRule> rules = new ArrayList<QueryRule>();
// parse the request into post data
final JQGridPostData postData = new JQGridPostData(request);
// convert any filters to query rules
final List<QueryRule> filterRules = createQueryRulesFromJQGridRequest(postData.getFilters());
if (CollectionUtils.isNotEmpty(filterRules))
{
rules.addAll(filterRules);
}
int residue = 0;
final int rowCount = tupleTable.getCount();
if (rowCount % postData.getRows() != 0)
{
residue = 1;
}
final int totalPages = (int) Math.floor(rowCount / (double) postData.getRows()) + residue;
// update page
postData.setPage(Math.min(postData.getPage(), totalPages));
final int offset = Math.max((postData.getPage() - 1) * postData.getRows(), 0);
final String exportSelection = request.getString("exportSelection");
if (!StringUtils.equalsIgnoreCase(exportSelection, "ALL"))
{
// data.rows == limit
tupleTable.setLimit(postData.getRows());
// data.rows * data.page
tupleTable.setOffset(offset);
}
if (StringUtils.isNotEmpty(postData.getSidx()))
{
final Operator sortOperator = StringUtils.equals(postData.getSord(), "asc") ? QueryRule.Operator.SORTASC