use of com.yahoo.elide.core.type.Type in project elide by yahoo.
the class DataStoreTransaction method loadObject.
/**
* Loads an object by ID. The reason we support both load by ID and load by filter is that
* some legacy stores are optimized to load by ID.
*
* @param entityProjection the collection to load.
* @param id - the ID of the object to load.
* @param scope - the current request scope
* @param <T> The model type being loaded.
* It is optional for the data store to attempt evaluation.
* @return the loaded object if it exists AND any provided security filters pass.
*/
default <T> T loadObject(EntityProjection entityProjection, Serializable id, RequestScope scope) {
Type<?> entityClass = entityProjection.getType();
FilterExpression filterExpression = entityProjection.getFilterExpression();
EntityDictionary dictionary = scope.getDictionary();
Type idType = dictionary.getIdType(entityClass);
String idField = dictionary.getIdFieldName(entityClass);
FilterExpression idFilter = new InPredicate(new Path.PathElement(entityClass, idType, idField), id);
FilterExpression joinedFilterExpression = (filterExpression != null) ? new AndFilterExpression(idFilter, filterExpression) : idFilter;
Iterable<T> results = loadObjects(entityProjection.copyOf().filterExpression(joinedFilterExpression).build(), scope);
Iterator<T> it = results == null ? null : results.iterator();
if (it != null && it.hasNext()) {
T obj = it.next();
if (!it.hasNext()) {
return obj;
}
// Multiple objects with the same ID.
throw new InvalidObjectIdentifierException(id.toString(), dictionary.getJsonAliasFor(entityClass));
}
return null;
}
use of com.yahoo.elide.core.type.Type in project elide by yahoo.
the class HashMapStoreTransaction method createObject.
@Override
public void createObject(Object entity, RequestScope scope) {
Type entityClass = EntityDictionary.getType(entity);
String idFieldName = dictionary.getIdFieldName(entityClass);
String id;
if (containsObject(entity)) {
throw new TransactionException(new IllegalStateException("Duplicate key"));
}
// GeneratedValue means the DB needs to assign the ID.
if (dictionary.getAttributeOrRelationAnnotation(entityClass, GeneratedValue.class, idFieldName) != null) {
// TODO: Id's are not necessarily numeric.
AtomicLong nextId;
synchronized (dataStore) {
nextId = getId(entityClass);
}
id = String.valueOf(nextId.getAndIncrement());
setId(entity, id);
} else {
id = dictionary.getId(entity);
}
replicateOperationToParent(entity, Operation.OpType.CREATE);
operations.add(new Operation(id, entity, EntityDictionary.getType(entity), Operation.OpType.CREATE));
}
use of com.yahoo.elide.core.type.Type in project elide by yahoo.
the class DefaultFilterDialect method parseGlobalExpression.
@Override
public FilterExpression parseGlobalExpression(String path, MultivaluedMap<String, String> filterParams, String apiVersion) throws ParseException {
List<FilterPredicate> filterPredicates;
filterPredicates = extractPredicates(filterParams, apiVersion);
/* Extract the first collection in the URL */
String normalizedPath = JsonApiParser.normalizePath(path);
String[] pathComponents = normalizedPath.split("/");
String firstPathComponent = "";
if (pathComponents.length > 0) {
firstPathComponent = pathComponents[0];
}
/* Comma separated filter parameters are joined with logical AND. */
FilterExpression joinedExpression = null;
for (FilterPredicate filterPredicate : filterPredicates) {
Type firstClass = filterPredicate.getPath().getPathElements().get(0).getType();
/* The first type in the predicate must match the first collection in the URL */
if (!dictionary.getJsonAliasFor(firstClass).equals(firstPathComponent)) {
throw new ParseException(String.format("Invalid predicate: %s", filterPredicate));
}
if ((filterPredicate.getOperator().equals(Operator.HASMEMBER) || filterPredicate.getOperator().equals(Operator.HASNOMEMBER)) && !FilterPredicate.isLastPathElementAssignableFrom(dictionary, filterPredicate.getPath(), COLLECTION_TYPE)) {
throw new ParseException("Invalid Path: Last Path Element has to be a collection type");
}
if (joinedExpression == null) {
joinedExpression = filterPredicate;
} else {
joinedExpression = new AndFilterExpression(joinedExpression, filterPredicate);
}
}
return joinedExpression;
}
use of com.yahoo.elide.core.type.Type in project elide by yahoo.
the class AggregationDataStore method populateEntityDictionary.
/**
* Populate an {@link EntityDictionary} and use this dictionary to construct a {@link QueryEngine}.
* @param dictionary the dictionary
*/
@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
if (dynamicCompiledClasses != null && dynamicCompiledClasses.size() != 0) {
dynamicCompiledClasses.stream().filter((type) -> !IS_TYPE_HIDDEN.test(type)).forEach(dynamicLoadedClass -> {
dictionary.bindEntity(dynamicLoadedClass, IS_FIELD_HIDDEN);
validateModelExpressionChecks(dictionary, dynamicLoadedClass);
dictionary.bindPermissionExecutor(dynamicLoadedClass, aggPermissionExecutor);
});
}
dictionary.getScanner().getAnnotatedClasses(AGGREGATION_STORE_CLASSES).stream().filter((type) -> !IS_TYPE_HIDDEN.test(ClassType.of(type))).forEach(cls -> {
dictionary.bindEntity(cls, IS_FIELD_HIDDEN);
validateModelExpressionChecks(dictionary, ClassType.of(cls));
dictionary.bindPermissionExecutor(cls, aggPermissionExecutor);
});
for (Table table : queryEngine.getMetaDataStore().getMetaData(ClassType.of(Table.class))) {
/* Add 'grain' argument to each TimeDimensionColumn */
for (TimeDimension timeDim : table.getAllTimeDimensions()) {
dictionary.addArgumentToAttribute(dictionary.getEntityClass(table.getName(), table.getVersion()), timeDim.getName(), new ArgumentType("grain", ClassType.STRING_TYPE, timeDim.getDefaultGrain().getGrain()));
}
/* Add argument to each Column */
for (Column col : table.getAllColumns()) {
for (ArgumentDefinition arg : col.getArgumentDefinitions()) {
dictionary.addArgumentToAttribute(dictionary.getEntityClass(table.getName(), table.getVersion()), col.getName(), new ArgumentType(arg.getName(), ValueType.getType(arg.getType()), arg.getDefaultValue()));
}
}
/* Add argument to each Table */
for (ArgumentDefinition arg : table.getArgumentDefinitions()) {
dictionary.addArgumentToEntity(dictionary.getEntityClass(table.getName(), table.getVersion()), new ArgumentType(arg.getName(), ValueType.getType(arg.getType()), arg.getDefaultValue()));
}
}
}
use of com.yahoo.elide.core.type.Type 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);
}
Aggregations