/*
 * 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.ORMappingVO;

import java.util.Collection;

import anyframe.core.query.IQueryService;

public class QueryServiceTestORMapping extends AbstractTest {

	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceTestORMapping queryTest = new QueryServiceTestORMapping();
		// 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_ORMapping IF EXISTS",
				new String[] {}, new Object[] {});
		queryService.updateBySQL("CREATE TABLE TBL_ORMapping ( "
				+ "ID varchar(13) NOT NULL, " + "NAME varchar(20), "
				+ "ADDRESS varchar(20), " + "PRIMARY KEY (ID))",
				new String[] {}, new Object[] {});
	}

	/**
	 * Query 서비스를 통해 DB에 신규 데이터를 입력하는 테스트 코드
	 */	
	public void testInsertQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		// ORMappingVO 객체에 초기값을 셋팅하고 이 객체를 통해 INSERT를 실행한다.
		// 다음 코드에 의해 실행되는 쿼리문은
		// INSERT INTO TBL_ORMapping (address ,name ,id ) values (
		// '1234567890123', 'HonggilDong', 'Ansan' )
		
		ORMappingVO ormappingVO = new ORMappingVO("1234567890123",
				"HonggilDong", "Ansan");
		
		int result = queryService.create(ormappingVO);
		if (result == -1) {
			throw new Exception("Insert failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 조회하는 테스트 코드
	 */	
	public void testSelectQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		// primary-key로 지정한 column에 mapping되는 VO에 값을 입력하고 find를 실행하면
		// primary-key를 조회 조건으로
		// SELECT를 실행한다.
		// 다음 코드에 의해 실행되는 쿼리문은
		// SELECT NAME, ADDRESS FROM TBL_ORMAPPING WHERE ID = '1234567890123'
		
		ORMappingVO ormappingVO = new ORMappingVO();
		ormappingVO.setId("1234567890123");

		Collection result = queryService.find(ormappingVO);
		if (result.size() != 1) {
			throw new Exception("Select failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 수정하는 테스트 코드
	 */	
	public void testUpdateQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		// 다음 코드에 의해 실행되는 쿼리문은
		// Update TBL_ORMapping set address = 'Seoul' ,name = 'JungHeeWon'  where id = '1234567890123'
		
		ORMappingVO ormappingVO = new ORMappingVO("1234567890123",
				"HonggilDong", "Seoul");
		
		int result = queryService.update(ormappingVO);
		if (result == -1) {
			throw new Exception("Update failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 삭제하는 테스트 코드
	 */	
	public void testDeleteQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		// 다음 코드에 의해 실행되는 퀴리문은
		// DELETE FROM TBL_ORMapping where id = '1234567890123'
		
		ORMappingVO ormappingVO = new ORMappingVO();
		ormappingVO.setId("1234567890123");

		int result = queryService.remove(ormappingVO);
		if (result == -1) {
			throw new Exception("Update 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" };
	}
}

