/*
 * 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.ArrayList;

import anyframe.core.query.IQueryService;

public class QueryServiceTestBatch extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceTestBatch queryTest = new QueryServiceTestBatch();
		// 1. initialize context
		queryTest.setup();
		// 2. testBatchInsertQuery
		queryTest.testBatchInsertQuery();
		// 3. testBatchInsertBySQLQuery
		queryTest.testBatchInsertBySQLQuery();
		// 4.testBatchInsertByObjectQuery
		queryTest.testBatchInsertByObjectQuery();
		// 5. 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[] {});
	}

	/**
	 * 매핑 XML 파일에 정의된 쿼리문을 batch update를 이용해 실행.
	 */
	public void testBatchInsertQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		//ArrayList에 입력할 데이터를 저장한다.
		ArrayList args = new ArrayList();
		Object[] arg = new Object[3];
		arg[0] = "1234567890123";
		arg[1] = "KimMinsu";
		arg[2] = "Ansan";
		args.add(arg);
		arg = new Object[3];
		arg[0] = "1234567890124";
		arg[1] = "LeeSungwook";
		arg[2] = "Seoul";
		args.add(arg);
		arg = new Object[3];
		arg[0] = "1234567890125";
		arg[1] = "ParkHeejin";
		arg[2] = "Seoul";
		args.add(arg);

		//ArrayList에 3개의 Object가 포함되어 있으므로 query id가 insertbatch인
		//쿼리문이 3번 실행된다.
		int[] results = queryService.batchUpdate("insertbatch", args);
		for (int i = 0; i < results.length; i++) {
			if (results[i] == -1) {
				throw new Exception("Batch Insert falied");
			}
		}
	}

	/**
	 * 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때, 실행할 쿼리문을 직접 입력하여
	 * batch update를 실행 
	 */
	public void testBatchInsertBySQLQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		//실행 할 쿼리문을 정의
		String sql = "INSERT INTO TBL_CUSTOMER ( ssno, name, address ) VALUES (?,?,?)";

		//입력할 parameter의 SQL Type을 정의
		String[] types = new String[3];
		types[0] = "VARCHAR";
		types[1] = "VARCHAR";
		types[2] = "VARCHAR";

		//ArrayList에 입력할 데이터를 저장한다.
		ArrayList args = new ArrayList();
		Object[] arg = new Object[3];
		arg[0] = "1234567890126";
		arg[1] = "HongGildong";
		arg[2] = "Suwon";
		args.add(arg);
		arg = new Object[3];
		arg[0] = "1234567890127";
		arg[1] = "LeeSoonsin";
		arg[2] = "Seongnam";
		args.add(arg);
		arg = new Object[3];
		arg[0] = "1234567890128";
		arg[1] = "ChoiMinsu";
		arg[2] = "Seoul";
		args.add(arg);

		//ArrayList에 3개의 Object가 포함되어 있으므로 해당 쿼리문이 3번 실행된다.	
		int[] results = queryService.batchUpdateBySQL(sql, types, args);

		for (int i = 0; i < results.length; i++) {
			if (results[i] == -1) {
				throw new Exception("BatchInsertBySQL falied");
			}
		}
	}

	/**
	 * OR Mapping을 이용해서 batch update를 실행
	 */
	public void testBatchInsertByObjectQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		ArrayList args = new ArrayList();
		CustomerVO customerVO = new CustomerVO();

		customerVO.setSsno("1234567890129");
		customerVO.setNm("Smith");
		customerVO.setAddr("LA");
		args.add(customerVO);

		customerVO = new CustomerVO();
		customerVO.setSsno("1234567890130");
		customerVO.setNm("Brown");
		customerVO.setAddr("Newyork");
		args.add(customerVO);

		customerVO = new CustomerVO();
		customerVO.setSsno("1234567890131");
		customerVO.setNm("Eugene");
		customerVO.setAddr("Boston");
		args.add(customerVO);

		// 실행되는 쿼리문은 기본적인 INSERT문인
		// insert into TBL_CUSTOMER (ssno ,address ,name ) values ( ?, ?, ? )이다.
		int[] results = queryService.batchCreate(args);

		for (int i = 0; i < results.length; i++) {
			if (results[i] == -1) {
				throw new Exception("BatchInsertByObject falied");
			}
		}
	}

	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" };
	}
}

