Value List

Value list, is one of the design pattern that help efficiency to browse through a list of records. Normally when user request a set of record, they’re only interest in small portion to view by each time.

Pagination is one of the usage by implement this design pattern. If you would like to improve the performance of the system, just implement the caching to intercept the process.

valuelist

Example Code : PersonDao

package com.loongest.dao;
import com.loongest.domain.Person;
import com.loongest.vls.ValueListHanlder;

public interface PersonDao {

	public ValueListHanlder<Person> getPersonsByPage(int pageIndex);

}

Example Code : PersonDaoImpl

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.loongest.dao.PersonDao;
import com.loongest.domain.Person;
import com.loongest.vls.PersonListHandler;
import com.loongest.vls.ValueListHanlder;

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
	private PersonListHandler valueList = null;
	// Assume one page can only display 10 records
	public ValueListHanlder<Person> getPersonsByPage(int pageIndex) {

		valueList = new PersonListHandler();

		int start = 0;
		if(pageIndex > 1){
			start = (pageIndex * 10) - 9;
		}
		int end   = pageIndex * 10;

		List<Person> list = getHibernateTemplate().find("from Person p").subList(start,end);
		valueList.setList(list);

		return valueList;
	}
}

Example Code : ValueListHandler

package com.loongest.vls;

import java.io.Serializable;
import java.util.List;

public interface ValueListHanlder<T extends Serializable> {

	public int getSize();

	public T getCurrentElement();

	public List<T> getNextElements(int i);

	public List<T> getPreviousElements(int i);

	public void resetIndex();

}

Example Code : PersonListHandler

package com.loongest.vls;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import com.loongest.domain.Person;

public class PersonListHandler implements ValueListHanlder<Person>{

	private List<Person> personList = null;
	private ListIterator<Person> iterator;

	public void setList(List<Person> list) {
		this.personList = list;
		if(list != null){
			iterator = (ListIterator<Person>) personList.iterator();
		}
	}

	public Person getCurrentElement() {
		if(personList != null) {
			int index = iterator.nextIndex();
			return personList.get(index);
		}
		return null;
	}

	public List<Person> getNextElements(int i) {
		List<Person> list = new ArrayList<Person>();
		if(iterator != null) {
			for(int index = 0 ; (iterator.hasNext() && index < i); index++){
				list.add(iterator.next());
			}
		}
		return list;
	}

	public List<Person> getPreviousElements(int i) {
		List<Person> list = new ArrayList<Person>();
		if(iterator != null) {
			for(int index = 0 ; (iterator.hasPrevious() && index < i); index++){
				list.add(iterator.previous());
			}
		}
		return list;
	}

	public int getSize() {
		int size = 0;
		if(personList != null) {
			size = personList.size();
		}
		return size;
	}

	public void resetIndex() {
		if(iterator != null) {
			iterator = personList.listIterator();
		}
	}

}

Example Code: Client

package com.loongest.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.loongest.dao.PersonDao;
import com.loongest.domain.Person;
import com.loongest.vls.PersonListHandler;
import com.loongest.vls.ValueListHanlder;

@Controller
@RequestMapping("/person")
public class ClientController {

	@Autowired PersonDao personDao;

	@RequestMapping(method = RequestMethod.GET)
	public String listPerson(HttpServletRequest request, ModelMap model) {
		String pageIndex = request.getParameter("page");
		int index = toInteger(pageIndex);
		model.put("valuelist", getPersonListPage(index));
		return "person/list";
	}	

	public ValueListHanlder<Person> getPersonListPage(int pageIndex) {
		ValueListHanlder<Person> listHandler = new PersonListHandler();
		listHandler = personDao.getPersonsByPage(pageIndex);
		return listHandler;
	}

	public int toInteger(String strValue)  {
		return (strValue==null|| strValue.trim().length() < 1 ) ?
		        -1: Integer.parseInt(strValue) ;
	}

}
You can leave a response, or trackback from your own site.

Leave a Reply

Security Code: