View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.core.api.changelog;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.ldap.model.entry.Attribute;
27  import org.apache.directory.api.ldap.model.ldif.LdifEntry;
28  import org.apache.directory.server.core.api.LdapPrincipal;
29  
30  
31  /**
32   * A loggable directory change event.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class ChangeLogEvent
37  {
38      /** */
39      private String zuluTime;
40  
41      /** The committer */
42      private LdapPrincipal committer;
43  
44      /** The revision number for this event */
45      private long revision;
46  
47      /** The modification */
48      private LdifEntry forwardLdif;
49  
50      /** The revert changes. Can contain more than one single change */
51      private List<LdifEntry> reverseLdifs;
52  
53  
54      /**
55       * Creates a new instance of ChangeLogEvent.
56       *
57       * @param revision the revision number for the change
58       * @param zuluTime the timestamp for when the change occurred in generalizedTime format
59       * @param committer The Committer
60       * @param forwardLdif The Forward LDIF entry
61       * @param reverseLdif The Reverse LDIF entry
62       */
63      public ChangeLogEvent( long revision, String zuluTime, LdapPrincipal committer, LdifEntry forwardLdif,
64          LdifEntry reverseLdif )
65      {
66          this.zuluTime = zuluTime;
67          this.revision = revision;
68          this.forwardLdif = forwardLdif;
69          this.reverseLdifs = new ArrayList<>( 1 );
70          reverseLdifs.add( reverseLdif );
71          this.committer = committer;
72      }
73  
74  
75      /**
76       * Creates a new instance of ChangeLogEvent.
77       *
78       * @param revision the revision number for the change
79       * @param zuluTime the timestamp for when the change occurred in generalizedTime format
80       * @param committer the user who did the modification
81       * @param forwardLdif the original operation
82       * @param reverseLdifs the reverted operations
83       */
84      public ChangeLogEvent( long revision, String zuluTime, LdapPrincipal committer, LdifEntry forwardLdif,
85          List<LdifEntry> reverseLdifs )
86      {
87          this.zuluTime = zuluTime;
88          this.revision = revision;
89          this.forwardLdif = forwardLdif;
90          this.reverseLdifs = reverseLdifs;
91          this.committer = committer;
92      }
93  
94  
95      /**
96       * @return the forwardLdif
97       */
98      public LdifEntry getForwardLdif()
99      {
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 }