package kccf.sch.service;

import java.util.ArrayList;

import kccf.sch.service.vo.SearchCollectionVO;



public class SearchCollectionResult {
	/** 디버그 메세지 */
	private String debugStr = "";
	/** 엔진 에러 코드 */
	private int errorCode = 0;
	/** 컬렉션 리스트 */
	private ArrayList<SearchCollectionVO> collectionList = null;
	public String getDebugStr() {
		return debugStr;
	}
	public void setDebugStr(String debugStr) {
		this.debugStr = debugStr;
	}
	public int getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}
	public ArrayList<SearchCollectionVO> getCollectionList() {
		ArrayList<SearchCollectionVO> collectionListRet = new ArrayList<SearchCollectionVO>();
		collectionListRet.addAll(collectionList);
		return collectionListRet;
	}
	public void setCollectionList(ArrayList<SearchCollectionVO> collectionList) {
		if(collectionList != null){
			this.collectionList = new ArrayList<SearchCollectionVO>();
			for(int i = 0; i < collectionList.size(); ++i){
				this.collectionList.add(collectionList.get(i));
			}
		}
	}
	
	/**
	 * 검색 결과를 XML 형태로 제공(선언부)
	 * @param charset xml 선언부 언어셋(euc-kr , UTF-8)
	 * @return 검색 결과를 XML 형태 String 값
	 */
	public String toXml(String charset){
		String encoding = "UTF-8";
		if(null != charset){
			if(charset.toUpperCase().equals("EUC-KR")){
				encoding = charset;
			}
		}
		StringBuffer xmlBr = new StringBuffer(1024);
		xmlBr.append("<?xml version=\"1.0\" encoding=\""+encoding+"\"?>").append(System.getProperty("line.separator"));
		xmlBr.append("<wisenutSearch>").append(System.getProperty("line.separator"));
		xmlBr.append("<DebugStr><![CDATA[").append(getDebugStr()).append("]]></DebugStr>").append(System.getProperty("line.separator"));
		xmlBr.append("<CollectionList>").append(System.getProperty("line.separator"));
		ArrayList<SearchCollectionVO>  collectionList =  getCollectionList();	
		int collectionListCount = collectionList.size();
		for(int i = 0 ; i < collectionListCount ; i++){
			SearchCollectionVO collVo = collectionList.get(i);
			xmlBr.append(collVo.getXml()).append(System.getProperty("line.separator"));
		}
		xmlBr.append("</CollectionList>").append(System.getProperty("line.separator"));
		xmlBr.append("</wisenutSearch>");
		return xmlBr.toString();
	}
	
	/**
	 * 검색결과를 json 형태로 반환
	 * @return 검색결과를 json 형태  String 값
	 */
	public String toJson(){
		StringBuffer jsonBr = new StringBuffer(1024);
		jsonBr.append("{\"wisenutSearch\":{");
		jsonBr.append("\"DebugStr\":").append("\"").append(getDebugStr()).append("\",");
		jsonBr.append("\"CollectionList\":");
		jsonBr.append("[");
		ArrayList<SearchCollectionVO>  collectionList = getCollectionList();
		int collectionListCount = collectionList.size();
		for(int i = 0 ; i < collectionListCount ; i++){
			SearchCollectionVO collVo = collectionList.get(i);
			jsonBr.append(collVo.getJson());
			if(i != collectionListCount-1 ){
				jsonBr.append(",");
			}
		}
		jsonBr.append("]}}");
		return jsonBr.toString();
	
	}
	
	/**
	 * 검색결과 String 형태로 반환
	 * @return 검색결과를  문자열 형태  String 값
	 */
	public String toStr(){
		StringBuffer br = new StringBuffer(1024);
		br.append("[DebugStr:").append(getDebugStr()).append(System.getProperty("line.separator"));
		ArrayList<SearchCollectionVO>  collectionList = getCollectionList();
		int collectionListCount =  collectionList.size();
		for(int i = 0 ; i < collectionListCount ; i++){
			SearchCollectionVO collVo = collectionList.get(i);
			br.append(collVo.getStr());
		}
		return br.toString();
	}
	
	
	
}
