{
try
{
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
: QueryRule.Operator.SORTDESC;
rules.add(new QueryRule(sortOperator, postData.getSidx()));
}
if ((tupleTable instanceof FilterableTupleTable) && !request.isNull("_search")
&& request.getBoolean("_search"))
{
((FilterableTupleTable) tupleTable).setFilters(rules);
}
renderData(request, postData, totalPages, tupleTable);
break;
case EDIT_RECORD:
if (!(tupleTable instanceof EditableTupleTable))
{
throw new UnsupportedOperationException("TupleTable is not editable");
}
// create a json object to take the message and success
// variables.
JSONObject result = new JSONObject();
try
{
((EditableTupleTable) tupleTable).update(request);
result.put("message", "Record updated");
result.put("success", true);
}
catch (TableException e)
{
result.put("message", e.getMessage());
result.put("success", false);
}
// Send this json string back the html.
request.getResponse().getOutputStream().println(result.toString());
break;
case ADD_RECORD:
if (!(tupleTable instanceof EditableTupleTable))
{
throw new UnsupportedOperationException("TupleTable is not editable");
}
// create a json object to take the message and success
// variables.
result = new JSONObject();
try
{
((EditableTupleTable) tupleTable).add(request);
result.put("message", "Record added");
result.put("success", true);
}
catch (TableException e)
{
result.put("message", e.getMessage());
result.put("success", false);
}
// Send this json string back the html.
request.getResponse().getOutputStream().println(result.toString());
break;
case DELETE_RECORD:
if (!(tupleTable instanceof EditableTupleTable))
{
throw new UnsupportedOperationException("TupleTable is not editable");
}
// create a json object to take the message and success
// variables.
result = new JSONObject();
try
{
((EditableTupleTable) tupleTable).remove(request);
result.put("message", "Record deleted");
result.put("success", true);
}
catch (TableException e)
{
result.put("message", e.getMessage());
result.put("success", false);
}
// Send this json string back the html.
request.getResponse().getOutputStream().println(result.toString());
break;
default:
break;
}
}
catch (JSONException e)
{
throw new HandleRequestDelegationException(e);
}
catch (IOException e)
{
throw new HandleRequestDelegationException(e);
}
catch (TableException e)
{
throw new HandleRequestDelegationException(e);
}
catch (SQLException e)
{
throw new HandleRequestDelegationException(e);
}
catch (DatabaseException e)
{
throw new HandleRequestDelegationException(e);
}
}