package itn.com.cmm.util;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 프로그램 파일명 : Criteria.java
 * 
 * 프로그램 설명 : 검색용 객체
 * 
 * 작 성 자 : jeong hoon hee
 * 
 * 작 성 일 : 2013. 12. 02.
 * 
 * Copyright(c) 2013 DAEYOON Co. Ltd. All rights reserved.
 */
public class Criteria implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 검색용 맵 객체
	 */
	private Map<String, Object> condition = new HashMap<String, Object>();
	
	/**
	 * getParams() 메소드에서 append 할지여부의 condition맵
	 */
	private Map<String, Boolean> paramViewMap = new HashMap<String, Boolean>();

	/**
	 * 정렬용 리스트 객체
	 */
	private List<Order> orderList = new ArrayList<Order>();

	/** 검색조건 */
	private String searchCondition = "";

	/** 검색Keyword */
	private String searchKeyword = "";

	/** 검색사용여부 */
	private String searchUseYn = "";

	/** 현재페이지 */
	private int pageIndex = 1;

	/** 페이지갯수 */
	private int pageUnit = 10;

	/** 페이지사이즈 */
	private int pageSize = 10;

	/** firstIndex */
	private int firstIndex = 1;

	/** lastIndex */
	private int lastIndex = 1;

	/** recordCountPerPage */
	private int recordCountPerPage = 10;

	/** 검색KeywordFrom */
	private String searchKeywordFrom = "";

	/** 검색KeywordTo */
	private String searchKeywordTo = "";

	/** 메뉴번호 */
	private Integer menuNo;
	
	/**
	 * 기본생성자
	 */
	public Criteria() {
	}

	/**
	 * request객체에서 넘어오는 검색파라미터를 셋팅한다. (* condition 맵처리)
	 * 
	 * @param request
	 */
	@SuppressWarnings("unchecked")
	public Criteria(HttpServletRequest request) {

		Set<String> set = request.getParameterMap().keySet();
		Iterator<String> iter = set.iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			if (key.equals("pageUnit")) {
				try {
					pageUnit = Integer.parseInt(request.getParameter(key));

					this.condition.put("pageUnit", pageUnit);
					this.paramViewMap.put("pageUnit", true);
				} catch (Exception e) {
				}
			} else if (key.equals("pageSize")) {
				try {
					pageSize = Integer.parseInt(request.getParameter(key));

					this.condition.put("pageSize", pageSize);
					this.paramViewMap.put("pageSize", true);
				} catch (Exception e) {
				}
			} else if (key.equals("pageIndex")) {
				try {
					pageIndex = Integer.parseInt(request.getParameter(key));

					this.condition.put("pageIndex", pageIndex);
					this.paramViewMap.put("pageIndex", true);
				} catch (Exception e) {
				}
			} else if (key.equals("searchCondition")) {
				try {
					searchCondition = request.getParameter(key);

					this.condition.put("searchCondition", searchCondition);
					this.paramViewMap.put("searchCondition", true);
				} catch (Exception e) {
				}
			} else if (key.equals("searchKeyword")) {
				try {
					searchKeyword = request.getParameter(key);

					this.condition.put("searchKeyword", searchKeyword);
					this.paramViewMap.put("searchKeyword", true);
				} catch (Exception e) {
				}
			} else if (key.equals("menuNo")) {
				try {
					menuNo = Integer.parseInt(request.getParameter(key));

					this.condition.put("menuNo", menuNo);
					this.paramViewMap.put("menuNo", true);
				} catch (Exception e) {
				}
			} else if (key.startsWith("condition.")) {
				String conditionKey = StringUtils.substringAfter(key, "condition.");
				String conditionValue = request.getParameter(key);
				if (StringUtils.isNotEmpty(conditionValue)) {
					this.condition.put(conditionKey, conditionValue);
					this.paramViewMap.put(conditionKey, true);
				}
			}
		}
	}
	
	public Map<String, Object> getCondition() {
		return condition;
	}

	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}

	public Object remove(Object key) {
		return this.condition.remove(key);
	}
	
	public Object get(String key) {
		return this.condition.get(key);
	}
	
	public Object put(String key, Object value) {
		this.paramViewMap.put(key, true);
		return this.condition.put(key, value);
	}
	
	public Object put(String key, Object value, boolean append) {
		this.paramViewMap.put(key, append);
		return this.condition.put(key, value);
	}

	public List<Order> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}

	public void addOrder(Order order) {
		this.orderList.add(order);
	}

	public String getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(String searchCondition) {
		this.searchCondition = searchCondition;
	}

	public String getSearchKeyword() {
		return searchKeyword;
	}

	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}

	public Integer getMenuNo() {
		return menuNo;
	}

	public void setMenuNo(Integer menuNo) {
		this.menuNo = menuNo;
	}

	public String getSearchUseYn() {
		return searchUseYn;
	}

	public void setSearchUseYn(String searchUseYn) {
		this.searchUseYn = searchUseYn;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
		condition.put("pageIndex", pageIndex);
	}

	public int getPageUnit() {
		return pageUnit;
	}

	public void setPageUnit(int pageUnit) {
		this.pageUnit = pageUnit;
		condition.put("pageUnit", pageUnit);
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		condition.put("pageSize", pageSize);
	}

	public int getFirstIndex() {
		return firstIndex;
	}

	public void setFirstIndex(int firstIndex) {
		this.firstIndex = firstIndex;
		condition.put("firstIndex", firstIndex);
	}

	public int getLastIndex() {
		return lastIndex;
	}

	public void setLastIndex(int lastIndex) {
		this.lastIndex = lastIndex;
		condition.put("lastIndex", lastIndex);
	}

	public int getRecordCountPerPage() {
		return recordCountPerPage;
	}

	public void setRecordCountPerPage(int recordCountPerPage) {
		this.recordCountPerPage = recordCountPerPage;
		condition.put("recordCountPerPage", recordCountPerPage);
	}

	public String getSearchKeywordFrom() {
		return searchKeywordFrom;
	}

	public void setSearchKeywordFrom(String searchKeywordFrom) {
		this.searchKeywordFrom = searchKeywordFrom;
	}

	public String getSearchKeywordTo() {
		return searchKeywordTo;
	}

	public void setSearchKeywordTo(String searchKeywordTo) {
		this.searchKeywordTo = searchKeywordTo;
	}

	/**
	 * 검색파라미터 문자열을 리턴한다.
	 * @return
	 */
	public String getParams() {
		return getParams(true);
	}
	
	/**
	 * 검색파라미터 문자열을 리턴한다.
	 * @param appendPageIndex-pageindex를 붙힐지말지여부
	 * @return
	 */
	public String getParams(boolean appendPageIndex) {
		StringBuffer parameterString = new StringBuffer();
		if (appendPageIndex) {
			parameterString.append("pageIndex=");
			parameterString.append(this.pageIndex);
			parameterString.append("&");
		}
		parameterString.append("pageUnit=");
		parameterString.append(this.pageUnit);
		parameterString.append("&pageSize=");
		parameterString.append(this.pageSize);
		parameterString.append("&searchCondition=");
		parameterString.append(StringUtils.defaultString(this.searchCondition));
		parameterString.append("&searchKeyword=");
		try {
			parameterString.append(URLEncoder.encode(StringUtils.defaultString(this.searchKeyword), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			System.out.println("UnsupportedEncodingException By Criteria.getParams() [searchValue] : " + e.getMessage());
		}
		parameterString.append("&menuNo=");
		parameterString.append((this.menuNo != null) ? this.menuNo : "");
		if (this.condition.size() > 0) {
			Set<String> set = this.condition.keySet();
			Iterator<String> iter = set.iterator();
			while (iter.hasNext()) {
				String key = iter.next();
				Object value = this.condition.get(key);
				Boolean append = this.paramViewMap.get(key)!=null?this.paramViewMap.get(key):true;
				if (append) {
					parameterString.append("&condition.");
					parameterString.append(key);
					parameterString.append("=");
					if (value instanceof String) {
						if (StringUtils.isNotEmpty((String) value)) {
							try {
								parameterString.append(URLEncoder.encode((String) value, "UTF-8"));
							} catch (UnsupportedEncodingException e) {
								System.out.println("UnsupportedEncodingException By Criteria.getParams() [condition] : " + e.getMessage());
							}
						}
					} else {
						parameterString.append(value);
					}
				}
			}
		}

		return parameterString.toString();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
