[table]
|连接LDAP数据库CRUD,包括分页查询的持久层代码|
[/table]
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,搜索以指定命名对象为根结点的整棵树
*/
// 声明属性名集合,并由Map类型转换为String类型数组
Map attriMap = father.getAttriName();
String returnedAtts[] = new String[attriMap.size()];
for (int i = 0, size = attriMap.size(); i < size; i++)
{
returnedAtts[i] = (String) (attriMap.get(i));
}
// 设置要返回的属性,即所有
searchControls.setReturningAttributes(returnedAtts);
// 设置handler,handler里存储了条目的list
CollectingNameClassPairCallbackHandler handler
= ldapTemplate.new AttributesMapperCallbackHandler(mapper);
// 设置页面大小
PagedResultsRequestControl requestControl
= new PagedResultsRequestControl(pageSize);
// 分为两种情况,第一种只取第一页,第二种取其他页数
if (curPage == 1)
{
try
{
// 只获取第一页
ldapTemplate.search(BASE, filter, searchControls, handler,
requestControl);
list = handler.getList();
}
catch (Exception e)
{
System.out.println("分页查询异常1" + e);
}
}
else
{
try
{
// 按分页搜索,并将结果存进handler,这里获取第一页
ldapTemplate.search(BASE, filter, searchControls, handler,
requestControl);
// 循环获取其余的页数,直至目标页
for (int i = 0, cur_1 = curPage - 1, cur_2 = curPage - 2; i < cur_1; i++)
{
// 声明临时变量handlerTemp。用于每次存储该次搜索的页面信息
CollectingNameClassPairCallbackHandler handlerTemp
= ldapTemplate.new AttributesMapperCallbackHandler(mapper);
// 设置页面大小
requestControl = new PagedResultsRequestControl(pageSize,
requestControl.getCookie());
// 在上次查询的基础上继续往后查询
ldapTemplate.search(BASE, filter, searchControls,
handlerTemp, requestControl);
// 检测为当前页,获取当前页的条目列表
if (i == cur_2)
list = handlerTemp.getList();
}
long endTime = System.currentTimeMillis();
System.out.println("查询所用时间:" + (endTime - starTime));
}
catch (Exception e)
{
System.out.println("分页查询异常2" + e);
}
}
return list;
}
/** 查询表的行数 */
public int getUserTotal(final Object parameterObject) {
long starTime = System.currentTimeMillis();
// 条目列表
List list = null;
// 设置filter
String filter = "objectclass=" + OBJECT_NAME;
// 声明搜索控件
SearchControls searchControls = new SearchControls();
// 指定检索范围
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 0:OBJECT_SCOPE,搜索指定命名的对象。1:ONELEVEL_SCOPE,只搜索指定命名对象的一个级别,这是缺省值。2:SUBTREE_SCOPE,搜索指定命名对象的子树,包括指定的命名对象
// 声明属性名集合,并转换为String数组
// 不返回属性对
searchControls.setReturningAttributes(null);
// 设置handler,handler里存储了条目的list
CollectingNameClassPairCallbackHandler handler
= ldapTemplate.new AttributesMapperCallbackHandler(mapper);
// 设置页面大小
PagedResultsRequestControl requestControl
= new PagedResultsRequestControl(MAX_PAGE);
try
{
// 只获取第一页
ldapTemplate.search(BASE, filter, searchControls, handler,
requestControl);
list = handler.getList();
long endTime = System.currentTimeMillis();
System.out.println("获得记录数所用时间:" + (endTime - starTime));
}
catch (Exception e)
{
System.out.println("分页查询异常1" + e);
}
return list.size();
}
/** 查询指定记录 */
public Object getUserInfoById(final Object parameterObject) {
// 条目列表
List list = null;
try
{
UserForLDAP userForLDAP = (UserForLDAP) parameterObject;
// 过滤器
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", OBJECT_NAME));
// 添加userUid的过滤器设置
if (userForLDAP.getUserUid() != "")
andFilter.and(new EqualsFilter(PRIMARY_KEY, userForLDAP.getUserUid()));
else
return null;
// 进行检索
list = ldapTemplate.search(BASE, andFilter.encode(),mapper);
}
catch (Exception e)
{
System.out.println("查询指定记录异常" + e);
}
return list;
}
}