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 java.util.List;
024import java.util.Map;
025
026import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
027import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
028import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
029import org.apache.directory.api.ldap.model.name.Dn;
030import org.apache.directory.api.ldap.model.name.Rdn;
031import org.apache.directory.server.core.api.CoreSession;
032import org.apache.directory.server.core.api.OperationEnum;
033import org.apache.directory.server.i18n.I18n;
034
035
036/**
037 * A Move And Rename context used for Interceptors. It contains all the informations
038 * needed for the modify Dn operation, and used by all the interceptors
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class MoveAndRenameOperationContext extends RenameOperationContext
043{
044    /** The new superior Dn */
045    private Dn newSuperiorDn;
046
047    /** The map of modified AVAs */
048    private Map<String, List<ModDnAva>> modifiedAvas;
049
050    /**
051     * Creates a new instance of MoveAndRenameOperationContext.
052     * 
053     * @param session The session to use
054     */
055    public MoveAndRenameOperationContext( CoreSession session )
056    {
057        super( session );
058
059        if ( session != null )
060        {
061            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE_AND_RENAME ) );
062        }
063    }
064
065
066    /**
067     * Creates a new instance of MoveAndRenameOperationContext.
068     *
069     * @param session The session to use
070     * @param oldDn the original source entry Dn to be moved and renamed
071     * @param newSuperiorDn the new entry superior of the target after the move
072     * @param newRdn the new rdn to use for the target once renamed
073     * @param delOldRdn true if the old rdn value is deleted, false otherwise
074     */
075    public MoveAndRenameOperationContext( CoreSession session, Dn oldDn, Dn newSuperiorDn, Rdn newRdn, boolean delOldRdn )
076    {
077        super( session, oldDn, newRdn, delOldRdn );
078        this.newSuperiorDn = newSuperiorDn;
079
080        if ( session != null )
081        {
082            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE_AND_RENAME ) );
083        }
084
085        try
086        {
087            newDn = newSuperiorDn.add( newRdn );
088        }
089        catch ( LdapInvalidDnException lide )
090        {
091            throw new IllegalArgumentException( lide.getMessage(), lide );
092        }
093    }
094
095
096    public MoveAndRenameOperationContext( CoreSession session, ModifyDnRequest modifyDnRequest )
097    {
098        // super sets the newRdn and the delOldRdn members and tests
099        super( session, modifyDnRequest );
100        newSuperiorDn = modifyDnRequest.getNewSuperior();
101        
102        if ( !newSuperiorDn.isSchemaAware() )
103        {
104            try
105            {
106                newSuperiorDn = new Dn( session.getDirectoryService().getSchemaManager(), newSuperiorDn );
107            }
108            catch ( LdapInvalidDnException lide )
109            {
110                throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ), lide );
111            }
112        }
113
114        if ( session != null )
115        {
116            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE_AND_RENAME ) );
117        }
118
119        if ( newSuperiorDn == null )
120        {
121            throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ) );
122        }
123
124        if ( requestControls.containsKey( ManageDsaIT.OID ) )
125        {
126            ignoreReferral();
127        }
128        else
129        {
130            throwReferral();
131        }
132
133        try
134        {
135            newDn = newSuperiorDn.add( newRdn );
136        }
137        catch ( LdapInvalidDnException lide )
138        {
139            throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ), lide );
140        }
141    }
142
143
144    /**
145     *  @return The new superior Dn
146     */
147    public Dn getNewSuperiorDn()
148    {
149        return newSuperiorDn;
150    }
151
152
153    /**
154     * Set the new Superior Dn
155     *
156     * @param newSuperiorDn The new Superior Dn
157     */
158    public void setNewSuperiorDn( Dn newSuperiorDn )
159    {
160        this.newSuperiorDn = newSuperiorDn;
161    }
162
163
164    /**
165     * @see Object#toString()
166     */
167    @Override
168    public String toString()
169    {
170        return "ReplaceContext for old Dn '" + getDn().getName() + "' : " + newDn;
171    }
172
173
174    /**
175     * @return the modifiedAvas
176     */
177    public Map<String, List<ModDnAva>> getModifiedAvas()
178    {
179        return modifiedAvas;
180    }
181
182
183    /**
184     * @param modifiedAvas the modifiedAvas to set
185     */
186    public void setModifiedAvas( Map<String, List<ModDnAva>> modifiedAvas )
187    {
188        this.modifiedAvas = modifiedAvas;
189    }
190}