package esos.client.data.ibatisPOJO;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.SortSpecifier;
import com.smartgwt.client.types.SortDirection;

/**
 * POJO containing paging information
 * @author meindert
 *
 */
public class Paging implements IsSerializable {
	
	private Integer startRow;
	private Integer endRow;
	private String sortBy;
	private Boolean onePage;

	public static String ASCENDING = "asc";
	public static String DESCENDING = "desc";
	
	public String toString(){
		StringBuffer retVal = new StringBuffer("[");
		retVal.append("startRow=" + (startRow==null? "NULL" : startRow) + ";");
		retVal.append("endRow=" + (endRow==null? "NULL" : endRow) + ";");
		retVal.append("sortBy=" + (sortBy==null? "NULL" : sortBy) + ";");
		return retVal.append("]").toString();
	}
	
	public Paging(){
		
	}
	
	public Paging(Integer startRow, Integer endRow, SortSpecifier[] sortBy) {
		this();
		this.startRow=startRow;
		this.endRow=endRow;
		if (sortBy!=null && sortBy.length>0){
			this.sortBy=sortBy[0].getField();
			if (sortBy[0].getSortDirection().equals(SortDirection.DESCENDING)){
				this.sortBy="-" + this.sortBy;
			}
		}
		
	}
	
	public Paging(Integer startRow, Integer endRow, String sortBy) {
		this();
		this.startRow=startRow;
		this.endRow=endRow;
		this.sortBy=sortBy;
	}
	
	public void setRequest(DSRequest request){
		this.startRow=request.getStartRow ();
		this.endRow=request.getEndRow ();
		
		//Smart GWT issue fixed in Feb 2010
		//http://forums.smartclient.com/showthread.php?t=8935&highlight=request.getSortBy
		//As a workaround we get the sortBy as string	
		String sortBy = request.getAttribute("sortBy");
		this.sortBy=sortBy;
		/*if (request.getSortBy()!=null && request.getSortBy().length>0){
			this.sortBy=request.getSortBy()[0].getField();
			if (request.getSortBy()[0].getSortDirection().equals(SortDirection.DESCENDING)){
				this.sortBy="-" + this.sortBy;
			}
		}*/
	}
	
	
	public void setStartRow(Integer startRow) {
		this.startRow = startRow;
	}
	public Integer getStartRow() {
		return startRow;
	}
	public void setEndRow(Integer endRow) {
		this.endRow = endRow;
	}
	public Integer getEndRow() {
		return endRow;
	}
	public void setSortBy(String sortBy) {
		this.sortBy = sortBy;
	}
	public String getSortBy() {
		if (sortBy!=null && sortBy.charAt(0)=='-'){
			return sortBy.substring(1);
		}else{
			return sortBy;
		}
	}

	public String getSortorder(){
		if (sortBy!=null && sortBy.charAt(0)=='-'){
			return DESCENDING;
		}else{
			return ASCENDING;
		}
	}
	
	public Map<String, Object> toMap() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("startRow", startRow);
		map.put("endRow", endRow);
		if (sortBy!=null && sortBy.charAt(0)=='-'){
			map.put("sortorder", "desc");
			sortBy=sortBy.substring(1);
		}else{
			map.put("sortorder", "asc");
		}
		map.put("sortBy", sortBy);
		return map;
	}

	public void setOnePage(Boolean onePage) {
		this.onePage = onePage;
	}

	/**
	 * Flag to indicate if there are more data available on the server
	 * @return true if there is only one page in the result set
	 */
	public Boolean getOnePage() {
		return onePage;
	}

	
}
