KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > simya > ldap > beans > LdapModifyEntryBean


1
2 package net.simya.ldap.beans;
3
4 import javax.naming.*;
5 import javax.naming.directory.*;
6 import java.util.*;
7
8 /**
9  * This code modifies the entry in the ldap
10  *
11  * next version will provide :xx binary attributes
12  *
13  */

14 public class LdapModifyEntryBean {
15     
16     private DirContext ctx = null;
17         
18     private ModificationItem[] item = null;
19     
20     private Attributes attrList = new BasicAttributes(true); // other attributes
21

22     private Hashtable attrTable = new Hashtable();
23
24     private Vector vector = new Vector();
25     
26     private String JavaDoc DN;
27             
28     
29     public void setContext (DirContext ctx)
30     {
31         this.ctx = ctx;
32     }
33     
34         
35     public void setDN ( String JavaDoc dn)
36     {
37         this.DN = dn ;
38     }
39         
40
41     public void setModifyAttribute ( String JavaDoc name, Object JavaDoc value, int operationType)
42     {
43         /* operation parameters
44         *
45         * DirContext.ADD_ATTRIBUTE
46         * DirContext.REPLACE_ATTRIBUTE
47         * DirContext.REMOVE_ATTRIBUTE
48         *
49         * adding multiple value :
50         * setModifyAttribute (name, value, operationtype)
51         * setModifyAttribute (name, value, 0)
52         */

53         // if op.type not 0 , that means user i creating new attribute
54
if (operationType != 0 )
55         {
56             Attribute attr = new BasicAttribute(name,value);
57             vector.add(new ModificationItem(operationType, attr));
58         }
59         else
60         // if operation type 0 , that means user already defined it and now
61
// he is try to add additional values
62
{
63             Enumeration en= vector.elements();
64             
65             for (int i=0; en.hasMoreElements(); i++)
66             {
67             
68                 ModificationItem moditem = (ModificationItem) en.nextElement();
69                 Attribute tmpattr = (Attribute) moditem.getAttribute();
70                 if ((tmpattr.getID()).equals(name));
71                     tmpattr.add(value);
72             
73             }
74         }
75         // some operations have no value (e.g. delete)
76

77
78     }
79     
80
81     public void modifyEntry ()
82     throws NamingException
83     {
84         Enumeration en= vector.elements();
85         
86         item = new ModificationItem[vector.size()];
87         
88         for (int i=0; en.hasMoreElements(); i++)
89         {
90             
91             item[i] = (ModificationItem) en.nextElement();
92         }
93         
94         ctx.modifyAttributes(DN, item);
95         
96     }
97     
98 }// end of class
99

100
Popular Tags