View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.interceptor.context;
21  
22  
23  import org.apache.directory.server.core.CoreSession;
24  import org.apache.directory.server.core.entry.ClonedServerEntry;
25  import org.apache.directory.shared.ldap.message.MessageTypeEnum;
26  import org.apache.directory.shared.ldap.message.ModifyDnRequest;
27  import org.apache.directory.shared.ldap.name.LdapDN;
28  import org.apache.directory.shared.ldap.name.Rdn;
29  
30  
31  /**
32   * A RenameService context used for Interceptors. It contains all the informations
33   * needed for the modify DN operation, and used by all the interceptors
34   * 
35   * This is used whne the modifyDN is about changing the RDN, not the base DN.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class RenameOperationContext extends AbstractChangeOperationContext
41  {
42      /** The new RDN */
43      private Rdn newRdn;
44  
45      /** Cached copy of the new DN */
46      private LdapDN newDn;
47  
48      /** The flag to remove the old DN Attribute  */
49      private boolean delOldDn;
50  
51      /** The entry after being renamed and altered for rdn attributes */ 
52      private ClonedServerEntry alteredEntry;
53      
54  
55      /**
56       * Creates a new instance of RenameOperationContext.
57       */
58      public RenameOperationContext( CoreSession session )
59      {
60      	super( session );
61      }
62  
63  
64      /**
65       * Creates a new instance of RenameOperationContext.
66       *
67       * @param oldDn the dn of the entry before the rename
68       * @param newRdn the new RDN to use for the target
69       * @param delOldDn true if we delete the old RDN value
70       */
71      public RenameOperationContext( CoreSession session, LdapDN oldDn, Rdn newRdn, boolean delOldDn )
72      {
73          super( session, oldDn );
74          this.newRdn = newRdn;
75          this.delOldDn = delOldDn;
76      }
77  
78  
79      public RenameOperationContext( CoreSession session, ModifyDnRequest modifyDnRequest )
80      {
81          super( session, modifyDnRequest.getName() );
82          this.newRdn = modifyDnRequest.getNewRdn();
83          
84          if ( newRdn == null )
85          {
86              throw new IllegalStateException( "newRdn must not be null for a rename: " + modifyDnRequest );
87          }
88          
89          this.delOldDn = modifyDnRequest.getDeleteOldRdn();
90          this.requestControls = modifyDnRequest.getControls();
91      }
92  
93  
94      /**
95       * @return The delete old DN flag
96       */
97      public boolean getDelOldDn() 
98      {
99          return delOldDn;
100     }
101 
102 
103     /**
104      * Set the flag to delete the old DN
105      * @param delOldDn the flag to set
106      */
107     public void setDelOldDn( boolean delOldDn ) 
108     {
109         this.delOldDn = delOldDn;
110     }
111 
112 
113     /**
114      * @return The new DN either computed if null or already computed
115      */
116     public LdapDN getNewDn() throws Exception
117     {
118         if ( newDn == null )
119         {
120             newDn = new LdapDN( getDn().getUpName() );
121             newDn.remove( newDn.size() - 1 );
122             newDn.add( newRdn.getUpName() );
123             newDn.normalize( session.getDirectoryService().getRegistries()
124                 .getAttributeTypeRegistry().getNormalizerMapping() );
125         }
126         
127         return newDn;
128     }
129 
130 
131     /**
132      * @return The new RDN
133      */
134     public Rdn getNewRdn()
135     {
136         return newRdn;
137     }
138 
139 
140     /**
141      * Set the new RDN
142      * @param newRdn The new RDN
143      */
144     public void setNewRdn( Rdn newRdn )
145     {
146         this.newRdn = newRdn;
147     }
148 
149 
150     /**
151      * @return the operation name
152      */
153     public String getName()
154     {
155         return MessageTypeEnum.MOD_DN_REQUEST.name();
156     }
157     
158     
159     /**
160      * Returns the entry after it has been renamed and potentially changed for 
161      * Rdn alterations.
162      *
163      * @return the new renamed entry
164      */
165     public ClonedServerEntry getAlteredEntry()
166     {
167         return alteredEntry;
168     }
169 
170     
171     public void setAlteredEntry( ClonedServerEntry alteredEntry ) 
172     {
173         this.alteredEntry = alteredEntry;
174     }
175     
176     
177     /**
178      * @see Object#toString()
179      */
180     public String toString()
181     {
182         return "RenameContext for old DN '" + getDn().getUpName() + "'" +
183         ", new RDN '" + newRdn + "'" +
184         ( delOldDn ? ", delete old Dn" : "" ) ; 
185     }
186 }