views:

491

answers:

1

Hi, I've got an issue with SortableDataProvider and DataTable in wicket.

I've defined my DataTable as such:

IColumn<Column>[] columns = new IColumn[9];

//column values are mapped to the private attributes listed in ColumnImpl.java
columns[0] =  new PropertyColumn<Column>(new Model<String>("#"), "columnPosition", "columnPosition");  

columns[1] =  new PropertyColumn<Column>(new Model<String>("Description"), "description"); 
columns[2] =  new PropertyColumn<Column>(new Model<String>("Type"), "dataType", "dataType");

Adding it to the table:

DataTable<Column> dataTable = new DataTable<Column>("columnsTable", columns, provider, maxRowsPerPage) {
    @Override
    protected Item<Column> newRowItem(String id, int index, IModel<Column> model) {
        return new OddEvenItem<Column>(id, index, model);
    }
};

My data provider:

public class ColumnSortableDataProvider extends SortableDataProvider<Column> {  
private static final long serialVersionUID = 1L;

private List<Column> list = null;

public ColumnSortableDataProvider(Table table, String sortProperty) {
    this.list = Arrays.asList(table.getColumns().toArray(new Column[0]));
    setSort(sortProperty, true);
}

public ColumnSortableDataProvider(List<Column> list, String sortProperty) {
    this.list = list;
    setSort(sortProperty, true);
}

@Override
public Iterator<? extends Column> iterator(int first, int count) {
    /*
     first - first row of data
     count - minimum number of elements to retrieve
     So this method returns an iterator capable of iterating over {first, first+count} items
    */ 
    Iterator<Column> iterator = null;

    try {
        if(getSort() != null) {
            Collections.sort(list, new Comparator<Column>() {
                private static final long serialVersionUID = 1L;

                @Override
                public int compare(Column c1, Column c2) {
                    int result=1;
                    PropertyModel<Comparable> model1= new PropertyModel<Comparable>(c1, getSort().getProperty());
                    PropertyModel<Comparable> model2= new PropertyModel<Comparable>(c2, getSort().getProperty());

                    if(model1.getObject() == null && model2.getObject() == null) 
                        result = 0;
                    else if(model1.getObject() == null) 
                        result = 1;
                    else if(model2.getObject() == null) 
                        result = -1;
                    else 
                        result = ((Comparable)model1.getObject()).compareTo(model2.getObject());

                    result = getSort().isAscending() ? result : -result;

                    return result;
                }
            });
        }

        if (list.size() > (first+count))
            iterator = list.subList(first, first+count).iterator();
        else
            iterator = list.iterator();
    } 
    catch (Exception e) {
        e.printStackTrace();
    }

    return iterator;
}

The problem is the following: - I click a column header to sort by that column. - I navigate to a different page - I click Back (or Forward if I do the opposite scenario) - Page has expired.

It'd be nice to generate the page using PageParameters but I somehow need to intercept the sort event to do so.

Any pointers would be greatly appreciated. Thanks a ton!!

A: 

I don't know at a quick glance what might be causing this, but in order to help diagnose, you might want to enable debug logging for org.apache.wicket.Session or possibly more of the wicket code.

The retrieval of a page definitely involves calls to a method

public final Page getPage(final String pageMapName, final String path, final int versionNumber)

in this class, and it has some debug logging.

For help with setting up this logging, have a look at http://stackoverflow.com/questions/1140358/how-to-initialize-log4j-properly or at the docs for log4j.

Don Roby