@Override public void populateEntityDictionary(EntityDictionary dictionary) { this.dictionary = dictionary; for (DataStore dataStore : dataStores) { EntityDictionary subordinateDictionary = new EntityDictionary(dictionary.getCheckMappings()); dataStore.populateEntityDictionary(subordinateDictionary); for (Class<?> cls : subordinateDictionary.getBindings()) { // route class to this database manager this.dataStoreMap.put(cls, dataStore); // bind to multiplex dictionary dictionary.bindEntity(cls); dictionary.bindInitializer(subordinateDictionary::initializeEntity, cls); } } }
public Path(Class<?> entityClass, EntityDictionary dictionary, String dotSeparatedPath) { List<PathElement> elements = new ArrayList<>(); String[] fieldNames = dotSeparatedPath.split("\\."); Class<?> currentClass = entityClass; for (String fieldName : fieldNames) { if (dictionary.isRelation(currentClass, fieldName)) { Class<?> relationClass = dictionary.getParameterizedType(currentClass, fieldName); elements.add(new PathElement(currentClass, relationClass, fieldName)); currentClass = relationClass; } else if (dictionary.isAttribute(currentClass, fieldName) || fieldName.equals(dictionary.getIdFieldName(entityClass))) { Class<?> attributeClass = dictionary.getType(currentClass, fieldName); elements.add(new PathElement(currentClass, attributeClass, fieldName)); } else if ("this".equals(fieldName)) { elements.add(new PathElement(currentClass, null, fieldName)); } else { String alias = dictionary.getJsonAliasFor(currentClass); throw new InvalidValueException(alias + " doesn't contain the field " + fieldName); } } pathElements = ImmutableList.copyOf(elements); }
/** * Binds the entity class if not yet bound. * @param entityClass the class to bind. */ private void bindIfUnbound(Class<?> entityClass) { if (! entityBindings.containsKey(lookupEntityClass(entityClass))) { bindEntity(entityClass); } } }
/** * Get a list of elide-bound relationships. * * @param entityClass Entity class to find relationships for * @return List of elide-bound relationship names. */ public List<String> getElideBoundRelationships(Class<?> entityClass) { return getRelationships(entityClass).stream() .filter(relationName -> getBindings().contains(getParameterizedType(entityClass, relationName))) .collect(Collectors.toList()); }
String entityName = dictionary.getJsonAliasFor(entityClass); String id = dictionary.getIdFieldName(entityClass); .type(GraphQLScalars.GRAPHQL_DEFERRED_ID)); for (String attribute : dictionary.getAttributes(entityClass)) { Class<?> attributeClass = dictionary.getType(entityClass, attribute); if (excludedEntities.contains(attributeClass)) { continue; for (String relationship : dictionary.getElideBoundRelationships(entityClass)) { Class<?> relationshipClass = dictionary.getParameterizedType(entityClass, relationship); if (excludedEntities.contains(relationshipClass)) { continue; String relationshipEntityName = dictionary.getJsonAliasFor(relationshipClass); RelationshipType type = dictionary.getRelationshipType(entityClass, relationship);
@BeforeClass public void initialize() { dictionary = new EntityDictionary(new HashMap<>()); dictionary.bindEntity(Book.class); dictionary.bindEntity(Author.class); dictionary.bindEntity(Publisher.class); dictionary.bindEntity(Chapter.class); }
@Override public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression, RequestScope scope) { Class idType = dictionary.getIdType(entityClass); String idField = dictionary.getIdFieldName(entityClass); FilterExpression idFilter = new InPredicate( new Path.PathElement(entityClass, idType, idField), id ); FilterExpression joinedFilterExpression = filterExpression .map(fe -> (FilterExpression) new AndFilterExpression(idFilter, fe)) .orElse(idFilter); Iterable<Object> results = loadObjects(entityClass, Optional.of(joinedFilterExpression), Optional.empty(), Optional.empty(), scope); Iterator<Object> it = results == null ? null : results.iterator(); if (it != null && it.hasNext()) { return it.next(); } return null; }
log.debug("Building input object for {}", clazz.getName()); String entityName = dictionary.getJsonAliasFor(clazz); String id = dictionary.getIdFieldName(clazz); builder.field(newInputObjectField() .name(id) .type(Scalars.GraphQLID)); for (String attribute : dictionary.getAttributes(clazz)) { Class<?> attributeClass = dictionary.getType(clazz, attribute);
typeAlias = dictionary.getJsonAliasFor(clazz); } catch (IllegalArgumentException e) { return super.resolve(type, context, next); List<String> attributeNames = dictionary.getAttributes(clazz); for (String attributeName : attributeNames) { Class<?> attributeClazz = dictionary.getType(clazz, attributeName); List<String> relationshipNames = dictionary.getRelationships(clazz); for (String relationshipName : relationshipNames) { Class<?> relationshipType = dictionary.getParameterizedType(clazz, relationshipName); Relationship relationship; try { relationship = new Relationship(dictionary.getJsonAliasFor(relationshipType));
Class<?> parentType = dictionary.lookupEntityClass(relationship.getParentType()); Class<?> idType = dictionary.getIdType(parentType); Object idVal = CoerceUtil.coerce(dictionary.getId(relationship.getParent()), idType); String idField = dictionary.getIdFieldName(parentType);
/** * Build an id filter expression for a particular entity type. * * @param ids Ids to include in the filter expression * @param entityType Type of entity * @return Filter expression for given ids and type. */ private static FilterExpression buildIdFilterExpression(List<String> ids, Class entityType, EntityDictionary dictionary, RequestScope scope) { Class<?> idType = dictionary.getIdType(entityType); String idField = dictionary.getIdFieldName(entityType); String typeAlias = dictionary.getJsonAliasFor(entityType); List<Object> coercedIds = ids.stream() .filter(id -> scope.getObjectById(typeAlias, id) == null) // these don't exist yet .map(id -> CoerceUtil.coerce(id, idType)) .collect(Collectors.toList()); /* construct a new SQL like filter expression, eg: book.id IN [1,2] */ FilterExpression idFilter = new InPredicate( new Path.PathElement( entityType, idType, idField), coercedIds); return idFilter; }
@Override public Object getRelation(DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { Object values = PersistentResource.getValue(entity, relationName, scope); // Gather list of valid id's from this parent Map<String, Object> idToChildResource = new HashMap<>(); if (dictionary.getRelationshipType(entity, relationName).isToOne()) { if (values == null) { return null; } idToChildResource.put(dictionary.getId(values), values); } else if (values instanceof Collection) { idToChildResource.putAll((Map) ((Collection) values).stream() .collect(Collectors.toMap(dictionary::getId, Function.identity()))); } else { throw new IllegalStateException("An unexpected error occurred querying a relationship"); } Class entityClass = dictionary.getParameterizedType(entity, relationName); return processData(entityClass, idToChildResource, filterExpression, sorting, pagination, scope); }
/** * Retrieve the parameterized type for the given field. * * @param entityClass the entity class * @param identifier the identifier * @return Entity type for field otherwise null. */ public Class<?> getParameterizedType(Class<?> entityClass, String identifier) { return getParameterizedType(entityClass, identifier, 0); }
private Path buildPath(Class rootEntityType, String selector) { String[] associationNames = selector.split("\\."); List<Path.PathElement> path = new ArrayList<>(); Class entityType = rootEntityType; for (String associationName : associationNames) { String typeName = dictionary.getJsonAliasFor(entityType); Class fieldType = dictionary.getParameterizedType(entityType, associationName); if (fieldType == null) { throw new RSQLParseException( String.format("No such association %s for type %s", associationName, typeName)); } path.add(new Path.PathElement(entityType, fieldType, associationName)); entityType = fieldType; } return new Path(path); }
public RootCollectionPageTotalsQueryBuilder(Class<?> entityClass, EntityDictionary dictionary, Session session) { super(dictionary, session); this.entityClass = dictionary.lookupEntityClass(entityClass); }
types[0] = dictionary.getEntityClass(type); final Class<?> entityClass = types[i - 1]; final Class<?> fieldType = ("id".equals(field.toLowerCase(Locale.ENGLISH))) ? dictionary.getIdType(entityClass) : dictionary.getParameterizedType(entityClass, field); if (fieldType == null) { throw new ParseException("Unknown field in filter: " + field);
/** * Extract the relationships of the entity. */ private void setRelationships() { if (this.data != null) { this.relationships = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isRelation(this.entityClass, entry.getKey())) { Set<Entity> entitySet = new LinkedHashSet<>(); Class<?> loadClass = dictionary.getParameterizedType(this.entityClass, entry.getKey()); Boolean isToOne = dictionary.getRelationshipType(this.entityClass, entry.getKey()).isToOne(); if (isToOne) { entitySet.add(new Entity(Optional.of(this), ((Map<String, Object>) entry.getValue()), loadClass, this.requestScope)); } else { for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) { entitySet.add(new Entity(Optional.of(this), row, loadClass, this.requestScope)); } } this.relationships.add(new Relationship(entry.getKey(), entitySet)); } } } }
/** * Get the id of the entity. * @return the optional id */ public Optional<String> getId() { EntityDictionary dictionary = this.requestScope.getDictionary(); String idFieldName = dictionary.getIdFieldName(this.entityClass); return this.attributes.stream() .filter(entry -> idFieldName.equalsIgnoreCase(entry.name)) .map(e -> (String) e.value) .findFirst(); }
/** * Extract the attributes of the entity. */ private void setAttributes() { if (this.data != null) { this.attributes = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); String idFieldName = dictionary.getIdFieldName(this.entityClass); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isAttribute(this.entityClass, entry.getKey())) { this.attributes.add(new Attribute(entry.getKey(), entry.getValue())); } if (Objects.equals(entry.getKey(), idFieldName)) { this.attributes.add(new Attribute(entry.getKey(), entry.getValue())); } } } }
/** * Get a relationship type. * * @param relation Name of relationship * @return Relationship type. RelationshipType.NONE if not found. */ public RelationshipType getRelationshipType(String relation) { return dictionary.getRelationshipType(obj, relation); }