Merge pull request #2373 from FOCONIS/pr/enh/post_process_find_controller

Allow post-processing in BeanFindController
This commit is contained in:
Rob Bygrave
2021-09-16 11:26:34 +12:00
committed by GitHub
7 changed files with 315 additions and 1 deletions
@@ -0,0 +1,51 @@
package org.tests.model.controller;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class FindControllerMain {
@Id
private Integer id;
private Integer targetId;
private String targetTableName;
@Transient
private Object target;
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public Integer getTargetId() {
return targetId;
}
public void setTargetId(final Integer targetId) {
this.targetId = targetId;
}
public String getTargetTableName() {
return targetTableName;
}
public void setTargetTableName(final String targetTableName) {
this.targetTableName = targetTableName;
}
public Object getTarget() {
return target;
}
public void setTarget(final Object target) {
this.target = target;
}
}
@@ -0,0 +1,29 @@
package org.tests.model.controller;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class SoftRefA {
@Id
private Integer id;
private String title;
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
@@ -0,0 +1,29 @@
package org.tests.model.controller;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class SoftRefB {
@Id
private Integer id;
private String title;
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
}
@@ -0,0 +1,89 @@
package org.tests.model.controller;
import io.ebean.bean.BeanCollection;
import io.ebean.event.BeanFindController;
import io.ebean.event.BeanQueryRequest;
import io.ebean.plugin.BeanType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestBeanFindController implements BeanFindController {
@Override
public boolean isRegisterFor(final Class<?> cls) {
return cls.isAssignableFrom(FindControllerMain.class);
}
@Override
public boolean isInterceptFind(final BeanQueryRequest<?> request) {
return false;
}
@Override
public <T> T find(final BeanQueryRequest<T> request) {
return null;
}
@Override
public boolean isInterceptFindMany(final BeanQueryRequest<?> request) {
return false;
}
@Override
public <T> BeanCollection<T> findMany(final BeanQueryRequest<T> request) {
return null;
}
@Override
public <T> BeanCollection<T> postProcessMany(final BeanQueryRequest<T> request, final BeanCollection<T> result) {
Map<Class<?>, List<Integer>> elementsMap = new HashMap<>();
Map<Class<?>, Map<Integer, FindControllerMain>> controllerLookup = new HashMap<>();
for (Object entry : result.getActualEntries()) {
FindControllerMain findControllerMain = (FindControllerMain) entry;
Class<?> beanType = beanTypeFor(findControllerMain.getTargetTableName(), request);
if (beanType != null) {
elementsMap.computeIfAbsent(beanType, key -> new ArrayList<>()).add(findControllerMain.getTargetId());
controllerLookup.computeIfAbsent(beanType, key -> new HashMap<>()).put(findControllerMain.getTargetId(), findControllerMain);
}
}
elementsMap.forEach((beanType, ids) -> {
final Map<Integer, FindControllerMain> idLookup = controllerLookup.get(beanType);
request.getEbeanServer().find(beanType).where()
.idIn(ids).setMapKey("id")
.findMap().forEach((id, bean) -> idLookup.get((Integer) id).setTarget(bean));
});
return result;
}
@Override
public <T> T postProcess(final BeanQueryRequest<T> request, final T result) {
FindControllerMain findControllerMain = (FindControllerMain) result;
Class<?> beanType = beanTypeFor(findControllerMain.getTargetTableName(), request);
if (beanType != null) {
findControllerMain.setTarget(request.getEbeanServer().find(beanType, findControllerMain.getTargetId()));
}
return result;
}
private Class<?> beanTypeFor(String tableName, BeanQueryRequest<?> request) {
List<? extends BeanType<?>> types = request.getEbeanServer()
.getPluginApi().getBeanTypes(tableName);
for (BeanType<?> type : types) {
if (type.isInheritanceRoot()) {
return type.getBeanType();
}
}
return null;
}
}