/*
 * 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 com.sds.emp.user.services.impl;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;

import anyframe.core.query.QueryServiceException;

import com.sds.emp.common.EmpException;
import com.sds.emp.common.Page;
import com.sds.emp.user.services.SearchVO;
import com.sds.emp.user.services.UserService;
import com.sds.emp.user.services.UserVO;

public class UserServiceImplWithXML implements UserService,
		ApplicationContextAware {

	/** an instance variable for the UserDAO. */
	private UserDAO userDAO;

	/** Used by <code>LOGGER</code> and for creating Exception message. */
	private MessageSource messageSource;

	/**
	 * sets User dao
	 * 
	 * @param userDAO
	 *            user dao
	 */
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	/**
	 * Returns User List as <code>Page</code> Object.
	 * 
	 * @param searchVO
	 *            Search Object.
	 * @return <code>Page</code> containing user List.
	 * @throws EmpException
	 *             if query fails.
	 * @see com.sds.emp.user.services.UserService#getUserList(com.sds.emp.user.services.SearchVO)
	 */
	public Page getUserList(SearchVO searchVO) throws EmpException {
		try {

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(messageSource.getMessage(
						"debug.user.get.list.condition",
						new String[] { searchVO.getSearchCondition() }, Locale
								.getDefault()));

				LOGGER.debug(messageSource.getMessage(
						"debug.user.get.list.keyword", new String[] { searchVO
								.getSearchKeyword() }, Locale.getDefault()));
				LOGGER.debug(messageSource.getMessage(
						"debug.user.get.list.pageIndex",
						new String[] { searchVO.getPageIndex() + "" }, Locale
								.getDefault()));
			}

			return userDAO.getUserList(searchVO);
		} catch (EmpException e) {
			throw e;
		} catch (QueryServiceException e) {
			LOGGER.error(messageSource.getMessage("error.user.get.list.query",
					new String[] {}, Locale.getDefault()), e);

			throw new EmpException(messageSource.getMessage(
					"error.user.get.list.query", new String[] {}, Locale
							.getDefault()), e);
		} catch (Exception e) {
			e.printStackTrace();

			LOGGER.error(messageSource.getMessage("error.user.get.list", null,
					Locale.getDefault()));
			throw new EmpException(messageSource
					.getMessage("error.user.get.list", new String[] {}, Locale
							.getDefault()), null);
		}
	}

	/**
	 * Returns <code>UserVO</code> for the userid passed.
	 * 
	 * @param userId
	 *            useri for which search will happen.
	 * @return UserVO object
	 * @throws EmpException
	 *             if </code>UserVo</code> is not found or if any exception is
	 *             raised.
	 * @see com.sds.emp.user.services.UserService#getUser(java.lang.String)
	 */
	public UserVO getUser(String userId) throws EmpException {
		try {
			UserVO userVO = userDAO.getUser(userId);

			if (userVO == null) {
				if (LOGGER.isDebugEnabled()) {
					LOGGER.debug(messageSource.getMessage("debug.user.get",
							new String[] { userId }, Locale.getDefault()));
				}
				throw new EmpException(messageSource.getMessage(
						"debug.user.get", new String[] { userId }, Locale
								.getDefault()), null);
			}

			return userVO;
		} catch (EmpException e) {
			throw e;
		} catch (QueryServiceException e) {
			LOGGER.error(messageSource.getMessage("error.user.get.query",
					new String[] { userId }, Locale.getDefault()), e);

			throw new EmpException(messageSource.getMessage(
					"error.user.get.query", new String[] { userId }, Locale
							.getDefault()), e);
		} catch (Exception e) {

			LOGGER.error(messageSource.getMessage("error.user.get",
					new String[] { userId }, Locale.getDefault()));
			throw new EmpException(messageSource.getMessage("error.user.get",
					new String[] { userId }, Locale.getDefault()), e);
		}
	}

	/**
	 * Adds User.
	 * 
	 * @param userVO
	 *            User Info
	 * @throws EmpException
	 *             if add query fails.
	 * @see com.sds.emp.user.services.UserService#addUser(com.sds.emp.user.services.UserVO)
	 */
	public void addUser(UserVO userVO) throws EmpException {

		try {
			userDAO.addUser(userVO);
		}

		catch (QueryServiceException e) {
			LOGGER.error(messageSource.getMessage("error.user.add.query",
					new String[] {}, Locale.getDefault()), e);

			throw new EmpException(messageSource.getMessage(
					"error.user.add.query", new String[] {}, Locale
							.getDefault()), e);
		} catch (Exception e) {
			String userId = userVO.getUserName();

			LOGGER.error(messageSource.getMessage("error.user.add",
					new String[] { userId }, Locale.getDefault()));
			throw new EmpException(messageSource.getMessage("error.user.add",
					new String[] { userId }, Locale.getDefault()), e);
		}

	}

	/**
	 * Update User.
	 * 
	 * @param userVO
	 *            User Info
	 * @throws EmpException
	 *             if update query fails or if user id is not found.
	 * @see com.sds.emp.user.services.UserService#updateUser(com.sds.emp.user.services.UserVO)
	 */
	public void updateUser(UserVO userVO) throws EmpException {

		try {
			userDAO.updateUser(userVO);
		} catch (EmpException e) {
			throw e;
		} catch (QueryServiceException e) {
			LOGGER.error(messageSource.getMessage("error.user.update.query",
					new String[] {}, Locale.getDefault()), e);

			throw new EmpException(messageSource.getMessage(
					"error.user.update.query", new String[] {}, Locale
							.getDefault()), e);
		} catch (Exception e) {
			String userId = userVO.getUserId();
			LOGGER.error(messageSource.getMessage("error.user.update",
					new String[] { userId }, Locale.getDefault()));
			throw new EmpException(messageSource.getMessage(
					"error.user.update", new String[] { userId }, Locale
							.getDefault()), e);
		}
	}

	/**
	 * check for duplicate user id.
	 * 
	 * @param userId
	 *            user id to be searched
	 * @return <code>true</code> if user exist else <code>false</code>
	 * @throws EmpException
	 *             if query fails
	 * @see com.sds.emp.user.services.UserService#checkDuplication(java.lang.String)
	 */
	public boolean checkDuplication(String userId) throws EmpException {
		try {
			int count = userDAO.checkDuplication(userId);
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(messageSource.getMessage(
						"debug.user.checkDuplication", new String[] { userId,
								count + "" }, Locale.getDefault()));
			}
			return !(count == 0);
		} catch (EmpException e) {
			throw e;
		} catch (QueryServiceException e) {
			LOGGER.error(messageSource.getMessage(
					"error.user.checkDuplication.query", new String[] {},
					Locale.getDefault()), e);

			throw new EmpException(messageSource.getMessage(
					"error.user.checkDuplication.query", new String[] {},
					Locale.getDefault()), e);
		} catch (Exception e) {

			LOGGER.error(messageSource.getMessage(
					"error.user.checkDuplication", new String[] { userId },
					Locale.getDefault()));
			throw new EmpException(messageSource.getMessage(
					"error.user.checkDuplication", new String[] { userId },
					Locale.getDefault()), e);
		}
	}

	/**
	 * This method is used to get the product details based on product No
	 * 
	 * @param applicationContext
	 *            <code>ApplicationContext</code> object.
	 * @throws BeansException
	 *             if bean is not found *
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		messageSource = (MessageSource) applicationContext
				.getBean("messageSource");
	}

	public void updateUserList(UserVO newUser, UserVO updateUser)
			throws EmpException {
		String userName = "";
		try {
			userName = newUser.getUserName();
			userDAO.addUser(newUser);

			userName = updateUser.getUserName();
			int result = userDAO.updateUser(updateUser);
			if (result <= 0) {
				throw new Exception("fail to update with wrong userid.");
			}
		} catch (Exception e) {
			LOGGER.error(messageSource.getMessage("error.user.update",
					new String[] { userName }, Locale.getDefault()));
			throw new EmpException(messageSource.getMessage(
					"error.user.update", new String[] { userName }, Locale
							.getDefault()), e);
		}
	}

	public void getUserWithException() throws EmpException {
		LOGGER.error(messageSource.getMessage("error.user.get",
				new String[] { "test for afterthrowing aspect" }, Locale
						.getDefault()));
		throw new EmpException(messageSource.getMessage("error.user.get",
				new String[] { "test for afterthrowing aspect" }, Locale
						.getDefault()));
	}

}

