// Path: web/src/main/java/org/sample/web/app/WebConstants.java
// public final class WebConstants {
// 	public static final int PAGE_SIZE=10;
// }
// 
// Path: web/src/main/java/org/sample/web/models/PersistableJpaRepositoryModel.java
// public class PersistableJpaRepositoryModel<T extends AbstractPersistable<Long>> extends LoadableDetachableModel<T> {
// 
// 	private static final long serialVersionUID = 1L;
// 	private Long id;
//     private JpaRepository<T, Long> jpaRepository;
//  
//     public PersistableJpaRepositoryModel(final Long id, final JpaRepository<T, Long> jpaRepository) {
//         super();
//         this.id = id;
//         this.jpaRepository = jpaRepository;
//     }
//  
//     public PersistableJpaRepositoryModel(final T t, final JpaRepository<T, Long> jpaRepository) {
//         super(t);
//         this.id = t.getId();
//         this.jpaRepository = jpaRepository;
//     }
//  
//     @Override
//     protected T load() {
//         return jpaRepository.findOne(id);
//     }
//    
//     
//     @Override
//     public int hashCode()
//     {
//         return Long.valueOf(id).hashCode();
//     }
//     
//     @SuppressWarnings("unchecked")
// 	@Override
//     public boolean equals(final Object obj)
//     {
//         if (obj == this)
//         {
//             return true;
//         }
//         else if (obj == null)
//         {
//             return false;
//         }
//         else if (obj instanceof PersistableJpaRepositoryModel)
//         {
//         	PersistableJpaRepositoryModel<T> other = (PersistableJpaRepositoryModel<T>)obj;
//             return other.id == id;
//         }
//         return false;
//     }
// }


import java.util.Iterator;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.sample.web.app.WebConstants;
import org.sample.web.models.PersistableJpaRepositoryModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * 
 */
package org.sample.web.components.providers.textchoice;



/**
 * @author mpostelnicu
 * 
 * Smart generic {@link SortableDataProvider} that binds to {@link JpaRepository}
 */
public class SortableJpaRepositoryDataProvider<T extends AbstractPersistable<Long>>
		extends SortableDataProvider<T, String> {

	private static final long serialVersionUID = 1L;

	protected JpaRepository<T, Long> jpaRepository;
	
	/**
	 * Always provide a proxy jpaRepository here! For example one coming from a {@link SpringBean}
	 * 
	 * @param jpaRepository
	 */
	public SortableJpaRepositoryDataProvider(
			JpaRepository<T, Long> jpaRepository) {
		this.jpaRepository = jpaRepository;
	}

	/**
	 * Translates from a {@link SortParam} to a Spring {@link Sort}
	 * 
	 * @return
	 */
	protected Sort translateSort() {
		if(getSort()==null) return null;
		return new Sort(getSort().isAscending() ? Direction.ASC
				: Direction.DESC, getSort().getProperty());
	}

	/**
	 * @see SortableDataProvider#iterator(long, long)
	 */
	@Override
	public Iterator<? extends T> iterator(long first, long count) {

// Path: web/src/main/java/org/sample/web/app/WebConstants.java
// public final class WebConstants {
// 	public static final int PAGE_SIZE=10;
// }
// 
// Path: web/src/main/java/org/sample/web/models/PersistableJpaRepositoryModel.java
// public class PersistableJpaRepositoryModel<T extends AbstractPersistable<Long>> extends LoadableDetachableModel<T> {
// 
// 	private static final long serialVersionUID = 1L;
// 	private Long id;
//     private JpaRepository<T, Long> jpaRepository;
//  
//     public PersistableJpaRepositoryModel(final Long id, final JpaRepository<T, Long> jpaRepository) {
//         super();
//         this.id = id;
//         this.jpaRepository = jpaRepository;
//     }
//  
//     public PersistableJpaRepositoryModel(final T t, final JpaRepository<T, Long> jpaRepository) {
//         super(t);
//         this.id = t.getId();
//         this.jpaRepository = jpaRepository;
//     }
//  
//     @Override
//     protected T load() {
//         return jpaRepository.findOne(id);
//     }
//    
//     
//     @Override
//     public int hashCode()
//     {
//         return Long.valueOf(id).hashCode();
//     }
//     
//     @SuppressWarnings("unchecked")
// 	@Override
//     public boolean equals(final Object obj)
//     {
//         if (obj == this)
//         {
//             return true;
//         }
//         else if (obj == null)
//         {
//             return false;
//         }
//         else if (obj instanceof PersistableJpaRepositoryModel)
//         {
//         	PersistableJpaRepositoryModel<T> other = (PersistableJpaRepositoryModel<T>)obj;
//             return other.id == id;
//         }
//         return false;
//     }
// }


// Path: web/src/main/java/org/sample/web/components/providers/textchoice/SortableJpaRepositoryDataProvider.java
import java.util.Iterator;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.sample.web.app.WebConstants;
import org.sample.web.models.PersistableJpaRepositoryModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * 
 */
package org.sample.web.components.providers.textchoice;



/**
 * @author mpostelnicu
 * 
 * Smart generic {@link SortableDataProvider} that binds to {@link JpaRepository}
 */
public class SortableJpaRepositoryDataProvider<T extends AbstractPersistable<Long>>
		extends SortableDataProvider<T, String> {

	private static final long serialVersionUID = 1L;

	protected JpaRepository<T, Long> jpaRepository;
	
	/**
	 * Always provide a proxy jpaRepository here! For example one coming from a {@link SpringBean}
	 * 
	 * @param jpaRepository
	 */
	public SortableJpaRepositoryDataProvider(
			JpaRepository<T, Long> jpaRepository) {
		this.jpaRepository = jpaRepository;
	}

	/**
	 * Translates from a {@link SortParam} to a Spring {@link Sort}
	 * 
	 * @return
	 */
	protected Sort translateSort() {
		if(getSort()==null) return null;
		return new Sort(getSort().isAscending() ? Direction.ASC
				: Direction.DESC, getSort().getProperty());
	}

	/**
	 * @see SortableDataProvider#iterator(long, long)
	 */
	@Override
	public Iterator<? extends T> iterator(long first, long count) {

		int page = (int)((double) first /  WebConstants.PAGE_SIZE);
