All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.agrest.cayenne.compiler.CayenneEntityCompiler Maven / Gradle / Ivy

There is a newer version: 5.0.M19
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy