response.setHeader("Content-Type", "application/json");
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
int length = rowIndices.length();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
try {
Evaluable eval = MetaParser.parse(expression);
writer.key("code"); writer.value("ok");
writer.key("results"); writer.array();
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
int rowIndex = rowIndices.getInt(i);
if (rowIndex >= 0 && rowIndex < project.rows.size()) {
Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex);
try {
ExpressionUtils.bind(bindings, row, rowIndex, columnName, cell);
result = eval.evaluate(bindings);
if (repeat) {
for (int r = 0; r < repeatCount && ExpressionUtils.isStorable(result); r++) {
Cell newCell = new Cell((Serializable) result, (cell != null) ? cell.recon : null);
ExpressionUtils.bind(bindings, row, rowIndex, columnName, newCell);
Object newResult = eval.evaluate(bindings);
if (ExpressionUtils.isError(newResult)) {
break;
} else if (ExpressionUtils.sameValue(result, newResult)) {
break;
} else {
result = newResult;
}
}
}
} catch (Exception e) {
// ignore
}
}
if (result == null) {
writer.value(null);
} else if (ExpressionUtils.isError(result)) {
writer.object();
writer.key("message"); writer.value(((EvalError) result).message);
writer.endObject();
} else {
StringBuffer sb = new StringBuffer();
writeValue(sb, result, false);
writer.value(sb.toString());
}
}
writer.endArray();
} catch (ParsingException e) {
writer.key("code"); writer.value("error");
writer.key("type"); writer.value("parser");
writer.key("message"); writer.value(e.getMessage());
} catch (Exception e) {
writer.key("code"); writer.value("error");
writer.key("type"); writer.value("other");
writer.key("message"); writer.value(e.getMessage());
}
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}