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.changelog;
021
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.directory.api.ldap.model.entry.Attribute;
027import org.apache.directory.api.ldap.model.ldif.LdifEntry;
028import org.apache.directory.server.core.api.LdapPrincipal;
029
030
031/**
032 * A loggable directory change event.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class ChangeLogEvent
037{
038    /** */
039    private String zuluTime;
040
041    /** The committer */
042    private LdapPrincipal committer;
043
044    /** The revision number for this event */
045    private long revision;
046
047    /** The modification */
048    private LdifEntry forwardLdif;
049
050    /** The revert changes. Can contain more than one single change */
051    private List<LdifEntry> reverseLdifs;
052
053
054    /**
055     * Creates a new instance of ChangeLogEvent.
056     *
057     * @param revision the revision number for the change
058     * @param zuluTime the timestamp for when the change occurred in generalizedTime format
059     * @param committer The Committer
060     * @param forwardLdif The Forward LDIF entry
061     * @param reverseLdif The Reverse LDIF entry
062     */
063    public ChangeLogEvent( long revision, String zuluTime, LdapPrincipal committer, LdifEntry forwardLdif,
064        LdifEntry reverseLdif )
065    {
066        this.zuluTime = zuluTime;
067        this.revision = revision;
068        this.forwardLdif = forwardLdif;
069        this.reverseLdifs = new ArrayList<>( 1 );
070        reverseLdifs.add( reverseLdif );
071        this.committer = committer;
072    }
073
074
075    /**
076     * Creates a new instance of ChangeLogEvent.
077     *
078     * @param revision the revision number for the change
079     * @param zuluTime the timestamp for when the change occurred in generalizedTime format
080     * @param committer the user who did the modification
081     * @param forwardLdif the original operation
082     * @param reverseLdifs the reverted operations
083     */
084    public ChangeLogEvent( long revision, String zuluTime, LdapPrincipal committer, LdifEntry forwardLdif,
085        List<LdifEntry> reverseLdifs )
086    {
087        this.zuluTime = zuluTime;
088        this.revision = revision;
089        this.forwardLdif = forwardLdif;
090        this.reverseLdifs = reverseLdifs;
091        this.committer = committer;
092    }
093
094
095    /**
096     * @return the forwardLdif
097     */
098    public LdifEntry getForwardLdif()
099    {
100        return forwardLdif;
101    }
102
103
104    /**
105     * @return the reverseLdif
106     */
107    public List<LdifEntry> getReverseLdifs()
108    {
109        return reverseLdifs;
110    }
111
112
113    /**
114     * @return the committer
115     */
116    public LdapPrincipal getCommitterPrincipal()
117    {
118        return committer;
119    }
120
121
122    /**
123     * Gets the revision of this event.
124     *
125     * @return the revision
126     */
127    public long getRevision()
128    {
129        return revision;
130    }
131
132
133    /**
134     * Gets the generalizedTime when this event occurred.
135     *
136     * @return the zuluTime when this event occurred
137     */
138    public String getZuluTime()
139    {
140        return zuluTime;
141    }
142
143
144    public Attribute get( String attributeName )
145    {
146        return forwardLdif.get( attributeName );
147    }
148
149
150    @Override
151    public String toString()
152    {
153        StringBuilder sb = new StringBuilder();
154        sb.append( "ChangeLogEvent { " );
155
156        sb.append( "principal=" )
157            .append( getCommitterPrincipal() )
158            .append( ", " );
159
160        sb.append( "zuluTime=" )
161            .append( getZuluTime() )
162            .append( ", " );
163
164        sb.append( "revision=" )
165            .append( getRevision() )
166            .append( ", " );
167
168        sb.append( "\nforwardLdif=" )
169            .append( getForwardLdif() )
170            .append( ", " );
171
172        if ( reverseLdifs != null )
173        {
174            sb.append( "\nreverseLdif number=" ).append( reverseLdifs.size() );
175            int i = 0;
176
177            for ( LdifEntry reverseLdif : reverseLdifs )
178            {
179                sb.append( "\nReverse[" ).append( i++ ).append( "] :\n" );
180                sb.append( reverseLdif );
181            }
182        }
183
184        sb.append( " }" );
185
186        return sb.toString();
187    }
188}