2010-01-27 08:52:05 +01:00
|
|
|
package com.metaweb.gridlock.browsing;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-27 08:52:05 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
import com.metaweb.gridlock.Jsonizable;
|
2010-01-27 08:52:05 +01:00
|
|
|
import com.metaweb.gridlock.browsing.facets.Facet;
|
|
|
|
import com.metaweb.gridlock.browsing.facets.ListFacet;
|
2010-02-02 02:36:02 +01:00
|
|
|
import com.metaweb.gridlock.browsing.facets.RangeFacet;
|
2010-01-30 02:05:30 +01:00
|
|
|
import com.metaweb.gridlock.browsing.filters.RowFilter;
|
2010-01-28 02:43:09 +01:00
|
|
|
import com.metaweb.gridlock.model.Project;
|
2010-01-27 08:52:05 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public class Engine implements Jsonizable {
|
2010-01-28 02:43:09 +01:00
|
|
|
protected Project _project;
|
|
|
|
protected List<Facet> _facets = new LinkedList<Facet>();
|
|
|
|
|
|
|
|
public Engine(Project project) {
|
|
|
|
_project = project;
|
|
|
|
}
|
2010-01-27 08:52:05 +01:00
|
|
|
|
|
|
|
public FilteredRows getAllFilteredRows() {
|
|
|
|
return getFilteredRows(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilteredRows getFilteredRows(Facet except) {
|
|
|
|
ConjunctiveFilteredRows cfr = new ConjunctiveFilteredRows();
|
2010-01-28 02:43:09 +01:00
|
|
|
for (Facet facet : _facets) {
|
2010-01-27 08:52:05 +01:00
|
|
|
if (facet != except) {
|
2010-01-30 02:05:30 +01:00
|
|
|
RowFilter rowFilter = facet.getRowFilter();
|
|
|
|
if (rowFilter != null) {
|
|
|
|
cfr.add(rowFilter);
|
|
|
|
}
|
2010-01-27 08:52:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cfr;
|
|
|
|
}
|
|
|
|
|
2010-01-28 02:43:09 +01:00
|
|
|
public void initializeFromJSON(JSONObject o) throws Exception {
|
2010-01-27 08:52:05 +01:00
|
|
|
JSONArray a = o.getJSONArray("facets");
|
|
|
|
int length = a.length();
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
JSONObject fo = a.getJSONObject(i);
|
2010-01-30 02:05:30 +01:00
|
|
|
String type = fo.has("type") ? fo.getString("type") : "list";
|
2010-01-27 08:52:05 +01:00
|
|
|
|
|
|
|
Facet facet = null;
|
|
|
|
if ("list".equals(type)) {
|
|
|
|
facet = new ListFacet();
|
2010-02-02 02:36:02 +01:00
|
|
|
} else if ("range".equals(type)) {
|
|
|
|
facet = new RangeFacet();
|
2010-01-27 08:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (facet != null) {
|
|
|
|
facet.initializeFromJSON(fo);
|
2010-01-28 02:43:09 +01:00
|
|
|
_facets.add(facet);
|
2010-01-27 08:52:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void computeFacets() throws JSONException {
|
2010-01-28 02:43:09 +01:00
|
|
|
for (Facet facet : _facets) {
|
2010-01-27 08:52:05 +01:00
|
|
|
FilteredRows filteredRows = getFilteredRows(facet);
|
|
|
|
|
2010-01-28 02:43:09 +01:00
|
|
|
facet.computeChoices(_project, filteredRows);
|
2010-01-27 08:52:05 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("facets"); writer.array();
|
|
|
|
for (Facet facet : _facets) {
|
|
|
|
facet.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endArray();
|
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-01-27 08:52:05 +01:00
|
|
|
}
|