package io.ebeanservice.docstore.api.support;
import io.ebean.FetchPath;
import io.ebean.text.PathProperties;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssoc;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DocStructure {
private final PathProperties doc;
private final Map<String, PathProperties> embedded = new HashMap<>();
private final Map<String, PathProperties> manyRoot = new HashMap<>();
public DocStructure(PathProperties pathProps) {
this.doc = pathProps;
}
public void addProperty(String name) {
doc.addToPath(null, name);
}
public void addNested(String path, PathProperties embeddedDoc) {
doc.addNested(path, embeddedDoc);
embedded.put(path, embeddedDoc);
}
public PathProperties doc() {
return doc;
}
public FetchPath getEmbedded(String path) {
return embedded.get(path);
}
public FetchPath getEmbeddedManyRoot(String path) {
return manyRoot.get(path);
}
public <T> void prepareMany(BeanDescriptor<T> desc) {
Set<String> strings = embedded.keySet();
for (String prop : strings) {
BeanPropertyAssoc<?> embProp = (BeanPropertyAssoc<?>) desc.findProperty(prop);
if (embProp.isMany()) {
prepare(prop, embProp);
}
}
}
private void prepare(String prop, BeanPropertyAssoc<?> embProp) {
BeanDescriptor<?> targetDesc = embProp.getTargetDescriptor();
PathProperties manyRootPath = new PathProperties();
manyRootPath.addToPath(null, targetDesc.getIdProperty().getName());
manyRootPath.addNested(prop, embedded.get(prop));
manyRoot.put(prop, manyRootPath);
}
}