4 Replies Latest reply: Mar 7, 2013 5:05 AM by 823810 RSS

    Appropriate way to transfer data

    823810
      I am using below lines to transfer data from LDAP server with JNDI.

      But it is really ugly and i do this for every where i should transfer data.

      I know there is Spring ODM and UnboundID solutions but i do not want to have such contexts.

      Is there an appropriate way in JNDI?

      NamingEnumeration answer = ctx.search(searchBaseDn, filter, ctls);
           try
                     {     
                     while (answer.hasMore())
                     {
                               SearchResult sr = (SearchResult) answer.next();
                               UserPojo userPojo = new UserPojo();
                               userPojo.setDn((String)sr.getNameInNamespace());
                               userPojo.setUid((String)sr.getAttributes().get("uid").get());
                               
                               userPojo.setCn((sr.getAttributes().get("cn")!=null)?(String)sr.getAttributes().get("cn").get():"");
                               userPojo.setSn((sr.getAttributes().get("sn")!=null)?(String)sr.getAttributes().get("sn").get():"");
                               userPojo.setUserAccountOption((sr.getAttributes().get("userAccountOption")!=null)?(String)sr.getAttributes().get("userAccountOption").get():"");
                               userPojo.setUserAddress((sr.getAttributes().get("userAddress")!=null)?(String)sr.getAttributes().get("userAddress").get():"");
                               userPojo.setUserCertificateType((sr.getAttributes().get("userCertificateType")!=null)?(String)sr.getAttributes().get("userCertificateType").get():"");
        • 1. Re: Appropriate way to transfer data
          EJP
          That's it, I'm afraid, except that you can obviously write a method to take care of the parameter in each case.
          • 2. Re: Appropriate way to transfer data
            gimbal2
            Indeed. Don't like the low-level API? Wrap something slightly higher level around it and use that.
            • 3. Re: Appropriate way to transfer data
              823810
              Thank very much for answers.

              I am happy i could tell what i have problem with.

              I want more configurable and standardized code.

              For example if i want to add some new field to my objects, it should be very small work to change the code.

              If i do like above i must edit all the code where i transfer data.


              I thought to use something like below (inspired from Spring ODM)

              ObjectMetaData metaData=getEntityData(managedClass).metaData;

              try {

              for (Field field : metaData) {
              AttributeMetaData attributeInfo = metaData.getAttribute(field);

              Attribute attribute = attributeValueMap.get(attributeInfo.getName());
              if (attribute != null) {
              Object value = attribute.get();
              if (value != null) {
              field.set(result, value);
              }
              }
              }
              }
              ...


              I want some automized code like this, but if i add a field to Java side, the code will fail.

              I thought to put an xml into my Java project, and write some code to update LDAP automatically but can not decide if it is an elegant way.
              • 4. Re: Appropriate way to transfer data
                823810
                Is using the code like in my reply appropriate?

                I mean using fields name as equal to attribute types in LDAP object.

                Might it be dangerous in further steps of project development?