001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.interceptor.context;
021    
022    
023    import org.apache.directory.server.core.CoreSession;
024    import org.apache.directory.server.i18n.I18n;
025    import org.apache.directory.shared.ldap.codec.controls.ManageDsaITControl;
026    import org.apache.directory.shared.ldap.message.internal.InternalModifyDnRequest;
027    import org.apache.directory.shared.ldap.name.DN;
028    import org.apache.directory.shared.ldap.name.RDN;
029    
030    
031    /**
032     * A Move And Rename context used for Interceptors. It contains all the informations
033     * needed for the modify DN operation, and used by all the interceptors
034     *
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev$, $Date$
037     */
038    public class MoveAndRenameOperationContext extends RenameOperationContext
039    {
040        /** The parent DN */
041        private DN parent;
042    
043        /** Cached calculated new DN after move and rename */
044        private DN newDn;
045    
046        /**
047         * Creates a new instance of MoveAndRenameOperationContext.
048         */
049        public MoveAndRenameOperationContext( CoreSession session )
050        {
051            super( session );
052        }
053    
054    
055        /**
056         * Creates a new instance of MoveAndRenameOperationContext.
057         *
058         * @param oldDn the original source entry DN to be moved and renamed
059         * @param parent the new entry superior of the target after the move
060         * @param newRdn the new rdn to use for the target once renamed
061         * @param delOldRdn true if the old rdn value is deleted, false otherwise
062         */
063        public MoveAndRenameOperationContext( CoreSession session, DN oldDn, DN parent, RDN newRdn, boolean delOldRdn )
064        {
065            super( session, oldDn, newRdn, delOldRdn );
066            this.parent = parent;
067        }
068    
069    
070        public MoveAndRenameOperationContext( CoreSession session, InternalModifyDnRequest modifyDnRequest )
071        {
072            // super sets the newRdn and the delOldRdn members and tests
073            super( session, modifyDnRequest );
074            this.parent = modifyDnRequest.getNewSuperior();
075            
076            if ( parent == null )
077            {
078                throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ) );
079            }
080            
081            if ( requestControls.containsKey( ManageDsaITControl.CONTROL_OID ) )
082            {
083                ignoreReferral();
084            }
085            else
086            {
087                throwReferral();
088            }
089        }
090    
091    
092        /**
093         *  @return The parent DN
094         */
095        public DN getParent()
096        {
097            return parent;
098        }
099    
100    
101        /**
102         * Set the parent DN
103         *
104         * @param parent The parent
105         */
106        public void setParent( DN parent )
107        {
108            this.parent = parent;
109        }
110    
111        
112        /**
113         * Gets cached copy of already computed new name or creates it if not 
114         *
115         * @return the normalized new name after move and rename
116         * @throws Exception if the name cannot be normalized
117         */
118        public DN getNewDn() throws Exception
119        {
120            if ( newDn == null )
121            {
122                newDn = new DN( getParent().getName() );
123                newDn.add( getNewRdn().getName() );
124                newDn.normalize( session.getDirectoryService()
125                    .getSchemaManager().getNormalizerMapping() );
126            }
127            
128            return newDn;
129        }
130        
131    
132        /**
133         * @see Object#toString()
134         */
135        public String toString()
136        {
137            return "ReplaceContext for old DN '" + getDn().getName() + "'" +
138            ", parent '" + parent + "'";
139        }
140    }