|
|
Query the “Krusty Krab” sub-tree from LDAP into your Company JavaBean
Similarly, using the nested nested-operation-mapping, you can read a whole LDAP sub-tree from LDAP to your complex JavaBean by a single java call.
1. Define the XML mapping file.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ldapMap PUBLIC "-//Spring LDAP - XML Data Mapper//DTD Ldap Map 1.0//EN" "ldap-map.dtd">
<ldapMap>
<parameterMap id="parametermap_search_company_by_name"
className="com.ldapmapper.sample.dto.CompanySearch"/>
<resultMap id="resultmap_company"
className="com.ldapmapper.sample.dto.Company">
<attrmap propertyName="name" attributeName="o"/>
<attrmap propertyName="address"
attributeName="registeredAddress"/>
<attrmap propertyName="phone" attributeName="telephoneNumber"/>
<attrmap propertyName="postalCode" attributeName="postalCode"/>
<attrmap propertyName="description" attributeName="description"/>
<actionmap propertyName="ceo" parameterId="ceoSearchDTO"
actionType="search" id="find_ceo"/>
<actionmap propertyName="departments"
parameterId="departmentsSearchDTO" actionType="search"
id="list_departments"/>
</resultMap>
<search id="search_company_by_name"
parameterMapId="parametermap_search_company_by_name"
resultMapId="resultmap_company">
<base>${base}</base>
<scope>${scope}</scope>
<filter>(&(objectclass=organization)(o=${name}))</filter>
</search>
...
</ ldapMap>
From the sample Map XML file, you will see that the ‘actionMap’ elements are used in ‘resultMap’ to support nested mapped LDAP operations.
2. Write Java code to populate a ‘CompanySearch’ JavaBean object as the input parameter object for the nested search operation.
public static CompanySearch buildCompanySearchDTO(){
CompanySearch req = new CompanySearch("o=krustyKrab", "",
SearchControls.SUBTREE_SCOPE, null, "krustyKrab",
new CommonSearchDTO(null, "o=krustyKrab",
SearchControls.ONELEVEL_SCOPE, null),
new DepartmentSearch(null, "o=krustyKrab",
SearchControls.ONELEVEL_SCOPE, null, null,
new CommonSearchDTO(null, null,
SearchControls.ONELEVEL_SCOPE, null))
);
return req;
}
3. Write Java code to read the whole sub-tree from LDAP to the local JavaBean object
LdapMapClient ldapClient = LdapMapClientFactory.buildLdapMapClient(
"com/ldapmapper/sample/ldap.properties", false);
List companies = (List)ldapClient.search(
"search_company_by_name", buildCompanySearchDTO());
for (Iterator it = companies.iterator(); it.hasNext() {
Company company = (Company)it.next();
// manipulating company object here ...
}
You can also find the extensive sample code of other CRUD operations on your complex JavaBean. |
|