use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class Entity method setId.
/**
* Set the id of the entity if it doesn't have one already.
*/
public void setId() {
if (!getId().isPresent()) {
EntityDictionary dictionary = this.requestScope.getDictionary();
String idFieldName = dictionary.getIdFieldName(this.entityClass);
String uuid = UUID.randomUUID().toString();
this.attributes.add(new Attribute(idFieldName, uuid));
}
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class MultiplexManager method populateEntityDictionary.
@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
this.dictionary = dictionary;
for (DataStore dataStore : dataStores) {
EntityDictionary subordinateDictionary = new EntityDictionary(dictionary.getCheckMappings(), dictionary.getRoleChecks(), dictionary.getInjector(), dictionary.getSerdeLookup(), dictionary.getEntitiesToExclude(), dictionary.getScanner());
dataStore.populateEntityDictionary(subordinateDictionary);
for (EntityBinding binding : subordinateDictionary.getBindings(false)) {
// route class to this database manager
this.dataStoreMap.put(binding.entityClass, dataStore);
// bind to multiplex dictionary
dictionary.bindEntity(binding);
}
for (Map.Entry<Type<?>, Function<RequestScope, PermissionExecutor>> entry : subordinateDictionary.getEntityPermissionExecutor().entrySet()) {
dictionary.bindPermissionExecutor(entry.getKey(), entry.getValue());
}
}
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class MultiplexTransaction method getRelationTransaction.
protected DataStoreTransaction getRelationTransaction(Object object, String relationName) {
EntityDictionary dictionary = multiplexManager.getDictionary();
Type<?> relationClass = dictionary.getParameterizedType(EntityDictionary.getType(object), relationName);
return getTransaction(relationClass);
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class PersistentResourceFetcher method fetchObject.
/**
* Fetches a root-level entity.
* @param requestScope Request scope
* @param projection constructed entityProjection for a class
* @param ids List of ids (can be NULL)
* @return {@link PersistentResource} object(s)
*/
public static ConnectionContainer fetchObject(RequestScope requestScope, EntityProjection projection, Optional<List<String>> ids) {
EntityDictionary dictionary = requestScope.getDictionary();
String typeName = dictionary.getJsonAliasFor(projection.getType());
/* fetching a collection */
Observable<PersistentResource> records = ids.map((idList) -> {
/* handle empty list of ids */
if (idList.isEmpty()) {
throw new BadRequestException("Empty list passed to ids");
}
return PersistentResource.loadRecords(projection, idList, requestScope);
}).orElseGet(() -> PersistentResource.loadRecords(projection, new ArrayList<>(), requestScope));
return new ConnectionContainer(records.toList(LinkedHashSet::new).blockingGet(), Optional.ofNullable(projection.getPagination()), typeName);
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class PersistentResourceFetcher method upsertObject.
/**
* updates or creates existing/new entities.
* @param entity Resource entity
* @param context The request context
* @return {@link PersistentResource} object
*/
private PersistentResource upsertObject(Entity entity, Environment context) {
Set<Entity.Attribute> attributes = entity.getAttributes();
Optional<String> id = entity.getId();
RequestScope requestScope = entity.getRequestScope();
PersistentResource upsertedResource;
EntityDictionary dictionary = requestScope.getDictionary();
PersistentResource parentResource = entity.getParentResource().map(Entity::toPersistentResource).orElse(null);
if (!id.isPresent()) {
// If the ID is generated, it is safe to assign a temporary UUID. Otherwise the client must provide one.
if (dictionary.isIdGenerated(entity.getEntityClass())) {
// Assign a temporary UUID.
entity.setId();
id = entity.getId();
}
upsertedResource = PersistentResource.createObject(parentResource, context.field.getName(), entity.getEntityClass(), requestScope, id);
} else {
try {
Set<PersistentResource> loadedResource = fetchObject(requestScope, entity.getProjection(), Optional.of(Collections.singletonList(id.get()))).getPersistentResources();
upsertedResource = loadedResource.iterator().next();
// The ID doesn't exist yet. Let's create the object.
} catch (InvalidObjectIdentifierException | InvalidValueException e) {
upsertedResource = PersistentResource.createObject(parentResource, context.field.getName(), entity.getEntityClass(), requestScope, id);
}
}
return updateAttributes(upsertedResource, entity, attributes);
}
Aggregations