2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks.browsing;
|
2010-01-27 08:52:05 +01:00
|
|
|
|
|
|
|
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-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.Jsonizable;
|
|
|
|
import com.metaweb.gridworks.browsing.facets.Facet;
|
|
|
|
import com.metaweb.gridworks.browsing.facets.ListFacet;
|
|
|
|
import com.metaweb.gridworks.browsing.facets.RangeFacet;
|
|
|
|
import com.metaweb.gridworks.browsing.facets.TextSearchFacet;
|
|
|
|
import com.metaweb.gridworks.browsing.filters.RowFilter;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
2010-01-27 08:52:05 +01:00
|
|
|
|
2010-03-21 08:14:39 +01:00
|
|
|
/**
|
|
|
|
* Faceted browsing engine.
|
|
|
|
*/
|
2010-02-01 20:16:09 +01:00
|
|
|
public class Engine implements Jsonizable {
|
2010-03-03 05:19:58 +01:00
|
|
|
protected Project _project;
|
|
|
|
protected List<Facet> _facets = new LinkedList<Facet>();
|
2010-03-12 02:06:23 +01:00
|
|
|
protected boolean _includeDependent;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-04-09 02:14:11 +02:00
|
|
|
public final static String INCLUDE_DEPENDENT = "includeDependent";
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public Engine(Project project) {
|
|
|
|
_project = project;
|
|
|
|
}
|
|
|
|
|
2010-03-12 02:06:23 +01:00
|
|
|
public FilteredRows getAllFilteredRows(boolean includeContextual) {
|
|
|
|
return getFilteredRows(null, includeContextual);
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
2010-01-27 08:52:05 +01:00
|
|
|
|
2010-03-12 02:06:23 +01:00
|
|
|
public FilteredRows getFilteredRows(Facet except, boolean includeContextual) {
|
|
|
|
ConjunctiveFilteredRows cfr = new ConjunctiveFilteredRows(includeContextual, _includeDependent);
|
2010-03-03 05:19:58 +01:00
|
|
|
for (Facet facet : _facets) {
|
|
|
|
if (facet != except) {
|
|
|
|
RowFilter rowFilter = facet.getRowFilter();
|
|
|
|
if (rowFilter != null) {
|
|
|
|
cfr.add(rowFilter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cfr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initializeFromJSON(JSONObject o) throws Exception {
|
2010-04-09 00:16:12 +02:00
|
|
|
if (o == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o.has("facets") && !o.isNull("facets")) {
|
|
|
|
JSONArray a = o.getJSONArray("facets");
|
|
|
|
int length = a.length();
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
JSONObject fo = a.getJSONObject(i);
|
|
|
|
String type = fo.has("type") ? fo.getString("type") : "list";
|
|
|
|
|
|
|
|
Facet facet = null;
|
|
|
|
if ("list".equals(type)) {
|
|
|
|
facet = new ListFacet();
|
|
|
|
} else if ("range".equals(type)) {
|
|
|
|
facet = new RangeFacet();
|
|
|
|
} else if ("text".equals(type)) {
|
|
|
|
facet = new TextSearchFacet();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (facet != null) {
|
|
|
|
facet.initializeFromJSON(_project, fo);
|
|
|
|
_facets.add(facet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-09 02:14:11 +02:00
|
|
|
if (o.has(INCLUDE_DEPENDENT) && !o.isNull(INCLUDE_DEPENDENT)) {
|
|
|
|
_includeDependent = o.getBoolean(INCLUDE_DEPENDENT);
|
2010-03-12 02:06:23 +01:00
|
|
|
}
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
2010-04-09 02:14:11 +02:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void computeFacets() throws JSONException {
|
|
|
|
for (Facet facet : _facets) {
|
|
|
|
FilteredRows filteredRows = getFilteredRows(facet, false);
|
|
|
|
|
|
|
|
facet.computeChoices(_project, filteredRows);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
2010-03-12 02:06:23 +01:00
|
|
|
writer.key("facets");
|
|
|
|
writer.array();
|
|
|
|
for (Facet facet : _facets) {
|
|
|
|
facet.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endArray();
|
2010-04-09 02:14:11 +02:00
|
|
|
writer.key(INCLUDE_DEPENDENT); writer.value(_includeDependent);
|
2010-03-03 05:19:58 +01:00
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-01-27 08:52:05 +01:00
|
|
|
}
|