package com.blueland.bpm.identity.service;

import java.util.List;





import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.identity.RoleService;
import com.blueland.bpm.identity.dao.RoleQueryDao;
import com.blueland.bpm.identity.dao.UserroleDao;
import com.blueland.bpm.identity.model.Role;
import com.blueland.bpm.identity.model.Userrole;
import com.blueland.bpm.identity.util.IdentityException;

@Service
public class RoleServiceImpl implements RoleService {
	private static final Log logger = LogFactory.getLog(RoleServiceImpl.class);
	@Autowired
	private RoleQueryDao roleQueryDao;
	@Autowired
	private UserroleDao userroleDao;
	@Override
	public Role getRoleById(String roleId) {
		Role role = null;
		try {
			role = roleQueryDao.getById(roleId);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return role;
	}

	@Override
	public List<Role> findRoleByUserId(String userId) {
		List<Role> roles = null;
		try {
			roles = roleQueryDao.findRoleByUser(userId);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return roles;
	}

	@Override
	public List<Role> findAllRole() {
		List<Role> roles = null;
		try {
			roles = roleQueryDao.findByExample(new Role());
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return roles;
	}

	@Override
	public List<Role> findRoleByEntityCriteria(Role paramEntity) {
		List<Role> roles = null;
		try {
			roles = roleQueryDao.findByExample(paramEntity);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return roles;
	}

	@Override
	public boolean isUserInRole(String roleId, String userId) {
		boolean isUserInRoleFlag = false;
		try {
			isUserInRoleFlag = roleQueryDao.isUserInRole(roleId, userId);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return isUserInRoleFlag;
	}
	@Override
	public Boolean addUsers2Role(Userrole userrole) {
		return userroleDao.save(userrole);
	}
	
	@Override
	public int delUsers2Role(Userrole userrole) {
		return userroleDao.delUserrole(userrole);
	}
	@Override
	public List<Role> findRoleByUserCode(String userCode) {
		List<Role> roles = null;
		try {
			roles = roleQueryDao.findRoleByUserCode(userCode);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return roles;
	}
}
