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 */
020package org.apache.directory.server.core.api.interceptor.context;
021
022
023import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
024import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
025import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
026import org.apache.directory.api.ldap.model.name.Dn;
027import org.apache.directory.api.ldap.model.name.Rdn;
028import org.apache.directory.server.core.api.CoreSession;
029import org.apache.directory.server.core.api.OperationEnum;
030import org.apache.directory.server.i18n.I18n;
031
032
033/**
034 * A RenameService context used for Interceptors. It contains all the informations
035 * needed for the modify Dn operation, and used by all the interceptors
036 * 
037 * This is used when the modifyDN is about changing the Rdn, not the base Dn.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class RenameOperationContext extends AbstractChangeOperationContext
042{
043    /** The new Rdn */
044    protected Rdn newRdn;
045
046    /** Cached copy of the new Dn */
047    protected Dn newDn;
048
049    /** The flag to remove the old Rdn Attribute  */
050    private boolean deleteOldRdn;
051
052
053    /**
054     * Creates a new instance of RenameOperationContext.
055     * 
056     * @param session The session to use
057     */
058    public RenameOperationContext( CoreSession session )
059    {
060        super( session );
061
062        if ( session != null )
063        {
064            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.RENAME ) );
065        }
066    }
067
068
069    /**
070     * Creates a new instance of RenameOperationContext.
071     *
072     * @param session The session to use
073     * @param oldDn the dn of the entry before the rename
074     * @param newRdn the new Rdn to use for the target
075     * @param deleteOldRdn true if we delete the old Rdn value
076     */
077    public RenameOperationContext( CoreSession session, Dn oldDn, Rdn newRdn, boolean deleteOldRdn )
078    {
079        super( session, oldDn );
080        this.newRdn = newRdn;
081        this.deleteOldRdn = deleteOldRdn;
082
083        if ( session != null )
084        {
085            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.RENAME ) );
086        }
087    }
088
089    /**
090     * Creates a new instance of RenameOperationContext
091     *  
092     * @param session The session to use
093     * @param modifyDnRequest The ModDn operation to apply
094     */
095    public RenameOperationContext( CoreSession session, ModifyDnRequest modifyDnRequest )
096    {
097        super( session, modifyDnRequest.getName() );
098        this.newRdn = modifyDnRequest.getNewRdn();
099
100        if ( session != null )
101        {
102            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.RENAME ) );
103        }
104
105        if ( newRdn == null )
106        {
107            throw new IllegalStateException( I18n.err( I18n.ERR_328, modifyDnRequest ) );
108        }
109
110        this.deleteOldRdn = modifyDnRequest.getDeleteOldRdn();
111        this.requestControls = modifyDnRequest.getControls();
112
113        if ( requestControls.containsKey( ManageDsaIT.OID ) )
114        {
115            ignoreReferral();
116        }
117        else
118        {
119            throwReferral();
120        }
121    }
122
123
124    /**
125     * @return The delete old Rdn flag
126     */
127    public boolean getDeleteOldRdn()
128    {
129        return deleteOldRdn;
130    }
131
132
133    /**
134     * Set the flag to delete the old Rdn
135     * @param deleteOldRdn the flag to set
136     */
137    public void setDelOldDn( boolean deleteOldRdn )
138    {
139        this.deleteOldRdn = deleteOldRdn;
140    }
141
142
143    /**
144     * @return The new Dn either computed if null or already computed
145     */
146    public Dn getNewDn()
147    {
148        return newDn;
149    }
150
151
152    /**
153     * @return The new Rdn
154     */
155    public Rdn getNewRdn()
156    {
157        return newRdn;
158    }
159
160
161    /**
162     * Set the new Rdn
163     * @param newRdn The new Rdn
164     */
165    public void setNewRdn( Rdn newRdn )
166    {
167        this.newRdn = newRdn;
168    }
169
170
171    /**
172     * Set the new Dn
173     * @param newDn The new Dn
174     */
175    public void setNewDn( Dn newDn )
176    {
177        this.newDn = newDn;
178    }
179
180
181    /**
182     * @return the operation name
183     */
184    public String getName()
185    {
186        return MessageTypeEnum.MODIFYDN_REQUEST.name();
187    }
188
189
190    /**
191     * @see Object#toString()
192     */
193    public String toString()
194    {
195        return "RenameContext for old Dn '" + getDn().getName() + "'" + ", new Rdn '" + newRdn + "'"
196            + ( deleteOldRdn ? ", delete old Rdn" : "" );
197    }
198}