Skip to content

Commit

Permalink
adding facet filter (like solr, ebay etc) first cut. Contribution com…
Browse files Browse the repository at this point in the history
…ing from SourceForge
  • Loading branch information
Pascal Robert committed Jul 7, 2012
1 parent 18ecfe8 commit 03d956d
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Frameworks/Core/ERDirectToWeb/Components/ERDFacetFilter.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wodefinitions>
<wo wocomponentcontent="false" class="ERDFacetFilter.java">

</wo>
</wodefinitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<webobject name = "HasKeyList">
<ul class = "keyList">
<webobject name = "KeyList">
<webobject name = "HasValueList">
<li class = "key">
<webobject name = "Key" />
<ul>
<webobject name = "ValueList">
<li>
<webobject name = "IsSelectedValue">[X]</webobject> <webobject name = "ValueLink"><webobject name = "Value" /></webobject> (<webobject name = "ValueCount" />)</li>
</webobject>
<webobject name = "HasSelectedValues"><li><webobject name = "ClearSelectionLink">[Clear]</webobject></li></webobject>
</ul>
</li>
</webobject>
</webobject>
</ul>
</webobject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
HasKeyList: WOConditional {
condition = keyList.@count;
}

KeyList: WORepetition {
list = keyList;
item = currentKey;
}

Key: WOString {
value = currentKey;
}

HasValueList: WOConditional {
condition = valueList.@count;
}

ValueList: WORepetition {
list = valueList;
item = currentValue;
}

IsSelectedValue: WOConditional {
condition = isCurrentValueSelected;
}

HasSelectedValues: WOConditional {
condition = hasSelectedValues;
}

ClearSelectionLink: WOHyperlink {
action = clearSelectedValues;
}

Value: WOString {
value = currentValue;
}

ValueCount: WOString {
value = currentValueCount;
}

ValueLink: WOHyperlink {
action = selectCurrentValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"WebObjects Release" = "WebObjects 5.0";
encoding = "UTF-8";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package er.directtoweb.components;

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WOContext;
import com.webobjects.eocontrol.EOEnterpriseObject;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSMutableDictionary;
import com.webobjects.foundation.NSMutableSet;

import er.extensions.eof.ERXS;
import er.extensions.foundation.ERXArrayUtilities;

public class ERDFacetFilter extends ERDCustomQueryComponent {

public Object currentValue;
public String currentKey;
private NSArray<EOEnterpriseObject> _allObjects;

private NSMutableDictionary<String, NSDictionary<Object, NSArray<EOEnterpriseObject>>> values = new NSMutableDictionary();
private NSMutableDictionary<String, NSMutableArray<Object>> selectedValues = new NSMutableDictionary();

public ERDFacetFilter(WOContext context) {
super(context);
}

@Override
public boolean synchronizesVariablesWithBindings() {
return false;
}

public NSArray<EOEnterpriseObject> allObjects() {
if(_allObjects == null && allKeys() != null) {
_allObjects = (NSArray<EOEnterpriseObject>) displayGroup().allObjects();
values.removeAllObjects();
selectedValues.removeAllObjects();
for (String keyPath : allKeys()) {
NSDictionary<Object, NSArray<EOEnterpriseObject>> groupedObjects = ERXArrayUtilities.arrayGroupedByKeyPath(_allObjects, keyPath);
values.setObjectForKey(groupedObjects, keyPath);
selectedValues.setObjectForKey(new NSMutableArray(), keyPath);
}
}
return _allObjects;
}

public NSArray<String> allKeys() {
return (NSArray<String>) valueForBinding("groupingKeys");
}

public NSArray valueList() {
return ERXS.asc("toString").sorted(values.objectForKey(currentKey).allKeys());
}

public int currentValueCount() {
return values.objectForKey(currentKey).objectForKey(currentValue).count();
}

public NSArray keyList() {
allObjects();
return values.allKeys();
}

public boolean isCurrentValueSelected() {
NSMutableArray<Object> selected = selectedValues.objectForKey(currentKey);
return selected.containsObject(currentValue);
}

public WOActionResults selectCurrentValue() {
NSMutableArray<Object> selected = selectedValues.objectForKey(currentKey);
if(selected.containsObject(currentValue)) {
selected.removeObject(currentValue);
} else {
selected.addObject(currentValue);
}
updateSelectedValues();
return null;
}

public void updateSelectedValues() {
if (selectedValues.count() > 0) {
NSMutableSet eos = new NSMutableSet(allObjects());
for (String key : selectedValues.allKeys()) {
NSMutableArray<Object> selection = selectedValues.objectForKey(key);
if (selection.count() > 0) {
NSMutableSet currentEos = new NSMutableSet();
for (Object value : selection) {
currentEos.addObjectsFromArray(values.objectForKey(key).objectForKey(value));
}
eos.intersectSet(currentEos);
}
}
displayGroup().setObjectArray(eos.allObjects());
displayGroup().setSortOrderings(displayGroup().sortOrderings());
} else {
displayGroup().setObjectArray(null);
}
}

public boolean hasSelectedValues() {
return selectedValues.objectForKey(currentKey).count() > 0;
}

public WOActionResults clearSelectedValues() {
selectedValues.objectForKey(currentKey).removeAllObjects();
updateSelectedValues();
return null;
}
}

0 comments on commit 03d956d

Please sign in to comment.