2010-01-25 23:51:25 +01:00
|
|
|
package com.metaweb.gridlock.commands;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.Properties;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-01-28 02:43:09 +01:00
|
|
|
import com.metaweb.gridlock.browsing.Engine;
|
|
|
|
import com.metaweb.gridlock.browsing.FilteredRows;
|
|
|
|
import com.metaweb.gridlock.browsing.RowVisitor;
|
2010-01-25 23:51:25 +01:00
|
|
|
import com.metaweb.gridlock.model.Project;
|
|
|
|
import com.metaweb.gridlock.model.Row;
|
|
|
|
|
|
|
|
public class GetRowsCommand extends Command {
|
|
|
|
@Override
|
2010-01-29 01:46:15 +01:00
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
2010-01-25 23:51:25 +01:00
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
try {
|
2010-01-28 02:43:09 +01:00
|
|
|
Project project = getProject(request);
|
|
|
|
Engine engine = getEngine(request, project);
|
|
|
|
|
|
|
|
int start = Math.min(project.rows.size(), Math.max(0, getIntegerParameter(request, "start", 0)));
|
|
|
|
int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20)));
|
|
|
|
Properties options = new Properties();
|
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
JSONWriter writer = new JSONWriter(response.getWriter());
|
|
|
|
writer.object();
|
2010-01-28 02:43:09 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
{
|
2010-01-28 02:43:09 +01:00
|
|
|
RowAccumulator acc = new RowAccumulator(start, limit) {
|
2010-02-01 20:16:09 +01:00
|
|
|
JSONWriter writer;
|
|
|
|
Properties options;
|
2010-01-28 02:43:09 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public RowAccumulator init(JSONWriter writer, Properties options) {
|
|
|
|
this.writer = writer;
|
2010-01-28 02:43:09 +01:00
|
|
|
this.options = options;
|
|
|
|
return this;
|
|
|
|
}
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-01-28 02:43:09 +01:00
|
|
|
@Override
|
2010-01-30 02:05:30 +01:00
|
|
|
public boolean internalVisit(int rowIndex, Row row) {
|
2010-01-28 02:43:09 +01:00
|
|
|
try {
|
2010-02-01 20:16:09 +01:00
|
|
|
options.put("rowIndex", rowIndex);
|
|
|
|
row.write(writer, options);
|
2010-01-28 02:43:09 +01:00
|
|
|
} catch (JSONException e) {
|
|
|
|
}
|
2010-01-30 02:05:30 +01:00
|
|
|
return false;
|
2010-01-28 02:43:09 +01:00
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
}.init(writer, options);
|
|
|
|
|
|
|
|
FilteredRows filteredRows = engine.getAllFilteredRows();
|
2010-01-28 02:43:09 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.key("rows"); writer.array();
|
2010-01-28 02:43:09 +01:00
|
|
|
filteredRows.accept(project, acc);
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.endArray();
|
2010-01-28 02:43:09 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.key("filtered"); writer.value(acc.total);
|
2010-01-25 23:51:25 +01:00
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
|
|
|
|
writer.key("start"); writer.value(start);
|
|
|
|
writer.key("limit"); writer.value(limit);
|
|
|
|
writer.key("total"); writer.value(project.rows.size());
|
|
|
|
|
|
|
|
writer.endObject();
|
2010-01-28 02:43:09 +01:00
|
|
|
} catch (Exception e) {
|
2010-01-25 23:51:25 +01:00
|
|
|
respondException(response, e);
|
|
|
|
}
|
|
|
|
}
|
2010-01-28 02:43:09 +01:00
|
|
|
|
|
|
|
static protected class RowAccumulator implements RowVisitor {
|
|
|
|
final public int start;
|
|
|
|
final public int limit;
|
|
|
|
|
|
|
|
public int total;
|
|
|
|
|
|
|
|
public RowAccumulator(int start, int limit) {
|
|
|
|
this.start = start;
|
|
|
|
this.limit = limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-02-01 00:15:50 +01:00
|
|
|
public boolean visit(Project project, int rowIndex, Row row) {
|
2010-01-30 02:05:30 +01:00
|
|
|
boolean r = false;
|
|
|
|
|
2010-01-28 02:43:09 +01:00
|
|
|
if (total >= start && total < start + limit) {
|
2010-01-30 02:05:30 +01:00
|
|
|
r = internalVisit(rowIndex, row);
|
2010-01-28 02:43:09 +01:00
|
|
|
}
|
|
|
|
total++;
|
2010-01-30 02:05:30 +01:00
|
|
|
return r;
|
2010-01-28 02:43:09 +01:00
|
|
|
}
|
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
protected boolean internalVisit(int rowIndex, Row row) {
|
|
|
|
return false;
|
2010-01-28 02:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-25 23:51:25 +01:00
|
|
|
}
|