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;
}
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;
}
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());
}
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;
}
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;
}
Aggregations