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.schema;
21
22
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.directory.api.i18n.I18n;
27 import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants;
28 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
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.DefaultEntry;
32 import org.apache.directory.api.ldap.model.entry.Entry;
33 import org.apache.directory.api.ldap.model.exception.LdapException;
34 import org.apache.directory.api.ldap.model.schema.registries.Schema;
35 import org.apache.directory.api.util.DateUtils;
36 import org.apache.directory.api.util.TimeProvider;
37
38
39
40
41
42
43
44
45 public class AttributesFactory
46 {
47 private TimeProvider timeProvider = TimeProvider.DEFAULT;
48
49
50
51
52
53
54
55
56
57
58 public Entry getAttributes( SchemaObject obj, Schema schema, SchemaManager schemaManager ) throws LdapException
59 {
60 if ( obj instanceof LdapSyntax )
61 {
62 return convert( ( LdapSyntax ) obj, schema, schemaManager );
63 }
64 else if ( obj instanceof MatchingRule )
65 {
66 return convert( ( MatchingRule ) obj, schema, schemaManager );
67 }
68 else if ( obj instanceof AttributeType )
69 {
70 return convert( ( AttributeType ) obj, schema, schemaManager );
71 }
72 else if ( obj instanceof ObjectClass )
73 {
74 return convert( ( ObjectClass ) obj, schema, schemaManager );
75 }
76 else if ( obj instanceof MatchingRuleUse )
77 {
78 return convert( ( MatchingRuleUse ) obj, schema, schemaManager );
79 }
80 else if ( obj instanceof DitStructureRule )
81 {
82 return convert( ( DitStructureRule ) obj, schema, schemaManager );
83 }
84 else if ( obj instanceof DitContentRule )
85 {
86 return convert( ( DitContentRule ) obj, schema, schemaManager );
87 }
88 else if ( obj instanceof NameForm )
89 {
90 return convert( ( NameForm ) obj, schema, schemaManager );
91 }
92
93 throw new IllegalArgumentException( I18n.err( I18n.ERR_13712_UNKNOWN_SCHEMA_OBJECT_TYPE, obj.getClass() ) );
94 }
95
96
97
98
99
100
101
102
103
104
105 public Entry convert( Schema schema, SchemaManager schemaManager ) throws LdapException
106 {
107 Entry entry = new DefaultEntry( schemaManager );
108
109 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SCHEMA_OC );
110 entry.put( SchemaConstants.CN_AT, schema.getSchemaName() );
111 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
112 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
113
114 if ( schema.isDisabled() )
115 {
116 entry.put( MetaSchemaConstants.M_DISABLED_AT, "TRUE" );
117 }
118
119 String[] dependencies = schema.getDependencies();
120
121 if ( dependencies != null && dependencies.length > 0 )
122 {
123 Attribute attr = new DefaultAttribute(
124 schemaManager.getAttributeType( MetaSchemaConstants.M_DEPENDENCIES_AT ) );
125
126 for ( String dependency : dependencies )
127 {
128 attr.add( dependency );
129 }
130
131 entry.put( attr );
132 }
133
134 return entry;
135 }
136
137
138
139
140
141
142
143
144
145
146 public Entry convert( SyntaxChecker syntaxChecker, Schema schema, SchemaManager schemaManager )
147 {
148 Entry entry = new DefaultEntry( schemaManager );
149
150 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_CHECKER_OC );
151 entry.put( MetaSchemaConstants.M_OID_AT, syntaxChecker.getOid() );
152 entry.put( MetaSchemaConstants.M_FQCN_AT, syntaxChecker.getClass().getName() );
153 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
154 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
155
156 return entry;
157 }
158
159
160
161
162
163
164
165
166
167
168
169 public Entry convert( LdapSyntax syntax, Schema schema, SchemaManager schemaManager ) throws LdapException
170 {
171 Entry entry = new DefaultEntry( schemaManager );
172
173 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_OC );
174 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
175 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
176 injectCommon( syntax, entry, schemaManager );
177
178 return entry;
179 }
180
181
182
183
184
185
186
187
188
189
190
191 public Entry convert( String oid, Normalizer normalizer, Schema schema, SchemaManager schemaManager )
192 {
193 Entry entry = new DefaultEntry( schemaManager );
194
195 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_NORMALIZER_OC );
196 entry.put( MetaSchemaConstants.M_OID_AT, oid );
197 entry.put( MetaSchemaConstants.M_FQCN_AT, normalizer.getClass().getName() );
198 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
199 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
200
201 return entry;
202 }
203
204
205
206
207
208
209
210
211
212
213
214 public Entry convert( String oid, LdapComparator<? super Object> comparator, Schema schema, SchemaManager schemaManager )
215 {
216 Entry entry = new DefaultEntry( schemaManager );
217
218 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_COMPARATOR_OC );
219 entry.put( MetaSchemaConstants.M_OID_AT, oid );
220 entry.put( MetaSchemaConstants.M_FQCN_AT, comparator.getClass().getName() );
221 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
222 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
223
224 return entry;
225 }
226
227
228
229
230
231
232
233
234
235
236
237 public Entry convert( MatchingRule matchingRule, Schema schema, SchemaManager schemaManager )
238 throws LdapException
239 {
240 Entry entry = new DefaultEntry( schemaManager );
241
242 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_MATCHING_RULE_OC );
243 entry.put( MetaSchemaConstants.M_SYNTAX_AT, matchingRule.getSyntaxOid() );
244 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
245 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
246 injectCommon( matchingRule, entry, schemaManager );
247
248 return entry;
249 }
250
251
252
253
254
255
256
257
258
259
260 public Entry convert( MatchingRuleUse matchingRuleUse, Schema schema, SchemaManager schemaManager )
261 {
262 Entry entry = new DefaultEntry( schemaManager );
263
264 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
265 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
266 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
267
268 return entry;
269 }
270
271
272
273
274
275
276
277
278
279
280 public Entry convert( DitStructureRule ditStructureRule, Schema schema, SchemaManager schemaManager )
281 {
282 Entry entry = new DefaultEntry( schemaManager );
283
284 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
285 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
286 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
287
288 return entry;
289 }
290
291
292
293
294
295
296
297
298
299
300 public Entry convert( DitContentRule dITContentRule, Schema schema, SchemaManager schemaManager )
301 {
302 Entry entry = new DefaultEntry( schemaManager );
303
304 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
305 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
306 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
307
308 return entry;
309 }
310
311
312
313
314
315
316
317
318
319
320
321 public Entry convert( NameForm nameForm, Schema schema, SchemaManager schemaManager )
322 {
323 Entry entry = new DefaultEntry( schemaManager );
324
325 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
326 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
327 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
328
329 return entry;
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 public Entry convert( AttributeType attributeType, Schema schema, SchemaManager schemaManager ) throws LdapException
354 {
355 Entry entry = new DefaultEntry( schemaManager );
356
357 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_ATTRIBUTE_TYPE_OC );
358 entry.put( MetaSchemaConstants.M_COLLECTIVE_AT, getBoolean( attributeType.isCollective() ) );
359 entry.put( MetaSchemaConstants.M_NO_USER_MODIFICATION_AT, getBoolean( !attributeType.isUserModifiable() ) );
360 entry.put( MetaSchemaConstants.M_SINGLE_VALUE_AT, getBoolean( attributeType.isSingleValued() ) );
361 entry.put( MetaSchemaConstants.M_USAGE_AT, attributeType.getUsage().toString() );
362 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
363 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
364
365 injectCommon( attributeType, entry, schemaManager );
366
367 String superiorOid = attributeType.getSuperiorOid();
368
369 if ( superiorOid != null )
370 {
371 entry.put( MetaSchemaConstants.M_SUP_ATTRIBUTE_TYPE_AT, superiorOid );
372 }
373
374 if ( attributeType.getEqualityOid() != null )
375 {
376 entry.put( MetaSchemaConstants.M_EQUALITY_AT, attributeType.getEqualityOid() );
377 }
378
379 if ( attributeType.getSubstringOid() != null )
380 {
381 entry.put( MetaSchemaConstants.M_SUBSTR_AT, attributeType.getSubstringOid() );
382 }
383
384 if ( attributeType.getOrderingOid() != null )
385 {
386 entry.put( MetaSchemaConstants.M_ORDERING_AT, attributeType.getOrderingOid() );
387 }
388
389 if ( attributeType.getSyntaxOid() != null )
390 {
391 entry.put( MetaSchemaConstants.M_SYNTAX_AT, attributeType.getSyntaxOid() );
392 }
393
394 return entry;
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 public Entry convert( ObjectClass objectClass, Schema schema, SchemaManager schemaManager )
420 throws LdapException
421 {
422 Entry entry = new DefaultEntry( schemaManager );
423
424 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_OBJECT_CLASS_OC );
425 entry.put( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT, objectClass.getType().toString() );
426 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
427 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime( timeProvider ) );
428
429 injectCommon( objectClass, entry, schemaManager );
430 Attribute attr;
431
432
433 if ( ( objectClass.getSuperiorOids() != null ) && !objectClass.getSuperiorOids().isEmpty() )
434 {
435 if ( schemaManager != null )
436 {
437 attr = new DefaultAttribute(
438 schemaManager.getAttributeType( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT ) );
439 }
440 else
441 {
442 attr = new DefaultAttribute( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT );
443 }
444
445 for ( String superior : objectClass.getSuperiorOids() )
446 {
447 attr.add( superior );
448 }
449
450 entry.put( attr );
451 }
452
453
454 if ( ( objectClass.getMustAttributeTypeOids() != null ) && !objectClass.getMustAttributeTypeOids().isEmpty() )
455 {
456 if ( schemaManager != null )
457 {
458 attr = new DefaultAttribute( schemaManager.getAttributeType( MetaSchemaConstants.M_MUST_AT ) );
459 }
460 else
461 {
462 attr = new DefaultAttribute( MetaSchemaConstants.M_MUST_AT );
463 }
464
465 for ( String mustOid : objectClass.getMustAttributeTypeOids() )
466 {
467 attr.add( mustOid );
468 }
469
470 entry.put( attr );
471 }
472
473
474 if ( ( objectClass.getMayAttributeTypeOids() != null ) && !objectClass.getMayAttributeTypeOids().isEmpty() )
475 {
476 if ( schemaManager != null )
477 {
478 attr = new DefaultAttribute( schemaManager.getAttributeType( MetaSchemaConstants.M_MAY_AT ) );
479 }
480 else
481 {
482 attr = new DefaultAttribute( MetaSchemaConstants.M_MAY_AT );
483 }
484
485 for ( String mayOid : objectClass.getMayAttributeTypeOids() )
486 {
487 attr.add( mayOid );
488 }
489
490 entry.put( attr );
491 }
492
493 return entry;
494 }
495
496
497 private void injectCommon( SchemaObject object, Entry entry, SchemaManager schemaManager )
498 throws LdapException
499 {
500 injectNames( object.getNames(), entry, schemaManager );
501 entry.put( MetaSchemaConstants.M_OBSOLETE_AT, getBoolean( object.isObsolete() ) );
502 entry.put( MetaSchemaConstants.M_OID_AT, object.getOid() );
503
504 if ( object.getDescription() != null )
505 {
506 entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, object.getDescription() );
507 }
508
509
510 Map<String, List<String>> extensions = object.getExtensions();
511
512 if ( extensions != null )
513 {
514 for ( Map.Entry<String, List<String>> mapEntry : extensions.entrySet() )
515 {
516 String key = mapEntry.getKey();
517 List<String> values = mapEntry.getValue();
518
519 for ( String value : values )
520 {
521 entry.add( key, value );
522 }
523 }
524 }
525 }
526
527
528 private void injectNames( List<String> names, Entry entry, SchemaManager schemaManager ) throws LdapException
529 {
530 if ( ( names == null ) || names.isEmpty() )
531 {
532 return;
533 }
534
535 Attribute attr;
536
537 if ( schemaManager != null )
538 {
539 attr = new DefaultAttribute( schemaManager.getAttributeType( MetaSchemaConstants.M_NAME_AT ) );
540 }
541 else
542 {
543 attr = new DefaultAttribute( MetaSchemaConstants.M_NAME_AT );
544 }
545
546 for ( String name : names )
547 {
548 attr.add( name );
549 }
550
551 entry.put( attr );
552 }
553
554
555 private String getBoolean( boolean value )
556 {
557 if ( value )
558 {
559 return "TRUE";
560 }
561 else
562 {
563 return "FALSE";
564 }
565 }
566 }