Changeset 4664


Ignore:
Timestamp:
03/10/14 13:41:24 (11 years ago)
Author:
Twan Goosen
Message:

Added support for sorting to field values provider

Location:
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/provider/FacetFieldValuesProvider.java

    r4606 r4664  
    1717package eu.clarin.cmdi.vlo.wicket.provider;
    1818
     19import com.google.common.collect.Ordering;
     20import eu.clarin.cmdi.vlo.pojo.FieldValuesOrder;
    1921import java.util.Iterator;
    2022import java.util.List;
    2123import org.apache.solr.client.solrj.response.FacetField;
    22 import org.apache.wicket.markup.repeater.data.IDataProvider;
     24import org.apache.solr.client.solrj.response.FacetField.Count;
     25import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
     26import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
    2327import org.apache.wicket.model.IModel;
    2428import org.apache.wicket.model.Model;
     
    2630/**
    2731 * Provides facet values and counts (both through the {@link Count} object of
    28  * SOLR) present on a {@link FacetField}
     32 * SOLR) present on a {@link FacetField}, sortable by either count or name
    2933 *
     34 * @see FieldValuesOrder
    3035 * @author twagoo
    3136 */
    32 public class FacetFieldValuesProvider implements IDataProvider<FacetField.Count> {
     37public class FacetFieldValuesProvider extends SortableDataProvider<FacetField.Count, FieldValuesOrder> {
    3338
    3439    private final IModel<FacetField> model;
     
    4550    }
    4651
     52    public FacetFieldValuesProvider(IModel<FacetField> model, int max) {
     53        this(model, max, new SortParam<FieldValuesOrder>(FieldValuesOrder.COUNT, false));
     54    }
     55
    4756    /**
    4857     * Creates a provider with a set maximum number of values
     
    5059     * @param model FacetField model to get values and counts for
    5160     * @param max maximum number of values to show
     61     * @param sort initial sort property and order
    5262     */
    53     public FacetFieldValuesProvider(IModel<FacetField> model, int max) {
     63    public FacetFieldValuesProvider(IModel<FacetField> model, int max, SortParam<FieldValuesOrder> sort) {
    5464        this.model = model;
    5565        this.maxNumberOfItems = max;
     66        setSort(sort);
    5667    }
    5768
     
    6172        // IGNORABLE_VALUES (like "unknown") are move to the back of the list and should only be shown when you click "more...", unless the list is too small then whe can just show them.
    6273        final List<FacetField.Count> values = model.getObject().getValues();
    63         return values.listIterator((int) first);
     74        return getOrdering().sortedCopy(values).listIterator((int) first);
    6475    }
    6576
     
    8091    }
    8192
     93    private Ordering getOrdering() {
     94        final Ordering ordering;
     95        if (getSort().getProperty() == FieldValuesOrder.COUNT) {
     96            ordering = COUNT_ORDERING;
     97        } else if (getSort().getProperty() == FieldValuesOrder.NAME) {
     98            ordering = NAME_ORDERING;
     99        } else {
     100            ordering = Ordering.natural();
     101        }
     102
     103        if (getSort().isAscending()) {
     104            return ordering;
     105        } else {
     106            return ordering.reverse();
     107        }
     108    }
     109
     110    private static Ordering<FacetField.Count> COUNT_ORDERING = new Ordering<FacetField.Count>() {
     111
     112        @Override
     113        public int compare(Count arg0, Count arg1) {
     114            return Long.compare(arg0.getCount(), arg1.getCount());
     115        }
     116    };
     117
     118    private static Ordering<FacetField.Count> NAME_ORDERING = new Ordering<FacetField.Count>() {
     119
     120        @Override
     121        public int compare(Count arg0, Count arg1) {
     122            return arg0.getName().compareTo(arg1.getName());
     123        }
     124    };
     125
    82126}
Note: See TracChangeset for help on using the changeset viewer.