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