diff --git a/src/main/java/com/metaweb/gridlock/browsing/Engine.java b/src/main/java/com/metaweb/gridlock/browsing/Engine.java index ff2ebee1b..88c3bc9f9 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/Engine.java +++ b/src/main/java/com/metaweb/gridlock/browsing/Engine.java @@ -13,6 +13,7 @@ import com.metaweb.gridlock.Jsonizable; import com.metaweb.gridlock.browsing.facets.Facet; import com.metaweb.gridlock.browsing.facets.ListFacet; import com.metaweb.gridlock.browsing.facets.RangeFacet; +import com.metaweb.gridlock.browsing.facets.TextSearchFacet; import com.metaweb.gridlock.browsing.filters.RowFilter; import com.metaweb.gridlock.model.Project; @@ -54,6 +55,8 @@ public class Engine implements Jsonizable { facet = new ListFacet(); } else if ("range".equals(type)) { facet = new RangeFacet(); + } else if ("text".equals(type)) { + facet = new TextSearchFacet(); } if (facet != null) { diff --git a/src/main/java/com/metaweb/gridlock/browsing/facets/RangeFacet.java b/src/main/java/com/metaweb/gridlock/browsing/facets/RangeFacet.java index 851871b83..5fea5e4d0 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/facets/RangeFacet.java +++ b/src/main/java/com/metaweb/gridlock/browsing/facets/RangeFacet.java @@ -7,7 +7,7 @@ import org.json.JSONObject; import org.json.JSONWriter; import com.metaweb.gridlock.browsing.FilteredRows; -import com.metaweb.gridlock.browsing.filters.ExpressionComparisonRowFilter; +import com.metaweb.gridlock.browsing.filters.ExpressionNumberComparisonRowFilter; import com.metaweb.gridlock.browsing.filters.RowFilter; import com.metaweb.gridlock.expr.Evaluable; import com.metaweb.gridlock.expr.Parser; @@ -69,19 +69,19 @@ public class RangeFacet implements Facet { @Override public RowFilter getRowFilter() { if ("min".equals(_mode)) { - return new ExpressionComparisonRowFilter(_eval, _cellIndex) { + return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { protected boolean checkValue(double d) { return d >= _min; }; }; } else if ("max".equals(_mode)) { - return new ExpressionComparisonRowFilter(_eval, _cellIndex) { + return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { protected boolean checkValue(double d) { return d <= _max; }; }; } else { - return new ExpressionComparisonRowFilter(_eval, _cellIndex) { + return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { protected boolean checkValue(double d) { return d >= _min && d <= _max; }; diff --git a/src/main/java/com/metaweb/gridlock/browsing/facets/TextSearchFacet.java b/src/main/java/com/metaweb/gridlock/browsing/facets/TextSearchFacet.java new file mode 100644 index 000000000..9d1994cf5 --- /dev/null +++ b/src/main/java/com/metaweb/gridlock/browsing/facets/TextSearchFacet.java @@ -0,0 +1,77 @@ +package com.metaweb.gridlock.browsing.facets; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONWriter; + +import com.metaweb.gridlock.browsing.FilteredRows; +import com.metaweb.gridlock.browsing.filters.ExpressionNumberComparisonRowFilter; +import com.metaweb.gridlock.browsing.filters.ExpressionStringComparisonRowFilter; +import com.metaweb.gridlock.browsing.filters.RowFilter; +import com.metaweb.gridlock.expr.Evaluable; +import com.metaweb.gridlock.expr.Parser; +import com.metaweb.gridlock.expr.VariableExpr; +import com.metaweb.gridlock.model.Project; + +public class TextSearchFacet implements Facet { + protected String _name; + protected int _cellIndex; + protected String _query; + + protected String _mode; + protected boolean _caseSensitive; + + public TextSearchFacet() { + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("name"); writer.value(_name); + writer.key("cellIndex"); writer.value(_cellIndex); + writer.key("query"); writer.value(_query); + writer.key("mode"); writer.value(_mode); + writer.key("caseSensitive"); writer.value(_caseSensitive); + writer.endObject(); + } + + @Override + public void initializeFromJSON(JSONObject o) throws Exception { + _name = o.getString("name"); + _cellIndex = o.getInt("cellIndex"); + _query = o.getString("query"); + _mode = o.getString("mode"); + _caseSensitive = o.getBoolean("caseSensitive"); + if (!_caseSensitive) { + _query = _query.toLowerCase(); + } + } + + @Override + public RowFilter getRowFilter() { + Evaluable eval = new VariableExpr("value"); + + if ("regex".equals(_mode)) { + return new ExpressionStringComparisonRowFilter(eval, _cellIndex) { + protected boolean checkValue(String s) { + return s.matches(_query); + }; + }; + } else { + return new ExpressionStringComparisonRowFilter(eval, _cellIndex) { + protected boolean checkValue(String s) { + return s.toLowerCase().contains(_query); + }; + }; + } + } + + @Override + public void computeChoices(Project project, FilteredRows filteredRows) { + // nothing to do + } +} diff --git a/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionComparisonRowFilter.java b/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionNumberComparisonRowFilter.java similarity index 84% rename from src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionComparisonRowFilter.java rename to src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionNumberComparisonRowFilter.java index 93744c0b0..7cc52a719 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionComparisonRowFilter.java +++ b/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionNumberComparisonRowFilter.java @@ -7,11 +7,11 @@ import com.metaweb.gridlock.model.Cell; import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Row; -abstract public class ExpressionComparisonRowFilter implements RowFilter { +abstract public class ExpressionNumberComparisonRowFilter implements RowFilter { final protected Evaluable _evaluable; final protected int _cellIndex; - public ExpressionComparisonRowFilter(Evaluable evaluable, int cellIndex) { + public ExpressionNumberComparisonRowFilter(Evaluable evaluable, int cellIndex) { _evaluable = evaluable; _cellIndex = cellIndex; } diff --git a/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionStringComparisonRowFilter.java b/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionStringComparisonRowFilter.java new file mode 100644 index 000000000..0038dba57 --- /dev/null +++ b/src/main/java/com/metaweb/gridlock/browsing/filters/ExpressionStringComparisonRowFilter.java @@ -0,0 +1,50 @@ +package com.metaweb.gridlock.browsing.filters; + +import java.util.Properties; + +import com.metaweb.gridlock.expr.Evaluable; +import com.metaweb.gridlock.model.Cell; +import com.metaweb.gridlock.model.Project; +import com.metaweb.gridlock.model.Row; + +abstract public class ExpressionStringComparisonRowFilter implements RowFilter { + final protected Evaluable _evaluable; + final protected int _cellIndex; + + public ExpressionStringComparisonRowFilter(Evaluable evaluable, int cellIndex) { + _evaluable = evaluable; + _cellIndex = cellIndex; + } + + @Override + public boolean filterRow(Project project, int rowIndex, Row row) { + if (_cellIndex < row.cells.size()) { + Cell cell = row.cells.get(_cellIndex); + if (cell != null) { + Properties bindings = new Properties(); + + bindings.put("cell", cell); + bindings.put("value", cell.value); + + Object value = _evaluable.evaluate(bindings); + if (value != null) { + if (value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + if (checkValue(v instanceof String ? ((String) v) : v.toString())) { + return true; + } + } + } else { + if (checkValue(value instanceof String ? ((String) value) : value.toString())) { + return true; + } + } + } + } + } + return false; + } + + abstract protected boolean checkValue(String s); +} diff --git a/src/main/webapp/project.html b/src/main/webapp/project.html index b5d3437b5..b6e401997 100644 --- a/src/main/webapp/project.html +++ b/src/main/webapp/project.html @@ -1 +1 @@ -