/*
 * 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.CamelCasedCustomerVO;
import integration.anyframe.services.query.vo.CompositionCustomerVO;
import integration.anyframe.services.query.vo.CustomerVO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import anyframe.core.query.IQueryService;

/**
 * DefaultQueryService가 제공하는 기능을 테스트하기 위한 샘플 테스트 코드
 */
public class QueryServiceTestGeneral extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceTestGeneral queryTest = new QueryServiceTestGeneral();
		// 1. initialize context
		queryTest.setup();
		// 2. testInsertQuery();
		queryTest.testInsertQuery();
		// 3. testSelectQuery();
		queryTest.testSelectQuery();
		// 4. testUpdateQuery();
		queryTest.testUpdateQuery();
		// 5. testDeleteQuery();
		queryTest.testDeleteQuery();
		// 6. close context
		queryTest.teardown();

		System.out.println("Successful!!!!!");
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 테스트 코드를 샐행하기 위한 data table 생성
	 */
	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[] {});
	}

	/**
	 * Query 서비스를 통해 DB에 신규 데이터를 입력하는 테스트 코드
	 */
	public void testInsertQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		//create() : 매핑 XML 파일에 정의되어 있는 query id를 이용하여 INSERT를 실행한다.
		int rs = queryService.create("create", new Object[] { "1234567890123",
				"AAAAA", "seoul" });
		if (rs == -1) {
			throw new Exception("Insert query failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 조회하는 테스트 코드
	 */
	public void testSelectQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		//find() : 매핑 XML 파일에 정의되어 있는 query id를 이용하여 SELECT를 실행한다.

		// 일반적인 경우
		ArrayList rsquery = (ArrayList) queryService.find("selectGeneral",
				new Object[] { "%12345%" });
		Map hsRsquery = new HashMap();
		for (int i = 0; i < rsquery.size(); i++) {
			hsRsquery = (Map) rsquery.get(i);
			String name = (String) hsRsquery.get("name");
		}
		
		// table-mapping을 정의한 경우
		Collection rsqueryUsingTableMapping = queryService.find(
				"selectUsingTableMapping", new Object[] { "%12345%" });
		Iterator rsqueryItr = rsqueryUsingTableMapping.iterator();
		while (rsqueryItr.hasNext()) {
			CustomerVO customer = (CustomerVO) rsqueryItr.next();
			String name = customer.getNm();
		}

		// result-mapping을 정의한 경우
		Collection rsqueryUsingResultMapping = queryService.find(
				"selectUsingResultMapping", new Object[] { "%12345%" });
		Iterator rsqueryItr_01 = rsqueryUsingResultMapping.iterator();
		while (rsqueryItr_01.hasNext()) {
			CompositionCustomerVO compositionCustomer = (CompositionCustomerVO) rsqueryItr_01
					.next();
			String name = compositionCustomer.getCompositionName();
		}
		
		// result class만 정의한 경우
		Collection rsqueryUsingOnlyResultClass = queryService.find(
				"selectUsingOnlyResultClass", new Object[] { "%12345%" });
		Iterator rsqueryItr_02 = rsqueryUsingOnlyResultClass.iterator();
		while (rsqueryItr_02.hasNext()) {
			CamelCasedCustomerVO camelCasedCustomerVO = (CamelCasedCustomerVO) rsqueryItr_02
					.next();
			String name = camelCasedCustomerVO.getName();
		}		

		if (rsquery.size() != 1) {
			throw new Exception("Select query failed");
		}
		if (rsqueryUsingTableMapping.size() != 1) {
			throw new Exception("Select query failed");
		}
		if (rsqueryUsingResultMapping.size() != 1) {
			throw new Exception("Select query failed");
		}
		if (rsqueryUsingOnlyResultClass.size() != 1) {
			throw new Exception("Select query failed");
		}		
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 수정하는 테스트 코드
	 */
	public void testUpdateQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		// update() : 매핑 XML 파일에 정의되어 있는 query id를 이용하여 UPDATE를 실행한다.
		int rs = queryService.update("update", new Object[] { "9999999999999",
				"AAAAA", "busan", "1234567890123" });
		if (rs == -1) {
			throw new Exception("Update query failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 삭제하는 테스트 코드
	 */
	public void testDeleteQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		// remove() : 매핑 XML 파일에 정의되어 있는 query id를 이용하여 DELETE를 실행한다.
		int rs = queryService
				.remove("delete", new Object[] { "9999999999999" });
		if (rs == -1) {
			throw new Exception("Delete 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" };
	}
}

