19
19
package com .redhat .lightblue .crud .ldap ;
20
20
21
21
import java .util .ArrayList ;
22
+ import java .util .Collection ;
22
23
import java .util .HashMap ;
23
24
import java .util .HashSet ;
24
25
import java .util .List ;
43
44
import com .redhat .lightblue .crud .CRUDUpdateResponse ;
44
45
import com .redhat .lightblue .crud .CrudConstants ;
45
46
import com .redhat .lightblue .crud .DocCtx ;
47
+ import com .redhat .lightblue .crud .ldap .model .NullLdapMetadataPropertyImpl ;
46
48
import com .redhat .lightblue .crud .ldap .translator .FilterTranslator ;
47
49
import com .redhat .lightblue .crud .ldap .translator .ResultTranslator ;
48
50
import com .redhat .lightblue .crud .ldap .translator .SortTranslator ;
@@ -205,15 +207,17 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
205
207
206
208
LDAPConnection connection = getNewLdapConnection (store );
207
209
210
+ LdapMetadataProperty property = getLdapMetadataProperty (md );
211
+
208
212
try {
209
213
//TODO: Support scopes other than SUB
210
214
SearchRequest request = new SearchRequest (
211
215
store .getBaseDN (),
212
216
SearchScope .SUB ,
213
- new FilterTranslator ().translate (query ),
214
- collectRequiredFields ( md , projection , query , sort ).toArray (new String [0 ]));
217
+ new FilterTranslator (property ).translate (query ),
218
+ translateFieldNames ( property , gatherRequiredFields ( md , projection , query , sort ) ).toArray (new String [0 ]));
215
219
if (sort != null ){
216
- request .addControl (new ServerSideSortRequestControl (false , new SortTranslator ().translate (sort )));
220
+ request .addControl (new ServerSideSortRequestControl (false , new SortTranslator (property ).translate (sort )));
217
221
}
218
222
if ((from != null ) && (from > 0 )){
219
223
int endPos = to .intValue () - from .intValue ();
@@ -223,11 +227,11 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
223
227
SearchResult result = connection .search (request );
224
228
225
229
response .setSize (result .getEntryCount ());
226
- ResultTranslator resultTranslator = new ResultTranslator (ctx .getFactory ().getNodeFactory ());
230
+ ResultTranslator resultTranslator = new ResultTranslator (ctx .getFactory ().getNodeFactory (), md , property );
227
231
List <DocCtx > translatedDocs = new ArrayList <DocCtx >();
228
232
for (SearchResultEntry entry : result .getSearchEntries ()){
229
233
try {
230
- translatedDocs .add (resultTranslator .translate (entry , md ));
234
+ translatedDocs .add (resultTranslator .translate (entry ));
231
235
}
232
236
catch (Exception e ){
233
237
DocCtx erroredDoc = new DocCtx (null );
@@ -276,13 +280,18 @@ public void beforeUpdateEntityInfo(Metadata m, EntityInfo ei, boolean newEntity)
276
280
*/
277
281
@ Override
278
282
public void beforeCreateNewSchema (Metadata m , EntityMetadata md ) {
283
+ LdapMetadataProperty property = getLdapMetadataProperty (md );
284
+
279
285
Fields fields = md .getEntitySchema ().getFields ();
280
- if (!fields .has (LdapConstant .FIELD_DN )){
281
- fields .addNew (new SimpleField (LdapConstant .FIELD_DN , StringType .TYPE ));
286
+ String dnFieldName = property .translateAttributeName (LdapConstant .ATTRIBUTE_DN );
287
+ if (!fields .has (dnFieldName )){
288
+ fields .addNew (new SimpleField (dnFieldName , StringType .TYPE ));
282
289
}
283
- if (!fields .has (LdapConstant .FIELD_OBJECT_CLASS )){
284
- fields .addNew (new ArrayField (LdapConstant .FIELD_OBJECT_CLASS , new SimpleArrayElement (StringType .TYPE )));
285
- fields .addNew (new SimpleField (LightblueUtil .createArrayCountFieldName (LdapConstant .FIELD_OBJECT_CLASS ), IntegerType .TYPE ));
290
+
291
+ String objectClassFieldName = property .translateAttributeName (LdapConstant .ATTRIBUTE_OBJECT_CLASS );
292
+ if (!fields .has (objectClassFieldName )){
293
+ fields .addNew (new ArrayField (objectClassFieldName , new SimpleArrayElement (StringType .TYPE )));
294
+ fields .addNew (new SimpleField (LightblueUtil .createArrayCountFieldName (objectClassFieldName ), IntegerType .TYPE ));
286
295
}
287
296
}
288
297
@@ -325,7 +334,7 @@ private LdapMetadataProperty getLdapMetadataProperty(EntityMetadata md){
325
334
Object o = md .getEntityInfo ().getProperties ().get (LdapConstant .BACKEND );
326
335
327
336
if (o == null ){
328
- return null ;
337
+ return new NullLdapMetadataPropertyImpl () ;
329
338
}
330
339
331
340
if (!(o instanceof LdapMetadataProperty )){
@@ -354,7 +363,7 @@ private String createDN(LdapDataStore store, String uniqueValue){
354
363
* @param sort - (optional) {@link Sort}.
355
364
* @return list of field names.
356
365
*/
357
- private Set <String > collectRequiredFields (EntityMetadata md ,
366
+ private Set <String > gatherRequiredFields (EntityMetadata md ,
358
367
Projection projection , QueryExpression query , Sort sort ){
359
368
Set <String > fields = new HashSet <String >();
360
369
@@ -382,6 +391,22 @@ private Set<String> collectRequiredFields(EntityMetadata md,
382
391
return fields ;
383
392
}
384
393
394
+ /**
395
+ * Translates a <code>Collection</code> of fieldNames into a <code>Set</code> of
396
+ * attributeNames
397
+ * @param property - {@link LdapMetadataProperty}.
398
+ * @param fieldNames - <code>Collection</code> of fieldNames to translated
399
+ * @return <code>Set</code> of translated attributeNames.
400
+ */
401
+ private Set <String > translateFieldNames (LdapMetadataProperty property , Collection <String > fieldNames ){
402
+ Set <String > attributes = new HashSet <String >();
403
+ for (String fieldName : fieldNames ){
404
+ attributes .add (property .translateFieldName (fieldName ));
405
+ }
406
+
407
+ return attributes ;
408
+ }
409
+
385
410
/**
386
411
* For Insert and Save (and possibly Update), this method will project the results back
387
412
* onto the documents.
@@ -396,7 +421,9 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
396
421
397
422
EntityMetadata md = ctx .getEntityMetadata (ctx .getEntityName ());
398
423
JsonNodeFactory factory = ctx .getFactory ().getNodeFactory ();
399
- Set <String > requiredFields = collectRequiredFields (md , projection , null , null );
424
+ LdapMetadataProperty property = getLdapMetadataProperty (md );
425
+
426
+ Set <String > requiredAttributes = translateFieldNames (property , gatherRequiredFields (md , projection , null , null ));
400
427
Projector projector = Projector .getInstance (
401
428
Projection .add (
402
429
projection ,
@@ -406,15 +433,17 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
406
433
),
407
434
md );
408
435
436
+ String dnFieldName = property .translateAttributeName (LdapConstant .ATTRIBUTE_DN );
437
+
409
438
for (Entry <DocCtx , String > insertedDn : documentToDnMap .entrySet ()){
410
439
DocCtx document = insertedDn .getKey ();
411
440
String dn = insertedDn .getValue ();
412
441
DocCtx projectionResponseJson = null ;
413
442
414
443
// If only dn is in the projection, then no need to query LDAP.
415
- if ((requiredFields .size () == 1 ) && requiredFields .contains (LdapConstant .FIELD_DN )){
444
+ if ((requiredAttributes .size () == 1 ) && requiredAttributes .contains (LdapConstant .ATTRIBUTE_DN )){
416
445
ObjectNode node = factory .objectNode ();
417
- node .set (LdapConstant . FIELD_DN , StringType .TYPE .toJson (factory , dn ));
446
+ node .set (dnFieldName , StringType .TYPE .toJson (factory , dn ));
418
447
projectionResponseJson = new DocCtx (new JsonDoc (node ));
419
448
}
420
449
//TODO: else fetch entity from LDAP and project results.
0 commit comments