@@ -365,29 +365,30 @@ private void processIncludes() {
365
365
* @author Evan Leonard
366
366
*/
367
367
private void processPolymorphicParent (BelongsToPolymorphicAssociation association ) {
368
- if (delegate .isEmpty ()){//no need to process children if no models selected.
368
+ if (delegate .isEmpty ()) { // no need to process children if no models selected.
369
+ return ;
370
+ }
371
+ //need to remove duplicates because more than one child can belong to the same parent.
372
+ Set <Object > distinctParentIds = collectDistinct ("parent_id" , "parent_type" , association .getParentClassName ());
373
+ distinctParentIds .remove (null ); // remove null parent id
374
+ if (distinctParentIds .isEmpty ()) {
369
375
return ;
370
376
}
371
-
372
377
final MetaModel parentMetaModel = Registry .instance ().getMetaModel (association .getTarget ());
373
- final Map <Object , Model > parentsHasByIds = new HashMap <Object , Model >();
374
-
375
- String parentClassName = association .getParentClassName ();
378
+ final Map <Object , Model > parentById = new HashMap <Object , Model >();
376
379
377
- //need to remove duplicates because more than one child can belong to the same parent.
378
- Object [] noDuplicateArray = new HashSet (collect ("parent_id" , "parent_type" , parentClassName )).toArray ();
379
380
StringBuilder query = new StringBuilder ().append (parentMetaModel .getIdName ()).append (" IN (" );
380
- appendQuestions (query , noDuplicateArray . length );
381
+ appendQuestions (query , distinctParentIds . size () );
381
382
query .append (')' );
382
- for (Model parent : new LazyList <Model >(query .toString (), parentMetaModel , noDuplicateArray )) {
383
- parentsHasByIds .put (parentClassName + ":" + parent .getId (), parent );
383
+ for (Model parent : new LazyList <Model >(query .toString (), parentMetaModel , distinctParentIds . toArray () )) {
384
+ parentById .put (association . getParentClassName () + ":" + parent .getId (), parent );
384
385
}
385
386
386
387
//now that we have the parents in the has, we need to distribute them into list of children that are
387
388
//stored in the delegate.
388
389
for (Model child : delegate ) {
389
- Model parent = parentsHasByIds . get ( parentClassName + ":" + child . get ( "parent_id" ));
390
- child .setCachedParent (parent ); //this could be null, which is fine
390
+ // parent could be null, which is fine
391
+ child .setCachedParent (parentById . get ( association . getParentClassName () + ":" + child . get ( "parent_id" )));
391
392
}
392
393
}
393
394
@@ -436,32 +437,43 @@ private void processParent(BelongsToAssociation association) {
436
437
* @return list of collected values for a column.
437
438
*/
438
439
public List <Object > collect (String attributeName ) {
439
- hydrate ();
440
440
List <Object > results = new ArrayList <Object >();
441
- for (Model model : delegate ) {
442
- results .add (model .get (attributeName ));
443
- }
441
+ collect (results , attributeName );
444
442
return results ;
445
443
}
446
444
447
445
public Set <Object > collectDistinct (String attributeName ) {
448
- hydrate ();
449
446
Set <Object > results = new LinkedHashSet <Object >();
447
+ collect (results , attributeName );
448
+ return results ;
449
+ }
450
+
451
+ private void collect (Collection <Object > results , String attributeName ) {
452
+ hydrate ();
450
453
for (Model model : delegate ) {
451
454
results .add (model .get (attributeName ));
452
455
}
453
- return results ;
454
456
}
455
457
456
458
public List <Object > collect (String attributeName , String filterAttribute , Object filterValue ) {
457
- hydrate ();
458
459
List <Object > results = new ArrayList <Object >();
460
+ collect (results , attributeName , filterAttribute , filterValue );
461
+ return results ;
462
+ }
463
+
464
+ public Set <Object > collectDistinct (String attributeName , String filterAttribute , Object filterValue ) {
465
+ Set <Object > results = new LinkedHashSet <Object >();
466
+ collect (results , attributeName , filterAttribute , filterValue );
467
+ return results ;
468
+ }
469
+
470
+ private void collect (Collection <Object > results , String attributeName , String filterAttribute , Object filterValue ) {
471
+ hydrate ();
459
472
for (Model model : delegate ) {
460
473
if (model .get (filterAttribute ).equals (filterValue )) {
461
474
results .add (model .get (attributeName ));
462
475
}
463
476
}
464
- return results ;
465
477
}
466
478
467
479
private void appendQuestions (StringBuilder sb , int count ) {
0 commit comments