Search in sources :

Example 1 with ClassType

use of com.yahoo.elide.core.type.ClassType in project elide by yahoo.

the class EntityDictionary method coerceCollection.

private Collection coerceCollection(Object target, Collection<?> values, String fieldName, Class<?> fieldClass) {
    ClassType<?> providedType = (ClassType) getParameterizedType(target, fieldName);
    // check if collection is of and contains the correct types
    if (fieldClass.isAssignableFrom(values.getClass())) {
        boolean valid = true;
        for (Object member : values) {
            if (member != null && !providedType.isAssignableFrom(getType(member))) {
                valid = false;
                break;
            }
        }
        if (valid) {
            return values;
        }
    }
    ArrayList<Object> list = new ArrayList<>(values.size());
    for (Object member : values) {
        list.add(CoerceUtil.coerce(member, providedType.getCls()));
    }
    if (Set.class.isAssignableFrom(fieldClass)) {
        return new LinkedHashSet<>(list);
    }
    return list;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) ClassType(com.yahoo.elide.core.type.ClassType)

Example 2 with ClassType

use of com.yahoo.elide.core.type.ClassType in project elide by yahoo.

the class SearchDataStore method populateEntityDictionary.

@Override
public void populateEntityDictionary(EntityDictionary entityDictionary) {
    wrapped.populateEntityDictionary(entityDictionary);
    if (indexOnStartup) {
        FullTextEntityManager em = Search.getFullTextEntityManager(entityManagerFactory.createEntityManager());
        try {
            for (Type<?> entityType : entityDictionary.getBoundClasses()) {
                if (entityDictionary.getAnnotation(entityType, Indexed.class) != null) {
                    Preconditions.checkState(entityType instanceof ClassType);
                    Class<?> entityClass = ((ClassType) entityType).getCls();
                    em.createIndexer(entityClass).startAndWait();
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException(e);
        } finally {
            em.close();
        }
    }
    this.dictionary = entityDictionary;
}
Also used : ClassType(com.yahoo.elide.core.type.ClassType) Indexed(org.hibernate.search.annotations.Indexed) FullTextEntityManager(org.hibernate.search.jpa.FullTextEntityManager)

Example 3 with ClassType

use of com.yahoo.elide.core.type.ClassType in project elide by yahoo.

the class DataStoreTransactionTest method setupMocks.

@BeforeEach
public void setupMocks() {
    // this will test with the default interface implementation
    scope = mock(RequestScope.class);
    EntityDictionary dictionary = mock(EntityDictionary.class);
    when(scope.getDictionary()).thenReturn(dictionary);
    when(dictionary.getIdType(STRING_TYPE)).thenReturn(new ClassType(Long.class));
    when(dictionary.getValue(ENTITY, NAME, scope)).thenReturn(3L);
    when(dictionary.getValue(ENTITY, NAME2, scope)).thenReturn(new DataStoreIterableBuilder(List.of(1L, 2L, 3L)).build());
}
Also used : ClassType(com.yahoo.elide.core.type.ClassType) RequestScope(com.yahoo.elide.core.RequestScope) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ClassType

use of com.yahoo.elide.core.type.ClassType in project elide by yahoo.

the class JsonApiModelResolver method processAttribute.

private Property processAttribute(Type<?> clazzType, String attributeName, Type<?> attributeType, ModelConverterContext context, Iterator<ModelConverter> next) {
    Preconditions.checkState(attributeType instanceof ClassType);
    Property attribute = super.resolveProperty(((ClassType) attributeType).getCls(), context, null, next);
    String permissions = getFieldPermissions(clazzType, attributeName);
    String description = getFieldDescription(clazzType, attributeName);
    attribute.setDescription(StringUtils.defaultIfEmpty(joinNonEmpty("\n", description, permissions), null));
    attribute.setExample((Object) StringUtils.defaultIfEmpty(getFieldExample(clazzType, attributeName), null));
    attribute.setReadOnly(getFieldReadOnly(clazzType, attributeName));
    attribute.setRequired(getFieldRequired(clazzType, attributeName));
    return attribute;
}
Also used : ClassType(com.yahoo.elide.core.type.ClassType) ApiModelProperty(io.swagger.annotations.ApiModelProperty) Property(io.swagger.models.properties.Property)

Example 5 with ClassType

use of com.yahoo.elide.core.type.ClassType in project elide by yahoo.

the class SwaggerBuilder method build.

/**
 * Builds a swagger object.
 * @return the constructed 'Swagger' object
 */
public Swagger build() {
    /* Used to convert Elide POJOs into Swagger Model objects */
    ModelConverters converters = ModelConverters.getInstance();
    ModelConverter converter = new JsonApiModelResolver(dictionary);
    converters.addConverter(converter);
    String apiVersion = swagger.getInfo().getVersion();
    if (apiVersion == null) {
        apiVersion = NO_VERSION;
    }
    if (allClasses.isEmpty()) {
        allClasses = dictionary.getBoundClassesByVersion(apiVersion);
    } else {
        allClasses = Sets.intersection(dictionary.getBoundClassesByVersion(apiVersion), allClasses);
        if (allClasses.isEmpty()) {
            throw new IllegalArgumentException("None of the provided classes are exported by Elide");
        }
    }
    /*
         * Create a Model for each Elide entity.
         * Elide entity could be of ClassType or DynamicType.
         * For ClassType, extract the class and pass it to ModelConverters#readAll method.
         * ModelConverters#readAll doesn't support Elide Dynamic Type, so calling the
         * JsonApiModelResolver#resolve method directly when its not a ClassType.
         */
    Map<String, Model> models = new HashMap<>();
    for (Type<?> clazz : allClasses) {
        if (clazz instanceof ClassType) {
            models.putAll(converters.readAll(((ClassType) clazz).getCls()));
        } else {
            ModelConverterContextImpl context = new ModelConverterContextImpl(Arrays.asList(converter));
            context.resolve(clazz);
            models.putAll(context.getDefinedModels());
        }
    }
    swagger.setDefinitions(models);
    rootClasses = allClasses.stream().filter(dictionary::isRoot).collect(Collectors.toSet());
    /* Find all the paths starting from the root entities. */
    Set<PathMetaData> pathData = rootClasses.stream().map(this::find).flatMap(Collection::stream).collect(Collectors.toSet());
    /* Prune the discovered paths to remove redundant elements */
    Set<PathMetaData> toRemove = new HashSet<>();
    pathData.stream().collect(Collectors.groupingBy(PathMetaData::getRootType)).values().forEach(pathSet -> {
        for (PathMetaData path : pathSet) {
            for (PathMetaData compare : pathSet) {
                /*
                             * We don't prune paths that are redundant with root collections to allow both BOTH
                             * root collection urls as well as relationship urls.
                             */
                if (compare.lineage.isEmpty() || path == compare) {
                    continue;
                }
                if (compare.shorterThan(path)) {
                    toRemove.add(path);
                    break;
                }
            }
        }
    });
    pathData = Sets.difference(pathData, toRemove);
    /* Each path constructs 3 URLs (collection, instance, and relationship) */
    for (PathMetaData pathDatum : pathData) {
        swagger.path(pathDatum.getCollectionUrl(), pathDatum.getCollectionPath());
        swagger.path(pathDatum.getUrl(), pathDatum.getInstancePath());
        /* We only construct relationship URLs if the entity is not a root collection */
        if (!pathDatum.lineage.isEmpty()) {
            swagger.path(pathDatum.getRelationshipUrl(), pathDatum.getRelationshipPath());
        }
    }
    /* We create Swagger 'tags' for each entity so Swagger UI organizes the paths by entities */
    List<Tag> tags = allClasses.stream().map((clazz) -> dictionary.getJsonAliasFor(clazz)).map((alias) -> new Tag().name(alias)).collect(Collectors.toList());
    swagger.tags(tags);
    return swagger;
}
Also used : Arrays(java.util.Arrays) ModelConverters(io.swagger.converter.ModelConverters) Getter(lombok.Getter) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) StringProperty(io.swagger.models.properties.StringProperty) Json(io.swagger.util.Json) HashMap(java.util.HashMap) ClassType(com.yahoo.elide.core.type.ClassType) Stack(java.util.Stack) Model(io.swagger.models.Model) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Path(io.swagger.models.Path) Map(java.util.Map) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) Datum(com.yahoo.elide.swagger.model.Datum) ModelConverter(io.swagger.converter.ModelConverter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Collection(java.util.Collection) Set(java.util.Set) Parameter(io.swagger.models.parameters.Parameter) ModelConverterContextImpl(io.swagger.converter.ModelConverterContextImpl) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Info(io.swagger.models.Info) QueryParameter(io.swagger.models.parameters.QueryParameter) Objects(java.util.Objects) Response(io.swagger.models.Response) List(java.util.List) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) Relationship(com.yahoo.elide.swagger.property.Relationship) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) Data(com.yahoo.elide.swagger.model.Data) HashMap(java.util.HashMap) ClassType(com.yahoo.elide.core.type.ClassType) ModelConverter(io.swagger.converter.ModelConverter) Model(io.swagger.models.Model) ModelConverters(io.swagger.converter.ModelConverters) Tag(io.swagger.models.Tag) ModelConverterContextImpl(io.swagger.converter.ModelConverterContextImpl) HashSet(java.util.HashSet)

Aggregations

ClassType (com.yahoo.elide.core.type.ClassType)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Path (com.yahoo.elide.core.Path)2 RequestScope (com.yahoo.elide.core.RequestScope)2 Operator (com.yahoo.elide.core.filter.Operator)2 Sorting (com.yahoo.elide.core.request.Sorting)2 Type (com.yahoo.elide.core.type.Type)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 SortableField (org.hibernate.search.annotations.SortableField)2 QueryBuilder (org.hibernate.search.query.dsl.QueryBuilder)2 SortFieldContext (org.hibernate.search.query.dsl.sort.SortFieldContext)2 Preconditions (com.google.common.base.Preconditions)1 Sets (com.google.common.collect.Sets)1 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)1 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)1