/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package integration.anyframe.services.query;

import integration.anyframe.services.AbstractTest;
import integration.anyframe.services.query.vo.CustomerVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import anyframe.core.query.IQueryService;

public class QueryServiceTestPagination extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceTestPagination queryTest = new QueryServiceTestPagination();
		// 1. initialize context
		queryTest.setup();
		// 2. select using paging
		queryTest.testSelectQuery();
		// 3. close context
		queryTest.teardown();

		System.out.println("Successful!!!!!");
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testInit() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		queryService.updateBySQL("DROP TABLE TBL_CUSTOMER IF EXISTS",
				new String[] {}, new Object[] {});
		queryService.updateBySQL("CREATE TABLE TBL_CUSTOMER ( "
				+ "SSNO varchar(13) NOT NULL, " + "NAME varchar(20), "
				+ "ADDRESS varchar(20), " + "PRIMARY KEY (SSNO))",
				new String[] {}, new Object[] {});
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890121", "AAAAAA", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890122", "BBBBBB", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890123", "CCCCCC", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890124", "DDDDDD", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890125", "EEEEEE", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890126", "FFFFFF", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890127", "GGGGGG", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890128", "HHHHHH", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890129", "IIIIII", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890130", "JJJJJJ", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890131", "KKKKKK", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890132", "LLLLLL", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890133", "MMMMMM", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890134", "OOOOOO", "seoul" });
		queryService
				.createBySQL(
						"INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)",
						new String[] { "VARCHAR", "VARCHAR", "VARCHAR" },
						new Object[] { "1234567890135", "PPPPPP", "seoul" });
	}

	/**
	 * SELECT 수행 결과를 특정 페이지별로 조회하는 테스트 코드
	 * 이 메소드에서는 TBL_CUSTOMER 테이블에 여러 데이터가 입력되었을 때, 한 페이지에 표현하고자 하는 데이터의 개수(5)와 
	 * 조회 페이지 페이지 번호(3)를 입력하여  특정 페이지에 속한 데이터만 조회해 본다. 
	 */
	public void testSelectQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		/** findWithRowCount() : 매핑 XML 파일에 정의되어 있는 query id를 이용하여 SELECT를 실행한다.
		 *                       findWithRowCount()는 한번의 호출로 해당 SELECT로 얻을 수 있는 전체 
		 *                       데이터의 개수와 특정 페이지에 해당하는 결과값들을 얻어 올 수 있다.
		 */		
		Map resultMap = queryService.findWithRowCount("selectUsingPagination",
				new Object[] { "%1234%" }, 3, 5);
		
		CustomerVO customerVO = new CustomerVO();
		Map rsMap = new HashMap();
		// 특정 페이지에 속한 결과값들 추출
		List resultList = (List) resultMap.get(IQueryService.LIST);
		for (int i = 0; i < resultList.size(); i++) {
			customerVO = (CustomerVO) resultList.get(i);
			customerVO.getNm();
		}
		// 해당 SELECT로 얻어질 수 있는 전체 데이터의 개수 추출
		int totalSize = ((Long) resultMap.get(IQueryService.COUNT)).intValue();
		if (resultList.size() != 5 || totalSize != 15) {
			throw new Exception("Select query failed");
		}
	}

	protected String[] getConfigLocations() {
		return new String[] {
				"classpath*:/common/applicationContext-*.xml",
				"classpath*:/services/datasource/applicationContext-datasource-common.xml",
				"classpath*:/services/query/applicationContext-query-common.xml",
				"classpath*:/services/query/applicationContext-query-sqlloader.xml" };
	}
}

