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
021package org.apache.directory.server.ldap.replication;
022
023
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.entry.Entry;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.message.controls.ChangeType;
028import org.apache.directory.server.core.api.entry.ClonedServerEntry;
029
030
031/**
032 * A place holder storing an Entry and the operation applied on it
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class ReplicaEventMessage
037{
038    /** The message change type */
039    private ChangeType changeType;
040    
041    /** The entry */
042    private Entry entry;
043
044    /**
045     * Create a new ReplicaEvent instance for a Add/Delete+Modify operation
046     * @param changeType The change type
047     * @param entry The entry
048     */
049    public ReplicaEventMessage( ChangeType changeType, Entry entry )
050    {
051        this.changeType = changeType;
052        
053        if ( entry instanceof ClonedServerEntry )
054        {
055            this.entry = ( ( ClonedServerEntry ) entry ).getClonedEntry();
056        }
057        else
058        {
059            this.entry = entry;
060        }
061    }
062
063
064    /**
065     * @return The changeType
066     */
067    public ChangeType getChangeType()
068    {
069        return changeType;
070    }
071
072
073    /**
074     * @return The stored Entry
075     */
076    public Entry getEntry()
077    {
078        return entry;
079    }
080
081
082    /**
083     * checks if the event's CSN is older than the given CSN
084     *
085     * @param csn the CSN
086     * @return true if the event's CSN is older than the given CSN
087     * @throws LdapException if there are any extreme conditions like a null entry or missing entryCSN attribute.
088     */
089    public boolean isEventOlderThan( String csn ) throws LdapException
090    {
091        if ( csn == null )
092        {
093            return false;
094        }
095        
096        String entryCsn = entry.get( SchemaConstants.ENTRY_CSN_AT ).getString();
097        
098        int i = entryCsn.compareTo( csn );
099        
100        return ( i <= 0 );
101    }
102}