All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jnosql.mapping.graph.GraphMapperSelect Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/*
 *  Copyright (c) 2023 Contributors to the Eclipse Foundation
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */
package org.eclipse.jnosql.mapping.graph;

import jakarta.data.exceptions.NonUniqueResultException;
import org.eclipse.jnosql.mapping.core.Converters;
import jakarta.nosql.QueryMapper.MapperFrom;
import jakarta.nosql.QueryMapper.MapperLimit;
import jakarta.nosql.QueryMapper.MapperNameCondition;
import jakarta.nosql.QueryMapper.MapperNameOrder;
import jakarta.nosql.QueryMapper.MapperNotCondition;
import jakarta.nosql.QueryMapper.MapperOrder;
import jakarta.nosql.QueryMapper.MapperSkip;
import jakarta.nosql.QueryMapper.MapperWhere;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;
import static org.apache.tinkerpop.gremlin.process.traversal.Order.asc;
import static org.apache.tinkerpop.gremlin.process.traversal.Order.desc;

final class GraphMapperSelect extends AbstractMapperQuery
        implements MapperFrom, MapperLimit,
        MapperSkip, MapperOrder, MapperNameCondition,
        MapperNotCondition, MapperNameOrder, MapperWhere {


    GraphMapperSelect(EntityMetadata mapping, Converters converters,
                      GraphTraversal traversal, GraphConverter converter) {
        super(mapping, converters, traversal, converter);
    }

    @Override
    public MapperNameCondition where(String name) {
        requireNonNull(name, "name is required");
        this.name = name;
        return this;
    }

    @Override
    public MapperNameCondition and(String name) {
        requireNonNull(name, "name is required");
        this.name = name;
        this.and = true;
        return this;
    }

    @Override
    public MapperNameCondition or(String name) {
        requireNonNull(name, "name is required");
        this.name = name;
        this.and = false;
        return this;
    }

    @Override
    public MapperSkip skip(long start) {
        this.traversal.skip(start);
        return this;
    }

    @Override
    public MapperLimit limit(long limit) {
        this.traversal.limit(limit);
        return this;
    }

    @Override
    public MapperOrder orderBy(String name) {
        requireNonNull(name, "name is required");
        this.name = name;
        return this;
    }

    @Override
    public  MapperWhere eq(T value) {
        eqImpl(value);
        return this;
    }

    @Override
    public MapperWhere like(String value) {
        likeImpl(value);
        return this;
    }

    @Override
    public  MapperWhere gt(T value) {
        gtImpl(value);
        return this;
    }

    @Override
    public  MapperWhere gte(T value) {
        gteImpl(value);
        return this;
    }

    @Override
    public  MapperWhere lt(T value) {
        ltImpl(value);
        return this;
    }

    @Override
    public  MapperWhere lte(T value) {
        lteImpl(value);
        return this;
    }

    @Override
    public  MapperWhere between(T valueA, T valueB) {
        betweenImpl(valueA, valueB);
        return this;
    }

    @Override
    public  MapperWhere in(Iterable values) {
        inImpl(values);
        return null;
    }

    @Override
    public MapperNotCondition not() {
        this.negate = true;
        return this;
    }

    @Override
    public MapperNameOrder asc() {
        traversal.order().by(mapping.columnField(mapping.columnField(name)), asc);
        return this;
    }

    @Override
    public MapperNameOrder desc() {
        traversal.order().by(mapping.columnField(mapping.columnField(name)), desc);
        return this;
    }

    @Override
    public  List result() {
        Stream stream = stream();
        return stream.toList();
    }

    @Override
    public  Stream stream() {
        if (condition != null) {
            traversal.filter(condition);
        }
        return traversal.toStream().map(converter::toEntity);
    }

    @Override
    public  Optional singleResult() {
        List result = result();
        if (result.isEmpty()) {
            return Optional.empty();
        }
        if (result.size() == 1) {
            return Optional.ofNullable(result.get(0));
        }
        throw new NonUniqueResultException("The query returns more than one result. Elements: " + result.size());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy