Search in sources :

Example 21 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class EntityDictionary method bindEntity.

/**
 * Add an EntityBinding instance to dictionary.
 *
 * @param entityBinding EntityBinding instance
 */
public void bindEntity(EntityBinding entityBinding) {
    Type<?> declaredClass = entityBinding.entityClass;
    if (entitiesToExclude.contains(declaredClass)) {
        // Exclude Entity
        return;
    }
    if (isClassBound(declaredClass)) {
        // Ignore duplicate bindings.
        return;
    }
    Include include = (Include) getFirstAnnotation(declaredClass, Collections.singletonList(Include.class));
    String version = getModelVersion(declaredClass);
    bindJsonApiToEntity.put(Pair.of(entityBinding.jsonApiType, version), declaredClass);
    entityBindings.put(declaredClass, entityBinding);
    apiVersions.add(version);
    if (include != null && include.rootLevel()) {
        bindEntityRoots.add(declaredClass);
    }
}
Also used : Include(com.yahoo.elide.annotation.Include)

Example 22 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class EntityDictionary method getEntityName.

/**
 * Looks up the API model name for a given class.
 * @param modelClass The model class to lookup.
 * @return the API name for the model class.
 */
public static String getEntityName(Type<?> modelClass) {
    Type<?> declaringClass = lookupAnnotationDeclarationClass(modelClass, Include.class);
    if (declaringClass == null) {
        declaringClass = lookupAnnotationDeclarationClass(modelClass, Entity.class);
    }
    String entityPrefix = getEntityPrefix(modelClass);
    Preconditions.checkNotNull(declaringClass);
    Include include = declaringClass.getAnnotation(Include.class);
    if (include != null && !"".equals(include.name())) {
        return entityPrefix + include.name();
    }
    Entity entity = (Entity) getFirstAnnotation(declaringClass, Arrays.asList(Entity.class));
    if (entity == null || "".equals(entity.name())) {
        return entityPrefix + StringUtils.uncapitalize(declaringClass.getSimpleName());
    }
    return entityPrefix + entity.name();
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include)

Example 23 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class MetaDataStore method getNamespace.

/**
 * Get a namespace object.
 *
 * @param modelType the model type
 * @return the namespace
 */
public Namespace getNamespace(Type<?> modelType) {
    String apiVersionName = EntityDictionary.getModelVersion(modelType);
    Include include = (Include) EntityDictionary.getFirstPackageAnnotation(modelType, Arrays.asList(Include.class));
    String namespaceName;
    if (include != null && !include.name().isEmpty()) {
        namespaceName = include.name();
    } else {
        namespaceName = DEFAULT;
    }
    return namespaces.stream().filter(namespace -> namespace.getName().equals(namespaceName)).filter(namespace -> namespace.getVersion().equals(apiVersionName)).findFirst().orElse(null);
}
Also used : HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) Getter(lombok.Getter) IS_FIELD_HIDDEN(com.yahoo.elide.datastores.aggregation.AggregationDataStore.IS_FIELD_HIDDEN) Include(com.yahoo.elide.annotation.Include) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) DuplicateMappingException(com.yahoo.elide.core.exceptions.DuplicateMappingException) NamespacePackage(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage) Pair(org.apache.commons.lang3.tuple.Pair) DEFAULT_NAMESPACE(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage.DEFAULT_NAMESPACE) TableType(com.yahoo.elide.datastores.aggregation.dynamic.TableType) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) ClassScanner(com.yahoo.elide.core.utils.ClassScanner) Map(java.util.Map) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) AggregationDataStore(com.yahoo.elide.datastores.aggregation.AggregationDataStore) Entity(javax.persistence.Entity) ApiVersion(com.yahoo.elide.annotation.ApiVersion) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) TableSource(com.yahoo.elide.datastores.aggregation.metadata.models.TableSource) DEFAULT(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage.DEFAULT) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) Collection(java.util.Collection) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition) TimeDimensionGrain(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimensionGrain) Set(java.util.Set) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Subselect(org.hibernate.annotations.Subselect) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) List(java.util.List) Versioned(com.yahoo.elide.datastores.aggregation.metadata.models.Versioned) TypeHelper.getClassType(com.yahoo.elide.core.utils.TypeHelper.getClassType) DataStore(com.yahoo.elide.core.datastore.DataStore) Type(com.yahoo.elide.core.type.Type) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Annotation(java.lang.annotation.Annotation) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) EMPTY(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage.EMPTY) MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) Include(com.yahoo.elide.annotation.Include)

