1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.entry;
21
22
23 import java.io.IOException;
24 import java.io.ObjectInput;
25 import java.io.ObjectOutput;
26
27 import org.apache.directory.api.i18n.I18n;
28 import org.apache.directory.api.ldap.model.exception.LdapException;
29 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
30 import org.apache.directory.api.ldap.model.schema.AttributeType;
31 import org.apache.directory.api.ldap.model.schema.SchemaManager;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41
42 public class DefaultModification implements Modification
43 {
44
45 private ModificationOperation operation;
46
47
48 private Attribute attribute;
49
50
51 private AttributeType attributeType;
52
53
54 protected static final Logger LOG = LoggerFactory.getLogger( Modification.class );
55
56
57
58
59
60 public DefaultModification()
61 {
62
63 }
64
65
66
67
68
69
70
71
72 public DefaultModification( ModificationOperation operation, Attribute attribute )
73 {
74 this.operation = operation;
75 this.attribute = attribute;
76 }
77
78
79
80
81
82
83
84
85
86 public DefaultModification( ModificationOperation operation, String attributeId, String... values )
87 {
88 this.operation = operation;
89 this.attribute = new DefaultAttribute( attributeId, values );
90 }
91
92
93
94
95
96
97
98
99
100 public DefaultModification( ModificationOperation operation, String attributeId, byte[]... values )
101 {
102 this.operation = operation;
103 this.attribute = new DefaultAttribute( attributeId, values );
104 }
105
106
107
108
109
110
111
112
113
114 public DefaultModification( ModificationOperation operation, String attributeId, Value... values )
115 {
116 this.operation = operation;
117 this.attribute = new DefaultAttribute( attributeId, values );
118 }
119
120
121
122
123
124
125
126
127 public DefaultModification( ModificationOperation operation, String attributeId )
128 {
129 this.operation = operation;
130 this.attribute = new DefaultAttribute( attributeId );
131 }
132
133
134
135
136
137
138
139
140
141
142 public DefaultModification( ModificationOperation operation, AttributeType attributeType, String... values )
143 throws LdapInvalidAttributeValueException
144 {
145 this.operation = operation;
146 this.attribute = new DefaultAttribute( attributeType, values );
147 }
148
149
150
151
152
153
154
155
156
157
158 public DefaultModification( ModificationOperation operation, AttributeType attributeType, byte[]... values )
159 throws LdapInvalidAttributeValueException
160 {
161 this.operation = operation;
162 this.attribute = new DefaultAttribute( attributeType, values );
163 }
164
165
166
167
168
169
170
171
172
173
174 public DefaultModification( ModificationOperation operation, AttributeType attributeType, Value... values )
175 throws LdapInvalidAttributeValueException
176 {
177 this.operation = operation;
178 this.attribute = new DefaultAttribute( attributeType, values );
179 }
180
181
182
183
184
185
186
187
188
189 public DefaultModification( ModificationOperation operation, AttributeType attributeType )
190 throws LdapInvalidAttributeValueException
191 {
192 this.operation = operation;
193 this.attribute = new DefaultAttribute( attributeType );
194 }
195
196
197
198
199
200
201
202
203 public DefaultModification( SchemaManager schemaManager, Modification modification )
204 {
205 operation = modification.getOperation();
206
207 Attribute modAttribute = modification.getAttribute();
208
209 try
210 {
211 AttributeType at = modAttribute.getAttributeType();
212
213 if ( at == null )
214 {
215 at = schemaManager.lookupAttributeTypeRegistry( modAttribute.getId() );
216 }
217
218 attribute = new DefaultAttribute( at, modAttribute );
219 }
220 catch ( LdapException ne )
221 {
222
223 LOG.error( I18n.err( I18n.ERR_13230_INCORRECT_ATTRIBUTE, modAttribute.getId() ) );
224 }
225 }
226
227
228
229
230
231 @Override
232 public ModificationOperation getOperation()
233 {
234 return operation;
235 }
236
237
238
239
240
241 @Override
242 public void setOperation( int operation )
243 {
244 this.operation = ModificationOperation.getOperation( operation );
245 }
246
247
248
249
250
251 @Override
252 public void setOperation( ModificationOperation operation )
253 {
254 this.operation = operation;
255 }
256
257
258
259
260
261 @Override
262 public Attribute getAttribute()
263 {
264 return attribute;
265 }
266
267
268
269
270
271 @Override
272 public void setAttribute( Attribute attribute )
273 {
274 this.attribute = attribute;
275 }
276
277
278
279
280
281 @Override
282 public void apply( AttributeType attributeType ) throws LdapInvalidAttributeValueException
283 {
284 this.attributeType = attributeType;
285
286 if ( attribute != null )
287 {
288 attribute.apply( attributeType );
289 }
290 }
291
292
293
294
295
296 public AttributeType getAttributeType()
297 {
298 return attributeType;
299 }
300
301
302
303
304
305
306 @Override
307 public boolean equals( Object that )
308 {
309
310 if ( this == that )
311 {
312 return true;
313 }
314
315 if ( !( that instanceof Modification ) )
316 {
317 return false;
318 }
319
320 Modification otherModification = ( Modification ) that;
321
322
323 if ( operation != otherModification.getOperation() )
324 {
325 return false;
326 }
327
328
329 if ( attribute == null )
330 {
331 return otherModification.getAttribute() == null;
332 }
333
334 return attribute.equals( otherModification.getAttribute() );
335 }
336
337
338
339
340
341
342 @Override
343 public int hashCode()
344 {
345 int h = 37;
346
347 h += h * 17 + operation.getValue();
348 h += h * 17 + attribute.hashCode();
349
350 return h;
351 }
352
353
354
355
356
357 @Override
358 public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
359 {
360
361 operation = ModificationOperation.getOperation( in.readInt() );
362
363
364 boolean hasAttribute = in.readBoolean();
365
366 if ( hasAttribute )
367 {
368 attribute = new DefaultAttribute();
369 attribute.readExternal( in );
370 }
371 }
372
373
374
375
376
377 @Override
378 public void writeExternal( ObjectOutput out ) throws IOException
379 {
380
381 out.writeInt( operation.getValue() );
382
383
384 if ( attribute != null )
385 {
386 out.writeBoolean( true );
387 attribute.writeExternal( out );
388 }
389 else
390 {
391 out.writeBoolean( false );
392 }
393
394 out.flush();
395 }
396
397
398
399
400
401 @Override
402 public DefaultModification clone()
403 {
404 try
405 {
406 DefaultModification clone = ( DefaultModification ) super.clone();
407
408 clone.attribute = this.attribute.clone();
409 return clone;
410 }
411 catch ( CloneNotSupportedException cnse )
412 {
413 return null;
414 }
415 }
416
417
418
419
420
421 @Override
422 public String toString()
423 {
424 StringBuilder sb = new StringBuilder();
425
426 sb.append( "Modification: " ).
427 append( operation ).
428 append( ", [" ).
429 append( attribute.getId() );
430
431 if ( attribute.size() == 0 )
432 {
433 if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
434 {
435 sb.append( " : 1" );
436 }
437 }
438 else
439 {
440 sb.append( " : " ).append( attribute );
441 }
442
443 sb.append( "]" );
444
445 return sb.toString();
446 }
447 }