/** * Create a {@link PathExpression} from given {@link Path}. * @param <T> Path type * @param path Suorce path * @return A new {@link PathExpression} with the same name, type and parent of the given path */ static <T> PathExpression<T> from(Path<T> path) { ObjectUtils.argumentNotNull(path, "Path must be not null"); DefaultPathExpression<T> expression = new DefaultPathExpression<>(path.getName(), path.getType()); path.getParent().ifPresent(p -> expression.parent(p)); return expression; }
/** * Gets the <em>relative</em> path name, separated by a dot <code>.</code> character, i.e. exclude any * {@link FinalPath} path instance from path name composition. * @return Path relative name, en empty String if this Path is a {@link FinalPath} itself. */ default String relativeName() { return relativeName(p -> p.getName()); }
public void path() { // tag::path[] Path<String> stringPath = Path.of("pathName", String.class); // <1> String name = stringPath.getName(); // <2> boolean root = stringPath.isRootPath(); // <3> Path<String> hierarchicalPath = Path.of("subName", String.class).parent(stringPath); // <4> String fullName = hierarchicalPath.fullName(); // <5> // end::path[] }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static Optional<Path<?>> getPathByName(PropertySet set, String propertyName) { if (set != null && propertyName != null) { return set.stream().filter(p -> Path.class.isAssignableFrom(p.getClass())) .filter(p -> propertyName.equals(((Path) p).getName())).findFirst(); } return Optional.empty(); }
@Override public boolean match(Path<?> path, Path<?> otherPath) { if (path != null && otherPath != null) { return path.relativeName().equals(otherPath.relativeName()); } return false; }
private static Optional<Path> getPathUsingDocumentContext(MongoResolutionContext context, String fieldName) { PathPropertySetAdapter adapter = MongoDocumentContext.isDocumentContext(context) .map(dc -> PathPropertySetAdapter.create(dc.getPropertySet())).orElse(null); if (adapter != null) { Optional<Path> path = adapter.getProperty(fieldName).flatMap(p -> adapter.getPath(p)).map(p -> (Path) p); if (path.isPresent()) { return path; } // check data path return adapter.propertyPaths() .filter(pp -> fieldName.equals(pp.getPath().relativeName(p -> p.getDataPath().orElse(p.getName())))) .map(pp -> (Path) pp.getPath()).findFirst(); } return Optional.empty(); }
@Override public Optional<QueryDslExpression> resolve(Path expression, QueryDslResolutionContext context) throws InvalidExpressionException { // validate expression.validate(); // intermediate resolution and validation Path path = context.resolve(expression, Path.class).orElse(expression); path.validate(); // get root com.querydsl.core.types.Path<?> root = getParentRootPath(path, context).orElse(getQueryRootPath(path, context)); // get query path from property final String queryPath = path.getName(); PathBuilder<?> rootPathBuilder = new PathBuilder<>(root.getType(), root.getMetadata()); @SuppressWarnings("unchecked") PathBuilder<?> pb = rootPathBuilder.get(queryPath, path.getType()); if (pb == null) { throw new InvalidExpressionException("Cannot get a valid PathBuilder for path expression [" + path + "]"); } return Optional.of(QueryDslExpression.create(pb)); }
/** * Gets the path full name, including any parent path, separated by a dot <code>.</code> character. * @param nameMapper The function to use to obtain the path name (not null) * @return Path full name */ default String fullName(Function<Path<?>, String> nameMapper) { ObjectUtils.argumentNotNull(nameMapper, "Path name mapper function must be not null"); return getParent().map(pr -> stream().map(p -> nameMapper.apply(p)) .collect(LinkedList<String>::new, LinkedList::addFirst, (a, b) -> a.addAll(0, b)).stream() .collect(Collectors.joining("."))).orElse(getName()); }
private static Path<?> getPathByName(String propertyName, Iterable<?> properties) { if (propertyName != null && properties != null) { for (Object property : properties) { if (String.class.isAssignableFrom(property.getClass()) && propertyName.equals(property)) { return Path.of(propertyName, Object.class); } if (property instanceof Path && propertyName.equals(((Path<?>) property).getName())) { return (Path<?>) property; } } } return null; }
/** * Create a new {@link BooleanProperty} from given <code>path</code>, using given {@link Path} <code>name</code>. * @param path Path from which to obtain the property path name (not null) * @return {@link BooleanProperty} builder */ static BooleanPropertyBuilder create(Path<Boolean> path) { ObjectUtils.argumentNotNull(path, "Path must be not null"); BooleanPropertyBuilder builder = create(path.getName()); path.getParent().ifPresent(p -> builder.parent(p)); return builder; }
@Override public <V> V read(Path<V> path, T instance) { ObjectUtils.argumentNotNull(path, "Path must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); final BeanProperty<?> property = (BeanProperty<?>) property(path.relativeName()); return read(property, instance, path.getType()); }
@Override public Optional<Path> resolve(FieldName expression, MongoResolutionContext context) throws InvalidExpressionException { // validate expression.validate(); // field name final String fieldName = expression.getFieldName(); // check document context or use field name return Optional.of(getPathUsingDocumentContext(context, fieldName).orElse(Path.of(fieldName, Object.class))); }
String fieldName = context.resolveOrFail(Path.of(name, Object.class), FieldName.class).getFieldName(); String fieldName = context.resolveOrFail(Path.of(name, Object.class), FieldName.class).getFieldName(); && !MongoDocumentContext.ID_FIELD_NAME.equals(property.relativeName())) {
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public PropertyBox read(final PropertyBox propertyBox, final T instance, final boolean ignoreMissing) { ObjectUtils.argumentNotNull(propertyBox, "PropertyBox must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); propertyBox.stream().filter(p -> !p.isReadOnly()).filter(p -> Path.class.isAssignableFrom(p.getClass())) .map(p -> (Path<?>) p).forEach(p -> { getProperty(p, ignoreMissing).ifPresent( bp -> propertyBox.setValue((Property) p, read(bp, instance, (Class<Object>) p.getType()))); }); return propertyBox; }
/** * Get the path data model name, using {@link DataMappable#getDataPath()} if available or returning the path name if * not. * @param path The path for which to obtain the data path name * @return The data path name */ private static String getPathName(Path<?> path) { return path.getDataPath().orElse(path.relativeName()); }
private static <T> T deserializeValue(Path<T> path, T value) throws JsonDeserializationException { if (value != null) { final Class<? extends T> type = path.getType(); try { if (TypeUtils.isEnum(type)) { && path.getTemporalType().orElse(TemporalType.DATE_TIME) == TemporalType.DATE) {
@Override protected String generateColumnKey(Property<?> property) { if (Path.class.isAssignableFrom(property.getClass())) { return ((Path<?>) property).fullName(); } String name = property.getName(); if (name != null) { return name; } return String.valueOf(property.hashCode()); }
@Override public Optional<FieldName> resolve(Path expression, MongoResolutionContext context) throws InvalidExpressionException { // validate expression.validate(); // use Path relative name return Optional.of(FieldName.create(getPathName(expression))); }
private static Optional<com.querydsl.core.types.Path<?>> getParentRootPath(Path<?> path, QueryDslResolutionContext context) { Optional<DataTarget> target = path.stream().filter(p -> DataTarget.class.isAssignableFrom(p.getClass())) .map(p -> (DataTarget) p).findFirst(); if (target.isPresent()) { JpaEntity<?> entity = context.resolve(target.get(), JpaEntity.class) .orElseThrow(() -> new InvalidExpressionException("Failed to resolve target [" + target.get() + "] declared as parent of path [" + path + "]")); QueryDslResolutionContext ctx = context; while (ctx != null) { final List<JoinExpression> joins = ctx.getQueryMetadata().map(q -> q.getJoins()) .orElseThrow(() -> new InvalidExpressionException("Missing context query metadata")); if (joins != null) { for (JoinExpression join : joins) { if (join.getTarget() != null && EntityPath.class.isAssignableFrom(join.getTarget().getClass()) && entity.getEntityClass() == join.getTarget().getType()) { return Optional.of((EntityPath<?>) join.getTarget()); } } } // parent ctx = ctx.getParent().orElse(null); } throw new InvalidExpressionException("None of the query joins corresponds to the target [" + target.get() + "] declared as parent of path [" + path + "]"); } return Optional.empty(); }