日期:2014-05-16  浏览次数:20502 次

连接LDAP数据库CRUD,包括分页查询的持久层代码
连接LDAP数据库CRUD,包括分页查询的持久层代码

package com.scnu.ytxt.persistence.sqlmapdao.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;

import org.springframework.ldap.AttributesMapper;
import org.springframework.ldap.CollectingNameClassPairCallbackHandler;
import org.springframework.ldap.LdapTemplate;
import org.springframework.ldap.support.DistinguishedName;
import org.springframework.ldap.support.control.PagedResultsRequestControl;
import org.springframework.ldap.support.filter.AndFilter;
import org.springframework.ldap.support.filter.EqualsFilter;

import com.scnu.ytxt.attribute.AttributeBuilder;
import com.scnu.ytxt.attribute.AttributeFather;
import com.scnu.ytxt.attribute.AttributeMapper;
import com.scnu.ytxt.persistence.iface.user.UserManageDao;

/**
 * 用户管理DaoImpl
 * 
 * @author Ray
 */
public class UserManageDaoImpl implements UserManageDao {
	private LdapTemplate ldapTemplate;
	/** objectClass类型 */
	private static final String OBJECT_NAME = "user";
	/** 节点名 */
	private static final String BASE = "ou=user";
	/** 所需要的类名称 */
	private static final String ENTITY_CLASSNAME
						= "com.scnu.ytxt.persistence.sqlmapdao.user.UserForLDAP";
	/** 主键 */
	private static final String PRIMARY_KEY = "userUid";
	/** 最大页数 */
	private static final int MAX_PAGE = 10000;
	
	AttributeFather father = null;

	AttributeBuilder builder = null;
	
	AttributeMapper mapper = null;
	
	public void setLdapTemplate(LdapTemplate ldapTemplate)
	{
		this.ldapTemplate = ldapTemplate;
	}

	/** 构造函数 */
	public UserManageDaoImpl()
	{
		father = new AttributeFather(ENTITY_CLASSNAME);
		
		builder = new AttributeBuilder();
		
		mapper = new AttributeMapper();
	}

	/** 创建条目对应的DN */
	public DistinguishedName getDN(Object parameterObject, String primaryKey,
			String ouBase)
	{
		UserForLDAP userForLDAP = (UserForLDAP) parameterObject;

		// 设置在ou=user下查找对象
		DistinguishedName newContactDN = new DistinguishedName(ouBase);

		// 设置需要删除的条目的DN
		newContactDN.add(primaryKey, userForLDAP.getUserUid());

		return newContactDN;
	}

	/** 添加 */
	public int addUser(final Object parameterObject)
	{
		try
		{
			// 获取用户传来的属性集
			Attributes attri = builder.getUserAttrs(parameterObject,
					OBJECT_NAME);

			DistinguishedName newContactDN = getDN(parameterObject,
					PRIMARY_KEY, BASE);
			// 把新条目绑定到LDAP
			ldapTemplate.bind(newContactDN, null, attri);
			// 参数说明("userUid=liling,ou=user" , null(绑定对象) , 要添加的字段集)

			return 1;

		}
		catch (Exception e)
		{
			System.out.println("异常:" + e);
			return 0;
		}
	}

	/** 删除 */
	public int delUser(final Object parameterObject)
	{
		DistinguishedName newContactDN
				= getDN(parameterObject, PRIMARY_KEY,BASE);
		try
		{

			// 进行删除,其中true表示同时删除其孩子结点
			ldapTemplate.unbind(newContactDN, true);

			return 1;
			
		}
		catch (Exception e)
		{
			return 0;
		}
	}

	/** 修改 */
	public int updateUserInfo(final Object parameterObject, String updateType)
	{
		// 获取用户传来的属性集
		Attributes attri = builder.getUserAttrs(parameterObject,
				OBJECT_NAME);

		DistinguishedName newContactDN = getDN(parameterObject, PRIMARY_KEY,
				BASE);
		try
		{
			ldapTemplate.rebind(newContactDN, null, attri);
			// 参数说明("userUid=liling,ou=user" , null , 要添加的字段值)
			return 1;
		}
		catch (Exception e)
		{
			return 0;
		}
	}

	/** 查询 */
	public List getUserInfo(final Object parameterObject, int curPage,
			int pageSize)
	{
		// 用于存储条目列表
		List list = new ArrayList();
		long starTime = System.currentTimeMillis();

		// 设置filter
		String filter = "objectclass=" + OBJECT_NAME;

		// 声明搜索控件
		SearchControls searchControls = new SearchControls();

		// 指定检索范围
		searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		/*
		 * 0:OBJECT_SCOPE,搜索指定的命名对象。 
		 * 1:ONELEVEL_SCOPE,只搜索指定命名对象的一个级别,这是缺省值。
		 * 2:SUBTREE_SCOPE,搜索以指定命名对象为根结点的整棵树
		 */