Interpreting the code cases in Spring LDAP help (2)

Source: Internet
Author: User
Tags ldap


The next chapter focuses on the Operation ideas and methods. Therefore, the previous chapter is very important. If the previous chapter has not been tested and understood, you 'd better take a look at the previous chapter or the official help first.

Next, let's take a look at Chapter 3. in chapter 3, the Work Department is a simple bridge. It is difficult to understand it. Let's keep up with the ideas in the previous chapter and look at the following code. The complete class summarized by spring

Package com. example. dao; import java. util. list; import javax. naming. name; import javax. naming. namingException; import javax. naming. directory. attributes; import org. springframework. ldap. core. attributesMapper; import org. springframework. ldap. core. contextMapper; import org. springframework. ldap. core. ldapTemplate; import org. springframework. ldap. core. dirContextAdapter; import org. springframework. ldap. core. support. distinguishedName; import org. springframework. ldap. filter. andFilter; import org. springframework. ldap. filter. equalsFilter; import org. springframework. ldap. filter. whitespaceWildcardsFilter;/** user DAO implementation Layer */public class PersonDaoImpl implements PersonDao {/** injection */private LdapTemplate ldapTemplate; public void setLdapTemplate (LdapTemplate ldapTemplate) {this. ldapTemplate = ldapTemplate;}/** this method encapsulates its overload method */protected Name buildDn (Person person) {return buildDn (person. getFullname (), person. getCompany (), person. getCountry ();}/** put the method into its cn value, ou value and c value, and return the established dn */protected Name buildDn (String fullname, string company, String country) {DistinguishedName dn = new DistinguishedName (); dn. add ("c", country); dn. add ("ou", company); dn. add ("cn", fullname); return dn;}/** similar to the attribute set encapsulated during creation in the previous chapter, set the attributes to be created and use an encapsulation class directory constructor structure to receive and put them back, in this way, you can create and update the directory constructor by adding attributes such as ou o, it will be saved to its basic attributes to facilitate searching for */protected void mapToContext (Person person, DirContextOperations context) {context when returning objects in the future. setAttributeValues ("objectclass", new String [] {"top", "person"}); context. setAttributeValue ("cn", person. getFullName (); context. setAttributeValue ("sn", person. getLastName (); context. setAttributeValue ("description", person. getDescription ();}/** the proxy returns the following class, ing user information, similar to the third parameter in the query written in the previous chapter, the obtained property set */protected ContextMapper getContextMapper () {return new PersonContextMapper ();}/** is the same as the object property class obtained during the previous query, however, here the value is obtained by the Directory constructor, and the generic type can be added, chapter 6 of the help document is written to */private static class PersonContextMapper extends AbstractContextMapper {public Object doMapFromContext (DirContextOperations context) {Person person Person = new person (); Person. setFullName (context. getStringAttribute ("cn"); person. setLastName (context. getStringAttribute ("sn"); person. setDescription (context. getStringAttribute ("description"); return person ;}/ ** the above is required for some tool classes. because they have all been implemented, I like to classify them and write them as well. It is okay to wait for the called method as an encapsulated method, the next step is to simplify the first method * // ** create user */public void create (Person person) {/** to instantiate a directory constructor and put buildDn () add */DirContextAdapter context = new DirContextAdapter (buildDn (person) to the dn returned by the method. // construct the dn and add mapToContext (person, context) to the directory constructor ); // put user attributes into the directory Constructor (the directory constructor method written above .) ldapTemplate. bind (context); // Add User information (such as dn and attribute set) in the directory constructor context .} /** update user information */public void update (Person person) {Name dn = buildDn (person); DirContextOperations context = ldapTemplate. lookupContext (dn); mapToContext (person, context); ldapTemplate. modifyAttributes (context); // update the user information (for example, dn and attribute set to be modified) in the directory constructor context .} /** delete user information. There is nothing to say about this. Get the dn kill. */public void delete (Person person) {ldapTemplate. unbind (buildDn (person);}/** find the user information based on the specified dn */public Person findByPrimaryKey (String name, String company, String country) {Name dn = buildDn (name, company, country); // obtain the dn return (Person) ldapTemplate. lookup (dn, getContextMapper (); // there are only two parameters here, because it is currently the lookup method and is understood as removing null of the useless object type in the middle .} /** perform fuzzy search based on the user's cn value. The search + three parameter methods will not be mentioned. */public List findByName (String name) {AndFilter filter = new AndFilter (); filter. and (new jsonsfilter ("objectclass", "person ")). and (new WhitespaceWildcardsFilter ("cn", name); return ldapTemplate. search (DistinguishedName. EMPTY_PATH, filter. encode (), getContextMapper ();}/** find all user information */public List findAll () {jsonsfilter filter = new jsonsfilter ("objectclass ", "person"); return ldapTemplate. search (DistinguishedName. EMPTY_PATH, filter. encode (), getContextMapper ());}}



The above operations are enough to encapsulate a simple DAO. Why is it simple? Because there are no pages, it is a headache.

To be continued, see [Chapter 5 of LDAP paging operation help].

This article from the "Write down on the right" blog, please be sure to keep this source http://jueshizhanhun.blog.51cto.com/4372226/1232397

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.