}
public static JSONObject convert(Map<String, FacetAccessible> facetValueMap, SenseiRequest req)
throws JSONException
{
JSONObject resMap = new FastJSONObject(25);
if (facetValueMap != null)
{
Set<Entry<String, FacetAccessible>> entrySet = facetValueMap.entrySet();
for (Entry<String, FacetAccessible> entry : entrySet)
{
String fieldname = entry.getKey();
BrowseSelection sel = req.getSelection(fieldname);
HashSet<String> selectedVals = new HashSet<String>();
if (sel != null)
{
String[] vals = sel.getValues();
if (vals != null && vals.length > 0)
{
selectedVals.addAll(Arrays.asList(vals));
}
}
FacetAccessible facetAccessible = entry.getValue();
List<BrowseFacet> facetList = facetAccessible.getFacets();
ArrayList<JSONObject> facets = new ArrayList<JSONObject>();
for (BrowseFacet f : facetList)
{
String fval = f.getValue();
if (fval != null && fval.length() > 0)
{
JSONObject fv = new FastJSONObject();
fv.put(PARAM_RESULT_FACET_INFO_COUNT, f.getFacetValueHitCount());
fv.put(PARAM_RESULT_FACET_INFO_VALUE, fval);
fv.put(PARAM_RESULT_FACET_INFO_SELECTED, selectedVals.remove(fval));
facets.add(fv);
}
}
if (selectedVals.size() > 0)
{
// selected vals did not make it in top n
for (String selectedVal : selectedVals)
{
if (selectedVal != null && selectedVal.length() > 0)
{
BrowseFacet selectedFacetVal = facetAccessible.getFacet(selectedVal);
JSONObject fv = new FastJSONObject(5);
fv.put(PARAM_RESULT_FACET_INFO_COUNT, selectedFacetVal == null ? 0 : selectedFacetVal.getFacetValueHitCount());
String fval = selectedFacetVal == null ? selectedVal : selectedFacetVal.getValue();
fv.put(PARAM_RESULT_FACET_INFO_VALUE, fval);
fv.put(PARAM_RESULT_FACET_INFO_SELECTED, true);
facets.add(fv);
}
}
// we need to sort it