/*
 * 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.user;

import integration.anyframe.services.AbstractTest;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import anyframe.core.query.IQueryService;

import com.sds.emp.common.EmpException;
import com.sds.emp.user.services.UserService;
import com.sds.emp.user.services.UserVO;

public class UserServiceWithProgrammaticTest extends AbstractTest {
	public static void main(String[] args) throws Exception {
		UserServiceWithProgrammaticTest userTest = new UserServiceWithProgrammaticTest();

		// 1. initialize context
		userTest.setup();
		// 2. test
		userTest.testAddUserUsingTransactionTemplate();
		userTest.testAddUserUsingTransactionManager();
		// 3. close context
		userTest.teardown();
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			// ignore this exception
		}
	}

	public void testInit() throws Exception {
		IQueryService m_service = (IQueryService) context
				.getBean("oracle_queryservice");

		// Try to drop the table. It may not exist and throw an exception.
		System.out.println("Attempting to drop old table");
		m_service.updateBySQL("DROP TABLE USERS", new String[] {},
				new Object[] {});

		// Create the table that we will use in this test.
		// Different depending on the db. Please add new statements as new
		// databases are tested.
		System.out.println("Create new table");
		m_service.updateBySQL("CREATE TABLE USERS ("
				+ "USER_ID VARCHAR(20) PRIMARY KEY, "
				+ "USER_NAME VARCHAR(50) NOT NULL, "
				+ "PASSWORD VARCHAR(10) NOT NULL, " + "ROLE VARCHAR(5), "
				+ "SSN VARCHAR(13), " + "SL_YN CHAR(1), "
				+ "BIRTH_DAY VARCHAR(8), " + "AGE NUMERIC(3), "
				+ "CELL_PHONE VARCHAR(14), " + "ADDR VARCHAR(100), "
				+ "EMAIL VARCHAR(50), " + "EMAIL_YN CHAR(1), "
				+ "IMAGE_FILE VARCHAR(100), " + "REG_DATE DATE )",
				new String[] {}, new Object[] {});
	}

	public void testAddUserUsingTransactionTemplate() throws Exception {
		TransactionTemplate transactionTemplate = (TransactionTemplate) context
				.getBean("transactionTemplate");
		final UserService userService = (UserService) context
				.getBean(UserService.ROLE);

		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			public void doInTransactionWithoutResult(TransactionStatus status) {

				try {
					UserVO userVO = new UserVO();
					userVO.setUserId("woos41");
					userVO.setUserName("gang");
					userVO.setPassword("gang");
					userVO.setRole("user");
					userVO.setSsn("1234567890");
					userVO.setSlYn("Y");
					userVO.setBirthDay("19750319");
					userVO.setAge(null);
					userVO.setCellPhone("1234567890");
					userVO.setAddr("kamala road");
					userVO.setEmail("ga@samsung.com");
					userVO.setEmailYn("y");
					userVO.setImageFile("ga");
					userVO.setRegDate(null);

					userService.addUser(userVO);
					userService.addUser(userVO);
				} catch (EmpException e) {
					status.setRollbackOnly();
				}
			}
		});

		try {
			userService.getUser("woos41");
			throw new Exception("fail to transaction management.");
		} catch (EmpException e) {
			System.out.println("Successful!");
		}
	}

	public void testAddUserUsingTransactionManager() throws Exception {
		PlatformTransactionManager transactionService = (PlatformTransactionManager) context
				.getBean("oracleTransactionManagerDataSource");
		UserService userService = (UserService) context
				.getBean(UserService.ROLE);

		DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
		txDefinition
				.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = transactionService
				.getTransaction(txDefinition);

		try {
			UserVO userVO = new UserVO();
			userVO.setUserId("woos41");
			userVO.setUserName("gang");
			userVO.setPassword("gang");
			userVO.setRole("user");
			userVO.setSsn("1234567890");
			userVO.setSlYn("Y");
			userVO.setBirthDay("19750319");
			userVO.setAge(null);
			userVO.setCellPhone("1234567890");
			userVO.setAddr("kamala road");
			userVO.setEmail("ga@samsung.com");
			userVO.setEmailYn("y");
			userVO.setImageFile("ga");
			userVO.setRegDate(null);

			userService.addUser(userVO);
			userService.addUser(userVO);

			transactionService.commit(status);
		} catch (EmpException e) {
			transactionService.rollback(status);
		}

		try {
			userService.getUser("woos41");
			throw new Exception("fail to transaction management.");
		} catch (EmpException e) {
			System.out.println("Successful!");
		}
	}

	protected String[] getConfigLocations() {
		// TODO Auto-generated method stub
		return new String[] {
				"classpath*:/common/applicationContext-*.xml",
				"classpath*:/applications/user/applicationContext-user-general.xml",
				"classpath*:/services/query/applicationContext-query-oracle.xml",
				"classpath*:/services/query/applicationContext-query-sqlloader.xml",
				"classpath*:/services/datasource/applicationContext-datasource-oracle.xml",
				"classpath*:/services/properties/applicationContext-*.xml",
				"classpath*:/services/transaction/applicationContext-transaction-datasource-oracle.xml",
				"classpath*:/services/transaction/applicationContext-transaction-template.xml" };
	}
}

