2010-01-27 08:52:05 +01:00
|
|
|
package com.metaweb.gridlock.browsing;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import com.metaweb.gridlock.browsing.facets.Facet;
|
|
|
|
import com.metaweb.gridlock.browsing.facets.ListFacet;
|
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
|
|
|
|
|
|
|
public class Engine {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONObject getJSON(Properties options) throws JSONException {
|
|
|
|
JSONObject o = new JSONObject();
|
|
|
|
|
2010-01-28 02:43:09 +01:00
|
|
|
List<JSONObject> a = new ArrayList<JSONObject>(_facets.size());
|
|
|
|
for (Facet facet : _facets) {
|
2010-01-27 08:52:05 +01:00
|
|
|
a.add(facet.getJSON(options));
|
|
|
|
}
|
|
|
|
o.put("facets", a);
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|