Example 24 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class NamespacePackage method buildAnnotations.

private static Map<Class<? extends Annotation>, Annotation> buildAnnotations(NamespaceConfig namespace) {
    Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();
    annotations.put(ReadPermission.class, new ReadPermission() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return ReadPermission.class;
        }

        @Override
        public String expression() {
            return namespace.getReadAccess();
        }
    });
    annotations.put(Include.class, new Include() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return Include.class;
        }

        @Override
        public boolean rootLevel() {
            return true;
        }

        @Override
        public String description() {
            return namespace.getDescription();
        }

        @Override
        public String friendlyName() {
            return namespace.getFriendlyName();
        }

        @Override
        public String name() {
            return namespace.getName();
        }
    });
    annotations.put(ApiVersion.class, new ApiVersion() {

        @Override
        public String version() {
            return namespace.getApiVersion();
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return ApiVersion.class;
        }
    });
    return annotations;
}
Also used : ApiVersion(com.yahoo.elide.annotation.ApiVersion) HashMap(java.util.HashMap) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Annotation(java.lang.annotation.Annotation)

Example 25 with Include

use of com.yahoo.elide.annotation.Include in project elide by yahoo.

the class JpaDataStoreTest method verifyManualEntityBinding.

@Test
public void verifyManualEntityBinding() {
    @Include(rootLevel = false)
    @Entity
    class Test {

        @Id
        private long id;

        private String name;
    }
    Metamodel mockModel = mock(Metamodel.class);
    when(mockModel.getEntities()).thenReturn(Sets.newHashSet());
    EntityManager managerMock = mock(EntityManager.class);
    when(managerMock.getMetamodel()).thenReturn(mockModel);
    JpaDataStore store = new JpaDataStore(() -> managerMock, unused -> null, ClassType.of(Test.class));
    EntityDictionary dictionary = EntityDictionary.builder().build();
    store.populateEntityDictionary(dictionary);
    assertNotNull(dictionary.lookupBoundClass(ClassType.of(Test.class)));
}
Also used : Entity(javax.persistence.Entity) EntityManager(javax.persistence.EntityManager) Test(org.junit.jupiter.api.Test) Include(com.yahoo.elide.annotation.Include) Metamodel(javax.persistence.metamodel.Metamodel) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Aggregations

Include (com.yahoo.elide.annotation.Include)46 Test (org.junit.jupiter.api.Test)41 Entity (javax.persistence.Entity)37 RequestScope (com.yahoo.elide.core.RequestScope)26 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)19 HashSet (java.util.HashSet)17 PersistentResource (com.yahoo.elide.core.PersistentResource)16 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)12 ReadPermission (com.yahoo.elide.annotation.ReadPermission)11 Annotation (java.lang.annotation.Annotation)4 Date (java.util.Date)4 ApiVersion (com.yahoo.elide.annotation.ApiVersion)3 NO_VERSION (com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION)3 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 Exclude (com.yahoo.elide.annotation.Exclude)2 AggregationStorePermissionExecutor (com.yahoo.elide.core.security.executors.AggregationStorePermissionExecutor)2 Expression (com.yahoo.elide.core.security.permissions.expressions.Expression)2 TableMeta (com.yahoo.elide.datastores.aggregation.annotation.TableMeta)2