Создание предиката querydsl с запросом nearSphere - PullRequest
0 голосов
/ 30 апреля 2019

Я пытаюсь создать предикат querydsl (BooleanBuilder)

Я добавил зависимости и плагин для создания моих Qclasses.Я создал все свои простые части, но я не знаю, как это сделать для моего Double [], nearSphere и distance.

Это мои зависимости:

<!-- Query dsl -->
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <scope>provided</scope>
    </dependency>

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-mongodb</artifactId>
</dependency>

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-sql-spatial</artifactId>
    <version>${querydsl.version}</version>
</dependency>

<dependency>
    <groupId>org.mongodb.morphia</groupId>
    <artifactId>morphia</artifactId>
    <version>${morphia.version}</version>
</dependency>

<dependency>
    <groupId>com.google.code</groupId>
    <artifactId>morphia</artifactId>
    <version>${morphia.code.version}</version>
</dependency>
<!-- QUery dsl -->

Этомой плагин

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>process</goal>
                </goals>
                <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.morphia.MorphiaAnnotationProcessor</processor>
                        <spatial>true</spatial>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>
        </dependencies>
</plugin>

Это мой класс, который содержит Double []

import org.mongodb.morphia.annotations.Entity;

@Entity
public class OwnerLocation implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8481550794454165168L;

    private Double[] geo;
    private String location;
    private Town town;

    public OwnerLocation() {}

    public OwnerLocation(Double[] geo, String location, Town town) {
        super();
        this.geo = geo;
        this.location = location;
        this.town = town;
    }
}

Это мой сгенерированный QClass

import static com.querydsl.core.types.PathMetadataFactory.*;

import com.querydsl.core.types.dsl.*;

import com.querydsl.core.types.PathMetadata;
import javax.annotation.Generated;
import com.querydsl.core.types.Path;


/**
 * QOwnerLocation is a Querydsl query type for OwnerLocation
 */
@Generated("com.querydsl.codegen.EntitySerializer")
public class QOwnerLocation extends EntityPathBase<OwnerLocation> {

    private static final long serialVersionUID = 905971258L;

    public static final QOwnerLocation ownerLocation = new QOwnerLocation("ownerLocation");

    // custom
    public final com.querydsl.mongodb.Point geo = new com.querydsl.mongodb.Point(forProperty("geo"));

    public final StringPath location = createString("location");

    public final SimplePath<Town> town = createSimple("town", Town.class);

    public QOwnerLocation(String variable) {
        super(OwnerLocation.class, forVariable(variable));
    }

    public QOwnerLocation(Path<? extends OwnerLocation> path) {
        super(path.getType(), path.getMetadata());
    }

    public QOwnerLocation(PathMetadata metadata) {
        super(OwnerLocation.class, metadata);
    }

}

И это класс, гдеЯ хочу использовать NearSphere

public class QueryAdHelper {

    public static Predicate buildQuery(SearchForm searchForm) {

        QAd qAd = QAd.ad;
        BooleanBuilder predicate = new BooleanBuilder();

        Category category = searchForm.getHeaderSearchForm().getCategory();

        // Header
        String keyword = searchForm.getHeaderSearchForm().getKeyWord();
        Town town = searchForm.getHeaderSearchForm().getTown();

        // Facet
        String adType = searchForm.getFacetSearchForm().getAdType();
        long minPrice = searchForm.getFacetSearchForm().getPriceRange().getMin();
        long maxPrice = searchForm.getFacetSearchForm().getPriceRange().getMax();
        String adContent = searchForm.getFacetSearchForm().getAdContent();

        if (keyword != null && !keyword.isEmpty()) {
            predicate.and(qAd.title.containsIgnoreCase(keyword));
        }

        if (category != null && category.getId() != null && !category.getId().isEmpty()) {
            // categories code.
            predicate.and(qAd.category.id.startsWith(category.getId()));
        }

        if (town != null && town.getCode() != null && !town.getCode().isEmpty()) {
            predicate.and(qAd.ownerLocation.town.eq(town));
        }

        ????????????????????????????
        MongodbExpressions.nearSphere();    

        if (adType != null && !AdTypeFacetEnum.ALL.getValue().equals(adType)) {
            predicate.and(qAd.type.eq(AdTypeEnum.valueOf(adType)));
        }

        if (minPrice > 0 || maxPrice > 0) {
            predicate.and(qAd.price.between(minPrice, maxPrice));
        }

        if (adContent != null) {
            if (AdContentFacetEnum.PICTURE.getValue().equals(adContent)) {
                predicate.and(qAd.images.isNotEmpty());
            }
        }

        return predicate;
    }
}

Моя недостающая часть находится в классе "QueryAdHelper", я добавил вопросительные знаки.Заранее спасибо.

...