io.agrest.cayenne.compiler.CayenneEntityCompiler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of agrest-cayenne Show documentation
Show all versions of agrest-cayenne Show documentation
Cayenne backend for Agrest
package io.agrest.cayenne.compiler;
import io.agrest.cayenne.persister.ICayennePersister;
import io.agrest.cayenne.processor.ICayenneQueryAssembler;
import io.agrest.cayenne.processor.select.ViaQueryResolver;
import io.agrest.cayenne.processor.select.ViaQueryWithParentExpResolver;
import io.agrest.meta.AgDataMap;
import io.agrest.meta.AgEntity;
import io.agrest.meta.AgEntityOverlay;
import io.agrest.meta.LazyAgEntity;
import io.agrest.meta.compiler.AgEntityCompiler;
import io.agrest.resolver.NestedDataResolver;
import io.agrest.resolver.RootDataResolver;
import org.apache.cayenne.di.Inject;
import org.apache.cayenne.map.EntityResolver;
import org.apache.cayenne.map.ObjEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* @since 1.24
*/
public class CayenneEntityCompiler implements AgEntityCompiler {
private static final Logger LOGGER = LoggerFactory.getLogger(CayenneEntityCompiler.class);
private final EntityResolver cayenneEntityResolver;
private final Map entityOverlays;
private final RootDataResolver defaultRootResolver;
private final NestedDataResolver defaultNestedResolver;
public CayenneEntityCompiler(
@Inject ICayennePersister cayennePersister,
@Inject ICayenneQueryAssembler queryAssembler,
@Inject Map entityOverlays) {
this.cayenneEntityResolver = cayennePersister.entityResolver();
this.entityOverlays = entityOverlays;
this.defaultRootResolver = createDefaultRootResolver(queryAssembler, cayennePersister);
this.defaultNestedResolver = createDefaultNestedResolver(queryAssembler, cayennePersister);
}
@Override
public AgEntity compile(Class type, AgDataMap dataMap) {
ObjEntity objEntity = cayenneEntityResolver.getObjEntity(type);
return objEntity != null
? new LazyAgEntity<>(type, () -> doCompile(type, dataMap))
: null;
}
protected RootDataResolver> createDefaultRootResolver(
ICayenneQueryAssembler queryAssembler,
ICayennePersister cayennePersister) {
return new ViaQueryResolver(queryAssembler, cayennePersister);
}
protected NestedDataResolver> createDefaultNestedResolver(
ICayenneQueryAssembler queryAssembler,
ICayennePersister cayennePersister) {
return new ViaQueryWithParentExpResolver(queryAssembler, cayennePersister);
}
private AgEntity doCompile(Class type, AgDataMap dataMap) {
LOGGER.debug("compiling Cayenne entity for type: {}", type);
return new CayenneAgEntityBuilder<>(type, dataMap, cayenneEntityResolver)
.overlay(entityOverlays.get(type.getName()))
.rootDataResolver(defaultRootResolver)
.nestedDataResolver(defaultNestedResolver)
.build();
}
}