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.message;
21
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.directory.api.ldap.model.entry.Attribute;
30 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
31 import org.apache.directory.api.ldap.model.entry.DefaultModification;
32 import org.apache.directory.api.ldap.model.entry.Modification;
33 import org.apache.directory.api.ldap.model.entry.ModificationOperation;
34 import org.apache.directory.api.ldap.model.name.Dn;
35 import org.apache.directory.api.util.StringConstants;
36
37
38
39
40
41
42
43 public class ModifyRequestImpl extends AbstractAbandonableRequest implements ModifyRequest
44 {
45 static final long serialVersionUID = -505803669028990304L;
46
47
48 private Dn name;
49
50
51 private List<Modification> mods = new ArrayList<Modification>();
52
53
54 private ModifyResponse response;
55
56
57
58
59
60
61
62
63
64 public ModifyRequestImpl()
65 {
66 super( -1, MessageTypeEnum.MODIFY_REQUEST );
67 }
68
69
70
71
72
73
74
75
76 public Collection<Modification> getModifications()
77 {
78 return Collections.unmodifiableCollection( mods );
79 }
80
81
82
83
84
85 public Dn getName()
86 {
87 return name;
88 }
89
90
91
92
93
94 public ModifyRequest setName( Dn name )
95 {
96 this.name = name;
97
98 return this;
99 }
100
101
102
103
104
105 public ModifyRequest addModification( Modification mod )
106 {
107 mods.add( mod );
108
109 return this;
110 }
111
112
113 private void addModification( ModificationOperation modOp, String attributeName, byte[]... attributeValue )
114 {
115 Attribute attr = new DefaultAttribute( attributeName, attributeValue );
116 addModification( attr, modOp );
117 }
118
119
120 private void addModification( ModificationOperation modOp, String attributeName, String... attributeValue )
121 {
122 Attribute attr = new DefaultAttribute( attributeName, attributeValue );
123 addModification( attr, modOp );
124 }
125
126
127
128
129
130 public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
131 {
132 mods.add( new DefaultModification( modOp, attr ) );
133
134 return this;
135 }
136
137
138
139
140
141 public ModifyRequest add( String attributeName, String... attributeValue )
142 {
143 addModification( ModificationOperation.ADD_ATTRIBUTE, attributeName, attributeValue );
144
145 return this;
146 }
147
148
149
150
151
152 public ModifyRequest add( String attributeName, byte[]... attributeValue )
153 {
154 addModification( ModificationOperation.ADD_ATTRIBUTE, attributeName, attributeValue );
155
156 return this;
157 }
158
159
160
161
162
163 public ModifyRequest add( Attribute attr )
164 {
165 addModification( attr, ModificationOperation.ADD_ATTRIBUTE );
166
167 return this;
168 }
169
170
171
172
173
174 public ModifyRequest replace( String attributeName )
175 {
176 addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, StringConstants.EMPTY_STRINGS );
177
178 return this;
179 }
180
181
182
183
184
185 public ModifyRequest replace( String attributeName, String... attributeValue )
186 {
187 addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, attributeValue );
188
189 return this;
190 }
191
192
193
194
195
196 public ModifyRequest replace( String attributeName, byte[]... attributeValue )
197 {
198 addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, attributeValue );
199
200 return this;
201 }
202
203
204
205
206
207 public ModifyRequest replace( Attribute attr )
208 {
209 addModification( attr, ModificationOperation.REPLACE_ATTRIBUTE );
210
211 return this;
212 }
213
214
215
216
217
218 public ModifyRequest removeModification( Modification mod )
219 {
220 mods.remove( mod );
221
222 return this;
223 }
224
225
226
227
228
229 public ModifyRequest remove( String attributeName, String... attributeValue )
230 {
231 addModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName, attributeValue );
232
233 return this;
234 }
235
236
237
238
239
240 public ModifyRequest remove( String attributeName, byte[]... attributeValue )
241 {
242 addModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName, attributeValue );
243
244 return this;
245 }
246
247
248
249
250
251 public ModifyRequest remove( Attribute attr )
252 {
253 addModification( attr, ModificationOperation.REMOVE_ATTRIBUTE );
254
255 return this;
256 }
257
258
259
260
261
262 public ModifyRequest setMessageId( int messageId )
263 {
264 super.setMessageId( messageId );
265
266 return this;
267 }
268
269
270
271
272
273 public ModifyRequest addControl( Control control )
274 {
275 return ( ModifyRequest ) super.addControl( control );
276 }
277
278
279
280
281
282 public ModifyRequest addAllControls( Control[] controls )
283 {
284 return ( ModifyRequest ) super.addAllControls( controls );
285 }
286
287
288
289
290
291 public ModifyRequest removeControl( Control control )
292 {
293 return ( ModifyRequest ) super.removeControl( control );
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307 public MessageTypeEnum getResponseType()
308 {
309 return MessageTypeEnum.MODIFY_RESPONSE;
310 }
311
312
313
314
315
316
317
318 public ModifyResponse getResultResponse()
319 {
320 if ( response == null )
321 {
322 response = new ModifyResponseImpl( getMessageId() );
323 }
324
325 return response;
326 }
327
328
329
330
331
332 @Override
333 public int hashCode()
334 {
335 int hash = 37;
336 if ( name != null )
337 {
338 hash = hash * 17 + name.hashCode();
339 }
340 hash = hash * 17 + mods.size();
341 for ( int i = 0; i < mods.size(); i++ )
342 {
343 hash = hash * 17 + ( ( DefaultModification ) mods.get( i ) ).hashCode();
344 }
345 hash = hash * 17 + super.hashCode();
346
347 return hash;
348 }
349
350
351
352
353
354
355
356
357
358
359 public boolean equals( Object obj )
360 {
361 if ( obj == this )
362 {
363 return true;
364 }
365
366 if ( !super.equals( obj ) )
367 {
368 return false;
369 }
370
371 ModifyRequest req = ( ModifyRequest ) obj;
372
373 if ( name != null && req.getName() == null )
374 {
375 return false;
376 }
377
378 if ( name == null && req.getName() != null )
379 {
380 return false;
381 }
382
383 if ( name != null && req.getName() != null && !name.equals( req.getName() ) )
384 {
385 return false;
386 }
387
388 if ( req.getModifications().size() != mods.size() )
389 {
390 return false;
391 }
392
393 Iterator<Modification> list = req.getModifications().iterator();
394
395 for ( int i = 0; i < mods.size(); i++ )
396 {
397 Modification item = list.next();
398
399 if ( item == null )
400 {
401 if ( mods.get( i ) != null )
402 {
403 return false;
404 }
405 }
406 else
407
408 if ( !item.equals( mods.get( i ) ) )
409 {
410 return false;
411 }
412 }
413
414 return true;
415 }
416
417
418
419
420
421
422
423 public String toString()
424 {
425
426 StringBuffer sb = new StringBuffer();
427
428 sb.append( " Modify Request\n" );
429 sb.append( " Object : '" ).append( name ).append( "'\n" );
430
431 if ( mods != null )
432 {
433
434 for ( int i = 0; i < mods.size(); i++ )
435 {
436
437 DefaultModification modification = ( DefaultModification ) mods.get( i );
438
439 sb.append( " Modification[" ).append( i ).append( "]\n" );
440 sb.append( " Operation : " );
441
442 switch ( modification.getOperation() )
443 {
444 case ADD_ATTRIBUTE:
445 sb.append( " add\n" );
446 break;
447
448 case REPLACE_ATTRIBUTE:
449 sb.append( " replace\n" );
450 break;
451
452 case REMOVE_ATTRIBUTE:
453 sb.append( " delete\n" );
454 break;
455 }
456
457 sb.append( " Modification\n" );
458 sb.append( modification.getAttribute() );
459 }
460 }
461
462
463 sb.append( super.toString() );
464
465 return super.toString( sb.toString() );
466 }
467 }