Skip to content

Commit 530ae00

Browse files
committed
#363 Added LazyList.collectDistinct()
1 parent 1777902 commit 530ae00

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/LazyList.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,29 +365,30 @@ private void processIncludes() {
365365
* @author Evan Leonard
366366
*/
367367
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()) {
369375
return;
370376
}
371-
372377
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>();
376379

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();
379380
StringBuilder query = new StringBuilder().append(parentMetaModel.getIdName()).append(" IN (");
380-
appendQuestions(query, noDuplicateArray.length);
381+
appendQuestions(query, distinctParentIds.size());
381382
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);
384385
}
385386

386387
//now that we have the parents in the has, we need to distribute them into list of children that are
387388
//stored in the delegate.
388389
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")));
391392
}
392393
}
393394

@@ -436,32 +437,43 @@ private void processParent(BelongsToAssociation association) {
436437
* @return list of collected values for a column.
437438
*/
438439
public List<Object> collect(String attributeName) {
439-
hydrate();
440440
List<Object> results = new ArrayList<Object>();
441-
for (Model model : delegate) {
442-
results.add(model.get(attributeName));
443-
}
441+
collect(results, attributeName);
444442
return results;
445443
}
446444

447445
public Set<Object> collectDistinct(String attributeName) {
448-
hydrate();
449446
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();
450453
for (Model model : delegate) {
451454
results.add(model.get(attributeName));
452455
}
453-
return results;
454456
}
455457

456458
public List<Object> collect(String attributeName, String filterAttribute, Object filterValue) {
457-
hydrate();
458459
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();
459472
for (Model model : delegate) {
460473
if (model.get(filterAttribute).equals(filterValue)) {
461474
results.add(model.get(attributeName));
462475
}
463476
}
464-
return results;
465477
}
466478

467479
private void appendQuestions(StringBuilder sb, int count) {

0 commit comments

Comments
 (0)